2023-05-22 10:35:30 +00:00
|
|
|
#include "glad/glad.h"
|
2023-05-23 07:03:04 +00:00
|
|
|
#include "shader.h"
|
2023-05-23 07:20:42 +00:00
|
|
|
#include "stb_image.h"
|
2023-05-22 10:35:30 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2023-05-23 07:03:04 +00:00
|
|
|
#include <cmath>
|
2023-05-24 07:51:39 +00:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2023-05-24 08:39:49 +00:00
|
|
|
#include <iostream>
|
2023-05-22 10:35:30 +00:00
|
|
|
|
2023-05-22 17:21:52 +00:00
|
|
|
float vertices[] = {
|
2023-05-24 09:24:34 +00:00
|
|
|
-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};
|
2023-05-22 17:21:52 +00:00
|
|
|
|
2023-05-24 09:17:49 +00:00
|
|
|
glm::vec3 cubePositions[] = {
|
2023-05-24 09:24:34 +00:00
|
|
|
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)};
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
|
|
void processInput(GLFWwindow *window);
|
|
|
|
|
|
|
|
// settings
|
2023-05-24 07:51:39 +00:00
|
|
|
const unsigned int SCR_WIDTH = 600;
|
2023-05-22 10:35:30 +00:00
|
|
|
const unsigned int SCR_HEIGHT = 600;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
// 设置 glfw
|
|
|
|
glfwInit();
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
|
|
|
|
// 创建窗口
|
2023-05-24 08:39:49 +00:00
|
|
|
GLFWwindow *window =
|
|
|
|
glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
2023-05-22 10:35:30 +00:00
|
|
|
if (window == NULL) {
|
|
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
|
|
glfwTerminate();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
// 加载glad
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置视口
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
|
|
|
|
// 创建着色器程序
|
2023-05-23 07:20:42 +00:00
|
|
|
Shader shader = Shader("src/tex.vs", "src/tex.fs");
|
|
|
|
|
2023-05-23 07:03:04 +00:00
|
|
|
unsigned int VAO, VBO, EBO;
|
2023-05-22 10:35:30 +00:00
|
|
|
// 创建顶点数组对象
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
|
|
|
|
// 创建顶点缓冲对象
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
2023-05-23 07:20:42 +00:00
|
|
|
// 顶点属性
|
2023-05-24 09:17:49 +00:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
2023-05-22 10:35:30 +00:00
|
|
|
glEnableVertexAttribArray(0);
|
2023-05-24 09:17:49 +00:00
|
|
|
|
|
|
|
// 纹理坐标属性
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
2023-05-23 07:20:42 +00:00
|
|
|
(void *)(3 * sizeof(float)));
|
|
|
|
glEnableVertexAttribArray(1);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
2023-05-24 07:16:30 +00:00
|
|
|
// 创建纹理
|
|
|
|
unsigned int texture1, texture2;
|
|
|
|
glGenTextures(1, &texture1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
// 设置纹理环绕方式
|
|
|
|
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_MIPMAP_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// 加载并生成纹理
|
|
|
|
int width, height, nrChannels;
|
|
|
|
// 图片翻转y轴
|
|
|
|
stbi_set_flip_vertically_on_load(true);
|
|
|
|
unsigned char *data =
|
|
|
|
stbi_load("resources/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);
|
|
|
|
|
|
|
|
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_MIPMAP_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// 加载并生成纹理
|
|
|
|
// 图片翻转y轴
|
|
|
|
stbi_set_flip_vertically_on_load(true);
|
|
|
|
data =
|
|
|
|
stbi_load("resources/awesomeface.png", &width, &height, &nrChannels, 0);
|
|
|
|
if (data) {
|
|
|
|
|
|
|
|
// note that the awesomeface.png has transparency and thus an alpha channel,
|
|
|
|
// so make sure to tell OpenGL the data type is of GL_RGBA
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 激活着色器
|
|
|
|
shader.use();
|
|
|
|
// 绑定纹理
|
|
|
|
shader.setInt("texture1", 0);
|
|
|
|
shader.setInt("texture2", 1);
|
2023-05-24 08:39:49 +00:00
|
|
|
|
|
|
|
// create transformations
|
|
|
|
glm::mat4 view = glm::mat4(1.0f);
|
|
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
|
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
|
|
|
projection = glm::perspective(
|
|
|
|
glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
|
|
|
shader.setMat4("view", view);
|
|
|
|
shader.setMat4("projection", projection);
|
2023-05-24 07:16:30 +00:00
|
|
|
|
2023-05-24 09:17:49 +00:00
|
|
|
// 开启深度测试
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
2023-05-22 10:35:30 +00:00
|
|
|
// 循环渲染
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
// 处理输入
|
|
|
|
processInput(window);
|
|
|
|
|
|
|
|
// 设置为白色
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
2023-05-24 09:24:34 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
2023-05-23 07:20:42 +00:00
|
|
|
// 绑定纹理
|
2023-05-24 07:16:30 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
2023-05-23 07:03:04 +00:00
|
|
|
|
|
|
|
// 绘制三角形
|
2023-05-24 07:16:30 +00:00
|
|
|
shader.use();
|
2023-05-24 07:51:39 +00:00
|
|
|
|
2023-05-24 08:39:49 +00:00
|
|
|
// 设置变换矩阵
|
2023-05-24 07:51:39 +00:00
|
|
|
glm::mat4 trans(1.0f);
|
2023-05-24 09:24:34 +00:00
|
|
|
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, 0.0));
|
2023-05-24 08:39:49 +00:00
|
|
|
shader.setMat4("trans", trans);
|
2023-05-22 10:35:30 +00:00
|
|
|
glBindVertexArray(VAO);
|
2023-05-24 09:24:34 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 10; i++) {
|
|
|
|
glm::mat4 model(1.0f);
|
|
|
|
model = glm::translate(model, cubePositions[i]);
|
|
|
|
float angle = 20.0f * i;
|
|
|
|
model =
|
|
|
|
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
|
|
shader.setMat4("model", model);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
}
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
// 检查并调用事件,交换缓冲
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 释放资源
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理输入
|
|
|
|
void processInput(GLFWwindow *window) {
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 窗口大小改变时回调该函数
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|