Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Problem in transparent

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2

    Problem in transparent

    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).

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    9
    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.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    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.

  4. #4
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2
    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.....

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Post a pic and I will see if I can tell what is happening

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •