Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - 04 - Textures
作者:互联网
Visual Studio c++ 文件和 qt 项目链接(在一个包内):
由于learnopengl此章节有两个代码示例,所以VS、qt版本分别给了多个文件和两个项目,都在同一个包内。
展示图:
单材质
双材质
Visual Studio 2019
代码如下:
单材质
Texture-01.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "shader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
float vertices[] = {
// positions // colors // texture coords
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
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
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, "Texture04-01", 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
//创建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, 8 * sizeof(float), (void*)0);
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(0);
//颜色属性 告诉OpenGL该如何解析颜色数据
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(1);
//颜色属性 告诉OpenGL该如何解析材质数据
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(2);
//创建texture
unsigned int texture;
glGenTextures(1, &texture); // 用来生成缓冲区对象
glBindTexture(GL_TEXTURE_2D, texture);//绑定texture
//纹理对象设置环绕、过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Shader ourShader("./4.1.texture.vs", "./4.1.texture.fs"); // you can name your shader files however you like
int width, height, nrChannels;
unsigned char* data = stbi_load("./container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
//解绑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);
// render the triangle
ourShader.use();
// bind Texture
glBindTexture(GL_TEXTURE_2D, texture);
// render container
ourShader.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
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 <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "shader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
float vertices[] = {
// positions // colors // texture coords
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
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
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, "Texture04-01", 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
//创建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, 8 * sizeof(float), (void*)0);
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(0);
//颜色属性 告诉OpenGL该如何解析颜色数据
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(1);
//颜色属性 告诉OpenGL该如何解析材质数据
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(2);
//创建texture
unsigned int texture;
glGenTextures(1, &texture); // 用来生成缓冲区对象
glBindTexture(GL_TEXTURE_2D, texture);//绑定texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Shader ourShader("./4.2.texture.vs", "./4.2.texture.fs"); // you can name your shader files however you like
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load("./container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
unsigned int texture2;
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
data = stbi_load("./awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
ourShader.use();
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
ourShader.setInt("texture2", 1);
//解绑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);
// bind Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// render container
ourShader.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
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
代码如下:
由于qt代码较多,直贴关键代码,项目可自行下载
项目Textures-03-01
单材质
Textures.cpp
#include "Textures.h"
float vertices[] = {
// positions // colors // texture coords
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
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
Textures::Textures(QWidget *parent)
: QOpenGLWidget(parent)
{
}
Textures::~Textures()
{
m_vao->release();
m_vbo->release();
m_ebo->release();
m_program->release(); //解绑
}
void Textures::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
//QOpenGLShaderProgram
m_program = new QOpenGLShaderProgram(this);
//vertex shader 顶点着色器
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl");
//fragment shader 片段着色器
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl");
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);
m_ebo = new QOpenGLBuffer(QOpenGLBuffer::Type::IndexBuffer);
m_ebo->create();
m_ebo->bind();
m_ebo->allocate(indices,sizeof(indices));
m_ebo->setUsagePattern(QOpenGLBuffer::StaticDraw);
GLint aPos = m_program->attributeLocation("aPos"); //获取aPos位置
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aPos, GL_FLOAT, 0, 3, 8*sizeof(GLfloat)); //设置顶点属性
m_program->enableAttributeArray(aPos); //使能顶点属性
GLint aColor = m_program->attributeLocation("aColor"); //获取aPos位置
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aColor, GL_FLOAT, 3*sizeof(GLfloat), 3, 8*sizeof(GLfloat)); //设置颜色属性
m_program->enableAttributeArray(aColor); //使能顶点属性
GLint aTexCoord = m_program->attributeLocation("aTexCoord"); //获取aPos位置
if(aTexCoord==-1)
{
return;
}
m_program->setAttributeBuffer(aTexCoord, GL_FLOAT, 6*sizeof(GLfloat), 3, 8*sizeof(GLfloat)); //设置颜色属性
m_program->enableAttributeArray(aTexCoord); //使能顶点属性
//画矩形
//加载图片
texture = new QOpenGLTexture(QImage(":/container.jpg").mirrored());
//纹理对象设置环绕、过滤方式
texture->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);
texture->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);
texture->setMinificationFilter(QOpenGLTexture::Nearest);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
m_vao->release();
m_vbo->release();
m_ebo->release();
}
void Textures::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
texture->bind();
//画矩形
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//画矩形
m_vao->release(); //解绑
}
void Textures::resizeGL(int w, int h)
{
}
项目Textures-03-02
双材质
Textures.cpp
#include "Textures.h"
float vertices[] = {
// positions // colors // texture coords
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
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
Textures::Textures(QWidget *parent)
: QOpenGLWidget(parent)
{
}
Textures::~Textures()
{
m_vao->release();
m_vbo->release();
m_ebo->release();
m_program->release(); //解绑
}
void Textures::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
//QOpenGLShaderProgram
m_program = new QOpenGLShaderProgram(this);
//vertex shader 顶点着色器
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl");
//fragment shader 片段着色器
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl");
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);
m_ebo = new QOpenGLBuffer(QOpenGLBuffer::Type::IndexBuffer);
m_ebo->create();
m_ebo->bind();
m_ebo->allocate(indices,sizeof(indices));
m_ebo->setUsagePattern(QOpenGLBuffer::StaticDraw);
GLint aPos = m_program->attributeLocation("aPos"); //获取aPos位置
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aPos, GL_FLOAT, 0, 3, 8*sizeof(GLfloat)); //设置顶点属性
m_program->enableAttributeArray(aPos); //使能顶点属性
GLint aColor = m_program->attributeLocation("aColor"); //获取aPos位置
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aColor, GL_FLOAT, 3*sizeof(GLfloat), 3, 8*sizeof(GLfloat)); //设置颜色属性
m_program->enableAttributeArray(aColor); //使能顶点属性
GLint aTexCoord = m_program->attributeLocation("aTexCoord"); //获取aPos位置
if(aTexCoord==-1)
{
return;
}
m_program->setAttributeBuffer(aTexCoord, GL_FLOAT, 6*sizeof(GLfloat), 3, 8*sizeof(GLfloat)); //设置颜色属性
m_program->enableAttributeArray(aTexCoord); //使能顶点属性
//画矩形
//加载图片
texture1 = new QOpenGLTexture(QImage(":/container.jpg").mirrored());
//纹理对象设置环绕、过滤方式
texture1->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);
texture1->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);
texture1->setMinificationFilter(QOpenGLTexture::Nearest);
texture1->setMagnificationFilter(QOpenGLTexture::Linear);
//加载图片
texture2 = new QOpenGLTexture(QImage(":/awesomeface.png").mirrored());
//纹理对象设置环绕、过滤方式
texture2->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);
texture2->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);
texture2->setMinificationFilter(QOpenGLTexture::Nearest);
texture2->setMagnificationFilter(QOpenGLTexture::Linear);
m_program->setUniformValue("texture1", 0);
m_program->setUniformValue("texture2", 1);
m_vao->release();
m_vbo->release();
m_ebo->release();
}
void Textures::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
glActiveTexture(GL_TEXTURE0);
texture1->bind();
glActiveTexture(GL_TEXTURE1);
texture2->bind();
//画矩形
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//画矩形
m_vao->release(); //解绑
}
void Textures::resizeGL(int w, int h)
{
}
标签:Textures,QOpenGLTexture,1.0,qt,04,0.0,0.5,program 来源: https://blog.csdn.net/zzjzmdx/article/details/115660732