Skip to main content
GameDev.net gamedev.net
🔒 Locked

A Problem in drawing 2D transparent shapes with ortho

Started by Graphic_Programmer Aug 9, 2012 at 11:30 AM 4 replies 2.1k views
Original Post
Graphic_Programmer
Graphic_Programmer
First, my English isn't very good, i can say wrong words so sorry.
I am beginner to making 2D/3D Graphics in Opengl and i have a problem about drawing 3D shapes under 2D ortho graphics with alpha transparent.
The ScreenShot
adszihx.png
I don't know could i tell. I think you understood my problem from the picture.
These are my drawing codes;


ciz()
{
glViewport(0,0,ewidth,eheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPushMatrix();
glOrtho(-1,1,-1,1,-20,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(1,1,0,1);
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(((float)mouse_x-(ewidth/2))/(ewidth/2),-((float)mouse_y-(eheight/2))/(eheight/2),0.1);
glBindTexture(GL_TEXTURE_2D,NULL);
glBegin(GL_POLYGON);
for(i=0; i<20; i++)
{
glVertex3f(cos(18*i*pi/180)/32,sin(18*i*pi/180)/32,0);
}
glEnd();
glColor4f(1,1,1,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,tex[0]);
glPopMatrix();
/* glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex3f(-0.9,0.9,0);
glTexCoord2i(1,0); glVertex3f(-0.6,0.9,0);
glTexCoord2i(1,1); glVertex3f(-0.6,0.6,0);
glTexCoord2i(0,1); glVertex3f(-0.9,0.6,0);
glEnd();

glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex3f(-0.6,0.9,0);
glTexCoord2i(1,0); glVertex3f(-0.3,0.9,0);
glTexCoord2i(1,1); glVertex3f(-0.3,0.6,0);
glTexCoord2i(0,1); glVertex3f(-0.6,0.6,0);
glEnd();*/
//glDisable(GL_BLEND);
//glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D,tex[1]);
glBegin(GL_QUADS);
for(i=0,j=0; i<=yazi_uz; i++)
{
if(yazi=='A')
{
ii=0;
jj=0;
glTexCoord2f((1+(float)ii+(float)ii*13)/512,(1+(float)jj+(float)jj*18)/256); glVertex3f(((0+(float)i*50 )/640)*2-1,-(((0+(float)j*80 )/480)*2-1),0);
glTexCoord2f((1+(float)ii+((float)ii+1)*13)/512,(1+(float)jj+(float)jj*18)/256); glVertex3f(((0+((float)i+1)*50 )/640)*2-1,-(((0+(float)j*80 )/480)*2-1),0);
glTexCoord2f((1+(float)ii+((float)ii+1)*13)/512,(1+(float)jj+((float)jj+1)*18)/256); glVertex3f(((0+((float)i+1)*50 )/640)*2-1,-(((0+((float)j+1)*80 )/480)*2-1),0);
glTexCoord2f((1+(float)ii+(float)ii*13)/512,(1+(float)jj+((float)jj+1)*18)/256); glVertex3f(((0+(float)i*50 )/640)*2-1,-(((0+((float)j+1)*80 )/480)*2-1),0);
}
}
glEnd();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPopMatrix();
gluPerspective(45,(float)(ewidth/eheight),.1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(c_x+cos((dir+180)*pi/180)*cos(-der*pi/180)*uz,c_y+sin((dir+180)*pi/180)*cos(-der*pi/180)*uz,c_z+sin(-der*pi/180)*uz,c_x,c_y,c_z, 0,1,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glColor3f(1,1,1);
//glDisable(GL_TEXTURE_2D);
/* glBegin(GL_POLYGON);
glTexCoord2i(0,0);
glVertex3f(-15,15,0);
glTexCoord2i(1,0);
glVertex3f(15,15,0);
glTexCoord2i(1,1);
glVertex3f(15,-15,0);
glTexCoord2i(0,1);
glVertex3f(-15,-15,0);
glEnd();*/
//glEnable(GL_TEXTURE_2D);
/*glBegin(GL_TRIANGLES);
for(i=0; i<10; i++)
{
glNormal3f(cos((i+0.5)*36*pi/180)*cos(90-atan(10/7)*pi/180),sin((i+0.5)*36*pi/180)*cos(90-atan(10/7)*pi/180),sin(90-atan(10/7)*pi/180));
glVertex3f(0,0,5);
glVertex3f(cos(36*i*pi/180)*7,sin(36*i*pi/180)*7,-5);
glVertex3f(cos(36*(i+1)*pi/180)*7,sin(36*(i+1)*pi/180)*7,-5);
}
glEnd();*/
glColor4f(1,1,1, 1);
//glDisable(GL_DEPTH_TEST);
//glDepthMask(GL_TRUE);
//glEnable(GL_ALPHA_TEST);
glBindTexture(GL_TEXTURE_2D, tex[2]);

glPushMatrix();
glTranslatef(obj[0].x,obj[0].y,obj[0].z);
glScalef(0.5,0.5,0.5);
glRotatef(der2*5,0.7,0.3,0.5);
//glBindTexture(GL_TEXTURE_2D,tex[1]);
glBegin(GL_QUADS);
//alt
glNormal3f(0,0,-1);
glTexCoord2i(0,0);
glVertex3f(-8,8,-8);
glTexCoord2i(1,0);
glVertex3f(8,8,-8);
glTexCoord2i(1,1);
glVertex3f(8,-8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
//üst
glNormal3f(0,0,1);
glTexCoord2i(0,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,8);
//sa?
glNormal3f(1,0,0);
glTexCoord2i(0,0);
glVertex3f(8,8,-8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,8);
glTexCoord2i(0,1);
glVertex3f(8,-8,-8);
//sol
glNormal3f(-1,0,0);
glTexCoord2i(0,0);
glVertex3f(-8,8,-8);
glTexCoord2i(1,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,1);
glVertex3f(-8,-8,8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
//yukar?
glNormal3f(0,1,0);
glTexCoord2i(0,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,8,-8);
//a?a??
glNormal3f(0,-1,0);
glTexCoord2i(0,0);
glVertex3f(-8,-8,8);
glTexCoord2i(1,0);
glVertex3f(8,-8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
glEnd();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
/* glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);*/
//resim_yukle("font.bmp");
SDL_GL_SwapBuffers();
}

Help please, Thanks for your helps.
Lazy Foo
Lazy Foo
You need to enable blending, set your blending calculation, and you should probably disable depth testing as it doesn't mix will with alpha blending.

You probably want this somewhere in your initialization code:
[source lang="cpp"]glEnable( GL_BLEND );
glDisable( GL_DEPTH_TEST );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );[/source]
I got a tutorial on blending and transparency with OpenGL
Learn to make games with my SDL 2 Tutorials
Graphic_Programmer
Graphic_Programmer

You need to enable blending, set your blending calculation, and you should probably disable depth testing as it doesn't mix will with alpha blending.

You probably want this somewhere in your initialization code:
[source lang="cpp"]glEnable( GL_BLEND );
glDisable( GL_DEPTH_TEST );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );[/source]
I got a tutorial on blending and transparency with OpenGL

Yes, i know but when i put glDisable(GL_DEPTH_TEST) to initialization, the red box looked on the transparent letter "A"(my 2D Ortho graphic).I think, the box must be under the 2D letter.
This is screenshot when i put glDisable(GL_DEPTH_TEST);
adszteo.png
Lazy Foo
Lazy Foo
In that case you want to
1) Draw your 3D scene first with depth buffer enabled.
2) Clear/Disable the depth buffer and enable blending.
3) Draw the text over the 3D scene.
4) Swap buffers to update the screen.
Learn to make games with my SDL 2 Tutorials
Graphic_Programmer
Graphic_Programmer

In that case you want to
1) Draw your 3D scene first with depth buffer enabled.
2) Clear/Disable the depth buffer and enable blending.
3) Draw the text over the 3D scene.
4) Swap buffers to update the screen.

Do you think i must change 2-3 and 1? OK i will try

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.