OpenGL_大球小球案例分析

2020-07-21  本文已影响0人  星星1024

1、地板绘制

1.1 声明变量
GLShaderManager     shaderManager;          // 着色器管理器
GLMatrixStack       modelViewMatrix;        // 模型视图矩阵堆栈
GLMatrixStack       projectionMatrix;       // 投影矩阵堆栈
GLFrustum           viewFrustum;            // 视景体
GLGeometryTransform transformPipeline;      // 几何图形变换管道

GLTriangleBatch     torusBatch;             //大球
GLTriangleBatch     sphereBatch;            //小球
GLBatch             floorBatch;          //地板

//角色帧 照相机角色帧
GLFrame   cameraFrame;
GLFrame  objectFrame;

1.2 设置投影矩阵,变换管道

//屏幕更改大小或已初始化
void ChangeSize(int nWidth, int nHeight)
{
    //1. 设置视口
    glViewport(0, 0, nWidth, nHeight);

    //2. 创建投影矩阵
    viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);
    projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
    
    //3. 变换管道设置2个矩阵堆栈(管理)
    transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
    
}
1.3 设置地板数据
   //1. 初始化
    glClearColor(0, 0, 0, 1);
    shaderManager.InitializeStockShaders();
    //2. 开启深度测试绘制小球需要用到
    glEnable(GL_DEPTH_TEST);
    
    //3. 地板数据(物体坐标系)
    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();
1.4 地板渲染
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    //1. 颜色(地板,大球颜色,小球颜色)
    static GLfloat vFloorColor[] = {0.0f,1.0f,0.0f,1.0f};
    static GLfloat vTorusColor[] = {1.0f,0.0f,0.0f,1.0f};
    static GLfloat vSpereColor[] = {0.0f,0.0f,1.0f,1.0f};
    
    
    //2. 动画
    static CStopWatch rotTimer;
    float yRot = rotTimer.GetElapsedSeconds()*60.0f;
    
    modelViewMatrix.PushMatrix();
    
    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.PushMatrix(mCamera);
    
    
    //4.地面绘制;
    shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vFloorColor);
    floorBatch.Draw();

2、大球绘制

2.1 设置大球数据
//4. 设置大球
gltMakeSphere(torusBatch, 0.4f, 40, 80);
2.2 大球渲染
    //3. 渲染大球
    //设置动画
    static CStopWatch rotTimer;
    float yRot = rotTimer.GetElapsedSeconds()* 60.f;
    
    //4. 设置点光源位置
    M3DVector4f vLightPos = {0, 10, 5, 1};
    
    //5. 大球平移
    modelViewMatrix.Translate(0, 0, -3.0);
    
    //6.大球旋转
    modelViewMatrix.PushMatrix();
    modelViewMatrix.Rotate(yRot, 0, 1, 0);
    shadermanager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightPos, vTorusColor);
    torusBatch.Draw();
    modelViewMatrix.PopMatrix();

3、小球绘制

3.1 小球设置
   //5. 设置小球
    gltMakeSphere(sphereBatch, 0.2f, 12, 24);
    for (int i = 0; i < NUM_SPHERES; i++) {
        //y轴不变,X,Z产生随机值
        GLfloat x = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
        GLfloat z = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
        
        //在y方向,将球体设置为0.0的位置,这使得它们看起来是飘浮在眼睛的高度
        //对spheres数组中的每一个顶点,设置顶点数据
        spheres[i].SetOrigin(x, 0.0f, z);
    }
3.2 小球渲染
   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();
    }
3.3 设置一个大球围绕小球旋转
//设置旋转
    modelViewMatrix.Rotate(-2*yRot, 0, 1, 0);
//设置平移
    modelViewMatrix.Translate(0.8, 0, 0);
    shadermanager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,transformPipeline.GetModelViewMatrix(),transformPipeline.GetProjectionMatrix(),vLightPos,vSphereColor);
       sphereBatch.Draw();

4、特殊函数

4.1 键位函数

void SpeacialKeys(int key, int x, int y)
{
    float linear = 0.1f;
    float angular = float(m3dDegToRad(5.0f));

    if (key == GLUT_KEY_UP) {
        cameraFrame.MoveForward(linear);
    }
    if (key == GLUT_KEY_DOWN) {
        cameraFrame.MoveForward(-linear);
    }

    if (key == GLUT_KEY_LEFT) {
        cameraFrame.RotateWorld(angular, 0, 1, 0);
    }
    if (key == GLUT_KEY_RIGHT) {
        cameraFrame.RotateWorld(-angular, 0, 1, 0);
    }
}
上一篇下一篇

猜你喜欢

热点阅读