Skip to main content
GameDev.net gamedev.net
🔒 Locked

Drawing a interface

Started by Cbswe Apr 19, 2009 at 12:38 PM 6 replies 900+ views
Original Post
Cbswe
Cbswe
Hello! I'm trying to find a solution to drawing a interface. In display(), after all gluLookAt() has been called and all the lists has been drawn, I'd like to draw a interface that should have a static position at the screen. However, I'm running a blank trying to figure out how to translate the coordinates of the interface to the front of the player. Should I do it mathematically or is there a better solution? Thanks in advance! Carl
Zakwayda
Zakwayda
Quote:
Original post by Cbswe
Hello!

I'm trying to find a solution to drawing a interface.

In display(), after all gluLookAt() has been called and all the lists has been drawn, I'd like to draw a interface that should have a static position at the screen.

However, I'm running a blank trying to figure out how to translate the coordinates of the interface to the front of the player. Should I do it mathematically or is there a better solution?

Thanks in advance!
Carl
The typical solution is as follows:

1. Push the modelview and projection matrix stacks
2. Set both to identity
3. Set up an orthographic projection
4. Render UI elements
5. Pop both stacks
Cbswe
Cbswe
Quote:
Original post by jyk
Quote:
Original post by Cbswe
Hello!

I'm trying to find a solution to drawing a interface.

In display(), after all gluLookAt() has been called and all the lists has been drawn, I'd like to draw a interface that should have a static position at the screen.

However, I'm running a blank trying to figure out how to translate the coordinates of the interface to the front of the player. Should I do it mathematically or is there a better solution?

Thanks in advance!
Carl
The typical solution is as follows:

1. Push the modelview and projection matrix stacks
2. Set both to identity
3. Set up an orthographic projection
4. Render UI elements
5. Pop both stacks


Thanks for answering!

I created a method that is called after the list wich looks like this:

public void renderInterface(){
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0f, 550f, 450f, 0.0f, -1.0f, 1.0f);
gl.glColor3i(1,1,1);
gl.glEnable(GL.GL_TEXTURE_2D);
hp.enable();
hp.bind();
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0, 0);gl.glVertex2f(10f,500f);
gl.glTexCoord2f(1f, 0);gl.glVertex2f(15f,500f);
gl.glTexCoord2f(1f, 1f);gl.glVertex2f(15f,505f);
gl.glTexCoord2f(0, 1f);gl.glVertex2f(10f,505f);
gl.glEnd();
hp.disable();
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glPopMatrix();
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glPopMatrix();

}


hp is a texture and currently the only part of the UI I'm trying to draw.

This method however doesn't seem to show any effect. Am I doing something wrong?
Zakwayda
Zakwayda
Is the quad inside the rectangle you've defined with glOrtho()?
Cbswe
Cbswe
Quote:
Original post by jyk
Is the quad inside the rectangle you've defined with glOrtho()?


You were right, it wasn't!

Now I actually got the texture at a static position on the screen. However it is unlit (only the contours are presented).
Is there a good solution to this as well, or do I have to be creative?
Cbswe
Cbswe
I solved it with disable lightning in the beginning of the method and enabling it again in the end. Don't seem to affect performance..
Cbswe
Cbswe
The interface works great!

However, now I'm about to implement "weapons" to my game and I want them to be affected by the lightning,
and I'm not sure how to do this.


I sat down to try and find a good solution to calculate the actual values to use if you want something to move simultaneously in front of you. I sort of landed on the solution that you have to calculate a vector perpendicular to the your current direction vector (your position -> the point your looking at) that points directly "upwards" from the players point of view that can define the y-axis and one that is perpendicular to both the direction vector and the "upwards" vector that can define the "x-axis". However, this is a little too hard for me, since I'm not that used to 3-space math yet. I thought of trying to calculate the cross product or something similar, but it's not really right.



Anyway, if there is any GL-function that can make this transformation for me, that would be great.

Any possible solution to this is greatly appreciated!
mightypigeon
mightypigeon
There is a simple way for rendering a gun in front of you. If you are doing matrix handling yourself, take the inverse of the modelview matrix and multiply that by your modelview matrix before rendering your gun.

Or, more simply, just load an identity matrix.

So in your rendering you would do something like this:

glMatrixMode( GL_MODELVIEW );SetCamera( ); // do gluLookAt etcRenderScene( );glLoadIdentity( );glTranslatef( 2, 0, -5 ); // Move the gun into the screen and to the right a littleRenderGun( );// render user interface etc


Are you just using standard OpenGL lighting or are you using a fragment shader to do your own lighting? If you're using a fragment shader (or even a vertex shader for per vertex lighting) I send two seperate matrices to it - the matrix I created above for the gun position, and the normal camera matrix for doing the lighting calculations (getting the light vector).

Hope this helps a bit!
[size="1"]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.