zoukankan      html  css  js  c++  java
  • 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
        }
    }
    
    }
  • 相关阅读:
    [Windows] 使用SC 命令管理与配置服务
    [SharePoint 2010] SharePoint 2010 部署、收回和删除解决方案----STSADM和PowerShell
    [SharePoint 2010] SharePoint 2010 FBA 配置以及自定义首页
    [Log]ASP.NET之HttpModule 事件执行顺序
    [SQL] 命令远程恢复数据库
    [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
    [工具] TreeSizeFree 查看每个文件夹的大小
    [APP] Android 开发笔记 006-使用短信验证SDK进行短信验证
    [APP] Android 开发笔记 004-Android常用基本控件使用说明
    [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
  • 原文地址:https://www.cnblogs.com/lovebay/p/15498377.html
Copyright © 2011-2022 走看看