Making a HUD/GUI/Overly work with 3D

Started by geekygenius, February 10, 2012, 05:53:57

Previous topic - Next topic

geekygenius

So, I have been working on a game, And it was going great until I needed to add a 2D HUD/overly/GUI. As of now, I can fully render the 3D scene perfectly, and the 2D scene perfectly, but not at the same time. Here are the important parts of my source. I'm wondering what I'm doing wrong, why it's wrong, and what I should do to fix it.

Main class:
public void start() {
	map = new NoiseMapGenerator().generateMap(1280 / 16, 720 / 16, System.nanoTime(), null);
	mapRenderer = new DeafultMapRenderer();
	resizeScreen(1280, 720);
	mapRenderer.setMap(map);
	
	guiRenderer = new DeafultGUIRenderer();
	guiRenderer.initilizeRenderer(1280, 720);

	while (!Display.isCloseRequested()) {
	    mapRenderer.ready();
	    mapRenderer.render();// Works fine, cannot be source of the problem
	    
	    guiRenderer.ready();
	    guiRenderer.render();// Works fine, cannot be source of the problem


	    pollInput(); //Works fine, cannot be the source of the problem
	    updateDelta();
	    Display.update();
	}
	mapRenderer.destructRenderer();
	guiRenderer.destructRenderer();
	Display.destroy();
    }

    public void resizeScreen(int width, int height) {
	try {
	    Display.destroy();
	    Display.setTitle("Neutrality");
	    Display.setFullscreen(fullscreen);
	    Display.setDisplayModeAndFullscreen(new DisplayMode(width, height));
	    Display.create();

	    mapRenderer.initilizeRenderer(map, width, height);
	} catch (LWJGLException e) {
	    ErrorHandler.handleError(e, "Failed to initlize the screen.", true);
	}
    }


Default Map Renderer:
    public void ready() {
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	gluPerspective(80f, (float) width / height, .1f, 1000f);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glRotatef(-30, 1.0f, 0.0f, 0.0f);
	glTranslatef(-(width / 32), 0, -10);
    }

public void initilizeRenderer(Map map, int width, int height) {
	
	glMatrixMode(GL11.GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(80f, (float) width / height, .1f, 1000f);
	glMatrixMode(GL11.GL_MODELVIEW);
	
	//glPolygonMode(GL_BACK, GL_LINE);
	
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	
	setUpLighting();

	loadTexture("resources/grass.png", "grassTexture", GL_LINEAR, GL_LINEAR);
	loadTexture("resources/water.png", "waterTexture", GL_LINEAR, GL_LINEAR);
	loadTexture("resources/beach.png", "beachTexture", GL_NEAREST, GL_NEAREST);
	loadTexture("resources/snow.png", "snowTexture", GL_NEAREST, GL_LINEAR);
	loadTexture("resources/underwater.png", "underwaterTexture", GL_NEAREST, GL_LINEAR);
	loadTexture("resources/gravel.png", "gravelTexture", GL_NEAREST, GL_NEAREST);
	loadTexture("resources/error.png", "errorTexture", GL_NEAREST, GL_LINEAR);
	
	tree = new Model("resources/tree10.obj");
	city = new Model("resources/city.obj");

	glRotatef(-30, 1.0f, 0.0f, 0.0f);
	glTranslatef(-(width / 32), 0, -10);
	
    }


DeafultGUIRenderer:
    public void initilizeRenderer(int width, int height) {

    }

    public void ready() {
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glLoadIdentity();
	glOrtho(0, 1280, 720, 0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
	glLoadIdentity();
    }

    public void destructRenderer() {

    }

    public void render() {
	glDisable(GL_LIGHTING);
	glColor3f(1f, .5f, 1f);
	// draw quad
	glBegin(GL_QUADS);
	glVertex2f(100, 100);
	glVertex2f(100 + 200, 100);
	glVertex2f(100 + 200, 100 + 200);
	glVertex2f(100, 100 + 200);
	glVertex2f(0, 0);
	glVertex2f(1, 0);
	glVertex2f(1, 1);
	glVertex2f(0, 1);
	glEnd();
	glEnable(GL_LIGHTING);
    }


Thanks for any help, I know this might be a basic question, but I'm just starting out with working with 3D from scratch, and I'm only beginning to understand some parts of OpenGL.

Fool Running

Without knowing exactly what is going wrong (you never mentioned :P), I would guess your problem is that you are not calling glLoadIdentity() before the call to gluPerspective().

Another possibility is that you are enabling texturing for the map, but not disabling it for the GUI (which doesn't specify any texture coordinates).

Also, the pushMatrix and popMatrix in the ready() methods is a little weird, I would remove them.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

geekygenius

I guess I wasn't very clear...
What I want is a 3D map rendered behind a gui on the top. What I'm getting is just the GUI, the background is completely black.

I tried doing all the things you said would help and I got the same results both times.

Mickelukas

This is what I do to draw my gui.

    protected 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();
    }

    protected 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);
    }


//Draw world in 3d
make2d();
//Draw gui in 2d
make3d();

CodeBunny

Where do you initially set up 3D rendering.

geekygenius

Thank you SO MUCH! IT feels good now to finally have this problem out of the way. I just inserted the code into the proper places and now it works like a charm! Thanks!

Quote from: Mickelukas on February 11, 2012, 10:45:20
This is what I do to draw my gui.

    protected 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();
    }

    protected 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);
    }


//Draw world in 3d
make2d();
//Draw gui in 2d
make3d();


Mickelukas

No problems, I'm just happy that I can contribute something here instead of just asking for help :)

Mike