From a3bf2ee860ca1cfc4bb999706476034450227ef7 Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Fri, 22 May 2020 18:01:27 +0200 Subject: [PATCH] Add first demo to repo (was still linking to old demo code). --- CMakeLists.txt | 3 +- .../11.1.anti_aliasing.fs} | 0 .../11.1.anti_aliasing.vs} | 0 .../anti_aliasing_msaa.cpp | 228 ++++++++++++++++++ .../11.2.aa_post.fs} | 0 .../11.2.aa_post.vs} | 0 .../11.2.anti_aliasing.fs | 7 + .../11.2.anti_aliasing.vs | 11 + .../anti_aliasing_offscreen.cpp | 4 +- 9 files changed, 250 insertions(+), 3 deletions(-) rename src/4.advanced_opengl/{11.anti_aliasing_offscreen/11.anti_aliasing.fs => 11.1.anti_aliasing_msaa/11.1.anti_aliasing.fs} (100%) rename src/4.advanced_opengl/{11.anti_aliasing_offscreen/11.anti_aliasing.vs => 11.1.anti_aliasing_msaa/11.1.anti_aliasing.vs} (100%) create mode 100644 src/4.advanced_opengl/11.1.anti_aliasing_msaa/anti_aliasing_msaa.cpp rename src/4.advanced_opengl/{11.anti_aliasing_offscreen/11.aa_post.fs => 11.2.anti_aliasing_offscreen/11.2.aa_post.fs} (100%) rename src/4.advanced_opengl/{11.anti_aliasing_offscreen/11.aa_post.vs => 11.2.anti_aliasing_offscreen/11.2.aa_post.vs} (100%) create mode 100644 src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.fs create mode 100644 src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.vs rename src/4.advanced_opengl/{11.anti_aliasing_offscreen => 11.2.anti_aliasing_offscreen}/anti_aliasing_offscreen.cpp (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1439ff..be0c5f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,7 +128,8 @@ set(4.advanced_opengl 10.1.instancing_quads 10.2.asteroids 10.3.asteroids_instanced - 11.anti_aliasing_offscreen + 11.1.anti_aliasing_msaa + 11.2.anti_aliasing_offscreen ) set(5.advanced_lighting diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.fs b/src/4.advanced_opengl/11.1.anti_aliasing_msaa/11.1.anti_aliasing.fs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.fs rename to src/4.advanced_opengl/11.1.anti_aliasing_msaa/11.1.anti_aliasing.fs diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.vs b/src/4.advanced_opengl/11.1.anti_aliasing_msaa/11.1.anti_aliasing.vs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.vs rename to src/4.advanced_opengl/11.1.anti_aliasing_msaa/11.1.anti_aliasing.vs diff --git a/src/4.advanced_opengl/11.1.anti_aliasing_msaa/anti_aliasing_msaa.cpp b/src/4.advanced_opengl/11.1.anti_aliasing_msaa/anti_aliasing_msaa.cpp new file mode 100644 index 0000000..8243bfe --- /dev/null +++ b/src/4.advanced_opengl/11.1.anti_aliasing_msaa/anti_aliasing_msaa.cpp @@ -0,0 +1,228 @@ +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +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); + +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + +// 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); + glEnable(GL_MULTISAMPLE); // enabled by default on some drivers, but not all so always enable to make sure + + // build and compile shaders + // ------------------------- + Shader shader("11.1.anti_aliasing.vs", "11.1.anti_aliasing.fs"); + + // set up vertex data (and buffer(s)) and configure vertex attributes + // ------------------------------------------------------------------ + float cubeVertices[] = { + // positions + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, -0.5f, + + -0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f + }; + // setup 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, 3 * sizeof(float), (void*)0); + + // render loop + // ----------- + while (!glfwWindowShouldClose(window)) + { + // per-frame time logic + // -------------------- + float currentFrame = 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); + + // set transformation matrices + shader.use(); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f); + shader.setMat4("projection", projection); + shader.setMat4("view", camera.GetViewMatrix()); + shader.setMat4("model", glm::mat4(1.0f)); + + glBindVertexArray(cubeVAO); + glDrawArrays(GL_TRIANGLES, 0, 36); + + // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) + // ------------------------------------------------------------------------------- + glfwSwapBuffers(window); + glfwPollEvents(); + } + + 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 xpos, double ypos) +{ + 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(yoffset); +} diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.fs b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.aa_post.fs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.fs rename to src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.aa_post.fs diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.vs b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.aa_post.vs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.vs rename to src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.aa_post.vs diff --git a/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.fs b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.fs new file mode 100644 index 0000000..fce61e0 --- /dev/null +++ b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.fs @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(0.0, 1.0, 0.0, 1.0); +} \ No newline at end of file diff --git a/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.vs b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.vs new file mode 100644 index 0000000..cc518f1 --- /dev/null +++ b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/11.2.anti_aliasing.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp similarity index 98% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp rename to src/4.advanced_opengl/11.2.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp index 935c07c..bcaccaa 100644 --- a/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp +++ b/src/4.advanced_opengl/11.2.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp @@ -76,8 +76,8 @@ int main() // build and compile shaders // ------------------------- - Shader shader("11.anti_aliasing.vs", "11.anti_aliasing.fs"); - Shader screenShader("11.aa_post.vs", "11.aa_post.fs"); + Shader shader("11.2.anti_aliasing.vs", "11.2.anti_aliasing.fs"); + Shader screenShader("11.2.aa_post.vs", "11.2.aa_post.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------