#include "myshader.hh" #include "stb_image_wrap.h" #include #include #include #include #include #include #include #include #include #include #include "mesh.hh" #include "camera.hh" constexpr unsigned int WINDOW_WIDTH = 800; constexpr unsigned int WINDOW_HEIGHT = 600; float mix_nb = 0.2f; glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); float deltaTime = 0.0f; // 当前帧与上一帧的时间差 float lastFrame = 0.0f; // 上一帧的时间 float lastX = 400.0f; float lastY = 300.0f; float yaw = -90.0f; float pitch = 0.0f; float fov = 45.0f; bool firstMouse = true; Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); void mouse_callback(GLFWwindow* window, double xpos, double ypos) { if (firstMouse) { lastX = xpos; lastY = ypos; firstMouse = false; } float xoffset = xpos - lastX; float yoffset = lastY - ypos; lastX = xpos; lastY = ypos; camera.process_mouse_movement(xoffset, yoffset); // float sensitivity = 0.05f; // xoffset *= sensitivity; // yoffset *= sensitivity; // // pitch += yoffset; // yaw += xoffset; // // if (pitch > 89.0f) { // pitch = 89.0f; // } // if (pitch < -89.0f) { // pitch = -89.0f; // } // // glm::vec3 front; // front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); // front.y = sin(glm::radians(pitch)); // front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); // cameraFront = glm::normalize(front); } void scroll_callback(GLFWwindow* window, double xOffset, double yOffset) { // if (fov >= 1.0f && fov <= 45.0f) // fov -= yOffset; // if (fov <= 1.0f) // fov = 1.0f; // if (fov >= 45.0f) // fov = 45.0f; // std::cout << "FOV: " << fov << std::endl; camera.process_mouse_scroll(yOffset); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } void process_input(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { mix_nb -= 0.001; if (mix_nb <= 0.0f) { mix_nb = 0.0f; } } if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { mix_nb += 0.001f; if (mix_nb >= 1.0f) { mix_nb = 1.0f; } } if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { camera.process_keyboard(FORWARD, deltaTime); } if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { camera.process_keyboard(BACKWARD, deltaTime); } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { camera.process_keyboard(LEFT, deltaTime); } if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { camera.process_keyboard(RIGHT, deltaTime); } } int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #if __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetScrollCallback(window, scroll_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } stbi_set_flip_vertically_on_load(true); glEnable(GL_DEPTH_TEST); Shader ourShader("vshader.glsl", "fshader.glsl"); Model ourModel("./backpack.obj"); while (!glfwWindowShouldClose(window)) { float currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; process_input(window); glClearColor(0.05f, 0.05f, 0.05f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ourShader.use(); // 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)); // glm::mat4 view = glm::mat4(1.0f); // view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); glm::mat4 projection = glm::mat4(1.0f); projection = glm::perspective(glm::radians(camera.zoom_), // 视野角 (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f); // 近裁剪平面,远裁剪平面 glm::mat4 view = camera.get_view_matrix(); unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model"); unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view"); // glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); ourShader.setMat4("projection", projection); ourShader.setMat4("view", view); glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.05f)); model = glm::scale(model, glm::vec3(1.0f, 1.0f, -1.0f)); ourShader.setMat4("model", model); ourModel.Draw(ourShader); // ourShader.setFloat("mix_nb", mix_nb); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }