Hi everybody. I meet a question about transparent texture.
I want to draw two trees which use transparent texture in my scene. My code is like this:
… // Draw all opacity
glEnable(GL_TEXTURE_2D);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, m_nID-1);
// Draw tree1
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-1.0f, -1.0f, -2.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(1.0f, -1.0f, -2.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(1.0f, 3.0f, -2.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-1.0f, 3.0f, -2.0f);
glEnd();
// Draw tree2
glBegin(GL_QUADS);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-1.0f, -1.0f, -10.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(1.0f, -1.0f, -10.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(1.0f, 3.0f, -10.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-1.0f, 3.0f, -10.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glDepthMask(GL_TURE);
glDisable(GL_BLEND);
when the viewpoint is (0,0,0) and the direction is -z axis, the tree1 is near the viewpoint. But tree2 is in front of tree1 in the screen because drawing tree1 is before tree2 and depth buffer is not writable. Must I sort the order of the two trees when draw the scene ? If there are lot of trees, sorting will waste long time. Who can tell me a good way to draw transparent texture?
Thanks for u’r help