OpenGL: 世界球体(移动,公转,自转)
void SetupRC()
- 绘制地板
很多个矩形组成
//绘制地板
floorBatch.Begin(GL_LINES, 300);
for (GLfloat x = -30; x <= 30; x += 0.5) {
floorBatch.Vertex3f(x, -0.55f, 30.0f);
floorBatch.Vertex3f(x, -0.55f, -30.0f);
floorBatch.Vertex3f(30.0f, -0.55f, x);
floorBatch.Vertex3f(-30.0f, -0.55f, x);
}
floorBatch.End();
- 绘制花托
void gltMakeTorus(GLTriangleBatch& torusBatch, GLfloat majorRadius, GLfloat minorRadius, GLint numMajor, GLint numMinor)
//绘制花托
gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
- 绘制球体
//绘制球体
gltMakeSphere(sphereBatch, 0.1f, 26, 13);
void RenderScene(void)
花托自转
modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
//绘制花托
shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vTorusColor);
torusBatch.Draw();
球体公转
//绘制公转
modelViewMatrix.Rotate(yRot * -0.2f, 0.0f, 1.0f, 0.0f);
modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vSphereColor);
sphereBatch.Draw();
世界坐标系移动
void SpecialKeys(int key,int x,int y)
{
float linar = 0.1f;
float angular = float(m3dDegToRad(0.5f));
if (key == GLUT_KEY_UP) {
cameraFrame.MoveForward(linar);
}
else if(key == GLUT_KEY_DOWN){
cameraFrame.MoveForward(-linar);
}
else if(key == GLUT_KEY_LEFT){
cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
}
else if(key == GLUT_KEY_RIGHT){
cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
}
}
代码:GitHub:(稍后上传)