OpenGL-经典案列之“自转 公转”

526 阅读2分钟

先睹为快:

绘制地板

  • 准备工作:
GLShaderManager shaderManager;//着色器管理器
GLMatrixStack modelViewMatrix;//模型视图矩阵
GLMatrixStack projectionMatrix;//投影矩阵
GLFrustum viewFrustum;//视景体
GLGeometryTransform transformPipeline;//几何图形变换管道
GLBatch floorBatch;//地板
GLTriangleBatch torusBatch;//大球
GLTriangleBatch sphereBatch;//小球
GLFrame cameraFrame;//观察者

#define NUM_SPHERES 50 //小球个数
GLFrame spheres[NUM_SPHERES];
  • 设置视口:初始化或屏幕更改大小时调用
void changeSize(int nWidth, int nHeight) {
    //设置视口
    glViewport(0, 0, nWidth, nHeight);
    //创建投影矩阵
    viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);
    //并将其加载到投影矩阵堆栈上
    //viewFrustum.GetProjectionMatrix()  获取viewFrustum投影矩阵
    projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
    //设置变换管道以使用两个矩阵堆栈(变换矩阵modelViewMatrix ,投影矩阵projectionMatrix
    transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
}
  • 绘制场景:
void renderSence(void) {
    //地板颜色
    static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };

    //清除颜色缓冲区和深度缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    //绘制地面
    shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vFloorColor);
    
    floorBatch.Draw();
    
    //执行缓冲区交换
    glutSwapBuffers();
}
  • 设置渲染环境:
void setupRC(void) {
    //初始化
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    shaderManager.InitializeStockShaders();
    
    //开启深度测试
    glEnable(GL_DEPTH_TEST);
    
    //设置地板顶点数据
    floorBatch.Begin(GL_LINES, 324);
    for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
        floorBatch.Vertex3f(x, -0.55f, 20.0f);
        floorBatch.Vertex3f(x, -0.55f, -20.0f);
        
        floorBatch.Vertex3f(20.0f, -0.55f, x);
        floorBatch.Vertex3f(-20.0f, -0.55f, x);
    }
    floorBatch.End();

}
  • main执行:
int main(int argc, char *argv[]) {
    gltSetWorkingDirectory(argv[0]);
    //初始化GLUT库
    glutInit(&argc, argv);
    //初始化双缓冲窗口,颜色和深度缓冲区
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    
    glutCreateWindow("OpenGL Test");
    
    glutReshapeFunc(changeSize);
    glutDisplayFunc(renderSence);

    setupRC();
    
    glutMainLoop();
    return 0;
}
  • 运行:

绘制大球+自转

  • 主要在renderSence()函数中绘制:
    //大球颜色
    static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    //基于时间动画
    static CStopWatch rotTimer;
    float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
    
    //获取光源位置
    M3DVector4f vLightPos = {0.0f,10.0f,5.0f,1.0f};
    
    //大球位置向屏幕里移动
    modelViewMatrix.Translate(0.0f, 0.0f, -3.0f);
    
    //压栈
    modelViewMatrix.PushMatrix();
    
    //大球绕y轴自转
    modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
    
    //指定合适着色器->点光源着色器
    shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightPos, vTorusColor);
    
    torusBatch.Draw();
    
    //绘制完毕,出栈
    modelViewMatrix.PopMatrix();
  • 绘制完关键一步:提交重新渲染
    glutPostRedisplay();
  • 运行:

额,这是gif哦!由于球体颜色均匀看不出它在转动。。。我们关闭深度测试看下:

    //开启深度测试
//    glEnable(GL_DEPTH_TEST);

这下就明显了~

绘制小球

  • 同理,也在renderSence()函数中绘制:
    //小球颜色
    static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f};
    
    //画小球
    for (int i = 0; i < NUM_SPHERES; i++) {
        modelViewMatrix.PushMatrix();
        modelViewMatrix.MultMatrix(spheres[i]);
        shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
                                     transformPipeline.GetProjectionMatrix(), vLightPos, vSphereColor);
        sphereBatch.Draw();
        modelViewMatrix.PopMatrix();
        
    }
  • 运行:

让一颗篮球绕红球转:

  • renderSence()中:
    //让一个小篮球围绕大球公转自转
    modelViewMatrix.PushMatrix();
    
    modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f);
    modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
    shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,transformPipeline.GetModelViewMatrix(),transformPipeline.GetProjectionMatrix(),vLightPos,vSphereColor);
    sphereBatch.Draw();
    
    modelViewMatrix.PopMatrix();
  • 运行:

移动观察者

  • renderSence()中:
    //加入观察者
    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.PushMatrix(mCamera);
  • specialKeys()函数根据方向键控制移动:
void specialKeys(int key, int x, int y) {
    //移动步长
    float linear = 0.1f;
    
    //旋转度数
    float angular = float(m3dDegToRad(5.0f));
    
    if (key == GLUT_KEY_UP) {
        //MoveForward 平移
        cameraFrame.MoveForward(linear);
    }
    if (key == GLUT_KEY_DOWN) {
        cameraFrame.MoveForward(-linear);
    }
    
    if (key == GLUT_KEY_LEFT) {
        //RotateWorld 旋转
        cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
    }
    
    if (key == GLUT_KEY_RIGHT) {
        cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
    }
}
  • 注册函数:
glutSpecialFunc(specialKeys);
  • 运行:即开头gif~