#### Archived

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

# OpenGL How do I rotate the perspective in OpenGL

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

## Recommended Posts

In all of the tutorials I''ve read when the user tries to change his view of the objects, the program just rotates the objects not the perspective. But I''m sort of in a bind because rotating all of my objects would screw up my game. So is there a way that I can rotate my view on the fly, or during initialisation (I can''t find what code changes that)

##### Share on other sites
Hi again Chrono999,

From what I understand, you want to be able to move around in 3D space rather than moving all your objects. Funnily enough, its not that different...

The main thing to understand is that when you move forward, its the same as moving the whole world backward (well, to you anyway). So what you need to do is define a translation (and rotations if you want to look in different directions) which applies to the whole world.

Ever wondered why the modelview matrix has its name? Its because it holds the viewing transformations as well as all the object model transformations, becuase they have the same effect. The way to set up a viewing transformation is simple... its just the first transformations that you do before drawing (NOTE: the view transform MUST be applied before any other transform). I''ll show you how I do it in a simple Quake-walking-around-mouselooking-type-thing to give you a better idea of what I mean:

void Render(){glClear(...);glLoadIdentity(); //I always have the modelview matrix active at this point//define viewing transformations//NOTE: this is done reverse to usual - rotate THEN translateglRotatef(xAngle, 1.0, 0.0, 0.0); //look up/downglRotatef(yAngle, 0.0, 1.0, 0.0); //look left/rightglTranslatef(-viewerX, -viewerY, -viewerZ);//now we can draw our world/objectsfor(how ever many objects){glPushMatrix();  //save matrix state with our view transform//Do any local transformations for the objectglTranslatef(...); //move to a positionglRotatef(...);    //rotate about this position...DrawObject();glPopMatrix();   //restore matrix with view transform}glFlush()/SwapBuffers();//whatever}

... oh, crap, I just lost the flow of what I was going to tell you. Well, hopefully you got something from that

Ah, yes... got it back. To be able to just arbitarily ''walk'' in the direction you''re facing, you must use some lovely trig to split translation into components along each axis depending on the angle you are facing, but I won''t go into that now.

Hope that helps some

-------------
squirrels are a remarkable source of protein...

##### Share on other sites
Thanks for your help but I just can''t figure it out (Well, I understand what your saying but the code isn''t working) here''s the rendering function I''m using:
glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT);

glRotatef(50.0f, 1.0, 0.0, 0.0); //look up/down
glTranslatef(500.0f,0.0f,0.0f);

glPushMatrix();

glTranslatef(ballx,0.0f,ballz);

glBindTexture(GL_TEXTURE_2D, texture[filter]);
xrot+=1.2f;

glPopMatrix();

##### Share on other sites
mabey it''s just the variables for the translate:

glTranslatef(-viewerX, -viewerY, -viewerZ);

do I need to have negative viewer or is that just a variable you made up. Thanks for any response

##### Share on other sites
There is a tutorial on NeHe''s site dealing with movement in
a 3d world
- nehe.gamedev.net -
which you may find useful.

##### Share on other sites
I found out the problem, I had glLoadIdentity after the camera change. With that gone it now works, but I''ll have to rewrite my code around this

• 16
• 9
• 15
• 10
• 13