//GLCamera.cpp
/********************************************************************
created: 2005/09/18
created: 18:9:2005 22:41
filename: D:\__Prog\Coding\OpenGL\GLPro\src\GLRender\GLCamera.cpp
file path: D:\__Prog\Coding\OpenGL\GLPro\src\GLRender
file base: GLCamera
file ext: cpp
author: x.brent
mail box: x.brent@gmail.com
mibole: 13864211195
msn: brent_yu@hotmail.com
oicq: 68516
purpose:
*********************************************************************/
#include <math.h>
#include <windows.h>
#include <gl\glaux.h>
#define SCREEN_WIDTH 800 // We want our screen width 800 pixels
#define SCREEN_HEIGHT 600 // We want our screen height 600 pixels
#define SCREEN_DEPTH 16 // We want 16 bits per pixel
#include "GLCamera.h"
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *
// This is how fast our camera moves (Sped up a bit due to normalizing our vectors)
#define kSpeed 5.5f
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *
/////////////////////////////////////// CROSS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns a perpendicular vector from 2 given vectors by taking the cross product.
/////
/////////////////////////////////////// CROSS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
///////////////////////////////// CCAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This is the class constructor
/////
///////////////////////////////// CCAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CCamera::CCamera()
{
CVector3 vZero = CVector3(0.0, 0.0, 0.0); // Init a vVector to 0 0 0 for our position
CVector3 vView = CVector3(0.0, 1.0, 0.5); // Init a starting view vVector (looking up and out the screen)
CVector3 vUp = CVector3(0.0, 0.0, 1.0); // Init a standard up vVector (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 vVector.
/////
///////////////////////////////// POSITION CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CCamera::PositionCamera(float positionX, float positionY, float positionZ,
float viewX, float viewY, float viewZ,
float upVectorX, float upVectorY, float upVectorZ)
{
CVector3 vPosition = CVector3(positionX, positionY, positionZ);
CVector3 vView = CVector3(viewX, viewY, viewZ);
CVector3 vUpVector = CVector3(upVectorX, upVectorY, upVectorZ);
// The code above just makes it cleaner to set the variables.
// Otherwise we would 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
//
/* char buf[128];
wsprintf(buf,"Position:%d %d %d LookAt:%d %d %d",int(m_vPosition.x),int(m_vPosition.y),int(m_vPosition.z),int(m_vView.x),int(m_vView.y),int(m_vView.z));
if(m_pText)
m_pText->EditItem(m_nTextID,0.0f,1.0f,0.0f,buf);
*/
}
///////////////////////////////// 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) ) / 1000.0f;
angleZ = (float)( (middleY - mousePos.y) ) / 1000.0f;
// 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 the
// camera's view vector and up vector. This will be the axis.
CVector3 vAxis = CrossProduct(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);
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 * 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 * 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;
}
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *
///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This strafes the camera left and right depending on the speed (-/+)
/////
///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CCamera::StrafeCamera(float speed)
{
// Strafing is quite simple if you understand what the cross product is.
// If you have 2 vectors (say the up vVector and the view vVector) you can
// use the cross product formula to get a vVector that is 90 degrees from the 2 vectors.
// For a better explanation on how this works, check out the OpenGL "Normals" tutorial at our site.
// In our new Update() function, we set the strafing vector (m_vStrafe). Due
// to the fact that we need this vector for many things includi
没有合适的资源?快使用搜索试试~ 我知道了~
VC++OpenGL例子下载.rar
共107个文件
h:33个
cpp:19个
lib:12个
3星 · 超过75%的资源 需积分: 9 145 下载量 91 浏览量
2009-12-22
17:21:52
上传
评论 2
收藏 374KB RAR 举报
温馨提示
VC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rarVC++OpenGL例子下载.rar
资源推荐
资源详情
资源评论
收起资源包目录
VC++OpenGL例子下载.rar (107个子文件)
glClientPro.aps 25KB
GLEnv.h.bak 1KB
Toolbar.bmp 238B
GLCameraBK.cpp 16KB
3DS.CPP 12KB
myGLView.cpp 7KB
GLRender.cpp 5KB
GLRenderCommFuc.cpp 5KB
GLListItem.cpp 4KB
GLFps.cpp 2KB
GLDrawText.cpp 2KB
GLReader.cpp 2KB
MainFrm.cpp 2KB
GLWindows.cpp 2KB
FileOperation.cpp 1KB
myGLApp.cpp 1KB
GLCamera.cpp 1KB
GLParser.cpp 977B
MyGLDoc.cpp 945B
GLFileRending.cpp 682B
StdAfx.cpp 549B
SecondGLView.cpp 542B
GLRender.dep 2KB
GLParser.dep 1KB
glClientPro.dep 713B
GLReader.dep 174B
glClientPro.dsp 5KB
GLRender.dsp 5KB
GLReader.dsp 4KB
GLParser.dsp 4KB
GLPro.dsw 1KB
glClientPro.dsw 545B
GLRender_d.exp 3KB
GLRender.exp 3KB
GLReader_d.exp 1KB
GLReader.exp 1KB
GLParser_d.exp 678B
GLParser.exp 670B
GLEXT.H 326KB
GL.H 67KB
glxext.h 30KB
glut.h 27KB
wglext.h 27KB
GLU.H 18KB
GLVector.h 13KB
GLAUX.H 12KB
GLRenderFuc.h 6KB
3dsMaterial.h 4KB
GLBoundSphere.h 3KB
GLBoundBox.h 3KB
GLNode.h 2KB
3dstypes.h 2KB
GLFunc.h 2KB
myGLView.h 2KB
GLRender.h 2KB
GLEntity.h 2KB
GLFace.h 2KB
GLTypes.h 1KB
MainFrm.h 1KB
MyGLDoc.h 1KB
GLEnv.h 966B
myGLApp.h 909B
GLReader.h 826B
resource.h 739B
GLParserFunc.h 735B
GLError.h 723B
GLParser.h 707B
StdAfx.h 690B
GLSCreenconfig.h 670B
SecondGLView.h 602B
GLObjBase.h 496B
asm_glTextures.hpp 923B
asm_glTextures.hpp 914B
glClientPro.ico 1KB
MyGLDoc.ico 1KB
GLAUX.LIB 1.25MB
OPENGL32.LIB 328KB
opengl.lib 237KB
GLU32.LIB 48KB
glu.lib 35KB
glut32.lib 28KB
GLRender_d.lib 6KB
GLRender.lib 5KB
GLReader_d.lib 3KB
GLReader.lib 3KB
GLParser_d.lib 2KB
GLParser.lib 2KB
glClientPro.mak 11KB
GLRender.mak 8KB
GLParser.mak 5KB
GLReader.mak 5KB
GLPro.opt 172KB
3DSFileFormat.rar 27KB
3ds.rar 6KB
OpenGL.rar 3KB
glClientPro.rc 8KB
glClientPro.rc2 403B
GLPro.sln 3KB
GLPro.suo 14KB
glClientPro.vcproj.BRENT_NEC.Administrator.user 1KB
共 107 条
- 1
- 2
fengyuxx1985
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页