目的:构造轮子自动旋转的自行车
主要使用了:
- 矩阵构造(平移、旋转)
- 模型视图矩阵
- 三角形批次类(创建轮子)
- 投影矩阵(透视投影)
/*
bicycle.cpp 框架完成
by GaoLei 2016/05/30
*/
#include <GLTools.h>
#include <GLShaderManager.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLFrame.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>//计时以算旋转
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif
GLShaderManager shaderManager; // 着色器管理器
GLMatrixStack modelViewMatrix; // 模型视图矩阵
GLMatrixStack projectionMatrix; // 投影矩阵
GLFrame cameraFrame;
GLFrustum viewFrustum; // 视景体
GLTriangleBatch front_wheel;
GLBatch bike_body;
GLTriangleBatch back_wheel;
GLGeometryTransform transformPipeline;
void SetupRC()
{
shaderManager.InitializeStockShaders();
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClearColor(1.0F, 1.0F, 1.0F, 1.0F);
//建立几何图形,圆盘也可以使用批次用三角形扇画出
gltMakeDisk(front_wheel,0.1,0.2,30,2);
gltMakeDisk(back_wheel, 0.1, 0.2, 30, 2);
bike_body.Begin(GL_LINES, 6, 0);
bike_body.Vertex3f(-0.22f, -0.1f, 0);
bike_body.Vertex3f(0.22f, -0.1f, 0);
bike_body.Vertex3f(0.0f, 0.1f, 0);
bike_body.Vertex3f(0.0f,- 0.1f, 0);
bike_body.Vertex3f(-0.05f, 0.1f, 0);
bike_body.Vertex3f(0.05f, 0.1f, 0);
bike_body.End();
cameraFrame.MoveForward(-2.0f);
}
//改变视景体和视口,在改变窗口大小时调用
void ChangeSize(int w, int h)
{
GLfloat fAspect;
//防止对0进行除法操作
if (h == 0)
h = 1;
//将视口设置为窗口大小
glViewport(0, 0, w, h);
fAspect = (GLfloat)w / (GLfloat)h;
//生成透视投影
viewFrustum.SetPerspective(50.0f, fAspect, 1.0, 100.0);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
}
//进行调用以绘制场景
void RenderScene(void)
{
static GLfloat vBODYColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };
static GLfloat vFWColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
static GLfloat vBWColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
static CStopWatch rotTimer;
float zRot = rotTimer.GetElapsedSeconds() * 60.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//用当前清除色清除窗口
//保存当前模型视图矩阵
modelViewMatrix.PushMatrix();
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.MultMatrix(mCamera);
shaderManager.UseStockShader(GLT_SHADER_FLAT,
transformPipeline.GetModelViewProjectionMatrix(),
vBODYColor);
bike_body.Draw();//车身
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(-0.35, -0.25, 0.0);
modelViewMatrix.Rotate(zRot, 0.0f, 0.0f, 1.0f);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
vFWColor);
front_wheel.Draw();
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0.35f, -0.25f, 0.0f);
modelViewMatrix.Rotate(zRot, 0.0f, 0.0f, 1.0f);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
vBWColor);
back_wheel.Draw();
modelViewMatrix.PopMatrix();
modelViewMatrix.PopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Bike");
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;
}