jeudi 26 février 2015

Depth buffer not working for alpha render pass in OpenGL ES 2


I have a scene with transparent and opaque 2d items. I first render the opaque items with depth test and depth mask (writing) enabled, in front to back order. Then I set the depth mask to false (without disabling the depth test), enable blending and render the transparent ones from back to front.


But the problem is that the transparent items are not drawn properly. When I use glDepthFunc(GL_LESS) for them they are not drawn at all and when I use glDepthFunc(GL_EQUAL) they are drawn but the ones that should be obscured by opaque items are not. They just render on top of everything really.


The code in the render routine looks like this:



// Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClearDepthf(1.f);
glDepthRangef(0.f, 1.f);

glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);

// Draw opaque items

glDepthFunc(GL_LESS);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

renderOpaque();


// Draw transparent items

glDepthMask(GL_FALSE);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

renderAlpha();

glDisable(GL_BLEND);


The z values for the items are set in the following manner:



// Bottom layer (background) is 0 and then layer is incremented
// by one for each view that sits on top
// I use the far value (something like 10000.f) to divide to get
// something between 0.0 and 1.0 (from back to front: 0.0/10k, 1.0/10k,
// 2.0/10k etc)

float zTranslation = static_cast<float>(GetLayer()) /
TheCamera::Instance().GetFar();
glm::mat4 model = glm::translate(glm::mat4(1.f),
glm::vec3(m_absoluteFrame.origin.x,
m_absoluteFrame.origin.y,
zTranslation));
glm::mat4 MVP = muiKit::TheCamera::Instance().GetProjection() *
muiKit::TheCamera::Instance().GetView() *
model;


The MVP matrix is then passed to the shader to set gl_Position...



gl_Position = MVP * vec4(imageVertex.xyz, 1);


And I also pass texture coordinates and indexes and so on and draw using glDrawElements in batches.


What am I doing wrong here?


Also, even though I'm new to this I get the feeling that the depth function should be GL_GREATER when I render transparent items... no? Somehow it makes sense to me knowing that I'm rendering them in back to front order.




Aucun commentaire:

Enregistrer un commentaire