OpenGL 绘制伞面

栏目: 后端 · 发布时间: 5年前

内容简介:在上一篇demo基础上修改详细代码已做注释:

在上一篇demo基础上修改 main.cpp 文件,完成使用 OpenGL 图片绘制和移动效果。

详细代码已做注释:

#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;
}

复制代码

以上所述就是小编给大家介绍的《OpenGL 绘制伞面》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Machine Learning in Action

Machine Learning in Action

Peter Harrington / Manning Publications / 2012-4-19 / GBP 29.99

It's been said that data is the new "dirt"—the raw material from which and on which you build the structures of the modern world. And like dirt, data can seem like a limitless, undifferentiated mass. ......一起来看看 《Machine Learning in Action》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具