• opengl之纹理贴图(三)


    opengl之纹理贴图

    h

    #ifndef TEXTURE_RENDER_H
    #define TEXTURE_RENDER_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QOpenGLBuffer>
    #include <QOpenGLVertexArrayObject>
    #include <QOpenGLShader>
    #include <QOpenGLShaderProgram>
    #include <QOpenGLTexture>
    
    #include <memory>
    
    namespace View3D{
    
        class TextureRender
        {
        public:
            TextureRender();
            ~TextureRender();
    
            void setTextures(std::vector<std::shared_ptr<QOpenGLTexture>>& textures_t);
            void inputVertices(
                    QOpenGLWidget* Widget_t,
                    std::unique_ptr<GLfloat[]>& vertices_t,
                    uint32_t num_t);
    
            void textureInitializeGL();
            void texturePaintGL();
    
         private:
            QOpenGLWidget* Widget;
    
            std::unique_ptr<GLfloat[]> vertices;
            //GLfloat* vertices;
            uint32_t num_vertices;
            std::vector<std::shared_ptr<QOpenGLTexture>> textures;
    
            QOpenGLShaderProgram program;
            QOpenGLBuffer vbo, ebo;
            QOpenGLVertexArrayObject vao;
    
        };
    }
    
    
    #endif // TEXTURE_RENDER_H

    cpp

    #include "texture_render.h"
    
    
    namespace View3D{
    /************************************************************/
    TextureRender::TextureRender(){}
    /************************************************************/
    TextureRender::~TextureRender(){
        vbo.destroy();
        ebo.destroy();
        vao.destroy();
    }
    
    /************************************************************/
    void TextureRender::setTextures(std::vector<std::shared_ptr<QOpenGLTexture>>& textures_t){
        textures = textures_t;
    }
    
    /************************************************************/
    void TextureRender::inputVertices(
        QOpenGLWidget* Widget_t,
        std::unique_ptr<GLfloat[]>& vertices_t,
        uint32_t num_t){
    
        Widget = Widget_t;
        vertices = std::move(vertices_t);
        num_vertices = num_t;
    }
    
    /************************************************************/
    void TextureRender::textureInitializeGL(){
    
        QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, Widget);
        const char *vsrc =R"(
            attribute highp vec4 vertex;
            attribute mediump vec4 texCoord;
            varying mediump vec4 texc;
            uniform mediump mat4 matrix;
            uniform mediump mat4 projection;
            void main(void)
            {
                gl_Position = projection* matrix * vertex;
                texc = texCoord;
            })";
        vshader->compileSourceCode(vsrc);
    
        QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, Widget);
        const char *fsrc =R"(
            uniform sampler2D texture;
            varying mediump vec4 texc;
            void main(void)
            {
               gl_FragColor = texture2D(texture, texc.st);
            })";
        fshader->compileSourceCode(fsrc);
    
        program.addShader(vshader);
        program.addShader(fshader);
        program.bindAttributeLocation("vertex", 0);
        program.bindAttributeLocation("texCoord", 1);
        program.link();
    
        program.bind();
        program.setUniformValue("texture", 0);
    
        vao.create();
        QOpenGLVertexArrayObject::Binder vaoBinder(&vao);
    
        vbo.create();
        vbo.bind();
        vbo.allocate(vertices.get(), sizeof(GLfloat)*num_vertices);
    
        program.enableAttributeArray(0);
        program.enableAttributeArray(1);
        program.setAttributeBuffer(0, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
        program.setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
    
        vbo.release();
    }
    
    /************************************************************/
    void TextureRender::texturePaintGL(){
    
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //GL_DEPTH_BUFFER_BIT
    
        QMatrix4x4 m;
        //m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f);
    //    m.translate(xPosition/1000.f, yPosition/1000.0f, -10.0f);
    //    m.rotate(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
    //    m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
    //    m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
        //m.scale(projectionfovy);
    
        QMatrix4x4 projection;
        //projection.perspective(projectionfovy, 1.0f*width()/height(), 0.1f, 100.0f);
        //projection.lookAt();
    
        program.setUniformValue("matrix", m);
        program.setUniformValue("projection", projection);
    
        for(int i = 0; i < textures.size(); ++i) {
            textures[i]->bind();
            glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);   //glDrawElements
        }
    }
    
    }
  • 相关阅读:
    NSString+URLEncoding
    编码解码
    RESTful架构理解
    jvm调优
    java大数据处理调优
    SQL 优化
    正确处理下载文件时HTTP头的编码问题(Content-Disposition)
    Spring 多媒体(文件上传)支持
    mybatis类型转换
    log4j Spring aop 注解的日志管理
  • 原文地址:https://www.cnblogs.com/lovebay/p/15498377.html
Copyright © 2020-2023  润新知