#### Archived

This topic is now archived and is closed to further replies.

# OpenGL Camera system in OpenGL!!!

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am having... well... I would say that I''m going through HELL to try and get a camera system working in OpenGL. The system I need to implement is a camera that can orbit around a movable point of interest, i.e. like a third person perspective (think of Soul Reaver, Tomb Raider... that kind of style). The problem I have is that I don''t exactly know what I am supposed to do, I have tried everything and more, but it still doesn''t work properly. Please, I need help, I''m starting to go insane If you could explain the process step by step I would be grateful! Thanks!

##### Share on other sites
There are two ways to do a camera system in OpenGL...the easy / less flexible way...or the harder / more flexible way....

The easier less flexible way is to use gl''s own functions like glTranslate, glRotate, and glu''s gluLookAt. Here you would rotate your scene, then translate it out.

The harder more flexible way is to make your own camera view matrix, and load it onto the opengl modelview matrix stack. To do it this way, you will need to know 3 and 4d matrix maths. You basically construct a 3d rotation matrix that is the inverse of your camera orientation and then multiply it by a translation matrix, which again is the inverse of your camera''s position. I find it easier to use a quaternion to represent the camera''s orientation, and then convert that into a 4d matrix to get the camera''s rotation matrix. The translation matrix is simply the negative of your camera''s world position( -xpos, -ypos, -zpos ). In openGL the translation part of the matrix would occupy the fourth column of the matrix, in D3D, it would occupy the fourth row.

##### Share on other sites
Thanks for the info, but what I am really looking for is to do it using GLUT and it''s functions, i.e. glRotatef, glTranslatef and glLookAt.

I do have maths knowledge, so that is not a problem, the problem is that I don''t know the order of things and where or how to do them... *snif*

##### Share on other sites
well....GLUT has nothing to do with glRotate and glTranslate. They are GL functions. Maybe it was a typo . Anyway....you''re being a little bit vague about what you are stuck on. You said you''re not sure about the order of things...well if you mean the order of glTranslate and glRotate, it depends on what you''re trying to do, but usually you will want to rotate the scene, then translate it. That''ll give a very different result to translating the scene, then rotating it.

##### Share on other sites
What would the data structure used to store the camera info be like? I mean, what would you actually store?

My data structure a the moment is:

/* Third person with fixed orbit camera */
typedef struct CAMERA_TAG
{
Vector3 interestPosition;

float viewInclination;
float viewDistance;

} CAMERA;

Being Vector3 a an array of 3 floats.

Would I need anything else? I just want to be able to rotate about the point of interest and move this point of interest around the world.

Right now I am able to rotate about the point of interest (at least I think it works ok), but I can''t manage to move the point of interest around the world properly.

Any ideas on how to solve this problem?

Thanks!

##### Share on other sites
Maybe you should try gluLookAt

take a look here.

--
Programmer of Star Torn

--

There are only 10 types of people in the world:
Those who understand binary, and those who don''t.

##### Share on other sites
I'm not sure if I understand what you want to do. Are you saying you want the camera to look at a certain point...and keep looking at that point when you move the camera in a circle around it ? Are you using gluLookAt() ? The data structure you have doesn't seem like it's generic enough for a camera 'system'. To implement a camera system, you need camera position, camera orientation( in quaternion or matrix form ), camera velocity, even camera angular momentum if you want the camera to be a physical body. It's hard to have a proper conversation on here....if you like you can email me on gowron67@lycos.co.uk. I'm particularly interested in what you're trying to do because I've just implemented a camera system in OpenGL myself

[edited by - gowron67 on March 31, 2003 9:19:05 AM]

##### Share on other sites
Heh... I tried to ask this similar question few time ago. I didn''t get real answer. So I had to do all by myself. Here the answer. If you have target vector, view distance, and orientation quaternion (i don''t know what is heading and inclination) the code should be

  void NCamera::UpdateView(){	// Get axis & angle	float ang;	NVector3 axis;	mOrientQuaternion.GetRotation(&ang, &axis);        // Recalculate eye	NQuaternion q(mOrientQuaternion);	q.Invert();	mEye.x = 2.f * (q.x * q.z + q.w * q.y);	mEye.y = 2.f * (q.y * q.z - q.w * q.x);	mEye.z = 1.f - 2.f * (q.x * q.x + q.y * q.y);	mEye *= mDistance;	mEye += mTarget;	// Load view matrix	glLoadIdentity();	glRotatef(ang * 180.f / PI, axis.x, axis.y, axis.z);	glTranslatef(-mEye.x, -mEye.y, -mEye.z);}

If you don''t know about quaternions I highly recomend you to read article about them, it is not hard, but have many advantages (i.e. nice smooth animation)

##### Share on other sites
I have a data structure in my player class that is basically location x,y,z and radian (view radian). I then have a pointer in my camera class that points to said structure. This allows me to switch to new objects by simply updating this pointer .

  struct Vertex_S{ float x,y,z;};struct MyView_S{ Vertex_S Pos; float ViewRadian;};class Player_C{public: MyView_S MyView;};class Camera_C{public: MyView_S *Follow;    //Our point to follow! Vertex_S Camera_Pos; //Our camera''s following position float Distance;   //Distance away from point//Just incase we want to follow a point a bit below/above our follow point! float FollowHeight; float UpRadian;   //up/down follow float ViewRadian; //Left/right followpublic: void DoCamera(void) {  float tMult;  tMult = cosf(UpRadian);  Camera_Pos.y = sinf(UpRadian) + Follow->Pos.y + FollowHeight;//Use this to use ViewRadian as a relation to the players view  Camera_Pos.x = cosf(ViewRadian+Follow->ViewRadian) + Follow->Pos.x;  Camera_Pos.z = sinf(ViewRadian+Follow->ViewRadian) + Follow->Pos.z;/* Use this to just look at them from a certain angle no matter where they''re facing!  Camera_Pos.x = cosf(ViewRadian) + Follow->Pos.x;  Camera_Pos.z = sinf(ViewRadian) + Follow->Pos.z;*///Reset our identity matrix, and do our thing  glLoadIdentity();  gluLookAt(Camera_Pos.x,Camera_Pos.y,Camera_Pos.z,   Follow->Pos.x,Follow->Pos.y+FollowHeight,Follow->Pos.z,   0,1,0); }};

Let me know if this is something like what you needed, and if you need more help, let me know.

##### Share on other sites

So basically I could move the centre of interest around the scene and rotate it with the keyboard; this centre of interest would have 2 vectors assigned to it (call them "view", "up" and "strafe").

Then I could rotate (orbit) the camera around the centre of interest using the mouse.

Does this sound about right? If so, would this be correct?:

/* Centre of interest (COI) structure */
typedef struct CENTRE_OF_INTEREST_TAG
{
Vector3 v3View;
Vector3 v3Up;
Vector3 v3Strafe;
Vector3 v3Position;

} CENTRE OF INTEREST;

/* Camera structure */
typedef struct CAMERA_TAG
{
float heading; /* Angle with the COI YZ plane */
float inclination; /* Angle with the COI XZ plane */
float orbitDistance; /* Distance from the COI */

} CAMERA;

Another question is how to implement this? Would you use gluLookAt()? And how can I make the camera orbit via the mouse? I have tried but I can''t manage to get it working

Thanks to all of you

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×