Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL ES2.0 working with colors
    primarykey
    data
    text
    <p>(iPhone) I'm trying to draw a cube in ES2 with a different color on each face. Right now the colors aren't coming out right and I can't figure out why. Here's the relevant code:</p> <pre><code>- (void) DrawES2 { glViewport ( 0, 0, backingWidth, backingHeight ); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glUseProgram ( programObject ); int colorIndex = 0; BOOL newFace = NO; for(int i = 0; i &lt; 36; i += 3) { GLfloat faceColor[] = { faceColors[colorIndex], faceColors[colorIndex+1], faceColors[colorIndex+2], faceColors[colorIndex+3] }; // Load the vertex data glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); glEnableVertexAttribArray ( 0 ); // Load the color data glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, faceColor); glEnableVertexAttribArray( 1 ); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &amp;indices[i]); newFace = ( i%2 == 0 ) ? NO : YES; if( newFace ) colorIndex+=4; } } GLfloat vVertices[] = { -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f }; // Used to draw cube more efficiently GLubyte indices[36] = { 4, 7, 3, //top face 4, 3, 0, 5, 6, 7, //front face 5, 7, 4, 3, 2, 1, //back face 0, 3, 1, 6, 2, 3, //right face 6, 3, 7, 5, 0, 1, //left face 5, 4, 0, 5, 2, 6, //bottom face 5, 1, 2 }; const GLfloat faceColors[] = { 0, 1, 0, 1, 1, 0.5f, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1 }; GLbyte vShaderStr[] = "uniform mat4 t_matrix; \n" "uniform mat4 r_matrix; \n" "uniform mat4 u_proj_matrix; \n" "attribute vec4 vPosition; \n" "attribute vec4 a_color; \n" "varying vec4 v_color; \n" "void main() \n" "{ \n" " mat4 model_matrix = t_matrix * r_matrix; \n" " mat4 mvp_matrix = u_proj_matrix * model_matrix; \n" " gl_Position = mvp_matrix * vPosition; \n" " v_color = a_color; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec4 v_color; \n" "void main() \n" "{ \n" " gl_FragColor = v_color; \n" "} </code></pre> <p><a href="http://img684.imageshack.us/img684/2316/unled1y.png" rel="nofollow noreferrer">http://img684.imageshack.us/img684/2316/unled1y.png</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload