1
\$\begingroup\$

I am attempting to redraw with the same arrays using glDrawArrays in a GLKView on iOS. When I comment out any two of these calls, the other draw call draws as I expect. My last hunch was that I was attempting to draw at the same depth, so I introduced a depth uniform and am setting the depth in the vertex shader like so:

gl_Position = vec4(position.xy, 0.1*depth, 1.0);

the depth coming in is a lowp vector with a value of 0, 1, or 2. This didn't have any effect.

Here is my drawRect: method

- (void)drawRect:(CGRect)rect
{
    glClearColor(0., 0.0, 0.0, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Render the object again with ES2
    glUseProgram(_program);


    // the colors
    glBindVertexArrayOES(_vertexArray);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _wheelMaskTexture);
    glUniform1i(uniforms[UNIFORM_TEXTURE], 0);

    glUniform1f(uniforms[UNIFORM_COLOR_DEPTH], COLOR_DRAW);
    glUniform1f(uniforms[UNIFORM_COLOR_ANGLE], _angle);

    glDrawArrays(GL_TRIANGLES, 0, 6);


    // the sat
    glBindVertexArrayOES(_vertexArray);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _satMaskTexture);
    glUniform1i(uniforms[UNIFORM_TEXTURE], 0);

    glUniform1i(uniforms[UNIFORM_COLOR_DEPTH], SATURATION_DRAW);
    glUniform1f(uniforms[UNIFORM_COLOR_ANGLE], _satAngle);

    glDrawArrays(GL_TRIANGLES, 0, 6);


    // the bright
    glBindVertexArrayOES(_vertexArray);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _brightMaskTexture);
    glUniform1i(uniforms[UNIFORM_TEXTURE], 0);

    glUniform1i(uniforms[UNIFORM_COLOR_DEPTH], BRIGHTNESS_DRAW);
    glUniform1f(uniforms[UNIFORM_COLOR_ANGLE], _brightnessAngle);

    glDrawArrays(GL_TRIANGLES, 0, 6);
}

My GLKView is set up with a drawableDepthFormat = GLKViewDrawableDepthFormat24. I haven't set up any transforms because what I'm drawing is basically a simple color wheel.

There is no world or model transform. I'm simply drawing the same square 3 times one on top of the other without transforming. There are 3 different mask images that are used to mask out the fragments that I care about in the fragment shader like so:

gl_FragColor = vec4(rgb, texture2D(wheelMask, texCoord).a);

Why wouldn't all three glDrawArrays calls draw as expected? As I mentioned early any one of them by themselves draws as expected. As it is, with all three, only the first glDrawArrays draws as expected.

\$\endgroup\$
-1
\$\begingroup\$

You do not apply any transformation to the rectangles. So the codes renders the same geometry 3 times with modification applied to the material. Suppose we have a 3x3 screen (i know very small)

and your rectangle ends up drawing over these pixels

[x][x][ ]
[x][x][ ]
[x][x][ ]

your final framebuffer after the first draw call could look like this:

  color      depth
[r][r][ ] [0.1][0.2][ ]
[r][r][ ] [0.1][0.2][ ]
[r][r][ ] [0.1][0.2][ ]

note: r is for red, i didn't feel like writing a full 4 byte color (1,0,0,1) .1 is the distance from the screen to the fragment at this location.

for the first quad, all the fragment were colored because there was nothing yet. so you end up with (in this case) 6 new fragment

now when you draw your second draw call. you don't change the geometry. when the top-left fragment is generated, it'll generate the exact same depth. So since the new depth is not closer then the old depth. a new fragment is not generated and the 2nd material is not colored.

A new fragment is generated according to

newDepth<oldDepth

and not

newDepth<=oldDepth

because if 2 fragment are at the same depth it assumes that they are at the same position. Since depth buffers are usually 16 bit (65536 different numbers) it means depth in opengl is discrete rather then continous (like real euclidean space). But they will skip the new fragment since it saves computation and that in reality you wouldn't really see the difference, except like in your case where you have 2 quads that are literally in the exact same position.

So to answer the question, if you want to see all the different materials. you need to transform the other instances of the quads (either rotate, translate or scale)

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site. Can you elaborate a little bit? Your answer seems like a reasonable explanation to me (I'm a layman), but a reference would help. If you are genuinely asking for clarification, the comments are the right place to do that. Otherwise, use a declarative sentence: "It is because..." \$\endgroup\$ Apr 17 '14 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.