how move and make zoom in the view?

Started by
6 comments, last by GMA965_X3100 13 years, 3 months ago
I'm looking for any sample that show me how i can do this but without lucky just leave this place, i hope that someone can help me!

i trying to do my own model viewer i just have how drag the scene but i want do that make zoom with the mouse wheel some know how im using visual C++ and wxwidgets
Advertisement
either u move the camera forward or you make the FOV smaller
i want move the camera
Hi, I think it's pretty common to use something like this: (it's what I use)
c++
void camera () {    //Look up/down    glRotatef(-yLookAngle,1.0,0.0,0.0);    //Look left/right    glRotatef(-xLookAngle,0.0,1.0,0.0);    //move    glTranslated(-xCameraPos,-yCameraPos,-zCameraPos);}


You'll need to get input, and do calculations to adjust all of these variables.

Also each frame you want to call this after you load identity. Then don't load identity again (unless that's what you want) Instead use glPushMatrix() and glPopMatrix()
Now, there is a little formulae I realized, for the purpose of this -
camera position axis movement = camera view + trig ( angle ) * camera mover

So, this may be done with simple sin and cos actions:


Let me whip up a quick sample:
// global variables
char keys [ 256 ];
const double pie = 3.142857142857;
GLfloat cam_pos_x = 0.0f, cam_pos_y = 0.0f, cam_pos_z = 0.0f;
GLfloat cam_view_x = 0.0f, cam_view_y = 0.0f, cam_view_z = 0.0f;
GLfloat cam_tilt_x = 0.0f, cam_tilt_y = 1.0f, cam_tilt_z = 0.0f;
GLfloat angle_of_cam_movement = 0.0f; // in degrees
GLfloat radians = 0.0f; // convert angle to radians
GLfloat cam_mover = 0.0f; // will be used to move cam

// control camera function
void render ( )
{
// first thing one learns in calculus trig class
radians = ( angle_of_cam_movement / 180 ) * ( 180 / pie );
// set cam view
cam_view_x = 0.0f;
cam_view_y = 0.0f;
cam_view_z = 0.0f;
// set cam positon relative to view
cam_pos_x = cam_view_x + sin ( radians ) * cam_mover;
cam_pos_y = cam_view_y + cam_mover/20.0f;
cam_pos_z = cam_view_z + cos ( radians ) * cam_mover;
// set camera using gluLookAt ( );
gluLookAt ( cam_pos_x, cam_pos_y, cam_pos_z, cam_view_x, cam_view_y, cam_view_z, cam_tilt_x, cam_tilt_y, cam_tilt_z );


// next you would draw objects in your screen
// you would be required to translate object, ( model translation -
// glTranslatef ), to move your object in camera's view. suggested
// glTranslatef ( -400.0f, -500.0f, 100.0f );
}


void control_camera ( )
{
// update cam rotation angle each time cam_mover increments
// rotate in opp dir to give illusion of proper azimuth( regular ) rotation
if ( cam_mover ++ )
{
angle_of_cam_movement -= 0.5f; // rotate in opp dir
}
if ( cam_mover -- )
{
angle_of_cam_movement += 0.5f; // rotate in opp dir
}


// now for key controls
// rotate cam right
if ( keys [ VK_RIGHT ] )
{
angle_of_cam_movement -= 0.05f;
}
// rotate cam left
if ( keys [ VK_RIGHT ] )
{
angle_of_cam_movement -= 0.05f;
}
// move cam forward [ zoom in ]
if ( keys [ VK_UP ] )
{
cam_mover -= 0.05f;
}
// move cam backward [ zoom out ]
if ( keys [ VK_DOWN ] )
{
cam_mover += 0.05f;
}
}

// win api
int WINAPI WinMain ( .... )
{
...

control_camera ( );
render ( );
SwapBuffers ( hdc );

...
}

// or if you don't use WinMain
// win api
main ( .... eg.int argc, char ** argv )
{
...

control_camera ( );
render ( );
SwapBuffers ( hdc );

...
}


Done!!!
The ... means the rest of your code DON'T include them!!!!


Using this method, both rotation and translation of camera is done...in your case translation, zooming in and out!!!

If you didn't want to move camera in and rather put camera to zoomed position, instead of incrementing cam_mover inc control_camera, just equate it to zoom amount!!! Simple yet effective!!!!
http://img440.images...gamedevsig.jpg/

I'm not the typical programmer.
@Geared, I wonder if it is wise to use glTranslatef ( ) function outside of model transformations, never found a use for it beyond manipulating objects.
http://img440.images...gamedevsig.jpg/

I'm not the typical programmer.
thanks I'll try it
@iozk here is sample of working camera, using this method:
http://www.filesavr.com/NSALUET1JRXR0KP

Press up button until u see a terrain mesh.

Press right to rotate right, left to rotate left, up to move in, down to move out.
http://img440.images...gamedevsig.jpg/

I'm not the typical programmer.

This topic is closed to new replies.

Advertisement