Sign in to follow this  
wagner_fsoares

Some weird alpha blending related problem

Recommended Posts

I'm having a weird problem with alpha: I have a tree model with a PNG texture that has transparency between its leafs and a floor (just GL_QUADS) with a JPG grass texture. I can see the floor through the tree, but I can't see a tree through another, it's just black instead of transparency. Here is my init code:
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
And here is the loop:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glPushMatrix();
glBindTexture(GL_TEXTURE_2D, m_tex_grass);
for (int x = 0; x < 6; x++) {
    for (int y = 0; y < 20; y++) {
	glPushMatrix();

	glTranslatef(-9.0f + (x * 4.0f), -10.0f + (y * 4.0f), 0.0f);

	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-2.0f, -2.0f, 0.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-2.0f, 2.0f, 0.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(2.0f, 2.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(2.0f, -2.0f, 0.0f);
	glEnd();

	glPopMatrix();
    }
}
glPopMatrix();

for (int i = 0; i < 10; i++) {
    glBindTexture(GL_TEXTURE_2D, m_tex_tree);
    glPushMatrix();
    glTranslatef(-5.0f - ((i % 2) * 2), 10.0f + (i * 2), 0.0f);
    m_cgm_tree.draw();
    glPopMatrix();
}

for (int i = 0; i < 10; i++) {
    glBindTexture(GL_TEXTURE_2D, m_tex_tree);
    glPushMatrix();
    glTranslatef(5.0f + ((i % 2) * 2), 10.0f + (i * 2), 0.0f);
    m_cgm_tree.draw();
    glPopMatrix();
}
this code: m_cgm_tree.draw(); call this code:
if (m_vertIDs.size() == 4) {
    glBegin(GL_QUADS);
} else {
    glBegin(GL_TRIANGLES);
}
for (int i = 0; i < m_vertIDs.size(); i++) {
    if (hasUVTex())
	glTexCoord2f(m_vertUVs[i].x(), m_vertUVs[i].y());
    Vert vert = vertList[m_vertIDs[i]];
    glNormal3f(vert.no().x(), vert.no().y(), vert.no().z());
    glVertex3f(vert.co().x(), vert.co().y(), vert.co().z());
}
glEnd();
For every face on the model. The only difference between the model and the floor, is I'm adding glNormal3f to the model. Please help me with this one. Thanks! PS: The leafs of the tree model are like thin blocks instead of planes, I did that to get correct light shading (So the leafs have 2 normals, one for every face). In case it matters. PS2: [Edited by - wagner_fsoares on December 27, 2009 3:28:25 PM]

Share this post


Link to post
Share on other sites
The issue is very likely render order. When dealing with transparency in computer graphics, you need to render back to front. The computer can't fill in the transparent holes after the fact.
That means you need to do two things:
1) render all opaque objects FIRST
2) sort all transparent objects at some level, so you can draw them from furthest from the camera to closest. This can be on a per-tree level of sorting, or if you need it, a per-triangle sorting. Look into something like radix sort for the sorting algorithm.
Then render all transparent objects in the sorted order.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this