"camera" in 3D scene

if i make a 3d scene with a bunch of quads and pyramids everywhere, if i make a "camera" that i can move around the world with, do i draw the scene around the origin (0,0,0) then move the camera to where i want to view then render that. or do i draw the scene as the camera as the origin?

or is there another way?

Jonatan Hedborg

A camera is (afaik) just a transformation. Once you have your scene ready, modify it by the inverse of the camera matrix. Ie, if your camera is at 5,0,0 - you move all world coords by -5, 0, 0.

can you give an example of how to do that. because if i remember correctly i tried it with glTranslatef(...) and glRotatef(...) before i did allegro_gl_flip(), and it didnt have an effect.

Jonatan Hedborg

In openGl you could use the simple

```void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz )
```

function.

But if you dont want to.. hm. well, my ogl is a bit rusty, but before you draw your scene, do a translate in the negative of the cameras position, and then a rotate in the negative of the cameras rotation. Push this and pop it for each object you want to draw.
If you do it before allegro_gl_flip(), no vertices will be affected by it.

im using allegroGL which from what i understand doesnt use GLU which that function does. is there an allegrogl function to do it?

Jonatan Hedborg

There is nothing preventing you from including glu and using it.
AllegroGL is simply a layer which sets up OpenGL together with allegro + some helper functions and defintions. And some other useful stuff I'm sure, but i don't know them

Anyway, anything you can do in openGL alone, you can do in allegroGl. AFAIK.

Archon
Quote:

A camera is (afaik) just a transformation. Once you have your scene ready, modify it by the inverse of the camera matrix. Ie, if your camera is at 5,0,0 - you move all world coords by -5, 0, 0.

You need to transform negative to the 'camera coords' before drawing. Just remember to use glPushMatrix and glPopMatrix rather than glLoadIdentity to reset the position of the models.

Quote:

im using allegroGL which from what i understand doesnt use GLU which that function does. is there an allegrogl function to do it?

You can use any GLU functions with AllegroGL - and I think that AllegroGL does use GLU in multiple places.

Jonatan Hedborg

Right, a bit of bad wording on my part there.

ok thanks guys

ill see if i can get one of them to work.

EDIT:: Ok, i sorta got it to work...

the thing is the moving forward and backward works, but the strafing only works some times, or only if im facing certain directions. here are the parts of the code that affect the camera movement.

camera struct

```struct CAMERA
{
float xoff, yoff, zoff;
float xfacing, yfacing, zfacing;
float xrot, yrot;
int mousex, mousey;
};
```

input routine

 1 void input(CAMERA* camera) 2 { 3 poll_keyboard(); 4 poll_mouse(); 5 if(key[KEY_W]) 6 { 7 camera->xoff -= (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 8 camera->zoff -= (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 9 camera->xfacing -= (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 10 camera->zfacing -= (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 11 } 12 if(key[KEY_S]) 13 { 14 camera->xoff += (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 15 camera->zoff += (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 16 camera->xfacing += (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 17 camera->zfacing += (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 18 } 19 if(key[KEY_A]) 20 { 21 camera->zoff -= (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 22 camera->xoff -= (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 23 camera->zfacing -= (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 24 camera->xfacing -= (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 25 } 26 if(key[KEY_D]) 27 { 28 camera->zoff += (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 29 camera->xoff += (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 30 camera->zfacing += (float)sin((camera->yrot*(PI/180.0))) * 0.05f; 31 camera->xfacing += (float)cos((camera->yrot*(PI/180.0))) * 0.05f; 32 } 33 if(camera->mousex != mouse_x || camera->mousey != mouse_y) 34 { 35 camera->yrot = -(mouse_x - camera->mousex)/4; 36 camera->xrot = -(mouse_y - camera->mousey)/4; 37 } 38 };

setting the "view"

```  glLoadIdentity();

glRotatef(-camera->xrot, 1.0f, 0.0f, 0.0f);
glRotatef(-camera->yrot, 0.0f, 1.0f, 0.0f);
gluLookAt(camera->xoff,camera->yoff,camera->zoff,camera->xfacing,camera->yfacing,camera->zfacing,0,1,0);
```