Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
108 user(s) are online (77 user(s) are browsing Forums)

Members: 0
Guests: 108

more...

Headlines

 
  Register To Post  

[Fixed] OpenGL - Cameras & Objects positionning.
Quite a regular
Quite a regular


See User information
Hi All,

I have checked many sites and tutorials on handling a camera and objects positionning but there are thing that does not work as they should ...

Firstly, here is my Display function for OpenGL :
void MyDisplayFunc(){
  if ( 
BasicCamera.Backdrop == ){
    
glClearGL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT );
   }
  
UpdateCamera();
  
Basic3D_UpdateWorld();
  
glSwapBuffers();
 }


Here is the UpdateCamera() function :
void UpdateCamera(){
  
glMatrixModeGL_PROJECTION );
  
glLoadIdentity();
  
// Rotation under the 3 axis
  
glRotatefCamera.XRotCurrentCamera ], 1.00.00.0 );
  
glRotatefCamera.YRotCurrentCamera ], 0.01.00.0 );
  
glRotatefCamera.ZRotCurrentCamera ], 0.00.01.0 );
  
// Positionning of the camera.
  
glTranslatefCamera.XPosCurrentCamera ], Camera.YPosCurrentCamera], Camera.ZPosCurrentCamera ] );
  
glMatrixModeGL_MODELVIEW );
 }


And Here is the object update function per object :
void INTERNAL_Trace3DObjectint ObjectID ){
  
Object3DPointer MyObject;
  
MyObject GetObjectPTRObjectID );
  if ( 
MyObject != NULL ){
    
int *ObjectMeshPTR;
    
ObjectMeshPTR GetObjectMeshPTRObjectID );
    
glMatrixModeGL_MODELVIEW );
    
glLoadIdentity();
    
glColor3fMyObject->RGBRedMyObject->RGBGreenMyObject->RGBBlue );
    
glTranslatefMyObject->XPosMyObject->YPosMyObject->ZPos );
    
glRotatefMyObject->XRot1.00.00.0 );
    
glRotatefMyObject->YRot0.01.00.0 );
    
glRotatefMyObject->ZRot0.00.01.0 );
    
glEnableClientStateGL_VERTEX_ARRAY );
    
ObjectMeshPTR ObjectMeshPTR 12;
    
glVertexPointer3GL_FLOAT24ObjectMeshPTR );
    
glDrawArraysGL_TRIANGLES012 );
    
glDisableClientStateGL_VERTEX_ARRAY );
   }
 }


The function Basic3D_UpdateWorld() only check if object exist and if it exist it call the INTERNAL_Trace3DObject function to display it.

Here are now the problems :
What work correctly :
1. Object rotation is ok

What's wrong :
2. Object positionning. If I modify X, Y or Z of object position ... it does no more appear on camera
3. Camera position. if I modify X, Y or Z nothing appear on screen (black screen).
4. Camera rotation. if I modify X, Y or Z nothing appear on screen (black screen). (even of 0.1 degree )

Does someone have any clue ?

PS : I have tried to understand how gluLookAt function work but I did not find any solution with it too...

Thank you.
Regards,
Freddix/AmiDARK


Edited by freddix on 2009/9/23 0:47:47
Edited by freddix on 2009/9/30 21:44:13
All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Cameras & Objects positionning.
Home away from home
Home away from home


See User information
@freddix

You have several problems:
- The model-view matrix controls the position and orientation, not the projection matrix. The projection matrix should be based on the camera's intrinsic parameters (focal point, scaling factors, etc.)
- you're treating the camera as an object, instead of the viewpoint. You have to "move" by the inverse transformation of the camera's position and orientation.

e.g.,
void UpdateCamera(){
  
glMatrixModeGL_PROJECTION);
  
// Load in the camera's projection matrix with glM
  
glMatrixModeGL_MODELVIEW);
  
glLoadIdentity();
  
// Rotation under the 3 axis
  
glRotatef( -Camera.XRotCurrentCamera ], 1.00.00.0 );
  
glRotatef( -Camera.YRotCurrentCamera ], 0.01.00.0 );
  
glRotatef( -Camera.ZRotCurrentCamera ], 0.00.01.0 );
  
// Positionning of the camera.
  
glTranslatef( -Camera.XPosCurrentCamera ], -Camera.YPosCurrentCamera], -Camera.ZPosCurrentCamera ] );
 }


You're quickly going to find your code to be inadequate, you need to have a proper transformation class that either stores the pose as a 4x4 matrix (and its inverse), or uses quaternions for the orientation and a 3D vector for position, in which case the forward and inverse matrices would be regenerated when necessary. Using quaterions should be faster in most cases.

Here's a code snippet from a camera class that I wrote a while back:
void Camera::useCamera() const
{
    
glViewport(0.00.0widthheight);

    
glEnable(GL_DEPTH_TEST);

    
// set projection
    
glMatrixMode(GL_PROJECTION);
    
glLoadMatrixd(projection.getGLMatrix());
    
    
// set model-view matrix to camera's view
    
glMatrixMode(GL_MODELVIEW);
    
glLoadMatrixd(pose.getInvGLMatrix());
}


Projection and pose are Transformation3D classes, which have all the methods that you need to move and rotate objects. The projection matrix can be left untouched if you don't want different cameras to have different focal lengths, etc. Just set it using the appropriate glu functions and leave it. Otherwise, the calculations to create an OpenGL projection matrix from real camera parameters can be found here.

Hans

http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more.
https://keasigmadelta.com/ - more of my work
Go to top
Re: OpenGL - Cameras & Objects positionning.
Quite a regular
Quite a regular


See User information
@Hans
C handle Classes ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Cameras & Objects positionning.
Quite a regular
Quite a regular


See User information
@freddix

No, C++ does.

Back to a quiet home... At last
Go to top
Re: OpenGL - Cameras & Objects positionning.
Quite a regular
Quite a regular


See User information
@abalaban
I'm under C only :p

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Cameras & Objects positionning.
Home away from home
Home away from home


See User information
@freddix

Sorry, I forgot that you were using C. I switched to C++ for stuff like this a long time ago. C++ really makes this stuff easier, once you've learnt the basics. You can define an Object class as base class for everything in the 3D world, and then the graphics/game engine can render/handle everything without having to know what kind of objects it's dealing with.

In C you can create Abstract Data Types (ADTs) which is like a primitive class, look this term up if you don't know what it is. The bottom line is, life will be much easier if you have a Transformation3D class/ADT with appropriate fuctions to move and rotate objects and cameras.

Hans

http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more.
https://keasigmadelta.com/ - more of my work
Go to top
Re: OpenGL - Cameras & Objects positionning.
Quite a regular
Quite a regular


See User information
@Hans
ok

I will check for this.

Thank you.

Regards,
Fred

All we have to decide is what to do with the time that is given to us.
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project