3D environnement + 2D HUD help plz

Started by Wicpar, September 07, 2014, 15:34:53

Previous topic - Next topic

Wicpar

Hello!

I am quite a newb in lwjgl even if I use it for some time now, and I wonder what I should do to render a 3D environnement with a 2D HUD containing slick text.

I know it should do something like that:
init 3D rendering
render all 3D
unload the 3D stuff
init the hud rendering
render the hud
clear the screen


In this I would like to know if it affects performance too much(if yes an alternative is welcome) and what is the code I have to write so it loads the 3D environnement with shaders and then the HUD containing textures and text.

Thanks for your help :)

quew8

Essentially the difference between rendering 3D stuff and 2D stuff, is the projection matrix and the depth test. For 3D you are either going to have a perspective (frustum) projection or a 3D orthographic one and you are going to have the depth test enabled. For 2D you are going to have a flat (near and far depth set to 0) orthographic and have the depth test disabled.

So that is all there is to it:

Enable Depth Test
Setup 3D Projection Matrix
Draw 3D Stuff
Disable Depth Test
Setup 2D Projection Matrix
Draw 2D Stuff

Wicpar

ok thanks, so I only have to do that. but do I have to disable the 3D projection matrix or does it do it when you load the 2D one?
here is the code I have:
private static float	fov	= 45, znear3D = 0.01f, zfar3D = 1000, znear2D = 1, zfar2D = -1;

	public static void init2D() {
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);

		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glClearDepth(1);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
		glOrtho(0, Display.getWidth(), Display.getHeight(), 0, znear2D, zfar2D);
		glMatrixMode(GL_MODELVIEW);
	}

	public static void init3D() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		gluPerspective(fov, ((float) Display.getDisplayMode().getWidth()) / ((float) Display.getDisplayMode().getHeight()), znear3D, zfar3D);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glEnable(GL_TEXTURE_2D);
		glShadeModel(GL_SMOOTH);
		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glClearDepth(1.0f);
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LEQUAL);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	}


Is it right or does it need some optimisation?

Mickelukas

Mine looks like this:

    public static void make2D() {
        //Remove the Z axis
        GL11.glDisable(GL11.GL_LIGHTING);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glPushMatrix();
        GL11.glLoadIdentity();
        GL11.glOrtho(0, Main.getAppletSize().getWidth(), 0, Main.getAppletSize().getHeight(), -1, 1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPushMatrix();
        GL11.glLoadIdentity();
    }

    public static void make3D() {
        //Restore the Z axis
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glPopMatrix();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPopMatrix();
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_LIGHTING);
    }

Wicpar

ok thanks, but how should I do from your code to init the other stuff? and make the window resizable?

Mickelukas

The code I sent was purely the overlay conversion, it doesn't have anything to do with resizing the window...?

Mike

Wicpar

ok, was just wondering how to make the window resizable from that code, but nevermind, i'm following the thinatrix tutorials