The camera works well.
I will study this camera further
Http://www.cpe.ku.ac.th /~ POM/courses/204481/Gallery/octree/camera. cpp
//************************************** *********************************//
////
//-"Talk to me like I'm a 3 year old! "Programming lessons -//
////
// $ Author: DigiBendigiben@gametutorials.com //
////
// $ Program: octree //
////
// $ Description: demonstrates octree space partitioning //
////
// $ Date: 11/20/01 //
////
//************************************** *********************************//
// This is used so that we can use timegettime () for time based movement
# Pragma comment (Lib, "winmm. lib ")
# Include "Main. H"
# Include "camera. H"
// This is how fast our camera moves
# Define kspeed50366f
// Our global float that stores the elapsed time between the current and last frame
Float g_frameinterval = 0.0f;
/// // Calculate Frame Rate //// ////////////////////////////*
/////
//// This function calculates the frame rate and time intervals between frames
/////
/// // Calculate Frame Rate //// ////////////////////////////*
Void calculateframerate ()
{
Static float framespersecond = 0.0f; // This will store our FPS
Static float lasttime = 0.0f; // This will hold the time from the last frame
Static char strframerate [50] = {0}; // We will store the string here for the window title
Static float frametime = 0.0f; // This stores the last frame's time
// Get the current time in seconds
Float currenttime = timegettime () * 0.001f;
// Here we store the elapsed time between the current and last frame,
// Then keep the current frame in our static variable for the next frame.
G_frameinterval = currenttime-frametime;
Frametime = currenttime;
// Increase the frame counter
++ Framespersecond;
// Now we want to subtract the current time by the last time that was stored
// To see if the time elapsed has been over a second, which means we found our FPS.
If (currenttime-lasttime> 1.0f)
{
// Here we set the lasttime to the currenttime
Lasttime = currenttime;
// Copy the frames per second into a string to display in the window title bar
Sprintf (strframerate, "Current frames per second: % d", INT (framespersecond ));
// * New * commented out for this tutorial
// Set the window title bar to our string
// Setwindowtext (g_hwnd, strframerate );
// Reset the frames per second
Framespersecond = 0;
}
}
/// // Ccamera ////// //////////////////////////*
/////
//// This is the class Constructor
/////
/// // Ccamera ////// //////////////////////////*
Ccamera: ccamera ()
{
Cvector3 vzero = cvector3 (0.0, 0.0, 0.0); // init a vector to 0 0 0 for our position
Cvector3 vview = cvector3 (0.0, 1.0, 0.5); // init a starting view vector (looking up and out of the screen)
Cvector3 vup = cvector3 (0.0, 0.0, 1.0); // init a standard up vector (rarely ever changes)
M_vposition = vzero; // init the position to zero
M_vview = vview; // init the view to a STD starting View
M_vupvector = vup; // init the upvector
}
/// // Position camera ///// ///////////////////////////*
/////
//// This function sets the camera's position and view and up vector.
/////
/// // Position camera ///// ///////////////////////////*
Void ccamera: positioncamera (float positionx, float positiony, float positionz,
Float viewx, float viewy, float viewz,
Float upvectorx, float upvectorz)
{
Cvector3 vposition = cvector3 (positionx, positiony, positionz );
Cvector3 vview = cvector3 (viewx, viewy, viewz );
Cvector3 vupvector = cvector3 (upvectorx, upvectorz, upvectorz );
// The code above just makes it cleaner to set the variables.
// Otherwise we wowould have to set each variable x y and Z.
M_vposition = vposition; // assign the position
M_vview = vview; // assign the view
M_vupvector = vupvector; // assign the up Vector
}
/// // Set view by mouse /// /////////////////////////////*
/////
//// This allows us to look around using the mouse, like in most first person games.
/////
/// // Set view by mouse /// /////////////////////////////*
Void ccamera: setviewbymouse ()
{
Point mousepos; // This is a window structure that holds an X and Y
Int middlex = screen_width> 1; // This is a binary shift to get half the width
Int middley = screen_height> 1; // This is a binary shift to get half the height
Float angley = 0.0f; // This is the direction for looking up or down
Float anglez = 0.0f; // This will be the value we need to rotate around the Y axis (left and right)
Static float currentrotx = 0.0f;
// Get the mouse's current X, Y position
Getcursorpos (& mousepos );
// If our cursor is still in the middle, we never moved... so don't update the screen
If (mousepos. x = middlex) & (mousepos. Y = middley) return;
// Set the mouse position to the middle of our window
Setcursorpos (middlex, middley );
// Get the direction the mouse moved in, but bring the number down to a reasonable amount
Angley = (float) (middlex-mousepos. X)/500366f;
Anglez = (float) (middley-mousepos. y)/500366f;
// Here we keep track of the current rotation (for up and down) so that
// We can restrict the camera from doing a full 360 loop.
Currentrotx-= anglez;
// If the current rotation (in radians) is greater than 1.0, we want to cap it.
If (currentrotx> 1.0f)
Currentrotx = 1.0f;
// Check if the rotation is below-1.0, if so we want to make sure it doesn't continue
Else if (currentrotx <-1.0f)
Currentrotx =-1.0f;
// Otherwise, we can rotate the view around our position
Else
{
// To find the axis we need to rotate around for up and down
// Movements, we need to get a perpendicular vector from
// Camera's view vector and up vector. This will be the axis.
Cvector3 vaxis = cross (m_vview-m_vposition, m_vupvector );
Vaxis = normalize (vaxis );
// Rotate around our Perpendicular Axis and along the y-axis
Rotateview (anglez, vaxis. X, vaxis. Y, vaxis. z );
}
// Rotate around the Y axis No matter what the currentrotx is
Rotateview (angley, 0, 1, 0 );
}
/// // Rotate View ///// ///////////////////////////*
/////
//// This rotates the view around the position using an axis-Angle Rotation
/////
/// // Rotate View ///// ///////////////////////////*
Void ccamera: rotateview (float angle, float X, float y, float Z)
{
Cvector3 vnewview;
// Get the view vector (the direction we are facing)
Cvector3 vview = m_vview-m_vposition;
// Calculate the sine and cosine of the angle once
Float costheta = (float) Cos (angle );
Float sintheta = (float) sin (angle );
// Find the new X position for the new rotated point
Vnewview. x = (costheta + (1-costheta) * x * X) * vview. X;
Vnewview. x + = (1-costheta) * x * Y-z * sintheta) * vview. Y;
Vnewview. x + = (1-costheta) * x * z + y * sintheta) * vview. Z;
// Find the New Y position for the new rotated point
Vnewview. Y = (1-costheta) * x * Y + z * sintheta) * vview. X;
Vnewview. Y + = (costheta + (1-costheta) * y) * vview. Y;
Vnewview. Y + = (1-costheta) * y * z-x * sintheta) * vview. Z;
// Find the new Z position for the new rotated point
Vnewview. z = (1-costheta) * x * z-y * sintheta) * vview. X;
Vnewview. Z + = (1-costheta) * y * z + x * sintheta) * vview. Y;
Vnewview. Z + = (costheta + (1-costheta) * z) * vview. Z;
// Now we just add the newly rotated vector to our position to set
// Our new rotated view of our camera.
M_vview = m_vposition + vnewview;
}
/// // Strafe camera ///// ///////////////////////////*
/////
//// This strafes the camera left and right depending on the speed (-/+)
/////
/// // Strafe camera ///// ///////////////////////////*
Void ccamera: strafecamera (float speed)
{
// Add the strafe vector to our position
M_vposition.x + = m_vstrafe.x * speed;
M_vposition.z + = m_vstrafe.z * speed;
// Add the strafe vector to our view
M_vview.x + = m_vstrafe.x * speed;
M_vview.z + = m_vstrafe.z * speed;
}
/// // Move camera ///// ///////////////////////////*
/////
//// This will move the camera forward or backward depending on the speed
/////
/// // Move camera ///// ///////////////////////////*
Void ccamera: movecamera (float speed)
{
// Get the current view vector (the direction we are looking)
Cvector3 vvector = m_vview-m_vposition;
Vvector = normalize (vvector );
M_vposition.x + = vvector. x * speed; // Add our acceleration to our position's x
M_vposition.y + = vvector. y * speed; // Add our acceleration to our position's Y
M_vposition.z + = vvector. z * speed; // Add our acceleration to our position's Z
M_vview.x + = vvector. x * speed; // Add our acceleration to our view's x
M_vview.y + = vvector. y * speed; // Add our acceleration to our view's Y
M_vview.z + = vvector. z * speed; // Add our acceleration to our view's Z
}
//// // Check for movement ///////// ///////////////////*
/////
//// This function handles the input faster than in the winproc ()
/////
//// // Check for movement ///////// ///////////////////*
Void ccamera: checkformovement ()
{
// Once we have the frame interval, we find the current speed
Float speed = kspeed * g_frameinterval;
// Check if we hit the up arrow or the 'W' key
If (getkeystate (vk_up) & 0x80 | getkeystate ('W') & 0x80 ){
// Move our camera forward by a positive speed
Movecamera (speed );
}
// Check if we hit the down arrow or the's 'key
If (getkeystate (vk_down) & 0x80 | getkeystate ('s') & 0x80 ){
// Move our camera backward by a negative speed
Movecamera (-speed );
}
// Check if we hit the left arrow or the 'A' key
If (getkeystate (vk_left) & 0x80 | getkeystate ('A') & 0x80 ){
// Strafe the camera left
Strafecamera (-speed );
}
// Check if we hit the right arrow or the 'D' key
If (getkeystate (vk_right) & 0x80 | getkeystate ('D') & 0x80 ){
// Strafe the camera right
Strafecamera (speed );
}
}
//// // Update ////// //////////////////////////*
/////
//// This updates the camera's view and strafe Vector
/////
//// // Update ////// //////////////////////////*
Void ccamera: Update ()
{
// Initialize a variable for the cross product result
Cvector3 vcross = cross (m_vview-m_vposition, m_vupvector );
// Normalize the strafe Vector
M_vstrafe = normalize (vcross );
// Move the camera's view by the mouse
Setviewbymouse ();
// This checks to see if the keyboard was pressed
Checkformovement ();
// Calculate our frame rate and set our frame interval for time-based movement
Calculateframerate ();
}
//// // Look ////// //////////////////////////*
/////
//// This updates the camera according to
/////
//// // Look ////// //////////////////////////*
Void ccamera: Look ()
{
// Give OpenGL our camera position, then camera view, then camera up Vector
Glulookat (m_vposition.x, m_vposition.y, m_vposition.z,
M_vview.x, m_vview.y, m_vview.z,
M_vupvector.x, m_vupvector.y, m_vupvector.z );
}
//////////////////////////////////////// /////////////////////////////////////////
//
// * Quick notes *
//
// Nothing was changed for the camera code in this tutorial folder t that we took
// Out the math functions like cross (), magnize () and normalize (), since they
// Were needed in octree. cpp. The prototypes are stored in octree. h.
//
//
// Ben Humphrey (digiben)
// Game programmer
// DigiBen@GameTutorials.com
// Co-web host of www.gametutorials.com
//
//