0
\$\begingroup\$

I'm programming a 3D game engine with 2D GUI. The GUI objects have partially transparent textures and the transparency works more or less. The sky color is rendered in transparent areas of the textures but the 3D objects aren't.

This is my initialization code:

private static void initializeOpenGL() {

    //Enabling OpenGL functions
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_BLEND);

    //Using them
    glCullFace(GL_BACK);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}

And this is my code to switch from 2D to 3D and vice versa. I am confident it is being run at the correct time.

public static void switchTo2D() {

    if (currentRenderDimension != Dimension.DIM_2) {

        glMatrixMode(GL_MODELVIEW);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, Display.getWidth(), Display.getHeight(), 0);
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
        glMatrixMode(GL_MODELVIEW);

        glPushMatrix();
        glLoadIdentity();

        currentRenderDimension = Dimension.DIM_2;

    }
}

public static void switchTo3D() {

    if (currentRenderDimension != Dimension.DIM_3) {

        glPopMatrix(); //From 2D
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(PreferenceHelper.getInteger("fieldOfView"), (float) Display.getWidth() / Display.getHeight(), 0.1f, 1000);
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
        glMatrixMode(GL_MODELVIEW);

        currentRenderDimension = Dimension.DIM_3;
    }
}

I believe it should correctly switch the settings, but clearly it does not:

mis-rendering

I have verified that the .png textures ARE transparent.

\$\endgroup\$
1
  • \$\begingroup\$ I think it would be a lot easier if you were using modern OpenGL. \$\endgroup\$
    – Lysol
    Commented Mar 30, 2014 at 2:44

1 Answer 1

1
\$\begingroup\$

The code you are using to switch between 2D and 3D seems OK. Your problem should be on the GL states. Usually, when rendering sprites and 2D GUI, I use the following states:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);

Then when switching back to 3D, I normally set:

glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the reply! I tried your suggestion, but it doesn't change anything. In addition, disabling the depthTest results in the 2D objects being behind the 3D objects. Is there a way to bring the 2D stuff to the front? \$\endgroup\$
    – mojomex
    Commented Mar 30, 2014 at 19:01
  • \$\begingroup\$ When drawing without depth test, the draw order is the order you get. So just make sure the 2D elements are drawn last and they will appear on top of the stuff rendered with depth enabled. \$\endgroup\$
    – glampert
    Commented Mar 30, 2014 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .