Reputation: 681
I want to color my figure of red. With using GL10 to draw element there aren't problem but now using GLES20 the figure is black colored instead red.
Here there is my code: To set color float array and buffer:
for (int i=0;i<colors.length;i++)
{
colors[i]=1f;
colors[++i]=0f;
colors[++i]=0f;
colors[++i]=1f;
}
byteBuffer=ByteBuffer.allocateDirect(4*colors.length);
byteBuffer.order(ByteOrder.nativeOrder());
colors_buff=byteBuffer.asFloatBuffer();
colors_buff.put(colors);
colors_buff.position(0);
My Vertex and Fragment shaders:
private String v_shader="" +
"attribute vec4 verte;" +
"attribute vec4 colors;" +
"varying vec4 vcolor;" +
"void main(){" +
"gl_Position=verte;" +
"vcolor=colors;" +
"}";
private String c_shader="" +
"precision mediump float;" +
"varying vec4 vcolor;" +
"void main(){" +
"gl_FragColor=vcolor;" +
"}";
My function to draw elements:
public void DrawShader(){
GLES20.glUseProgram(progra);
int v_handler=GLES20.glGetAttribLocation(progra,"verte");
int f_handler=GLES20.glGetUniformLocation(progra,"colors");
GLES20.glEnableVertexAttribArray(v_handler);
GLES20.glEnableVertexAttribArray(f_handler);
GLES20.glVertexAttribPointer(v_handler,3,GLES20.GL_FLOAT,false,12,vertexbuffer);
GLES20.glVertexAttribPointer(f_handler,4,GLES20.GL_FLOAT,false,16,colors_buff);
GLES20.glDrawElements(GLES20.GL_LINES,indicies.length,GLES20.GL_UNSIGNED_SHORT,indicies_buff);
GLES20.glDisableVertexAttribArray(v_handler);
GLES20.glDisableVertexAttribArray(f_handler);
}
An other question if i change the color array and put it in the buffer when i called drw the color doesn't change while when i used GL10 to draw element it worked. Thanks indeead to all.
Upvotes: 0
Views: 37
Reputation: 16774
You are mixing uniforms
and attributes
. Decide which one to have.
In the shader you used attribute
: attribute vec4 colors;
. To change this into the uniform then move it to fragment shader as uniform vec4 uColor;
.
When getting a location you are treating it as an uniform
: int f_handler=GLES20.glGetUniformLocation(progra,"colors");
to change it into the attribute you should use int f_handler=GLES20.glGetAttribLocation(progra,"colors");
. If you use it as uniform then remove the attribute enabling: GLES20.glEnableVertexAttribArray(f_handler);
.
You assign the pointer as an attribute GLES20.glVertexAttribPointer(f_handler,4,GLES20.GL_FLOAT,false,16,colors_buff);
to use uniform you need to use it as GLES20.glUniform4f(f_handler, 1.0f, .0f, .0f, 1.0f);
and you can remove your color buffer.
How to chose between which to use you should look into the definition of the two (uniform
and attribute
). But to make it very short when you use uniform the value will be set as a constant for the whole draw call. In your case it means your shape will have one color over the whole surface. The attribute is used per vertex and each vertex will have a specific color, the pixels between the vertices then will have interpolated values (gradient) if the values are different.
Upvotes: 0