Thread: How to set up a top view camera?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    How to set up a top view camera?

    Hey guys,

    Can someone tell me how can i set up top view camera for my scene in OpenGL? I've tried using gluLookAt() but i couldn't figure out the way i wanted. Any ideas with starting code would be really apperciated. Thanks in advance!! =]

  2. #2

    Join Date
    May 2005
    Posts
    1,042
    You have to use the gluLookAt function properly, and you need to know the proper coordinates to set. If you know the angles about the X, Y and Z axis and the position you can use these functions I wrote (they take degrees not radians):

    Code:
    void	R_GenerateViewMatrix(float	pitch, float yaw, float roll, Vector3 & translation)
    {
    	if(pitch)
    	{
    		NT_GL(glRotatef(-pitch,1,0,0));
    	}
    	if(yaw)
    	{
    		NT_GL(glRotatef(-yaw,0,1,0));
    	}
    	if(roll)
    	{
    		NT_GL(glRotatef(-roll,0,0,1));
    	}
    		NT_GL(glTranslatef(-translation.x,-translation.y,-translation.z));
    }
    Note the NT_GL hook is something I use because I work with software implementations of OpenGL (but it works the same, just remove it). Be careful though, because these functions modify whatever the current matrix is (it could modify the projection matrix, the texture matrix, etc)...I use this funciton by determining where I want the camera to be located, and the angles about each axis the view vector points (I need to convert from radians to degrees), I set the matrixmode to GL_MODELVIEW, call glLoadIdentity, and then call this function, as illustrated below:

    Code:
    void	GLRenderer::GLRenderScene()
    {
    ...preliminary garbage goes up here to help setup the scene
    	NT_GL(glMatrixMode(GL_MODELVIEW))
    	NT_GL(glPushMatrix())
    	NT_GL(glLoadIdentity())
    
    	Pos		= (Export.mpPlayer.mpCam->mPosition);
    
    	R_GenerateViewMatrix(RAD2DEG(Export.mpPlayer.mpCam->xRadians),RAD2DEG(Export.mpPlayer.mpCam->yRadians),0,Pos);
    
    	/*
    		-Render everything that has been sent to the renderer
    	*/
    ....renderer does a butt load of work to render everything given to it
    	NT_GL(glPopMatrix())
    	
    	SwapBuffers(gpNTGLAPI->NT_DEVICE_CONTEXT);
    		/*
    		-This clears the containers so they can be re-filled for the next scene
    	*/
    	ResetScene();
    }
    The way the OpenGL coordinate system works (when the camera is in its default position and orientation), the X axis goes off to the right, the z axis comes out of the screen pointing towards you, and the y axis goes up the screen. If you want a camera 'up' pointing 'down' my interpretation is to use the functions I've posted as follows:

    Code:
    	NT_GL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT))
    	NT_GL(glMatrixMode(GL_MODELVIEW))
    	NT_GL(glLoadIdentity())
    
    //This is the position of the camera....it is 500 units 'up'
    	Pos		=  Vector3(0, 500,0);
    
    //An angle of -90 degrees about the x axis makes the camera look down, same as 270 degrees about x axis
    	R_GenerateViewMatrix(-90,0,0,Pos);
    
    ...Render your chessboard here
    I gave you a lot of information so you may be confused. Don't forget to go to:

    MSDN
    Last edited by BobMcGee123; 08-07-2005 at 07:30 PM.
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Thanks for lots of info. There are couple of things i'm confused about. First, Pos is what type of variable? Second do i really need to use GLRenderScene() method? Again, thanks for all the info.

  4. #4

    Join Date
    May 2005
    Posts
    1,042
    Oh, those are all things I have programmed, you do not need to use the glRenderScene method... I was just showing you my code. Pos is a variable of type 'Vector3' which is a class I wrote. It's just a 3D vector with X,Y,Z coordinates stored as 32 bit floating points. Instead of using that type you can modify my code and just pass in three floating point variables to the R_GenerateViewMatrix function, so instead of it looking like this:

    R_GenerateViewMatrix(float AngleX, float AngleY, float AngleZ, Vector3 & )

    it would insead just be:

    R_GenerateViewMatrix(float AngleX,float AngleY,float AngleZ, float X, float Y, float Z)

    Again, like I said, this is all stuff I've written, but it give an alternative to gluLookAt which can be somewhat confusing (but i guess this isn't exactly easy).

    Again that's kind of a lot of info to just dump on you...it wouldn't be fair to present all of that and just expect it to all make sense so feel free to ask questions...even if you think they're stupid questions.
    Last edited by BobMcGee123; 08-07-2005 at 08:18 PM.
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Thank you for everything. I've kind of figured out how to do which i wanted by using gluLookAt (). But thanks to you now i know 2 ways and it's always good to have 2 different ideas to solve one problem. Although i've vector class and have inculded in my class, when i compile it gives me the error when i define pos as following:
    Code:
    vector3 Pos =  Vector3(0, 500,0);
    Please correct me if i'm doing it wrong
    Since i got compiling error with old code you provided so i decided to replace it with the new code you gave me. So here i want to clarify few things becuase when i used it my chessbaord is out of the screen.

    1. Instead of having
    Code:
     void R_GenerateViewMatrix(float	pitch, float yaw, float roll, Vector3 & translation)
    {
    	if(pitch)
    	{
    		NT_GL(glRotatef(-pitch,1,0,0));
    	}
    	if(yaw)
    	{
    		NT_GL(glRotatef(-yaw,0,1,0));
    	}
    	if(roll)
    	{
    		NT_GL(glRotatef(-roll,0,0,1));
    	}
    		NT_GL(glTranslatef(-translation.x,-translation.y,-translation.z));
    }
    Should i've like following?
    
    void R_GenerateViewMatrix(float angleX, float angleY, float angleZ, float x, float y, float z)
    {
    	if(angleX)
    	{
    		glRotatef(-angleX, 1, 0, 0);
    	}
    	if(angleY)
    	{
    		glRotatef(-angleY, 0, 1, 0);
    	}
    	if(angleZ)
    	{
    		glRotatef(-angleZ, 0, 0, 1);
    	}
    		glTranslatef(-x, -y, -z);
    }
    2. Moving on then instead of having:
    Code:
    //This is the position of the camera....it is 500 units 'up'
    	Pos		=  Vector3(0, 500,0);
    
    //An angle of -90 degrees about the x axis makes the camera look down, same as 270 degrees about x axis
    	R_GenerateViewMatrix(-90,0,0,Pos);
    Should i have?
    //An angle of -90 degrees about the x axis makes the camera look down, same as 270 degrees about x axis
    	R_GenerateViewMatrix(-90, 0, 0, 0, 200, 0);
    Just tell me whenever u could. I really apperciated your help and will be looking forward to these kinds of replies in future =] Thank you very musch sir!!

  6. #6

    Join Date
    May 2005
    Posts
    1,042
    vector3 Pos = Vector3(0, 500,0);
    vector3 is not the same as Vector3. This is a simple programming error that you should have found on your own (I can't find your mistakes forever).

    Should i've like following?
    Yup


    2. Moving on then instead of having:
    Yup...except you accidently typed 200 instead of 500 (not that it matters, the format is correct).

    So here i want to clarify few things becuase when i used it my chessbaord is out of the screen.
    Does it work with gluLookAt? If so, show me what you are sending as parameters to the gluLookAt function...the chessobard going out of the screen is probably just a coordinate problem though.


    I really apperciated your help and will be looking forward to these kinds of replies in future
    I'm trying to give you a jumpstart...I won't just hand out this many answers in the future (I'm a realist) so I suggest you try as hard as you can to understand this stuff on your own
    I'm not immature, I'm refined in the opposite direction.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    Unhappy

    Reply is apperciated!!
    vector3 is not the same as Vector3. This is a simple programming error that you should have found on your own (I can't find your mistakes forever).
    I've "vector3" not "Vector3" so that's not the problem!! I think i'm stupid but not that much !
    Here are coordinates for gluLookAt():
    Code:
    gluLookAt( 0, 20, -10, 0, 0, 0, 0, 1, 0 );
    and here is the code for chessboard. It may help you to clarify the coordinates:
    Code:
    void Model :: initChessWStripes(GLfloat x, GLfloat z) {
    	  
        glPushMatrix();
    			glTranslatef(-5.7f + x, -1.0f, 7.0f - z);  
    		   texture[0] = LoadGLTexture("Data/BoardWhite.bmp" );
    		   //glColor3f(0.0f, 0.0f, 0.0f);
    		   glBegin(GL_QUADS);
    			   glTexCoord2f(0.0f, 0.0f);glVertex3f(  1.0f, -1.0f,  1.0f);			// Top Right 
    				glTexCoord2f(1.0f, 0.0f);glVertex3f( -1.0f, -1.0f,  1.0f);			// Top Left 
    				glTexCoord2f(0.0f, 1.0f);glVertex3f( -1.0f, -1.0f, -1.0f);			// Bottom Left 
    				glTexCoord2f(1.0f, 1.0f);glVertex3f(  1.0f, -1.0f, -1.0f);			// Bottom Right 
    		glEnd();	
    	glPopMatrix( );   
    }
    
    void Model :: initChessBStripes(GLfloat x, GLfloat z) {
    	  
    	  glPushMatrix();
    		glTranslatef(-5.7f + x, -1.0f, 7.0f - z);  
    		texture[0] = LoadGLTexture("Data/BoardBlack.bmp" );
    		   //glColor3f(0.0f, 0.0f, 0.0f);
    		   glBegin(GL_QUADS);
    			   glTexCoord2f(0.0f, 0.0f);glVertex3f(  1.0f, -1.0f,  1.0f);			// Top Right 
    				glTexCoord2f(1.0f, 0.0f);glVertex3f( -1.0f, -1.0f,  1.0f);			// Top Left 
    				glTexCoord2f(0.0f, 1.0f);glVertex3f( -1.0f, -1.0f, -1.0f);			// Bottom Left 
    				glTexCoord2f(1.0f, 1.0f);glVertex3f(  1.0f, -1.0f, -1.0f);			// Bottom Right 
    		glEnd();	
    	glPopMatrix( );   
    	  
    }
    And if you don't mind can you please tell me where i can find the source of loading ".3ds" models in opengl. I've tried to search everywhere but couldn't find anything useful. Any source i found have bunch of syntax errors which i can't solve . Or if you have any code which can help me please post. It would be amazing and would make my life easier. I would be very thankful to you and would really apperciate your help! =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM