• Advertisement
Sign in to follow this  

[solved] problem rendering 2D layer on top 3D scene

This topic is 3877 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 trying to make a 2D layer on top of a 3D scene i am able to draw 2D shapes, but unfortunely, i have to disable GL_Blend in order to see that. i don't know what's wrong and i really would like to use alpha channel in my 2D layer, so i would be able to see the 3D scene below with a sort of transparency. when i enable GL_Blend when im rendering my 2D layer on top of the 3D -> i can't see any of the 2D layer. when i disable GL_Blend when im rendering my 2D layer on top of the 3D -> i can't see any alpha. here's my main rendering loop :
glColor4f(1.0, 1.0, 1.0, 1);
glEnable(GL_BLEND);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(camerax,cameray,cameraz);
int counter = 0;
while (counter != quad.size())
	{
	glPushMatrix();
	glTranslatef(((quad[counter].x1+quad[counter].x2)/2),
             ((quad[counter].y1+quad[counter].y2)/2),
             quad[counter].z);
	glRotatef(quad[counter].thisangle, 0, 0, 1);
	glTranslatef(-((quad[counter].x1+quad[counter].x2)/2),
             -((quad[counter].y1+quad[counter].y2)/2),
             -quad[counter].z);
	if (quad[counter].selected == 1)
		{
		GLuint texture;
		if (texturestring[SELECTTEXTURE].loaded == 0)
			{
			createtexture(&(texture), texturestring[SELECTTEXTURE].file.c_str());
			texturestring[SELECTTEXTURE].loaded = 1;
			texturestring[SELECTTEXTURE].texture = texture;
			}
			else
			texture = texturestring[SELECTTEXTURE].texture;
		glBindTexture(GL_TEXTURE_2D, texture);
		glBegin(GL_QUADS);
		glTexCoord2f(0,0);
		glVertex3f(quad[counter].x1-2, quad[counter].y2-2,  quad[counter].z-0.2);	
		glTexCoord2f(1,0);
		glVertex3f(quad[counter].x2+2, quad[counter].y2-2,  quad[counter].z-0.2);
		glTexCoord2f(1,1);
		glVertex3f( quad[counter].x2+2,  quad[counter].y1+2,  quad[counter].z-0.2);
		glTexCoord2f(0,1);
		glVertex3f(quad[counter].x1-2,  quad[counter].y1+2, quad[counter].z-0.2);
		glEnd();
		}
	glBindTexture(GL_TEXTURE_2D, quad[counter].texture);
	glLoadName(counter);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0);
	glVertex3f(quad[counter].x1, quad[counter].y2,  quad[counter].z);	
	glTexCoord2f(1,0);
	glVertex3f(quad[counter].x2, quad[counter].y2,  quad[counter].z);
	glTexCoord2f(1,1);
	glVertex3f( quad[counter].x2,  quad[counter].y1,  quad[counter].z);
	glTexCoord2f(0,1);
	glVertex3f(quad[counter].x1,  quad[counter].y1, quad[counter].z);
	glEnd();
	glPopMatrix();
	++counter;
	}
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glDisable(GL_BLEND);
gluOrtho2D(0.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(0.0, 1.0, 0.0, 0.5);
glBegin(GL_TRIANGLES);
glVertex2f(0.0,0.0);
glVertex2f(0.0,0.5);
glVertex2f(1.0,1.0);
glEnd();
		
glMatrixMode(GL_PROJECTION);
glPopMatrix();
SDL_GL_SwapBuffers( );


if you want to see in a better view ... http://rafb.net/p/1Xta9E29.html [Edited by - itay390 on July 15, 2007 2:48:19 AM]

Share this post


Link to post
Share on other sites
Advertisement
I am not near my computer currently so i cannot tel you what the problem is as i dont have access to my compiler to test a theroy.

But i do recommneed the Book begining OpenGL
its informative and it will probaly explain if you read it exactly what the problem is.

As i sead i have a theroie about what the problem is however i cannot test it so im not going to say what it is as i could be wrong.

But read the book its not hard to get ahold of.

Regards Jouei.

Share this post


Link to post
Share on other sites
You need to disable depth testing when blending:

glDisable( GL_DEPTH_TEST );

don't forget to enable it again before the next rendering pass though.

Share this post


Link to post
Share on other sites
what, like that?


glDisable( GL_DEPTH_TEST );
glColor4f(1.0, 1.0, 1.0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(camerax,cameray,cameraz);
int counter = 0;
while (counter != quad.size())
{
glPushMatrix();
glTranslatef(((quad[counter].x1+quad[counter].x2)/2),
((quad[counter].y1+quad[counter].y2)/2),
quad[counter].z);
glRotatef(quad[counter].thisangle, 0, 0, 1);
glTranslatef(-((quad[counter].x1+quad[counter].x2)/2),
-((quad[counter].y1+quad[counter].y2)/2),
-quad[counter].z);
if (quad[counter].selected == 1)
{
GLuint texture;
if (texturestring[SELECTTEXTURE].loaded == 0)
{
createtexture(&(texture), texturestring[SELECTTEXTURE].file.c_str());
texturestring[SELECTTEXTURE].loaded = 1;
texturestring[SELECTTEXTURE].texture = texture;
}
else
texture = texturestring[SELECTTEXTURE].texture;
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(quad[counter].x1-2, quad[counter].y2-2, quad[counter].z-0.2);
glTexCoord2f(1,0);
glVertex3f(quad[counter].x2+2, quad[counter].y2-2, quad[counter].z-0.2);
glTexCoord2f(1,1);
glVertex3f( quad[counter].x2+2, quad[counter].y1+2, quad[counter].z-0.2);
glTexCoord2f(0,1);
glVertex3f(quad[counter].x1-2, quad[counter].y1+2, quad[counter].z-0.2);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, quad[counter].texture);
glLoadName(counter);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,0);
glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,1);
glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z);
glTexCoord2f(0,1);
glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z);
glEnd();
glPopMatrix();
++counter;
}
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(0.0, 1.0, 0.0, 0.5);
glBegin(GL_TRIANGLES);
glVertex2f(0.0,0.0);
glVertex2f(0.0,0.5);
glVertex2f(1.0,1.0);
glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glEnable( GL_DEPTH_TEST );
SDL_GL_SwapBuffers( );



this doesn't work. define "when im blending"..is that when im adding quads or triangles to the scene? if so, i tried also putting glEnable/disable( GL_DEPTH_TEST ); there and it still doesn't work.

Share this post


Link to post
Share on other sites
In your last example you are no longer blending?! I'll show you how to do it correct, at least as far as I understand it, I'm a newbie at OpenGL myself:


void drawRoutine()
{

// First render your normal 3D geometry here

glDisable( GL_DEPTH_TEST );
glEnable(GL_BLEND);

// Render your blended/transparent 2D/3D geometry in farthest-to-closest order here

glEnable( GL_DEPTH_TEST );
glDisable(GL_BLEND);

}

Share this post


Link to post
Share on other sites
the thing is, im always blending. all the quads/triangles are with an alpha channel. so this solution is not working.

Share this post


Link to post
Share on other sites
SOLVED!
the thing was, because i had textures enabled, it was expecting glTexCoord2f(x,y);

thanks anyway for anyone who tried to help.

Share this post


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

  • Advertisement