diff --git a/LearnOpenGL/src/Main/4高级OpenGL/4.3.2混合-窗户-正确.cpp b/LearnOpenGL/src/Main/4高级OpenGL/4.3.2混合-窗户-正确.cpp index 0f5d6f2..01a4133 100644 --- a/LearnOpenGL/src/Main/4高级OpenGL/4.3.2混合-窗户-正确.cpp +++ b/LearnOpenGL/src/Main/4高级OpenGL/4.3.2混合-窗户-正确.cpp @@ -83,7 +83,6 @@ int main() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - // build and compile shaders // ------------------------- Shader shader("assest/shader/4߼OpenGL/3.1.-.vs", "assest/shader/4߼OpenGL/3.1.-.fs"); diff --git a/LearnOpenGL/src/Main/4高级OpenGL/4.3.3.面剔除.cpp b/LearnOpenGL/src/Main/4高级OpenGL/4.3.3.面剔除.cpp new file mode 100644 index 0000000..6469472 --- /dev/null +++ b/LearnOpenGL/src/Main/4高级OpenGL/4.3.3.面剔除.cpp @@ -0,0 +1,344 @@ +#include +#include +#include + +#include +#include +#include + +#include "Core/Shader/Shader.h" +#include "Core/Camera/Camera.h" +#include "Core/LoadModel/Model.h" + +#include +#include "MyFileSystem.h" + +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void mouse_callback(GLFWwindow* window, double xpos, double ypos); +void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); +void processInput(GLFWwindow* window); +unsigned int loadTexture(const char* path); + +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + +// camera +Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); +float lastX = (float)SCR_WIDTH / 2.0; +float lastY = (float)SCR_HEIGHT / 2.0; +bool firstMouse = true; + +// timing +float deltaTime = 0.0f; +float lastFrame = 0.0f; + +int main() +{ + // glfw: initialize and configure + // ------------------------------ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + // glfw window creation + // -------------------- + 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); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetCursorPosCallback(window, mouse_callback); + glfwSetScrollCallback(window, scroll_callback); + + // tell GLFW to capture our mouse + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + // glad: load all OpenGL function pointers + // --------------------------------------- + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } + + // configure global opengl state + // ----------------------------- + glEnable(GL_DEPTH_TEST); + //glDepthFunc(GL_ALWAYS); // always pass the depth test (same effect as glDisable(GL_DEPTH_TEST)) + glDepthFunc(GL_LESS); + + + + // 1.ȷIJü + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + // 2. ޳ǰ + //glEnable(GL_CULL_FACE); + //glCullFace(GL_BACK); + //glFrontFace(GL_CW);// ˳ʱ + + //glEnable(GL_CULL_FACE); + //glCullFace(GL_FRONT); + + // build and compile shaders + // ------------------------- + Shader shader("assest/shader/4߼OpenGL/1.1.depth_testing.vs", "assest/shader/4߼OpenGL/1.1.depth_testing.fs"); + + // set up vertex data (and buffer(s)) and configure vertex attributes + // ------------------------------------------------------------------ + float cubeVertices[] = { // ȷʱ붨 + // Back face棬ӽǿĻ˳ʱ˳Ⱦģʵʱ붨 + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left + // Front face棬ӽǿĻʱȾģʵҲʱ붨 + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // top-left + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left + // Left face + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-left + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right + // Right face + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left + // Bottom face + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right + // Top face + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f // bottom-left + }; + float planeVertices[] = { + // positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat) + // ʱ + 5.0f, -0.5f, 5.0f, 2.0f, 0.0f,// + 5.0f, -0.5f, -5.0f, 2.0f, 2.0f,// + -5.0f, -0.5f, -5.0f, 0.0f, 2.0f,// + + -5.0f, -0.5f, -5.0f, 0.0f, 2.0f,// + -5.0f, -0.5f, 5.0f, 0.0f, 0.0f,// + 5.0f, -0.5f, 5.0f, 2.0f, 0.0f,// + }; + // cube VAO + unsigned int cubeVAO, cubeVBO; + glGenVertexArrays(1, &cubeVAO); + glGenBuffers(1, &cubeVBO); + glBindVertexArray(cubeVAO); + glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glBindVertexArray(0); + // plane VAO + unsigned int planeVAO, planeVBO; + glGenVertexArrays(1, &planeVAO); + glGenBuffers(1, &planeVBO); + glBindVertexArray(planeVAO); + glBindBuffer(GL_ARRAY_BUFFER, planeVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glBindVertexArray(0); + + // load textures + // ------------- + unsigned int cubeTexture = loadTexture(FileSystem::getPath("assest/textures/container2.png").c_str()); + unsigned int floorTexture = loadTexture(FileSystem::getPath("assest/textures/wall.jpg").c_str()); + + // shader configuration + // -------------------- + shader.use(); + shader.setInt("texture1", 0); + + // render loop + // ----------- + while (!glfwWindowShouldClose(window)) + { + // per-frame time logic + // -------------------- + float currentFrame = static_cast(glfwGetTime()); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + + // input + // ----- + processInput(window); + + // render + // ------ + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + shader.use(); + glm::mat4 model = glm::mat4(1.0f); + 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.setMat4("view", view); + shader.setMat4("projection", projection); + // cubes + glBindVertexArray(cubeVAO); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, cubeTexture); + model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f)); + shader.setMat4("model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + model = glm::mat4(1.0f); + model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f)); + shader.setMat4("model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + // floor + glBindVertexArray(planeVAO); + glBindTexture(GL_TEXTURE_2D, floorTexture); + model = glm::mat4(1.0f); + shader.setMat4("model", model); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); + + // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) + // ------------------------------------------------------------------------------- + glfwSwapBuffers(window); + glfwPollEvents(); + } + + // optional: de-allocate all resources once they've outlived their purpose: + // ------------------------------------------------------------------------ + glDeleteVertexArrays(1, &cubeVAO); + glDeleteVertexArrays(1, &planeVAO); + glDeleteBuffers(1, &cubeVBO); + glDeleteBuffers(1, &planeVBO); + + glfwTerminate(); + return 0; +} + +// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly +// --------------------------------------------------------------------------------------------------------- +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); +} + +// glfw: whenever the window size changed (by OS or user resize) this callback function executes +// --------------------------------------------------------------------------------------------- +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + // make sure the viewport matches the new window dimensions; note that width and + // height will be significantly larger than specified on retina displays. + glViewport(0, 0, width, height); +} + +// glfw: whenever the mouse moves, this callback is called +// ------------------------------------------------------- +void mouse_callback(GLFWwindow* window, double xposIn, double yposIn) +{ + float xpos = static_cast(xposIn); + float ypos = static_cast(yposIn); + + if (firstMouse) + { + lastX = xpos; + lastY = ypos; + firstMouse = false; + } + + float xoffset = xpos - lastX; + float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top + + lastX = xpos; + lastY = ypos; + + camera.ProcessMouseMovement(xoffset, yoffset); +} + +// glfw: whenever the mouse scroll wheel scrolls, this callback is called +// ---------------------------------------------------------------------- +void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) +{ + camera.ProcessMouseScroll(static_cast(yoffset)); +} + +// utility function for loading a 2D texture from file +// --------------------------------------------------- +unsigned int loadTexture(char const* path) +{ + unsigned int textureID; + glGenTextures(1, &textureID); + + int width, height, nrComponents; + unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0); + if (data) + { + GLenum format; + if (nrComponents == 1) + format = GL_RED; + else if (nrComponents == 3) + format = GL_RGB; + else if (nrComponents == 4) + format = GL_RGBA; + + glBindTexture(GL_TEXTURE_2D, textureID); + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + + 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); + + stbi_image_free(data); + } + else + { + std::cout << "Texture failed to load at path: " << path << std::endl; + stbi_image_free(data); + } + + return textureID; +} \ No newline at end of file diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.ilk b/LearnOpenGL/x64/Debug/LearnOpenGL.ilk index ac9ca0e..853117a 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.ilk and b/LearnOpenGL/x64/Debug/LearnOpenGL.ilk differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.command.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.command.1.tlog index 217d5b6..9c6783b 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.command.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.command.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.read.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.read.1.tlog index 9a58ac7..80f6695 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.read.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.read.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.write.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.write.1.tlog index 27806c6..cab0c64 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.write.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/CL.write.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.command.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.command.1.tlog index 048bff8..f5de65d 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.command.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.command.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.read.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.read.1.tlog index 25c80f3..1fa0e96 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.read.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.read.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.write.1.tlog b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.write.1.tlog index e6cc264..e5349cc 100644 Binary files a/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.write.1.tlog and b/LearnOpenGL/x64/Debug/LearnOpenGL.tlog/link.write.1.tlog differ diff --git a/LearnOpenGL/x64/Debug/vc142.idb b/LearnOpenGL/x64/Debug/vc142.idb index 0919719..4122fd9 100644 Binary files a/LearnOpenGL/x64/Debug/vc142.idb and b/LearnOpenGL/x64/Debug/vc142.idb differ diff --git a/LearnOpenGL/x64/Debug/vc142.pdb b/LearnOpenGL/x64/Debug/vc142.pdb index 4875f51..3cbe62a 100644 Binary files a/LearnOpenGL/x64/Debug/vc142.pdb and b/LearnOpenGL/x64/Debug/vc142.pdb differ diff --git a/x64/Debug/LearnOpenGL.pdb b/x64/Debug/LearnOpenGL.pdb index 3a665e0..02c57e8 100644 Binary files a/x64/Debug/LearnOpenGL.pdb and b/x64/Debug/LearnOpenGL.pdb differ