• 纹理


    多纹理

    #ifndef TRIANGLE_H
    #define TRIANGLE_H
    
    #include <QWidget>
    #include <QGLWidget>
    #include <QOpenGLShader>
    #include <QOpenGLShaderProgram>
    #include <QDebug>
    #include <QOpenGLFunctions>
    #include <QOpenGLFunctions_3_3_Core>
    #include <QTime>
    #include <QtMath>
    #include <QTimer>
    #include <QOpenGLTexture>
    
    #include "shader.h"
    
    namespace Ui {
    class Triangle;
    }
    
    class Triangle : public QGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    
    public:
        explicit Triangle();
        ~Triangle();
    protected:
        virtual void initializeGL();
        virtual void paintGL();
        virtual void resizeGL(int w, int h);
    private:
        QOpenGLFunctions_3_3_Core *core;
        QOpenGLTexture *texture1, *texture2;
        Shader *ourShader;
    };
    
    #endif // TRIANGLE_H
    widget.h
    #include "triangle.h"
    #include "ui_triangle.h"
    
    GLuint VBO, VAO, EBO;
    
    Triangle::Triangle()
    {
    
    }
    
    Triangle::~Triangle()
    {
        delete ourShader;
        core->glDeleteVertexArrays(1, &VAO);
        core->glDeleteBuffers(1, &VBO);
        core->glDeleteBuffers(1, &EBO);
        texture1->destroy();
        texture2->destroy();
    }
    
    void Triangle::initializeGL()
    {
        //着色器部分
        core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
        if (!core) {
              qWarning() << "Could not obtain required OpenGL context version";
              exit(1);
        }
        ourShader = new Shader(":/vertexshader.vert", ":/fragmentshader.frag");
    
        //VAO,VBO数据部分
        GLfloat vertices[] = {
            0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right //注意新的数据,有纹理单元
            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
           -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
           -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
        };
    
        GLuint indices[] = {
            0, 1, 3, // first triangle
            1, 2, 3  // second triangle
        };
    
        core->glGenVertexArrays(1, &VAO);
        core->glGenBuffers(1, &VBO);
        core->glGenBuffers(1, &EBO);
    
        core->glBindVertexArray(VAO);
    
        core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
        core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
        core->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        core->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
        //position attribute
        core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), nullptr);
        core->glEnableVertexAttribArray(0);
        //color attribute
        core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3 * sizeof(float)));
        core->glEnableVertexAttribArray(1);
    
        core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void *)(6*sizeof(float)));
        core->glEnableVertexAttribArray(2);
    
        //纹理1
        texture1 = new QOpenGLTexture(QImage(":/box.jpg").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
        if(!texture1->isCreated()) {
            qDebug()<<"Failed to load texture" << endl;
        }
        texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
        texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);
    
        texture1->setMinificationFilter(QOpenGLTexture::Linear);
        texture1->setMagnificationFilter(QOpenGLTexture::Linear);
        texture1->setFormat(QOpenGLTexture::RGBFormat);
    
    
        texture2 = new QOpenGLTexture(QImage(":/awesomeface.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
        if(!texture2->isCreated()) {
            qDebug()<<"Failed to load texture" << endl;
        }
        texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
        texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);
    
        texture2->setMinificationFilter(QOpenGLTexture::Linear);
        texture2->setMagnificationFilter(QOpenGLTexture::Linear);
        texture2->setFormat(QOpenGLTexture::RGBFormat);
    
    
    
    }
    
    void Triangle::paintGL()
    {
        core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        core->glClear(GL_COLOR_BUFFER_BIT);
    
        ourShader->use();
        ourShader->shaderProgram.setUniformValue("texture1", 0);
        ourShader->shaderProgram.setUniformValue("texture2", 1);
        core->glActiveTexture(GL_TEXTURE1);
        texture2->bind();
        core->glActiveTexture(GL_TEXTURE0);
        texture1->bind();
    
        core->glBindVertexArray(VAO);
        core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
    
    }
    
    void Triangle::resizeGL(int w, int h)
    {
        core->glViewport(0, 0, w, h);
    }
    widget.cpp
    #version 330 core
    layout (location = 0) in vec3 aPos;
    layout (location = 1) in vec3 aColor;
    layout (location = 2) in vec2 aTexCoord;
    
    out vec3 ourColor;
    out vec2 TexCoord;
    
    void main(){
      gl_Position = vec4(aPos, 1.0f);
      ourColor = aColor;
      TexCoord = aTexCoord;
    }
    vertexshader.vert
    #version 330 core
    out vec4 FragColor;
    
    in vec3 ourColor;
    in vec2 TexCoord;
    
    uniform sampler2D texture1;
    uniform sampler2D texture2;
    
    void main()
    {
        FragColor = mix(texture2D(texture1, TexCoord), texture2D(texture2, TexCoord), 0.2f);
    }
    fragmentshader.frag
  • 相关阅读:
    [Unit Testing] Test Mongoose model
    2021CSPJ 组初级真题答案及全部解析
    AcWing 1097. 池塘计数
    AcWing 1076. 迷宫问题
    AcWing 1107 魔板
    K进制试题解析
    AcWing 1100. 抓住那头牛
    AcWing 1106. 山峰和山谷
    AcWing 188. 武士风度的牛
    AcWing 173. 矩阵距离
  • 原文地址:https://www.cnblogs.com/ch122633/p/12073408.html
Copyright © 2020-2023  润新知