PDA

View Full Version : Problem in transparent



ghghgghh
11-02-2012, 04:55 AM
my purpose is to create a big semi-transparent cube box, and inside of it contains a non-transparent sphere.

here is my simplified code:

GLUquadricObj* object;
object = gluNewQuadric();
gluQuadricNormals(object, 0);

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

while(true)
{
glPushMatrix();
drawCube(); //this function contains glVertex3fs to draw a cube.

glPushMatrix();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gluSphere(object, 0.25, 25, 25);
glPopMatrix();

glPopMatrix();
}

the problem i encounter is, the sphere seems appear infront of the box (instead of inside of the box).
http://img37.imageshack.us/img37/3734/46595602.png

ssallen
11-02-2012, 09:49 AM
Hello, your problem is that Opengl blend functions expect the geometry to be ordered from furthest to nearest. That is the front(s) of your box are closest to the camera so they need to be drawn AFTER the sphere. You will need to break up the drawing of the box into what is in front of the sphere and what is behind it.

As an aside, I really wouldn't waste a lot of time learning the fixed function pipeline.

http://en.wikibooks.org/wiki/Opengl

Is an excellent resource to learn modern opengl programming with shaders and such. Learning the old fixed function pipeline will (IMO) just frustrate you more when you move on. Also, most of it is deprecated and you run the risk of it not working well on new graphics cards.

Steve A.

tonyo_au
11-02-2012, 11:05 PM
You need to draw the sphere first. and enable depth testing. For blending to work correctly you must draw opaque objects first then the translucent objects furtherest from the camera first.

ghghgghh
11-03-2012, 01:30 AM
thankyou for your reply :)

so i changed my code:

glEnable(GL_DEPTH_TEST);
while(true)
{
glPushMatrix();

glPushMatrix();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gluSphere(object, 0.25, 25, 25);
glPopMatrix();

drawCube();

glPopMatrix();

and yes the box appear in the box and it is not transparent anymore, but some surface of my box is become black in colour. i assume is these surface has been culling out by depth test function.....

tonyo_au
11-03-2012, 08:32 PM
Post a pic and I will see if I can tell what is happening