Jump to content
  • Advertisement
Sign in to follow this  
DjMaSh

texture mapping with alpha problem

This topic is 3754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am drawing two texture mapped quads, one behind the other and slightly off to the side. The texture is a tree with a transparent background. When I draw the quad closest to the camera first, I get a weird white outline around the non-transparent part of the texture where it overlaps the quad behind it. If I draw them in the reverse order (i.e furthest from the camera first) then all is fine. Here is some code snippets:
void Tree::Render(float x, float y, float z)
{	
        glPushMatrix();
	glTranslatef(x, y, z);
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0);
	
	glBindTexture(GL_TEXTURE_2D, trunkTexture);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glBegin(GL_QUADS);
		size = 10;
		glTexCoord2f(0.0, 0.0);
		glVertex3f( -size, size, 0);
		
		glTexCoord2f(1.0, 0.0);
		glVertex3f(size, size, 0);
		
		glTexCoord2f(1.0, 1.0);
		glVertex3f(size, -size, 0);
		
		glTexCoord2f(0.0, 1.0);
		glVertex3f(-size, -size, 0);
	glEnd();
	glDisable(GL_ALPHA_TEST);
	glDisable(GL_BLEND);
	glPopMatrix();
}

void GLinit(){
	
    glShadeModel(GL_SMOOTH); 
    glEnable(GL_TEXTURE_2D); 

    glEnable(GL_DEPTH_TEST);
	
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f;
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glLoadIdentity();

    gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0);

    tree.Render(6, 0, -20);
    tree.Render(5, 0, -30);
	
    glutSwapBuffers();
  
}


Share this post


Link to post
Share on other sites
Advertisement
Here is a bit more of a visual description of what is going on.

When I draw closest quad to camera first:
Weird outline bug
When I draw furthest quad to camera first:
Weird outline bug

Share this post


Link to post
Share on other sites
I've always been under the impression that it's best to draw the furthest items from the camera first. Your image samples certainly seem to justify this.

Share this post


Link to post
Share on other sites
The one that looks right is typically the right way to draw transparent objects, back to front. You draw them from the front the first blends with whats in the buffer already (i guess your white clear color?) then the second one gets drawn behind, but only where the front one doesnt exsist, so you get the outline.

Share this post


Link to post
Share on other sites
hmmm.

I guess that is an option. But it means that if i have dynamically changing position then some more sophisticated algorithm/data structure is needed to always draw the furthest objects first.

I thought there may have been a way around this that would allow me to draw the objects in any order...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!