学习learnopengl文章对应地址:https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/
此篇有个需要注意的地方就是 qt 不需要对角度转化,如下代码所示。
// qt
model.rotate(-55.0f, 1.0f, 0.0f, 0.0f);
projection.perspective(45.0f, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
// qt
// VS
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
// VS
Visual Studio c++ 文件和 qt 项目链接(在一个包内):https://download.csdn.net/download/zzjzmdx/16757600
VS、qt版本分别给了多个文件和三个项目,都在同一个包内。
展示图:
Visual Studio 2019
代码如下:
CoordinateSystems-01.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
float vertices[] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 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(SCR_WIDTH, SCR_HEIGHT, "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, 5 * 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(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(1);
//创建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("./6.1.coordinate_systems.vs", "./6.1.coordinate_systems.fs");
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);
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));// glm::value_ptr(model) 传一个矩阵返回一个数组 相当于下一行
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, &model[0][0]);// glm::value_ptr(model) 传一个矩阵返回一个数组
//ourShader.setMat4("model", model);//等于上两行
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
//unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
ourShader.setMat4("view", view);//等于上两行
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
//unsigned int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
//glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, &projection[0][0]);
ourShader.setMat4("projection", projection);//等于上两行
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);
}
CoordinateSystems-02.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
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(SCR_WIDTH, SCR_HEIGHT, "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
//用来开启更新深度缓冲区的功能,也就是,如果通过比较后深度值发生变化了,会进行更新深度缓冲区的操作。
//启动它,OpenGL就可以跟踪再Z轴上的像素,这样,它只会再那个像素前方没有东西时,才会绘画这个像素。
//在做绘画3D时,这个功能最好启动,视觉效果比较真实。
//glClear(GL_COLOR_BUFFER_BIT)--> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//每次渲染迭代之前清除深度缓冲(否则前一帧的深度信息仍然保存在缓冲中)↑
glEnable(GL_DEPTH_TEST);
//创建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, 5 * 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(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(1);
//创建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("./6.1.coordinate_systems.vs", "./6.1.coordinate_systems.fs");
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);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glm::mat4 model = glm::mat4(1.0f);
//model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
//旋转
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));
//unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
ourShader.setMat4("model", model);//等于上两行
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
//unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
ourShader.setMat4("view", view);//等于上两行
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
//unsigned int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
//glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, &projection[0][0]);
ourShader.setMat4("projection", projection);//等于上两行
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);//画正方形
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);
}
CoordinateSystems-03.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
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(SCR_WIDTH, SCR_HEIGHT, "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
//用来开启更新深度缓冲区的功能,也就是,如果通过比较后深度值发生变化了,会进行更新深度缓冲区的操作。
//启动它,OpenGL就可以跟踪再Z轴上的像素,这样,它只会再那个像素前方没有东西时,才会绘画这个像素。
//在做绘画3D时,这个功能最好启动,视觉效果比较真实。
//glClear(GL_COLOR_BUFFER_BIT)--> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//每次渲染迭代之前清除深度缓冲(否则前一帧的深度信息仍然保存在缓冲中)↑
glEnable(GL_DEPTH_TEST);
//创建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, 5 * 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(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
//允许顶点着色器读取GPU(服务器端)数据
glEnableVertexAttribArray(1);
//创建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("./6.1.coordinate_systems.vs", "./6.1.coordinate_systems.fs");
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);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
//glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
旋转
//model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
//ourShader.setMat4("model", model);//等于上两行
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
//unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
ourShader.setMat4("view", view);//等于上两行
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
//unsigned int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
//glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, &projection[0][0]);
ourShader.setMat4("projection", projection);//等于上两行
glBindVertexArray(VAO);
for (unsigned int i = 0; i < 10; i++)
{
// calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
//if (i%2==0) {//双数旋转
// model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));
//}
float angle = 20.0f * i;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
ourShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
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
代码如下:
01
CoordinateSystems.cpp
#include "CoordinateSystems.h"
float vertices[] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
CoordinateSystems::CoordinateSystems(QWidget *parent)
: QOpenGLWidget(parent)
{
}
CoordinateSystems::~CoordinateSystems()
{
m_vao->release();
m_vbo->release();
m_ebo->release();
m_program->release(); //解绑
}
void CoordinateSystems::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");
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aPos, GL_FLOAT, 0, 3, 5*sizeof(GLfloat));
m_program->enableAttributeArray(aPos);
GLint aTexCoord = m_program->attributeLocation("aTexCoord");
if(aTexCoord==-1)
{
return;
}
m_program->setAttributeBuffer(aTexCoord, GL_FLOAT, 3*sizeof(GLfloat), 2, 5*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 CoordinateSystems::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
// 渲染Shader
m_program->bind(); //绑定激活Program对象
glActiveTexture(GL_TEXTURE0);
texture1->bind();
glActiveTexture(GL_TEXTURE1);
texture2->bind();
QMatrix4x4 model;
model.rotate(-55.0f, 1.0f, 0.0f, 0.0f);
m_program->setUniformValue("model", model);
QMatrix4x4 view;
view.translate(0.0f, 0.0f, -3.0f);
m_program->setUniformValue("view", view);
QMatrix4x4 projection;
projection.perspective(45.0f, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
m_program->setUniformValue("projection", projection);
//qDebug()<<qDegreesToRadians(45.0f);
m_vao->bind(); //绑定激活vao
//画矩形
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//画矩形
m_vao->release(); //解绑
update();
}
void CoordinateSystems::resizeGL(int w, int h)
{
}
02
CoordinateSystems.cpp
#include "CoordinateSystems.h"
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
CoordinateSystems::CoordinateSystems(QWidget *parent)
: QOpenGLWidget(parent)
{
}
CoordinateSystems::~CoordinateSystems()
{
m_vao->release();
m_vbo->release();
m_ebo->release();
m_program->release(); //解绑
}
void CoordinateSystems::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");
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aPos, GL_FLOAT, 0, 3, 5*sizeof(GLfloat));
m_program->enableAttributeArray(aPos);
GLint aTexCoord = m_program->attributeLocation("aTexCoord");
if(aTexCoord==-1)
{
return;
}
m_program->setAttributeBuffer(aTexCoord, GL_FLOAT, 3*sizeof(GLfloat), 2, 5*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();
m_time = QTime::currentTime();
}
void CoordinateSystems::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
// 渲染Shader
m_program->bind(); //绑定激活Program对象
glActiveTexture(GL_TEXTURE0);
texture1->bind();
glActiveTexture(GL_TEXTURE1);
texture2->bind();
float timeValue = m_time.msecsTo(QTime::currentTime())/20.0f;
QMatrix4x4 model;
model.rotate(timeValue, 0.5f, 1.0f, 0.0f);
m_program->setUniformValue("model", model);
QMatrix4x4 view;
view.translate(0.0f, 0.0f, -3.0f);
m_program->setUniformValue("view", view);
QMatrix4x4 projection;
projection.perspective(45.0f, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
m_program->setUniformValue("projection", projection);
//qDebug()<<qDegreesToRadians(45.0f);
m_vao->bind(); //绑定激活vao
//画矩形
glDrawArrays(GL_TRIANGLES, 0, 36);//画正方形
//画矩形
m_vao->release(); //解绑
update();
}
void CoordinateSystems::resizeGL(int w, int h)
{
}
03
CoordinateSystems.cpp
#include "CoordinateSystems.h"
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
QVector3D cubePositions[] = {
QVector3D(0.0f, 0.0f, 0.0f),
QVector3D(2.0f, 5.0f, -15.0f),
QVector3D(-1.5f, -2.2f, -2.5f),
QVector3D(-3.8f, -2.0f, -12.3f),
QVector3D(2.4f, -0.4f, -3.5f),
QVector3D(-1.7f, 3.0f, -7.5f),
QVector3D(1.3f, -2.0f, -2.5f),
QVector3D(1.5f, 2.0f, -2.5f),
QVector3D(1.5f, 0.2f, -1.5f),
QVector3D(-1.3f, 1.0f, -1.5f)
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
CoordinateSystems::CoordinateSystems(QWidget *parent)
: QOpenGLWidget(parent)
{
}
CoordinateSystems::~CoordinateSystems()
{
m_vao->release();
m_vbo->release();
m_ebo->release();
m_program->release(); //解绑
}
void CoordinateSystems::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");
if(aPos==-1)
{
return;
}
m_program->setAttributeBuffer(aPos, GL_FLOAT, 0, 3, 5*sizeof(GLfloat));
m_program->enableAttributeArray(aPos);
GLint aTexCoord = m_program->attributeLocation("aTexCoord");
if(aTexCoord==-1)
{
return;
}
m_program->setAttributeBuffer(aTexCoord, GL_FLOAT, 3*sizeof(GLfloat), 2, 5*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();
m_time = QTime::currentTime();
}
void CoordinateSystems::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
// 渲染Shader
m_program->bind(); //绑定激活Program对象
glActiveTexture(GL_TEXTURE0);
texture1->bind();
glActiveTexture(GL_TEXTURE1);
texture2->bind();
QMatrix4x4 view;
view.translate(0.0f, 0.0f, -3.0f);
m_program->setUniformValue("view", view);
QMatrix4x4 projection;
projection.perspective(45.0f, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
m_program->setUniformValue("projection", projection);
//qDebug()<<qDegreesToRadians(45.0f);
m_vao->bind(); //绑定激活vao
//画矩形
for (unsigned int i = 0; i < 10; i++)
{
QMatrix4x4 model;
model.translate(cubePositions[i]);
// if (i%2==0) {//双数旋转
// float timeValue = m_time.msecsTo(QTime::currentTime())/20.0f;
// model.rotate(timeValue, 0.5f, 1.0f, 0.0f);
// }
float angle = 20.0f * i;
model.rotate(angle, 0.5f, 1.0f, 0.0f);
m_program->setUniformValue("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
m_vao->release(); //解绑
update();
}
void CoordinateSystems::resizeGL(int w, int h)
{
}