game information display - sticking to the camera view

Hi,

How do I control an object so it looks like it is “stuck” to the window?

i.e. as in game information displays?

I have tried drawing the game info before my rotations of the camera but nothing is drawn…?

Any ideas.

Cheers

Alkon

Hi !

If the information is 2D then I would suggest you switch to orthographic mode and draw it, then switch to perspective view and draw the rest of the stuff.

Mikael

Check your object’s coordinates again, maybe they are too close and the near clipping plane is getting them.

renderYourFrame();
glPopMatrix(); //assuming you already used glPushMatrix();
glClear(GL_DEPTH_BUFFER_BIT);
renderYourInterface();

Nice idea about Orthog!

How would this help when the camera swings round?

Alkon - v.confused

Isn’t there also a way to temporarly disable GL_DEPTH ?

And thus whatever you draw then will come up in front, no matter what’s in front of it.
(Note: distance will affect size, unless in ortho 2D)

I once tried to make a window popup in orthographic mode, but I just ended up losing
my 2 planes with textures I had drawn.

Hope I could help…

[This message has been edited by Red15 (edited 05-07-2003).]

Yeah Red15, calling glDisable(GL_DEPTH_TEST) might be quicker than clearing the depth buffer. I’ll have to try that since I don’t have any 3d elements in my interface.

You don’t need to switch around between orthographic and projection matrices though. In projection, just make sure your camera is facing straight down the z-axis and calculate the height and width of the textured poly in proportion to your window size in pixels and it’ll be displayed pixel for pixel.

Thanks guys. I’ll try this tonight!

Ok I have given this a try and I can’t get it to work…

When should I call the gluLookAt()?

During or after I draw my scene?
Or after I draw the game display?

Cheers

Alkon

Alkon,

I would suggest that you finish rendering your entire scene as you normally would. And then disable depth test, as already posted. Then, set the matrix mode to projection, and load the identity matrix. Then you can make your ortho calls to set up the 2D window the way you want it.

I just downloaded some tutorial code from NeHe which suggests the very same cheers!

See if I can get it working!

Alkon

I’ve been working on an example of my own for the last day(s).
One that still needed a HUD.

This is the way I got it :

glModelview(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 640.0, 480.0, 0.0, 1.0, 100.0);
glModelview(GL_MODELVIEW);

// Note : the (0,0) point is now located
// in the upper left corner (like any normal
// 2d program)

glTranslatef(0.0, 0.0, -2.0);
// I have to move a few pixels backward
// since the zNear value = 1.0 and otherwise
// our hud would be clipped.

// Just draw here.

// And now set the display to Perspective
// using your favorite function.

// and then use gluLookAt();

Also:
I’ve tried glDisable(GL_DEPTH_TEST)
But it seemed to only work with lines.
And even then if the lines were behind textured polygons they dissapeared.
Any ideas on this?

-Red15-

Ok u figured it out. But still take a look at this.

Here is the solution. It happens that I was working on the same topic when I read your post! The Camera routines have been taken from the tutorials at www.gametutorials.com and the actual code that takes care of displaying the HUD has been taken from the program skydome from www.spheregames.com. I have combined them with my cut ‘n’ paste planet-moon test program.


The main program :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include “support.h”

#define PI 3.14159
#define SPEED 0.05

GLUquadricObj *quadric;
GLfloat xrot,yrot,zrot;
int WindW, WindH;
Camera c;

void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5, 0.5, -0.5, 0.5, 1, 1000);
glMatrixMode(GL_MODELVIEW);

WindW = width;
WindH = height;
}

void Draw(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(1.0f);

glLoadIdentity();
gluLookAt(c.p.x, c.p.y, c.p.z, c.v.x, c.v.y, c.v.z, c.u.x, c.u.y, c.u.z); 

glColor3ub(0,255,0);
glTranslatef(0,0,0);
for(float i = -50; i &lt;= 50; i += 5)
{
	glBegin(GL_LINES);
	glVertex3f(-50, 0, i);
	glVertex3f(50, 0, i);
	glVertex3f(i, 0, -50);
	glVertex3f(i, 0, 50);
	glEnd();
}

static float angle,angle2;

quadric=gluNewQuadric();
gluQuadricNormals(quadric,GLU_SMOOTH);
glColor3ub(100,0,200);
glPushMatrix();

glTranslatef(0,0,0);
glColor3ub(255,255,0);
gluSphere(quadric,2,32,32);

glColor3ub(0,0,255);
glTranslatef(20*cos(angle),30*sin(angle),0);
gluSphere(quadric,2,32,32);
glColor3ub(255,255,255);
glTranslatef(10*cos(angle2),10*sin(angle2),0);
gluSphere(quadric,2,32,32);

glPopMatrix();

angle+=.04;
angle2+=.08;
xrot+=.1f;
yrot+=.1f;
zrot+=.1f;

// This is the code that does the HUD display

glLoadIdentity();
glPushAttrib(GL_DEPTH_BUFFER_BIT);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	  glPushMatrix();
		glLoadIdentity();
		glDisable(GL_DEPTH_TEST);
		gluOrtho2D (0, 10, 10,0 );
		glColor3ub(255,255,255);
		gprintf("HUD DISPLAY",1,1);
	  glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
glPopAttrib();
glLoadIdentity();

glFlush();  
glutSwapBuffers();

}

static void Key(unsigned char key, int x, int y)
{
switch (key)
{
case 27: exit(0);
break;

case 97:
case 65:
	c.RotateAroundPoint(c.v,-SPEED, 0, 1, 0);
	break;

case 87:
case 119:
	c.MoveCamera(SPEED);
	break;

case 83:
case 115:
	c.MoveCamera(-SPEED);
	break;

case 68:
case 100:
	c.RotateAroundPoint(c.v,SPEED, 0, 1, 0);		
	break;

}
}

void timf(int value)
{
glutPostRedisplay();
glutTimerFunc(20, timf, 0);
}

int main(int argc, char *argv[])
{

WindW = 400;
WindH = 400;

c.PositionCamera(0,10,86,0,1,0,0,1,0);

//glutInit(&argc, argv);

/* glutInit(…) will cause an illegal operation if u are
compiling this program as a Win32 app (not console app)
under VC++. Decomment that line for a console app.
*/

glutInitWindowPosition((800-WindW)/2,(600-WindH)/2);
glutInitWindowSize(WindW, WindH);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
(void)glutCreateWindow(“HUD Display”);
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
glutKeyboardFunc(Key);
glutTimerFunc(20, timf, 0);
glClearColor(0, 0, 0, 1);
glutMainLoop();

return 0;
}


support.h :

struct Point3d
{
public:
Point3d() {}
Point3d(float X, float Y, float Z)
{
x = X; y = Y; z = Z;
}
Point3d operator+(Point3d vVector)
{

	return Point3d(vVector.x + x, vVector.y + y, vVector.z + z);
}
Point3d operator-(Point3d vVector)
{

	return Point3d(x - vVector.x, y - vVector.y, z - vVector.z);
}
Point3d operator*(float num)
{

	return Point3d(x * num, y * num, z * num);
}
Point3d operator/(float num)
{

	return Point3d(x / num, y / num, z / num);
}
float x, y, z;						

};

class Camera {

public:
Point3d p;
Point3d v;
Point3d u;

Camera();	
void PositionCamera(float positionX, float positionY, float positionZ,
		    float viewX,     float viewY,     float viewZ,
		    float upVectorX, float upVectorY, float upVectorZ);
void RotateAroundPoint(Point3d vCenter, float angle, float x, float y, float z);
void MoveCamera(float speed);

};

Camera::Camera()
{
Point3d vZero = Point3d(0.0, 0.0, 0.0);
Point3d vView = Point3d(0.0, 1.0, 0.5);
Point3d vUp = Point3d(0.0, 0.0, 1.0);

p	= vZero;					
v	= vView;					
u	= vUp;	

}

void Camera::PositionCamera(float positionX, float positionY, float positionZ,
float viewX, float viewY, float viewZ,
float upVectorX, float upVectorY, float upVectorZ)
{
Point3d vPosition = Point3d(positionX, positionY, positionZ);
Point3d vView = Point3d(viewX, viewY, viewZ);
Point3d vUpVector = Point3d(upVectorX, upVectorY, upVectorZ);

p = vPosition;					
v = vView;					
u = vUpVector;					

}

void Camera::RotateAroundPoint(Point3d vCenter, float angle, float x, float y, float z)
{
Point3d vNewPosition;

Point3d vPos = p - vCenter;

float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

vNewPosition.x  = (cosTheta + (1 - cosTheta) * x * x)		* vPos.x;
vNewPosition.x += ((1 - cosTheta) * x * y - z * sinTheta)	* vPos.y;
vNewPosition.x += ((1 - cosTheta) * x * z + y * sinTheta)	* vPos.z;

vNewPosition.y  = ((1 - cosTheta) * x * y + z * sinTheta)	* vPos.x;
vNewPosition.y += (cosTheta + (1 - cosTheta) * y * y)		* vPos.y;
vNewPosition.y += ((1 - cosTheta) * y * z - x * sinTheta)	* vPos.z;

vNewPosition.z  = ((1 - cosTheta) * x * z - y * sinTheta)	* vPos.x;
vNewPosition.z += ((1 - cosTheta) * y * z + x * sinTheta)	* vPos.y;
vNewPosition.z += (cosTheta + (1 - cosTheta) * z * z)		* vPos.z;

p = vCenter + vNewPosition;

}

void Camera::MoveCamera(float speed)
{
Point3d vVector = v - p;

p.x += vVector.x * speed;		
p.z += vVector.z * speed;		
v.x += vVector.x * speed;		
v.z += vVector.z * speed;		

}

#include<string.h>

void gprintf(char *str,int x,int y)
{
int i,l=strlen(str);

glRasterPos2i(x,y);
for(i=0;i<l;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,*str++);
}


Hope this helps.

Regards.

Wow!

What a reply!

Cheers. I actually got this working a couple of nights ago, by simply going into 2D mode. Although I had a bitch of a time getting the pop and push matroix stuff right.

Thanks for the help guys.

Alkon