Camera without GLUT

Hi all

Just wondering if anyone has seen tutorials or functions for “moving the camera” without needing the glut.dll - I am basically looking for a function exactly the same as gluLookAt, which looks like this (in delphi)

gluLookAt: procedure(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz: GLdouble); stdcall;

takes x,y,z of camera, x,y,z of point its looking at, x,y,z of “up” direction

Any help greatly appreciated! And i don’t mind if its in c++ or anything else…

Cheers,
Peter

p.s. I don’t want to use any dll at all!

Hi !

First of all, gluLookAt() is NOT a part of GLUT, it is a part of the OpenGL utility library (GLU), but GLU is always available when you have OpenGL installed with maybe a few exceptions like embedded system or something.

If you do feel so bad about using glu32.dll I would suggest that you download Mesa or the OpenGL sample application, this includes the source code for gluLookAt so that you can use that and put it in your own code (check the licence text first).

Mikael

So GLU is part of the standard openGL distribution? Does that mean that I can call functions from it and NOT include the DLL with my program when I distribute that?

Thanks for the quick replies BTW…

Peter

Anyway, I am also trying to figure out how to do proper matrix stack operations. I want to work out the transforms I need to move the scene with respect to the viewer (i.e camera), and place that on the bottom of the stack. Then I want to put all the ‘local’ transformations on top of that (i.e moving objects around the scene which is already nicely aligned with the viewer.

therefore, learning how others move the camera would be very beneficial

Search for redbook.pdf on the web. It’s the OpenGL Programming Guide, all your previous questions are answered in there.

Originally posted by peter.vullings:
[b]So GLU is part of the standard openGL distribution? Does that mean that I can call functions from it and NOT include the DLL with my program when I distribute that?

Thanks for the quick replies BTW…

Peter[/b]

Yes, because everyone that can run an opengl program has glu dll on his system. It’s always included with opengl ! (glu is NOT glut !)

Hi !

Yes, you can assume that GLU is included with OpenGL so you do not have to include the glu (and should not) dll with your application.

Mikael

sweet thanks people

I believe that glut stands for graphics library utility toolkit. Since oplengl was designed to be platform independent, they did not include alot of useful functions with it (for a good reason). I believe that glut can only be used with windows. It contains a set of useful functions like accepting joystick input etc. To write a program without glut you have to know a little bit more about windows programming but glut is not necessary for OpenGL programming.

to create a camera you can do something simple like this. Create a camera struct:

struct camera
{
float orientation[3];
float position[3];
} CAMERA_X

this simple struct reprents the cameras potition in a 3D graph (XYZ). It also represents the cameras orientation. You can change the x position for instance by coding:

CAMERA_X.position[2] +=1;

or change the rotation about the x axis with the following code (similar to looking down/up):

CAMERA_X.orientation[0] += 1;

Then you just need to change the transformation pipeline so that it has the correct transformation matrix. You can do this I believe with gluLookAt(). I however use this code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(cameraData.orientation[0], 1,0,0);
glRotatef(cameraData.orientation[1], 0,1,0);
glRotatef(cameraData.orientation[2], 0,0,1);
glTranslatef(-cameraData.position[0],
-cameraData.position[1],
cameraData.position[2]);

Note: you only need to call glMatrixMode(GL_MODELVIEW) if the modelview matrix is not the currently selected matrix. The first two lines set the MODELVIEW matrix to the current matrix and reset it to eye coordinates. This means that whatever orientation you were at before is erased and you will be looking down the negative Z axis from the origin. The next lines perform the rotations and translations. It is absolutely necessary to rotate first and then translate. Trust me.

OOPs the code should be:

glRotatef(CAMERA_X.orientation[0], 1,0,0);
glRotatef(CAMERA_X.orientation[1], 0,1,0);
glRotatef(CAMERA_X.orientation[2], 0,0,1);
glTranslatef(-CAMERA_X.position[0],
-CAMERA_X.position[1],
CAMERA_X.position[2]);

Hey thanks very much! Exactly the kind of thing I’m after!

The reason your camera Z coordinate is not negative in the glTranslatef - are you doing this because you don’t like how -ve is in to the screen in openGL?

So now, if I push another matrix on to the MODELVIEW stack and position my objects in the scene just as if the camera was still at 0,0,0, it would work as expected?

I mean, without the camera moved, if I draw a house just left of the origin, it would appear on the screen just left of the origin. After moving the camera, can I then just do a glTranslatef to move the house to the left just as without moving the camera?

p.s. Im not getting email notifications that there have been posts. Does anyone have that same problem?

[This message has been edited by peter.vullings (edited 06-12-2003).]

As for why the x and y rotations are negative, it simply changes the direction you rotate. The z axis maybe could be negative. In my program I do not use a z rotation (this could have been entered as the integer constant 0) so I am not sure how making that negative would effect movement. A z axis rotation would be the same as a roll in a flight simulation. An x rotation would be like looking up/down. A y rotation is the same as looking right or left.

Here is an example of how you can place objects:

void setCamera()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(CAMERA_X.orientation[0], 1,0,0);
glRotatef(CAMERA_X.orientation[1], 0,1,0);
glRotatef(CAMERA_X.orientation[2], 0,0,1);
glTranslatef(-CAMERA_X.position[0],
-CAMERA_X.position[1],
CAMERA_X.position[2]);
}

void checkKeyPresses()
{

if (GetAsyncKeyState(VK_LEFT) )
{
CAMERA_X.position[0] += sin(DEGTORAD(CAMERA_X.orientation[1]-90)) * linearSpeed;

CAMERA_X.position[2] += cos(DEGTORAD(CAMERA_X.orientation[1]-90)) * linearSpeed;
}

if (GetAsyncKeyState(VK_RIGHT))
{
CAMERA_X.position[0] += sin(DEGTORAD(CAMERA_X.orientation[1]+90)) * linearSpeed;
CAMERA_X.position[2] += cos(DEGTORAD(CAMERA_X.orientation[1]+90)) * linearSpeed;
}

if (GetAsyncKeyState(VK_UP))
{
CAMERA_X.position[0] += sin(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed;
CAMERA_X.position[2] += cos(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed;
}

if (GetAsyncKeyState(VK_DOWN) )
{
CAMERA_X.position[0] -= sin(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed;
CAMERA_X.position[2] -= cos(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed;
}

if (GetAsyncKeyState(VK_INSERT))
{
linearSpeed += .1;
angularSpeed += .1;
}

if (GetAsyncKeyState(VK_DELETE))
{
if (!(linearSpeed < 0)) linearSpeed -= .1;
if (!(angularSpeed < 0)) angularSpeed -= .1;
}
}

void RenderScene(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

checkKeyPresses();
setCamera();



glBegin(GL_LINES);
	glColor3f(1,0,0);
	glVertex3f(-100,0,0);
	glVertex3f(100,0,0);
	
	glColor3f(0,1,0);
	glVertex3f(0,-100,0);
	glVertex3f(0,100,0);
	
	glColor3f(0,0,1);	  
	glVertex3f(0,0,-100);
	glVertex3f(0,0,100);

glEnd();

The render code first clears the screen. Then I call code to check for key presses (this moves the camera by changing the camera struct data). Then I set where the camera is supposed to be. Then I place the primitives. I know many programmers will often create an object at the origin and then translate/rotate the object to its place. I have not tested this method with my camera model although I do not see why it would not work. It is a method I am not to familiar with although I am studying it. Up till now I have been simply placeing objects at their absolute location.

glBegin(GL_POINTS);
glVertex3f(10,0,0);
glEnd();

will place a point 10 units out on the x axis. If you are oriented to look out from the origin along the positive x axis you will see the point directly in front of you. If you move to orient yourself to look down the negative z axis from the origin the point will be to your right. Hope this helps. Let me know if you have success with the other method (placing at center and then translating out).

I know many programmers will often create an object at the origin and then translate/rotate the object to its place. I have not tested this method with my camera model although I do not see why it would not work. It is a method I am not to familiar with although I am studying it. Up till now I have been simply placeing objects at their absolute location.

This is what I want to do create objects at origian and translate/rotate them. I’ll give it a go this weekend. Works over for the week, so plenty of time!

Thanks for all your help!

I did some reading on the matter. This should work:

void RenderScene()
{
glClearColor(0,0,0,1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for(***;***;***)
{
glPushMatrix();
//Then draw the object at the origin
//Then do rotations and translations
glPopMatrix();
}

CheckKeyPresses();
setCamera();

}

What you are doing here is refferencing everything around the origin. Everytime you draw the object it is at the origin. Then you rotate and translate the object to it’s correct location in the scene graph. Then you pop yourself back to the origin for the next object (hence the for loop). Finally you check to see if the camera has moved and then position the camera with the code I posted earlier. Hope this works for you. Let me know what you come up with on your own.

So geo, in your example the camera translation is set up after the object rendering ready for the next frame?

I guess this makes sense because it allows the user to see the result of the last camera/player move before it is moved again…

Originally posted by geoHoffman:
I believe that glut can only be used with windows.

actually GLUT can be used with other OS’s as well.