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();

}

}

 

posted @ 2021-11-02 12:51  玥茹苟  阅读(422)  评论(0编辑  收藏  举报