• opengl之三角形绘制glDrawArrays(四)


    opengl之三角形绘制glDrawArrays

    .h

     

    #ifndef TRIANGLE_RENDER_H
    #define TRIANGLE_RENDER_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QOpenGLBuffer>
    #include <QOpenGLVertexArrayObject>
    #include <QOpenGLShader>
    #include <QOpenGLShaderProgram>
    
    #include <memory>
    
    namespace View3D{
    
        class TriangleRender
        {
        public:
            TriangleRender();
            ~TriangleRender();
    
            void setPointColor();
            void setTransformProject(const QMatrix4x4& model_t, const QMatrix4x4& view_t, const QMatrix4x4& projection_t);
    
            void inputVertices(
                    QOpenGLWidget* Widget_t,
                    GLfloat* vertices_t,
                    uint32_t num_t);
    
            void triangleInitializeGL();
            void trianglePaintGL();
    
         private:
            QOpenGLWidget* Widget;
    
            GLfloat* vertices;
            uint32_t num_vertices;
    
            QOpenGLShaderProgram program;
            QOpenGLBuffer vbo, ebo;
            QOpenGLVertexArrayObject vao;
    
            QMatrix4x4 model, view, projection;
        };
    }
    
    #endif // TRIANGLE_RENDER_H

     

    .cpp

    #include <QOpenGLShader>
    #include "triangle_render.h"
    
    namespace View3D{
    
    /************************************************************/
    TriangleRender::TriangleRender():
        Widget(nullptr),
        vertices(nullptr),
        num_vertices(0){}
    
    /************************************************************/
    TriangleRender::~TriangleRender(){
        vbo.destroy();
        ebo.destroy();
        vao.destroy();
    }
    
    /************************************************************/
    void TriangleRender::setPointColor(){}
    
    /************************************************************/
    void TriangleRender::setTransformProject(const QMatrix4x4& model_t,
                                             const QMatrix4x4& view_t,
                                             const QMatrix4x4& projection_t){
        model = model_t;
        view = view_t;
        projection = projection_t;
    }
    
    /************************************************************/
    void TriangleRender::inputVertices(
            QOpenGLWidget* Widget_t,
            GLfloat* vertices_t,
            uint32_t num_t){
    
        Widget = Widget_t;
        vertices = vertices_t;
        num_vertices = num_t;
    }
    
    /************************************************************/
    void TriangleRender::triangleInitializeGL(){
    
        QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, Widget);
         const char *vsrc =R"(
            attribute highp vec3 vertex;
            uniform mediump mat4 model;
            uniform mediump mat4 view;
            uniform mediump mat4 projection;
            void main(void)
            {
                gl_Position = projection * view * model * vec4(vertex, 1.0f);
            })";
    
         vshader->compileSourceCode(vsrc);
    
         QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, Widget);
         const char *fsrc =R"(
            uniform vec3 colorset;
            void main(void)
            {
                gl_FragColor = vec4(colorset, 1.0f);
            })";
    
         fshader->compileSourceCode(fsrc);
    
        program.addShader(vshader);
        program.addShader(fshader);
        program.bindAttributeLocation("vertex", 0);
        //program.bindAttributeLocation("color", PROGRAM_TEXCOORD_ATTRIBUTE);
        program.link();
        program.bind();
    
        QOpenGLVertexArrayObject::Binder vaoBinder(&vao);
    
        vbo.create();
        vbo.bind();
        vbo.allocate(vertices, sizeof(GLfloat)*num_vertices);
    
        program.setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(GLfloat) * 3);
        program.enableAttributeArray(0);
    
        //    attr = program->attributeLocation("aColor");
        //    program->setAttributeBuffer(attr, GL_FLOAT, sizeof(GLfloat) * 3, 3, sizeof(GLfloat) * 6);
        //    program->enableAttributeArray(attr);
    
        vbo.release();
        program.release();
    
    }
    
    /************************************************************/
    void TriangleRender::trianglePaintGL(){
    
        program.bind();
        {
            program.setUniformValue("model", model);
            program.setUniformValue("view", view);
            program.setUniformValue("projection", projection);
    
            QVector3D colorset = QVector3D(1.0f, 0.2f, 1.0f);
            program.setUniformValue("colorset", colorset);
    
            QOpenGLVertexArrayObject::Binder vaoBind(&vao);
            int v_num = num_vertices/3;
            glDrawArrays(GL_TRIANGLES, 0, v_num);  //GL_TRIANGLE_FAN GL_TRIANGLES  GL_LINE_LOOP  GL_LINE_STRIP
    
        }
        program.release();
    
    }
    
    }
  • 相关阅读:
    面试官:HashMap死循环形成的原因是什么?
    这几个IDEA高级调试技巧,用完就是香
    图示JVM工作原理
    写二进制,姿势一定要骚,省字段,省带宽,提效率...
    阿里大佬总结的40个多线程面试题,你能答上来几个?
    全网最全RabbitMQ总结,别再说你不会RabbitMQ
    .NETCore微服务探寻(三)
    .NETCore微服务探寻(二)
    .NETCore微服务探寻(一)
    谈谈spring-boot-starter-data-redis序列化
  • 原文地址:https://www.cnblogs.com/lovebay/p/15498391.html
Copyright © 2020-2023  润新知