本篇继承上篇的矩阵变换知识,加上了利用CStopWatch类来定义速度的思路
- static CStopWatch rotTimer;
- float yRot=rotTimer.GetElapsedSeconds()*30;
- m3dRotationMatrix44(mRottion,m3dDegToRad(yRot),0.0f,1.0f,0.0f);
- 利用GLFrustum类的SetPerspective函数来设置透视投影
- 利用GLFrustum类的GetProjectionMatrix()函数来得到投影矩阵与旋转操作相乘进行矩阵变换
#include<GLTools.h>
#include<GLMatrixStack.h>
#include<GLFrame.h>
#include<GLFrustum.h>
#include<GLGeometryTransform.h>
#include<GLBatch.h>
#include<StopWatch.h>
#include<GLUT/GLUT.h>
GLFrustum viewFrustum;
GLShaderManager shaderManager;
GLTriangleBatch torusBatch;
void ChangeSize(int w,int h)
{
if(h==0)
h=1;
glViewport(0,0,w,h);
viewFrustum.SetPerspective(35.0f,float(w)/float(h),1.0f,1000.0f);
//GlFrustum::SetPerspective(float fFov,float fAspect,float fNear,float fFar);
//四个参数分别为从顶点方向看过去的视场角度,宽高比,视角到近剪切面的距离,视角到远剪切面的距离
}
void RenderScene(void)
{
static CStopWatch rotTimer;
float yRot=rotTimer.GetElapsedSeconds()*30.0f;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
M3DMatrix44f mTranslate, mRotate, mModelView,mModelViewProjection;
m3dTranslationMatrix44(mTranslate,0.0f,0.0f,-2.5f);
m3dRotationMatrix44(mRotate,m3dDegToRad(yRot),0.0f,1.0f,0.0f);
m3dMatrixMultiply44(mModelView,mTranslate,mRotate);
m3dMatrixMultiply44(mModelViewProjection,viewFrustum.GetProjectionMatrix(),mModelView);
GLfloat vBlack[]={0.0f,0.0f,0.0f,1.0f};
shaderManager.UseStockShader(GLT_SHADER_FLAT,mModelViewProjection,vBlack);
torusBatch.Draw();
glutSwapBuffers();
glutPostRedisplay();
}
void SetupRC()
{
glClearColor(0.8f,0.8f,0.8f,1.0f);
glEnable(GL_DEPTH_TEST);
shaderManager.InitializeStockShaders();
gltMakeTorus(torusBatch,0.4f,0.15f,30,30);
//花托创建函数,gltMakeTorus(GLTriangleBatch& torusBatch,GLfloat majorRaidus,GLfloat minorRadius,GLint numMajor,Glint numMinor);后四个参数分别为中心到外边缘的半径,中心到内边缘的半径,沿主半径和内部较小半径的细分单元的数量。
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("ModelViewProjection Example");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}