OpenGL 繪製傘面

前言

在上一篇OpenGL 繪圖移動基礎上修改main.cpp文件,完成使用OpenGL圖片繪製和移動效果。bash

代碼

詳細代碼已作註釋:oop

#include "GLTools.h"
#include "GLMatrixStack.h"
#include "GLFrame.h"
#include "GLFrustum.h"
#include "GLBatch.h"
#include "GLGeometryTransform.h"
#include <math.h>
#include <GLUT/GLUT.h>


GLShaderManager  shaderManager;
GLMatrixStack    modelViewMatrix;
GLMatrixStack    projectionMatrix;
//參考幀
GLFrame          cameraFrame;
GLFrame          objectFrame;

//投影矩陣
GLFrustum        viewFrustum;

GLBatch          triangleFanBatch;


//幾何變換管道
GLGeometryTransform transformPipeline;

void changeSize(int w, int h) {
    glViewport(0, 0, w, h);
    //設置透視投影
    viewFrustum.SetPerspective(30.0f, float(w) / float(h), 2.0f, 400.0f);
    //加載透視矩陣
    projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
    
    //加載一個單元矩陣
    modelViewMatrix.LoadIdentity();
    
}

void specialKeys(int key, int x, int y){
    if (key == GLUT_KEY_UP) {
        objectFrame.RotateWorld(m3dDegToRad(-10.0f), 1.0f, 0.0f, 0.0f);
    } else if (key == GLUT_KEY_DOWN) {
        objectFrame.RotateWorld(m3dDegToRad(10.0f), 1.0f, 0.0f, 0.0f);
    } else if (key == GLUT_KEY_LEFT) {
        objectFrame.RotateWorld(m3dDegToRad(-10.0f), 0.0f, -1.0f, 0.0f);
    } else if (key == GLUT_KEY_RIGHT) {
        objectFrame.RotateWorld(m3dDegToRad(10.0f), 0.0f, 1.0f, 0.0f);
    }
    glutPostRedisplay();
}

void renderScene() {
    //清理緩衝區
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
    //壓棧
    modelViewMatrix.PushMatrix();
    //獲取透視矩陣
    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    
    modelViewMatrix.MultMatrix(mCamera);
    
    M3DMatrix44f mObjectFrame;
    objectFrame.GetMatrix(mObjectFrame);
    
    modelViewMatrix.MultMatrix(mObjectFrame);
    GLfloat vBlue[] = {0.41f, 0.35f, 0.0f, 0.8f};
    shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlue);
    triangleFanBatch.Draw();
    
    //邊框繪製
    GLfloat vMoccasin[] = {1.0f,0.9f,0.71f,1.0f};
    glPolygonOffset(-1.0f, -1.0f);
    glEnable(GL_POLYGON_OFFSET_LINE);
    glLineWidth(3.0f);
    
    glEnable(GL_LINE_SMOOTH);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vMoccasin);
    triangleFanBatch.Draw();
    
    //恢復
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glDisable(GL_POLYGON_OFFSET_LINE);
    glLineWidth(1.0f);
    glDisable(GL_LINE_SMOOTH);
    modelViewMatrix.PopMatrix();
    glutSwapBuffers();
    
}

void setupRC() {
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    shaderManager.InitializeStockShaders();
    glEnable(GL_DEPTH_TEST);
    
    transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
    cameraFrame.MoveForward(-20.0f);
    
    GLfloat vPoints[100][3];
    int nVert = 0;
    GLfloat r = 3.5f;
    
    //原點
    vPoints[nVert][0] = 0.0f;
    vPoints[nVert][1] = 0.0f;
    vPoints[nVert][2] = 0.0f;
    
    //繪製圓形
    for (GLfloat angle = 0; angle < M3D_2PI; angle += (M3D_2PI / 6.0f)) {
        nVert++;
        //x點座標
        vPoints[nVert][0] = float(cos(angle)) * r;
        //y點座標
        vPoints[nVert][1] = float(sin(angle)) * r;
        //z點座標
        vPoints[nVert][2] = -1.5f;
    }
    
    nVert++;
    vPoints[nVert][0] = r;
    vPoints[nVert][1] = 0;
    vPoints[nVert][2] = 0.0f;
    
    triangleFanBatch.Begin(GL_TRIANGLE_FAN, 8);
    triangleFanBatch.CopyVertexData3f(vPoints);
    triangleFanBatch.End();
    
    
    
}

int main(int argc, char* argv[]) {
    
    gltSetWorkingDirectory(argv[0]);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(600, 600);
    glutCreateWindow("dowZhang");
    glutReshapeFunc(changeSize);
    glutSpecialFunc(specialKeys);
    glutDisplayFunc(renderScene);
    
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        return 1;
    }
    
    setupRC();
    
    glutMainLoop();
    
    return 0;
}

複製代碼
相關文章
相關標籤/搜索