🔒 Locked
camera problem
Advertisement
Original Post
ok how come when i set the camera position once it works but when i try to set it every frame it doesn''t work. i''m using gluLookAt.
i can''t remember how to change the current mattrix mode.
ok i know that i need to transform the viewing matrix but i can''t find how to switch to it.
you can set the current matrix with glMatrixMode();
also, as invective already said, you must not forget glLoadIdentity() before each call to glLookAt(), since glLookAt only multiplies the current matrix with the right viewmatrix.
if it still doesn''t work, you should check the parameters.
if you don''t want to use glLookAt() and you use a matrix for your camera-pos & "rotation", you can use my code (below).
first some description:
the FIRST function sets the camera ''absolute''. that means,
you don''t have to call glLoadIdentity();
the SECOND function sets the camera realtive to the current
modelview-matrix. you would have to call glLoadIdentity()
before, but you can still glRotate & Translate your scene
before (maybe doesn''t make sense... but for the case it''s
needed, i wrote it
ViewMatrix is a "temporary"-matrix & Matrix holds the
Position & "angle" of the Camera.
Matrix->X[0] is the x-part of the x-axis
Matrix->X[1] is the y-part of the x-axis
and so on...
''Moved'' is set ''true'', each time the camera was moved...
and the code:
void nxCamera::Set()
{
if (Moved)
{
ViewMatrix.X[0] = Matrix->X[0];
ViewMatrix.X[1] = Matrix->Y[0];
ViewMatrix.X[2] = Matrix->Z[0];
ViewMatrix.Y[0] = Matrix->X[1];
ViewMatrix.Y[1] = Matrix->Y[1];
ViewMatrix.Y[2] = Matrix->Z[1];
ViewMatrix.Z[0] = Matrix->X[2];
ViewMatrix.Z[1] = Matrix->Y[2];
ViewMatrix.Z[2] = Matrix->Z[2];
Moved = false;
}
glLoadMatrixf((float*)&ViewMatrix);
glTranslated(-Matrix->P[0], -Matrix->P[1], -Matrix->P[2]);
}
void nxCamera::SetRel()
{
if (Moved)
{
ViewMatrix.X[0] = Matrix->X[0];
ViewMatrix.X[1] = Matrix->Y[0];
ViewMatrix.X[2] = Matrix->Z[0];
ViewMatrix.Y[0] = Matrix->X[1];
ViewMatrix.Y[1] = Matrix->Y[1];
ViewMatrix.Y[2] = Matrix->Z[1];
ViewMatrix.Z[0] = Matrix->X[2];
ViewMatrix.Z[1] = Matrix->Y[2];
ViewMatrix.Z[2] = Matrix->Z[2];
Moved = false;
}
glMultMatrixf((float*)&ViewMatrix);
glTranslated(-Matrix->P[0], -Matrix->P[1], -Matrix->P[2]);
}
hope, this helps...
also, as invective already said, you must not forget glLoadIdentity() before each call to glLookAt(), since glLookAt only multiplies the current matrix with the right viewmatrix.
if it still doesn''t work, you should check the parameters.
if you don''t want to use glLookAt() and you use a matrix for your camera-pos & "rotation", you can use my code (below).
first some description:
the FIRST function sets the camera ''absolute''. that means,
you don''t have to call glLoadIdentity();
the SECOND function sets the camera realtive to the current
modelview-matrix. you would have to call glLoadIdentity()
before, but you can still glRotate & Translate your scene
before (maybe doesn''t make sense... but for the case it''s
needed, i wrote it
ViewMatrix is a "temporary"-matrix & Matrix holds the
Position & "angle" of the Camera.
Matrix->X[0] is the x-part of the x-axis
Matrix->X[1] is the y-part of the x-axis
and so on...
''Moved'' is set ''true'', each time the camera was moved...
and the code:
void nxCamera::Set()
{
if (Moved)
{
ViewMatrix.X[0] = Matrix->X[0];
ViewMatrix.X[1] = Matrix->Y[0];
ViewMatrix.X[2] = Matrix->Z[0];
ViewMatrix.Y[0] = Matrix->X[1];
ViewMatrix.Y[1] = Matrix->Y[1];
ViewMatrix.Y[2] = Matrix->Z[1];
ViewMatrix.Z[0] = Matrix->X[2];
ViewMatrix.Z[1] = Matrix->Y[2];
ViewMatrix.Z[2] = Matrix->Z[2];
Moved = false;
}
glLoadMatrixf((float*)&ViewMatrix);
glTranslated(-Matrix->P[0], -Matrix->P[1], -Matrix->P[2]);
}
void nxCamera::SetRel()
{
if (Moved)
{
ViewMatrix.X[0] = Matrix->X[0];
ViewMatrix.X[1] = Matrix->Y[0];
ViewMatrix.X[2] = Matrix->Z[0];
ViewMatrix.Y[0] = Matrix->X[1];
ViewMatrix.Y[1] = Matrix->Y[1];
ViewMatrix.Y[2] = Matrix->Z[1];
ViewMatrix.Z[0] = Matrix->X[2];
ViewMatrix.Z[1] = Matrix->Y[2];
ViewMatrix.Z[2] = Matrix->Z[2];
Moved = false;
}
glMultMatrixf((float*)&ViewMatrix);
glTranslated(-Matrix->P[0], -Matrix->P[1], -Matrix->P[2]);
}
hope, this helps...
ok that works but what i want to do is the camera to move with the player in a 2d birdseye view sort of thing. i was wondering how i can set the viewmatrix to current one and translate that. then switch back to modelview. i dont know the parameter to switch to viewing matrix.
don''t think there''s such a thing as a "ViewMatrix".
the ViewMatrix in my code is only a temporary one, that can be dircetly copied to the ModelViewMatrix, if the camera didn''t move.
btw... what do you call "2d birdseye" ? a top-down-view ?
to "switch" between the matrices you should
glPushMatrix(); // pushe the current matrix to stack
glLoadMatrixf (&MyMatrix); // load your "viewmatrix" for top-down
...
... // draw stuff
...
glPopMatrix(); // restore old matrix
don''t understand, why you would have to switch to one and switch back in one frame ?
the ViewMatrix in my code is only a temporary one, that can be dircetly copied to the ModelViewMatrix, if the camera didn''t move.
btw... what do you call "2d birdseye" ? a top-down-view ?
to "switch" between the matrices you should
glPushMatrix(); // pushe the current matrix to stack
glLoadMatrixf (&MyMatrix); // load your "viewmatrix" for top-down
...
... // draw stuff
...
glPopMatrix(); // restore old matrix
don''t understand, why you would have to switch to one and switch back in one frame ?
i mean looking straight down the z axis the default. i can''t get the camera to move left to right.
mmh... ''down the z-axis''... ok...
i assume you want to view your player from above... you would just have to use the player-position, move the camera up & rotate your camera view down... there are thousands of possibilities to do this... depends on what EXACTLY you want...
1) move ''around'' the player (on top) for 3rd-person view
2) have everything look like a 2D-game and view from above (as in action-games like raptor / gta / ...)
3) look into a 3d world from above
4) write a routine to create a top-view for whatever you want to look at, once, and be able to freely move it afterwards
5) have a top-view relative to the object you look at (if he/it falls/rotates, the camera also rotates)
6) have a top-view relative to your world (or absolute coordinates) - so it always looks onto the object but doesn''t rotate with him
7) ...
whatever you want to do... it also depends on how you manage your objects/camera... do you keep position and 3 angles in memory or do you have a matrix for every object (also the camera - as in my case & code - that''s what i recommend).
normally you would just have to move the camera to the object''s position, move it upwards & rotate the camera''s view ''down''. that would create a top-view for your object. but maybe that''s not exactly what you need
don''t know
i assume you want to view your player from above... you would just have to use the player-position, move the camera up & rotate your camera view down... there are thousands of possibilities to do this... depends on what EXACTLY you want...
1) move ''around'' the player (on top) for 3rd-person view
2) have everything look like a 2D-game and view from above (as in action-games like raptor / gta / ...)
3) look into a 3d world from above
4) write a routine to create a top-view for whatever you want to look at, once, and be able to freely move it afterwards
5) have a top-view relative to the object you look at (if he/it falls/rotates, the camera also rotates)
6) have a top-view relative to your world (or absolute coordinates) - so it always looks onto the object but doesn''t rotate with him
7) ...
whatever you want to do... it also depends on how you manage your objects/camera... do you keep position and 3 angles in memory or do you have a matrix for every object (also the camera - as in my case & code - that''s what i recommend).
normally you would just have to move the camera to the object''s position, move it upwards & rotate the camera''s view ''down''. that would create a top-view for your object. but maybe that''s not exactly what you need
This will let you change your view only by translating, but always looking down the +Z axis.
edit fixed code... don't edit/copy/paste code at 7 in the morning...
[edited by - invective on June 15, 2002 1:47:56 AM]
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (camera.x,camera.y,camera.z, //Eye
camera.x,camera.y,camera.z+1, //Look At (always +Z)
0,1,0); //Up +Y
glPushMatrix();
//Render Stuff
glPopMatrix();
glPushMatrix();
//Render more Stuff
glPopMatrix();
...
edit fixed code... don't edit/copy/paste code at 7 in the morning...
[edited by - invective on June 15, 2002 1:47:56 AM]
the views like gta. when i try adjust the projection matrix every frame the screen goes blank and not because i move the camera to much to see the sceen.
i can''t do loadidentity on the projection matrix because it will wipe my projection stuff. and i load identity on the modelview matrix before i draw anything so there no point tring to translate that. show i loadidentity on the modelview matrix before i draw anything?
As JuNC implies, the projection matrix is only for setting up your projection. Typically, you do this only once at the top of your program, then switch to modelview. Anyone who tells you different should be concentrating on learning OpenGL, not trying to teach it.
This question comes up a lot, so I cut and pasted my previous response:
The camera position is always at the position 0,0,0. Moving the camera is an illusion. Instead, you rotate or translate the modelview matrix to move all the objects in the world, which gives effect of moving the camera. So, you might call glTranslatef(-1,0,0) to move the "camera" (1,0,0).
Alternatively, use gluLookat, as invective suggests, but bear in mind that you should really be looking down the Y axis to look down. A typical render() function would look like this (called once per frame):
All this is of course done in the MODELVIEW matrixmode.
____________________________________________________________
www.elf-stone.com
This question comes up a lot, so I cut and pasted my previous response:
The camera position is always at the position 0,0,0. Moving the camera is an illusion. Instead, you rotate or translate the modelview matrix to move all the objects in the world, which gives effect of moving the camera. So, you might call glTranslatef(-1,0,0) to move the "camera" (1,0,0).
Alternatively, use gluLookat, as invective suggests, but bear in mind that you should really be looking down the Y axis to look down. A typical render() function would look like this (called once per frame):
glLoadIdentity();
glPushMatrix(); //store the identity matrix on the stack
gluLookat(...); //setup camera transform
glPushMatrix(); //store camera transform on the stack
glTranslatef(...); //move drawing position by translating modelview
...draw stuff...
glPopMatrix(); //revert to the saved camera
glPushMatrix(); //save the camera position on the stack
glTranslatef(...); //move drawing position by translating modelview
...draw more stuff...
glPopMatrix();//go back to the saved camera position
glPopMatrix();//go back to the identity matrix
All this is of course done in the MODELVIEW matrixmode.
____________________________________________________________
www.elf-stone.com
why is looking down the y axis down?
this won''t help hello_there but...
benjamin bunny said, "the projection matrix is only for setting up your projection. Typically, you do this only once at the top of your program, then switch to modelview."
does this include if you need to go back to the ortho projection to do a HUD or 2D menu item? or will everything work alright if you go from perspective to ortho in the model_view?
i hope someone understands what i''m doing.
later,
DarkMonkey
benjamin bunny said, "the projection matrix is only for setting up your projection. Typically, you do this only once at the top of your program, then switch to modelview."
does this include if you need to go back to the ortho projection to do a HUD or 2D menu item? or will everything work alright if you go from perspective to ortho in the model_view?
i hope someone understands what i''m doing.
later,
DarkMonkey
you know when you push the matrix to the stack can you then pop the matrix of the stack in anouther funstion? i think you can but i must make sure.
also when you pop something off the matrix then push it on again is the current matrix got the setting from the stuff you popped off.
[edited by - hello_there on June 15, 2002 2:26:13 AM]
[edited by - hello_there on June 15, 2002 2:26:13 AM]
yyyyyyyyyyyyeeeeeeeeeeeeeeeeeeessssssssssssssss!!!!! it works thanks
DarkMonkey - whether you do perspective or ortho you should be in the PROJECTION matrix mode.
You can save the current projection matrix on the stack in exactly the same way as the modelview so if you need to switch back without losing the perspective projection simply push the projection matrix and pop it back afterwards.
You can save the current projection matrix on the stack in exactly the same way as the modelview so if you need to switch back without losing the perspective projection simply push the projection matrix and pop it back afterwards.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement