LWJGL - Mixing 2D and 3D

Posted by nathan on Game Development See other posts from Game Development or by nathan
Published on 2012-11-25T22:17:20Z Indexed on 2012/11/25 23:24 UTC
Read the original article Hit count: 264

Filed under:
|
|

I'm trying to mix 2D and 3D using LWJGL.

I have wrote 2D little method that allow me to easily switch between 2D and 3D.

protected static void make2D() {
    glEnable(GL_BLEND);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    glOrtho(0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 0.0f, 1.0f);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
}

protected static void make3D() {
    glDisable(GL_BLEND);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity(); // Reset The Projection Matrix
    GLU.gluPerspective(45.0f, ((float) SCREEN_WIDTH / (float) SCREEN_HEIGHT), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    glLoadIdentity();
}

The in my rendering code i would do something like:

make2D();
//draw 2D stuffs here

make3D();
//draw 3D stuffs here

What i'm trying to do is to draw a 3D shape (in my case a quad) and i 2D image. I found this example and i took the code from TextureLoader, Texture and Sprite to load and render a 2D image.

Here is how i load the image.

TextureLoader loader = new TextureLoader();
Sprite s = new Sprite(loader, "player.png")

And how i render it:

make2D();
s.draw(0, 0);

It works great.

Here is how i render my quad:

glTranslatef(0.0f, 0.0f, 30.0f);
glScalef(12.0f, 9.0f, 1.0f);
DrawUtils.drawQuad();

Once again, no problem, the quad is properly rendered.

DrawUtils is a simple class i wrote containing utility method to draw primitives shapes.

Now my problem is when i want to mix both of the above, loading/rendering the 2D image, rendering the quad. When i try to load my 2D image with the following:

s = new Sprite(loader, "player.png);

My quad is not rendered anymore (i'm not even trying to render the 2D image at this point). Only the fact of creating the texture create the issue. After looking a bit at the code of Sprite and TextureLoader i found that the problem appears after the call of the glTexImage2d. In the TextureLoader class:

glTexImage2D(target,
        0,
        dstPixelFormat,
        get2Fold(bufferedImage.getWidth()),
        get2Fold(bufferedImage.getHeight()),
        0,
        srcPixelFormat,
        GL_UNSIGNED_BYTE,
        textureBuffer);

Commenting this like make the problem disappear. My question is then why? Is there anything special to do after calling this function to do 3D? Does this function alter the render part, the projection matrix?

© Game Development or respective owner

Related posts about opengl

Related posts about java