249 lines
No EOL
8.2 KiB
C++
249 lines
No EOL
8.2 KiB
C++
#include "camera.h"
|
|
#include "glad/glad.h"
|
|
#include "shader.h"
|
|
#include "stb_image.h"
|
|
#include <GLFW/glfw3.h>
|
|
#include <cmath>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <iostream>
|
|
|
|
// settings
|
|
const unsigned int SCR_WIDTH = 600;
|
|
const unsigned int SCR_HEIGHT = 600;
|
|
|
|
// 四边形顶点
|
|
float vertices[] = {
|
|
// ---- 位置 ---- -- 法向量-- - 纹理坐标 -
|
|
0.2f, 0.4f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // 右上
|
|
0.2f, -0.4f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // 右下
|
|
-0.2f, -0.4f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下
|
|
-0.2f, 0.4f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f // 左上
|
|
};
|
|
|
|
// 索引
|
|
unsigned int indices[] = {
|
|
0, 1, 3, // 第一个三角形
|
|
1, 2, 3 // 第二个三角形
|
|
};
|
|
|
|
float deltaTime, lastTime;
|
|
Camera camera = Camera(SCR_HEIGHT / 2.0f, SCR_HEIGHT / 2.0f);
|
|
|
|
// lighting
|
|
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
void processInput(GLFWwindow *window);
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
|
|
|
|
int main() {
|
|
|
|
// 设置 glfw
|
|
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, "LearnOpenGL", NULL, NULL);
|
|
if (window == NULL) {
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
glfwSetScrollCallback(window, scroll_callback);
|
|
|
|
// 加载glad
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
// 创建着色器程序
|
|
Shader shader = Shader("src/tex.vs", "src/tex.fs");
|
|
|
|
unsigned int VAO, VBO, EBO;
|
|
// 创建顶点数组对象
|
|
glGenVertexArrays(1, &VAO);
|
|
glBindVertexArray(VAO);
|
|
|
|
// 创建顶点缓冲对象
|
|
glGenBuffers(1, &VBO);
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
// 创建索引缓冲对象
|
|
glGenBuffers(1, &EBO);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
|
|
GL_STATIC_DRAW);
|
|
|
|
// 顶点属性
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
|
glEnableVertexAttribArray(0);
|
|
// 法向量属性
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
|
(void *)(3 * sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
// 纹理坐标属性
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
|
(void *)(6 * sizeof(float)));
|
|
glEnableVertexAttribArray(2);
|
|
|
|
// 创建纹理
|
|
unsigned int texture;
|
|
glGenTextures(1, &texture);
|
|
glBindTexture(GL_TEXTURE_2D, 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_MIPMAP_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
// 加载并生成纹理
|
|
// 图片翻转y轴
|
|
stbi_set_flip_vertically_on_load(true);
|
|
int width, height, nrChannels;
|
|
unsigned char *data =
|
|
stbi_load("resources/bf-half.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("tex", 0);
|
|
// // 直射光
|
|
shader.setVec3("dirLight.direction", 2.0f, 0.0f, 0.0f);
|
|
shader.setVec3("dirLight.ambient", 0.2f, 0.2f, 0.2f);
|
|
shader.setVec3("dirLight.diffuse", 0.6f, 0.6f, 0.6f);
|
|
shader.setVec3("dirLight.specular", 0.7f, 0.7f, 0.7f);
|
|
// 点光源
|
|
shader.setVec3("pointLighs.position", glm::vec3(-0.5f, -2.0f, -0.5f));
|
|
shader.setVec3("pointLight.ambient", 0.2f, 0.2f, 0.2f);
|
|
shader.setVec3("pointLight.diffuse", 3.0f, 3.0f, 3.0f);
|
|
shader.setVec3("pointLight.specular", 3.0f, 3.0f, 3.0f);
|
|
shader.setFloat("pointLight.constant", 1.0f);
|
|
shader.setFloat("pointLight.linear", 0.00014f);
|
|
shader.setFloat("pointLight.quadratic", 0.0000007f);
|
|
|
|
// 开启深度测试
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
// 循环渲染
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
// 计算帧时间差
|
|
float currentFrame = glfwGetTime();
|
|
deltaTime = currentFrame - lastTime;
|
|
lastTime = currentFrame;
|
|
|
|
// 处理输入
|
|
processInput(window);
|
|
|
|
// 初始化
|
|
glClearColor(0.3f, 0.3f, 0.3f, 0.3f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// 绑定纹理
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
glm::mat4 view = camera.GetViewMatrix();
|
|
glm::mat4 projection =
|
|
glm::perspective(glm::radians(camera.Zoom),
|
|
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
|
|
|
// 绘制三角形
|
|
shader.use();
|
|
glBindVertexArray(VAO);
|
|
|
|
shader.setVec3("viewPos", camera.Position);
|
|
shader.setMat4("view", view);
|
|
shader.setMat4("projection", projection);
|
|
|
|
// 设置材质
|
|
shader.setVec3("material.ambient", 1.0f, 0.5f, 0.31f);
|
|
shader.setVec3("material.diffuse", 1.0f, 0.5f, 0.31f);
|
|
shader.setVec3("material.specular", 0.5f, 0.5f, 0.5f);
|
|
shader.setFloat("material.shininess", 4.0f);
|
|
glm::mat4 trans(1.0f);
|
|
trans = glm::rotate(trans, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
|
|
trans = glm::rotate(trans, float(-sin((float)glfwGetTime())),
|
|
glm::vec3(0.0, 1.0, 0.0));
|
|
trans = glm::translate(trans, glm::vec3(-0.2, 0.0, 0.0));
|
|
shader.setMat4("trans", trans);
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
// 设置材质
|
|
shader.setVec3("material.ambient", 1.0f, 0.5f, 0.31f);
|
|
shader.setVec3("material.diffuse", 0.75f, 0.6f, 0.23f);
|
|
shader.setVec3("material.specular", 0.62f, 0.55f, 0.37f);
|
|
shader.setFloat("material.shininess", 64.0f);
|
|
trans = glm::mat4(1.0f);
|
|
trans = glm::rotate(trans, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
|
|
trans = glm::rotate(trans, float(sin((float)glfwGetTime())),
|
|
glm::vec3(0.0, 1.0, 0.0));
|
|
trans = glm::translate(trans, glm::vec3(0.2, 0.0, 0.0));
|
|
trans = glm::rotate(trans, glm::radians(180.0f), glm::vec3(0.0, 1.0, 0.0));
|
|
shader.setMat4("trans", trans);
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
// 检查并调用事件,交换缓冲
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// 释放资源
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
// 处理输入
|
|
void processInput(GLFWwindow *window) {
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, true);
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(UP, deltaTime);
|
|
}
|
|
|
|
// 窗口大小改变时回调该函数
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
|
camera.ProcessMouseMovement(xpos, ypos);
|
|
}
|
|
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
|
camera.ProcessMouseScroll(yoffset);
|
|
} |