📄 3dcamera.cpp
字号:
#include "stdafx.h"
#include "3dCamera.h"
#include <math.h>
#define X 0
#define Y 1
#define Z 2
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// C3dCamera
IMPLEMENT_DYNAMIC(C3dCamera, CObject)
/////////////////////////////////////////////////////////////////////////////
// C3dCamera construction
C3dCamera::C3dCamera()
{
// Initialize our cameras' member variables
m_fOrigin[0] = 0.0f;
m_fOrigin[1] = 2.0f;
m_fOrigin[2] = 15.0f;
m_fRotation[0] = -75.0f;
m_fRotation[1] = 0.0f;
m_fRotation[2] = -15.0f;
m_fFovY = 45.0f;
m_fNear = 1.0f;
m_fFar = 5000.0f;
m_bPerspective = TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// C3dCamera Destructor
C3dCamera::~C3dCamera()
{
}
/////////////////////////////////////////////////////////////////////////////
// C3dCamera Procedures
void C3dCamera::PositionCamera()
{
// Translate to the camera position. We invert the sign
// of the camera positions because we are actually moving
// the World coordinates, not the cameras..
glTranslated(-m_fOrigin[X], -m_fOrigin[Y], -m_fOrigin[Z]);
// Rotate the camera
glRotatef(m_fRotation[X], 1.0f, 0.0f, 0.0f);
glRotatef(m_fRotation[Y], 0.0f, 1.0f, 0.0f);
glRotatef(m_fRotation[Z], 0.0f, 0.0f, 1.0f);
}
void C3dCamera::GetOrigin(GLfloat *x, GLfloat *y, GLfloat *z)
{
*x = m_fOrigin[X];
*y = m_fOrigin[Y];
*z = m_fOrigin[Z];
}
void C3dCamera::GetRotation(GLfloat *x, GLfloat *y, GLfloat *z)
{
*x = m_fRotation[X];
*y = m_fRotation[Y];
*z = m_fRotation[Z];
}
void C3dCamera::SetOrigin(GLfloat x, GLfloat y, GLfloat z)
{
m_fOrigin[X] = x;
m_fOrigin[Y] = y;
m_fOrigin[Z] = z;
}
void C3dCamera::SetRotation(GLfloat x, GLfloat y, GLfloat z)
{
m_fRotation[X] = x;
m_fRotation[Y] = y;
m_fRotation[Z] = z;
}
void C3dCamera::ResetView(int w, int h)
{
// Save the screen width and height
if(w || h) {
m_iScreenWidth = (GLsizei)w;
m_iScreenHeight = (GLsizei)h;
}
// calculate the aspect ratio of the screen
if (m_iScreenHeight==0)
m_fAspect = (GLfloat)m_iScreenWidth;
else
m_fAspect = (GLfloat)m_iScreenWidth/(GLfloat)m_iScreenHeight;
// Calculate the clipping volume along the y-axis, then set our
// right, left, top and bottom clipping volumn
GLfloat viewDepth = fabs(m_fOrigin[2]);
GLfloat clipY = tan(0.0174*(m_fFovY/2))*viewDepth;
if(m_iScreenWidth <= m_iScreenHeight) {
m_fLeft = -clipY;
m_fRight = clipY;
m_fBottom = -clipY*m_iScreenHeight/m_iScreenWidth;
m_fTop = clipY*m_iScreenHeight/m_iScreenWidth;
}
else {
m_fLeft = -clipY*m_iScreenWidth/m_iScreenHeight;
m_fRight = clipY*m_iScreenWidth/m_iScreenHeight;
m_fBottom = -clipY;
m_fTop = clipY;
}
// Set Viewport to window dimensions
glViewport(0, 0, m_iScreenWidth, m_iScreenHeight);
// Reset the projection matrix (coordinate system)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(m_bPerspective)
{
// Perspective transformations.
gluPerspective(m_fFovY, m_fAspect, m_fNear, m_fFar);
}
else {
// Orthographic transformations.
glOrtho(m_fLeft, m_fRight, m_fBottom, m_fTop, -m_fOrigin[2], m_fFar);
}
// Reset the ModelView matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -