Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Getting-started - 02 - Hello-Triangle

学习learnopengl文章对应地址:https://learnopengl-cn.github.io/01%20Getting%20started/04%20Hello%20Triangle/

Visual Studio c++ 文件和 qt 项目链接(在一个包内):https://download.csdn.net/download/zzjzmdx/16605193

展示图:

Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Getting-started - 02 - Hello-Triangle_第1张图片

Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Getting-started - 02 - Hello-Triangle_第2张图片

此部分代码分为两部分,如果看矩形就把三角形标注内的全部注释,矩形标注内的全部打开

Visual Studio 2019

代码如下:

三角形:

#include 
#include 
#include 

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

//点数组 每三个值 确定一个点
float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
};


const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";



int main()
{
    //init
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window = glfwCreateWindow(800, 600, "DrawTriangle01", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
	  //将该窗口作为当前线程的主上下文
    glfwMakeContextCurrent(window);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    //init

    //vertex shader 顶点着色器
    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);//创建顶点着色器
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);//将着色器中的源代码设置为string指定的字符串数组中的源代码
    glCompileShader(vertexShader);//编译着色器
    //测试编译是否成功
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "vertex shader 顶点着色器::编译失败\n" << infoLog << std::endl;
    }
    //测试编译是否成功
    //vertex shader 顶点着色器

    //fragment shader 片段着色器
    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);//创建片段着色器
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);//将着色器中的源代码设置为string指定的字符串数组中的源代码
    glCompileShader(fragmentShader);//编译着色器
    //测试编译是否成功
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    //测试编译是否成功
    //fragment shader 片段着色器

    //链接着色器到program对象
    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();//创建一个program对象
    glAttachShader(shaderProgram, vertexShader);//将顶点着色器对象附加到program对象
    glAttachShader(shaderProgram, fragmentShader);//将着片段色器对象附加到program对象
    glLinkProgram(shaderProgram);//连接一个program对象

    //测试链接是否成功
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    //测试链接是否成功
    //链接着色器到program对象

    //删除片段着色器
    glDeleteShader(vertexShader);
    //删除顶点着色器
    glDeleteShader(fragmentShader);

    //创建VAO   VAO: 顶点数组对象:Vertex Array Object,  
    unsigned int VAO;
    glGenVertexArrays(1, &VAO); // 用来生成缓冲区对象
    glBindVertexArray(VAO);//绑定VAO

    //创建VBO,  VBO:顶点缓冲对象:Vertex Buffer Object
    unsigned int VBO;
    glGenBuffers(1, &VBO);//用来生成缓冲区对象
    //把新创建的缓冲绑定到GL_ARRAY_BUFFER目标上
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    //复制顶点数组到缓冲中供OpenGL使用
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    //告诉OpenGL该如何解析顶点数据
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    //允许顶点着色器读取GPU(服务器端)数据
    glEnableVertexAttribArray(0);


    //解绑VAO
    glBindVertexArray(0);
    //解绑VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    while (!glfwWindowShouldClose(window))
    {
        processInput(window);
        //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);//使用程序对象作为当前渲染状态的一部分
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

矩形:

#include 
#include 
#include 

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

float vertices[] = {
      0.5f,  0.5f, 0.0f,  // top right
      0.5f, -0.5f, 0.0f,  // bottom right
     -0.5f, -0.5f, 0.0f,  // bottom left
     -0.5f,  0.5f, 0.0f   // top left 
};
unsigned int indices[] = {  // note that we start from 0!
    0, 1, 3,  // first Triangle
    1, 2, 3   // second Triangle
};


const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";



int main()
{
    //init
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window = glfwCreateWindow(800, 600, "DrawRectangle02-VAOandVBO", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    //init

    //vertex shader 顶点着色器
    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);//创建顶点着色器
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);//将着色器中的源代码设置为string指定的字符串数组中的源代码
    glCompileShader(vertexShader);//编译着色器
    //测试编译是否成功
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "vertex shader 顶点着色器::编译失败\n" << infoLog << std::endl;
    }
    //测试编译是否成功
    //vertex shader 顶点着色器

    //fragment shader 片段着色器
    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);//创建片段着色器
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);//将着色器中的源代码设置为string指定的字符串数组中的源代码
    glCompileShader(fragmentShader);//编译着色器
    //测试编译是否成功
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    //测试编译是否成功
    //fragment shader 片段着色器

    //链接着色器到program对象
    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();//创建一个program对象
    glAttachShader(shaderProgram, vertexShader);//将顶点着色器对象附加到program对象
    glAttachShader(shaderProgram, fragmentShader);//将着片段色器对象附加到program对象
    glLinkProgram(shaderProgram);//连接一个program对象

    //测试链接是否成功
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    //测试链接是否成功
    //链接着色器到program对象

    //删除片段着色器
    glDeleteShader(vertexShader);
    //删除顶点着色器
    glDeleteShader(fragmentShader);

    //创建VAO   VAO: 顶点数组对象:Vertex Array Object,  
    unsigned int VAO;
    glGenVertexArrays(1, &VAO); // 用来生成缓冲区对象
    glBindVertexArray(VAO);//绑定VAO

       //创建EBO 
    unsigned int VBO;
    glGenBuffers(1, &VBO);// 用来生成缓冲区对象
    //把新创建的缓冲绑定到GL_ARRAY_BUFFER目标上
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    //复制顶点数组到缓冲中供OpenGL使用
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    //创建EBO 
    unsigned int EBO;
    glGenBuffers(1, &EBO);// 用来生成缓冲区对象
    //把新创建的缓冲绑定到GL_ELEMENT_ARRAY_BUFFER目标上
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    //把我们的顶点数组复制到一个顶点缓冲中,供OpenGL使用
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    //告诉OpenGL该如何解析顶点数据
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

    glEnableVertexAttribArray(0);

    //解绑VAO
    glBindVertexArray(0);
    //解绑VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    //解绑EBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    while (!glfwWindowShouldClose(window))
    {
        processInput(window);
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);//使用程序对象作为当前渲染状态的一部分
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &EBO);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

qt  5.15.1 

代码如下:

main.cpp

#include "HelloTriangle.h"

#include 

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   QSurfaceFormat format;
   format.setDepthBufferSize(24);
   format.setVersion(3, 3);
   QSurfaceFormat::setDefaultFormat(format);
   app.setApplicationName("Hello-Triangle");
   HelloTriangle hellotriangle;
   hellotriangle.resize(800, 600);
   hellotriangle.show();
   return app.exec();
}

HelloTriangle.h

#ifndef HELLOTRIANGLE_H
#define HELLOTRIANGLE_H


#include 
#include 
#include 
#include 
#include 


class HelloTriangle : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    HelloTriangle(QWidget *parent = nullptr);
    ~HelloTriangle();

protected:
    void paintGL() override;
    void initializeGL() override;
    void resizeGL(int w,int h) override;

private:
    QOpenGLShaderProgram *m_program;
    QOpenGLVertexArrayObject *m_vao;
    QOpenGLBuffer *m_vbo;
    QOpenGLBuffer *m_ebo;

};

#endif // HELLOTRIANGLE_H

HelloTriangle.cpp

#include "HelloTriangle.h"

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";

float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
};

float vertices_triangle[] = {
      0.5f,  0.5f, 0.0f,  // top right
      0.5f, -0.5f, 0.0f,  // bottom right
     -0.5f, -0.5f, 0.0f,  // bottom left
     -0.5f,  0.5f, 0.0f   // top left
};
unsigned int indices[] = {  // note that we start from 0!
    0, 1, 3,  // first Triangle
    1, 2, 3   // second Triangle
};

HelloTriangle::HelloTriangle(QWidget *parent)
    : QOpenGLWidget(parent)
{

}

HelloTriangle::~HelloTriangle()
{
    qDebug()<<"~HelloTriangle";
    m_vao->release();
    m_vbo->release();
    m_program->release();  //解绑
}

void HelloTriangle::initializeGL()
{
    initializeOpenGLFunctions();
    glEnable(GL_DEPTH_TEST);

    //QOpenGLShaderProgram
    m_program = new QOpenGLShaderProgram(this);
    //vertex shader 顶点着色器
    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    //fragment shader 片段着色器
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    m_program->link();
    m_program->bind();//激活Program对象

    m_vao = new QOpenGLVertexArrayObject();
    m_vao->create();
    m_vao->bind();



    //画三角形
    m_vbo = new QOpenGLBuffer(QOpenGLBuffer::Type::VertexBuffer);
    m_vbo->create();
    m_vbo->bind();
    m_vbo->allocate(vertices, sizeof(vertices));
    m_vbo->setUsagePattern(QOpenGLBuffer::StreamDraw);
    //以下是两种实现方式
    //方式1
     //void setAttributeBuffer (int location, GLenum type, int offset, int tupleSize, int stride = 0);
    m_program->setAttributeBuffer(0, GL_FLOAT, 0,  3, 0);   //设置顶点属性
    m_program->enableAttributeArray(0); //使能顶点属性
    //方式1
    //方式2
//    //void setAttributeBuffer  (const char *name, GLenum type, int offset, int tupleSize, int stride = 0);
//    GLint aPos = m_program->attributeLocation("aPos");        //获取aPos位置
//    if(aPos==-1)
//    {
//        return;
//    }
//    m_program->setAttributeBuffer(aPos, GL_FLOAT, 0,  3, 0);   //设置顶点属性
//    m_program->enableAttributeArray(aPos); //使能顶点属性
    //方式2
    //画三角形

    //画矩形
//    m_vbo = new QOpenGLBuffer(QOpenGLBuffer::Type::VertexBuffer);
//    m_vbo->create();
//    m_vbo->bind();
//    m_vbo->allocate(vertices_triangle, sizeof(vertices_triangle));
//    m_vbo->setUsagePattern(QOpenGLBuffer::StreamDraw);

//    m_ebo = new QOpenGLBuffer(QOpenGLBuffer::Type::IndexBuffer);
//    m_ebo->create();
//    m_ebo->bind();
//    m_ebo->allocate(indices,sizeof(indices));
//    m_ebo->setUsagePattern(QOpenGLBuffer::StaticDraw);

    //方式1
//    //void setAttributeBuffer (int location, GLenum type, int offset, int tupleSize, int stride = 0);
//    m_program->setAttributeBuffer(0, GL_FLOAT, 0,  3, 0);   //设置顶点属性
//    m_program->enableAttributeArray(0); //使能顶点属性
    //方式1
    //方式2
//    GLint aPos = m_program->attributeLocation("aPos");        //获取aPos位置
//    if(aPos==-1)
//    {
//        return;
//    }
//    m_program->setAttributeBuffer(aPos, GL_FLOAT, 0,  3, 0);   //设置顶点属性
//    m_program->enableAttributeArray(aPos); //使能顶点属性
    //方式2
//     m_ebo->release();
    //画矩形

    m_vao->release();
    m_vbo->release();


}

void HelloTriangle::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    // 渲染Shader
    m_program->bind(); //绑定激活Program对象
    m_vao->bind();      //绑定激活vao
    //画三角形
    glDrawArrays(GL_TRIANGLES, 0, 3);    //绘制3个定点,样式为三角形
    //画三角形
    //画矩形
//    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    //画矩形
    m_vao->release();       //解绑
}

void HelloTriangle::resizeGL(int w, int h)
{

}

 

你可能感兴趣的:(opengl,qt,opengl,c++)