Jump to content
  • Advertisement

Archived

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

Chrono999

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 translate
glRotatef(xAngle, 1.0, 0.0, 0.0); //look up/down
glRotatef(yAngle, 0.0, 1.0, 0.0); //look left/right
glTranslatef(-viewerX, -viewerY, -viewerZ);


//now we can draw our world/objects
for(how ever many objects)
{
glPushMatrix(); //save matrix state with our view transform
//Do any local transformations for the object
glTranslatef(...); //move to a position
glRotatef(...); //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 this post


Link to post
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);
glLoadIdentity();

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

glPushMatrix();

glLoadIdentity();
glTranslatef(ballx,0.0f,ballz);

glBindTexture(GL_TEXTURE_2D, texture[filter]);
gluSphere(quadratic,1.3f,32,32);
xrot+=1.2f;

glPopMatrix();

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!