From c6385183b36e519864c3c64983066fa669089315 Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei Date: Mon, 4 Apr 2022 01:44:24 +0200 Subject: [PATCH 01/10] compute shader --- .../computeShader.comp | 28 +++ .../computeShader.h | 49 +++++ .../computer_shader_hello_world.cpp | 196 ++++++++++++++++++ .../5.computeshader_helloworld/screenQuad.fs | 12 ++ .../5.computeshader_helloworld/screenQuad.vs | 11 + 5 files changed, 296 insertions(+) create mode 100644 src/8.guest/2022/5.computeshader_helloworld/computeShader.comp create mode 100644 src/8.guest/2022/5.computeshader_helloworld/computeShader.h create mode 100644 src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp create mode 100644 src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs create mode 100644 src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp new file mode 100644 index 0000000..f614624 --- /dev/null +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp @@ -0,0 +1,28 @@ +#version 430 core + +layout (local_size_x = 10, local_size_y = 10, local_size_z = 1) in; + +// ---------------------------------------------------------------------------- +// +// uniforms +// +// ---------------------------------------------------------------------------- + +layout(rgba32f, binding = 0) uniform image2D imgOutput; + +layout (location = 0) uniform float t; /**< Time */ + +// ---------------------------------------------------------------------------- +// +// functions +// +// ---------------------------------------------------------------------------- + +void main() { + vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0); + ivec2 pixelCoord = ivec2(gl_GlobalInvocationID.xy); + float speed = 0.5; + pixel.x = float(int((float(pixelCoord.x)/(gl_NumWorkGroups.x*gl_WorkGroupSize.x)+t*speed)*100)%100)/100; + pixel.y = float(pixelCoord.y)/(gl_NumWorkGroups.y*gl_WorkGroupSize.y); + imageStore(imgOutput, pixelCoord, pixel); +} \ No newline at end of file diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.h b/src/8.guest/2022/5.computeshader_helloworld/computeShader.h new file mode 100644 index 0000000..ebe5c85 --- /dev/null +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.h @@ -0,0 +1,49 @@ +#include + + +class ComputeShader : public Shader +{ + public: + ComputeShader(const char* computePath) + { + // 1. retrieve the vertex/fragment source code from filePath + std::string computeCode; + std::ifstream cShaderFile; + // ensure ifstream objects can throw exceptions: + cShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try + { + // open files + cShaderFile.open(computePath); + + std::stringstream cShaderStream; + // read file's buffer contents into streams + cShaderStream << cShaderFile.rdbuf(); + // close file handlers + cShaderFile.close(); + // convert stream into string + computeCode = cShaderStream.str(); + } + catch (std::ifstream::failure& e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; + } + const char* cShaderCode = computeCode.c_str(); + // 2. compile shaders + unsigned int compute; + // compute shader + compute = glCreateShader(GL_COMPUTE_SHADER); + glShaderSource(compute, 1, &cShaderCode, NULL); + glCompileShader(compute); + checkCompileErrors(compute, "COMPUTE"); + + // shader Program + ID = glCreateProgram(); + glAttachShader(ID, compute); + glLinkProgram(ID); + checkCompileErrors(ID, "PROGRAM"); + // delete the shaders as they're linked into our program now and no longer necessery + glDeleteShader(compute); + + } +}; \ No newline at end of file diff --git a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp new file mode 100644 index 0000000..369f5df --- /dev/null +++ b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp @@ -0,0 +1,196 @@ +#include +#include + +#include +#include +#include + +#include +#include + +#include + +#include "computeShader.h" + +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void renderQuad(); + +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + +// texture size +const unsigned int TEXTURE_WIDTH = 1000, TEXTURE_HEIGHT = 1000; + +// timing +float deltaTime = 0.0f; // time between current frame and last frame +float lastFrame = 0.0f; // time of last frame + +int main(int argc, char* argv[]) +{ + // glfw: initialize and configure + // ------------------------------ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + 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); + glfwSwapInterval(0); + + // glad: load all OpenGL function pointers + // --------------------------------------- + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } + + // query limitations + // ----------------- + int max_compute_work_group_count[3]; + int max_compute_work_group_size[3]; + int max_compute_work_group_invocations; + + for (int idx = 0; idx < 3; idx++) { + glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, idx, &max_compute_work_group_count[idx]); + glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, idx, &max_compute_work_group_size[idx]); + } + glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &max_compute_work_group_invocations); + + std::cout << "OpenGL Limitations: " << std::endl; + std::cout << "maxmimum number of work groups in X dimension " << max_compute_work_group_count[0] << std::endl; + std::cout << "maxmimum number of work groups in Y dimension " << max_compute_work_group_count[1] << std::endl; + std::cout << "maxmimum number of work groups in Z dimension " << max_compute_work_group_count[2] << std::endl; + + std::cout << "maxmimum size of a work group in X dimension " << max_compute_work_group_size[0] << std::endl; + std::cout << "maxmimum size of a work group in Y dimension " << max_compute_work_group_size[1] << std::endl; + std::cout << "maxmimum size of a work group in Z dimension " << max_compute_work_group_size[2] << std::endl; + + std::cout << "Number of invocations in a single local work group that may be dispatched to a compute shader " << max_compute_work_group_invocations << std::endl; + + // build and compile shaders + // ------------------------- + Shader screenQuad("screenQuad.vs", "screenQuad.fs"); + ComputeShader computeShader("computeShader.comp"); + + screenQuad.use(); + screenQuad.setInt("tex", 0); + + // Create texture for opengl operation + // ----------------------------------- + GLuint texture; + + glGenTextures(1, &texture); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL); + + glBindImageTexture(0, texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); + + // render loop + // ----------- + int fCounter = 0; + while (!glfwWindowShouldClose(window)) + { + // Set frame time + GLfloat currentFrame = glfwGetTime(); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + if(fCounter > 500) { + std::cout << "FPS: " << 1 / deltaTime << std::endl; + fCounter = 0; + } else { + fCounter++; + } + + computeShader.use(); + computeShader.setFloat("t", currentFrame); + glDispatchCompute((GLuint)TEXTURE_WIDTH/10, (GLuint)TEXTURE_HEIGHT/10, 1); + + // make sure writing to image has finished before read + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + + // render image to quad + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + screenQuad.use(); + + renderQuad(); + + // 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: + // ------------------------------------------------------------------------ + glDeleteTextures(1, &texture); + glDeleteProgram(screenQuad.ID); + glDeleteProgram(computeShader.ID); + + glfwTerminate(); + + return EXIT_SUCCESS; +} + +// renderQuad() renders a 1x1 XY quad in NDC +// ----------------------------------------- +unsigned int quadVAO = 0; +unsigned int quadVBO; +void renderQuad() +{ + if (quadVAO == 0) + { + float quadVertices[] = { + // positions // texture Coords + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + }; + // setup plane VAO + glGenVertexArrays(1, &quadVAO); + glGenBuffers(1, &quadVBO); + glBindVertexArray(quadVAO); + glBindBuffer(GL_ARRAY_BUFFER, quadVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, 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(quadVAO); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindVertexArray(0); +} + +// 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); +} \ No newline at end of file diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs new file mode 100644 index 0000000..bcfde96 --- /dev/null +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs @@ -0,0 +1,12 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D tex; + +void main() +{ + vec3 texCol = texture(tex, TexCoords).rgb; + FragColor = vec4(texCol, 1.0); +} \ No newline at end of file diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs new file mode 100644 index 0000000..9f93e29 --- /dev/null +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoords; + +out vec2 TexCoords; + +void main() +{ + TexCoords = aTexCoords; + gl_Position = vec4(aPos, 1.0); +} \ No newline at end of file From 801be81baa9d34da2238119bf4dfd700ba317b30 Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei Date: Mon, 4 Apr 2022 01:48:17 +0200 Subject: [PATCH 02/10] end of file --- CMakeLists.txt | 3 + includes/learnopengl/shader_m.h | 6 +- .../computeShader.comp | 2 +- .../computeShader.h | 2 +- .../computer_shader_hello_world.cpp | 2 +- .../5.computeshader_helloworld/screenQuad.fs | 2 +- .../5.computeshader_helloworld/screenQuad.vs | 2 +- src/8.guest/2022/composite.fs | 51 +++ src/8.guest/2022/composite.vs | 9 + src/8.guest/2022/weighted_blended.cpp | 382 ++++++++++++++++++ 10 files changed, 455 insertions(+), 6 deletions(-) create mode 100644 src/8.guest/2022/composite.fs create mode 100644 src/8.guest/2022/composite.vs create mode 100644 src/8.guest/2022/weighted_blended.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 38aea7e..194824f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,7 @@ set(GUEST_ARTICLES #8.guest/2021/3.tessellation/terrain_gpu_dist #8.guest/2021/3.tessellation/terrain_cpu_src 8.guest/2021/4.dsa + 8.guest/2022/5.computeshader_helloworld ) configure_file(configuration/root_directory.h.in configuration/root_directory.h) @@ -205,6 +206,7 @@ function(create_project_from_sources chapter demo) "src/${chapter}/${demo}/*.vs" "src/${chapter}/${demo}/*.fs" "src/${chapter}/${demo}/*.gs" + "src/${chapter}/${demo}/*.comp" ) if (demo STREQUAL "") SET(replaced "") @@ -235,6 +237,7 @@ function(create_project_from_sources chapter demo) # "src/${chapter}/${demo}/*.frag" "src/${chapter}/${demo}/*.fs" "src/${chapter}/${demo}/*.gs" + "src/${chapter}/${demo}/*.comp" ) foreach(SHADER ${SHADERS}) if(WIN32) diff --git a/includes/learnopengl/shader_m.h b/includes/learnopengl/shader_m.h index b554064..b8c96e0 100644 --- a/includes/learnopengl/shader_m.h +++ b/includes/learnopengl/shader_m.h @@ -13,6 +13,10 @@ class Shader { public: unsigned int ID; + Shader() { + ID = -1; + } + // constructor generates the shader on the fly // ------------------------------------------------------------------------ Shader(const char* vertexPath, const char* fragmentPath) @@ -135,7 +139,7 @@ public: glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); } -private: +protected: // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ void checkCompileErrors(GLuint shader, std::string type) diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp index f614624..c7f11d6 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp @@ -25,4 +25,4 @@ void main() { pixel.x = float(int((float(pixelCoord.x)/(gl_NumWorkGroups.x*gl_WorkGroupSize.x)+t*speed)*100)%100)/100; pixel.y = float(pixelCoord.y)/(gl_NumWorkGroups.y*gl_WorkGroupSize.y); imageStore(imgOutput, pixelCoord, pixel); -} \ No newline at end of file +} diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.h b/src/8.guest/2022/5.computeshader_helloworld/computeShader.h index ebe5c85..d1a4ecb 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computeShader.h +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.h @@ -46,4 +46,4 @@ class ComputeShader : public Shader glDeleteShader(compute); } -}; \ No newline at end of file +}; diff --git a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp index 369f5df..44aa433 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp +++ b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp @@ -193,4 +193,4 @@ 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); -} \ No newline at end of file +} diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs index bcfde96..736e174 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs @@ -9,4 +9,4 @@ void main() { vec3 texCol = texture(tex, TexCoords).rgb; FragColor = vec4(texCol, 1.0); -} \ No newline at end of file +} diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs index 9f93e29..38324be 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs @@ -8,4 +8,4 @@ void main() { TexCoords = aTexCoords; gl_Position = vec4(aPos, 1.0); -} \ No newline at end of file +} diff --git a/src/8.guest/2022/composite.fs b/src/8.guest/2022/composite.fs new file mode 100644 index 0000000..ab1c21f --- /dev/null +++ b/src/8.guest/2022/composite.fs @@ -0,0 +1,51 @@ +#version 400 core + +// shader outputs +layout (location = 0) out vec4 frag; + +// color accumulation buffer +layout (binding = 0) uniform sampler2D accum; + +// revealage threshold buffer +layout (binding = 1) uniform sampler2D reveal; + +// epsilon number +const float EPSILON = 0.00001f; + +// caluclate floating point numbers equality accurately +bool isApproximatelyEqual(float a, float b) +{ + return abs(a - b) <= (abs(a) < abs(b) ? abs(b) : abs(a)) * EPSILON; +} + +// get the max value between three values +float max3(vec3 v) +{ + return max(max(v.x, v.y), v.z); +} + +void main() +{ + // fragment coordination + ivec2 coords = ivec2(gl_FragCoord.xy); + + // fragment revealage + float revealage = texelFetch(reveal, coords, 0).r; + + // save the blending and color texture fetch cost if there is not a transparent fragment + if (isApproximatelyEqual(revealage, 1.0f)) + discard; + + // fragment color + vec4 accumulation = texelFetch(accum, coords, 0); + + // suppress overflow + if (isinf(max3(abs(accumulation.rgb)))) + accumulation.rgb = vec3(accumulation.a); + + // prevent floating point precision bug + vec3 average_color = accumulation.rgb / max(accumulation.a, EPSILON); + + // blend pixels + frag = vec4(average_color, 1.0f - revealage); +} \ No newline at end of file diff --git a/src/8.guest/2022/composite.vs b/src/8.guest/2022/composite.vs new file mode 100644 index 0000000..87fa99c --- /dev/null +++ b/src/8.guest/2022/composite.vs @@ -0,0 +1,9 @@ +#version 400 core + +// shader inputs +layout (location = 0) in vec3 position; + +void main() +{ + gl_Position = vec4(position, 1.0f); +} \ No newline at end of file diff --git a/src/8.guest/2022/weighted_blended.cpp b/src/8.guest/2022/weighted_blended.cpp new file mode 100644 index 0000000..b382f37 --- /dev/null +++ b/src/8.guest/2022/weighted_blended.cpp @@ -0,0 +1,382 @@ +#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 process_input(GLFWwindow *window); +glm::mat4 calculate_model_matrix(const glm::vec3& position, const glm::vec3& rotation = glm::vec3(0.0f), const glm::vec3& scale = glm::vec3(1.0f)); + +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + +// camera +Camera camera(glm::vec3(0.0f, 0.0f, 5.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(int argc, char* argv[]) +{ + // glfw: initialize and configure + // ------------------------------ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + 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; + } + + // build and compile shaders + // ------------------------- + Shader solidShader("solid.vs", "solid.fs"); + Shader transparentShader("transparent.vs", "transparent.fs"); + Shader compositeShader("composite.vs", "composite.fs"); + Shader screenShader("screen.vs", "screen.fs"); + + // set up vertex data (and buffer(s)) and configure vertex attributes + // ------------------------------------------------------------------ + float quadVertices[] = { + // positions // uv + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f + }; + + // quad VAO + unsigned int quadVAO, quadVBO; + glGenVertexArrays(1, &quadVAO); + glGenBuffers(1, &quadVBO); + glBindVertexArray(quadVAO); + glBindBuffer(GL_ARRAY_BUFFER, quadVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, 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); + + // set up framebuffers and their texture attachments + // ------------------------------------------------------------------ + unsigned int opaqueFBO, transparentFBO; + glGenFramebuffers(1, &opaqueFBO); + glGenFramebuffers(1, &transparentFBO); + + // set up attachments for opaque framebuffer + unsigned int opaqueTexture; + glGenTextures(1, &opaqueTexture); + glBindTexture(GL_TEXTURE_2D, opaqueTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_HALF_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + unsigned int depthTexture; + glGenTextures(1, &depthTexture); + glBindTexture(GL_TEXTURE_2D, depthTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + + glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, opaqueTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + std::cout << "ERROR::FRAMEBUFFER:: Opaque framebuffer is not complete!" << std::endl; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + // set up attachments for transparent framebuffer + unsigned int accumTexture; + glGenTextures(1, &accumTexture); + glBindTexture(GL_TEXTURE_2D, accumTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_HALF_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + unsigned int revealTexture; + glGenTextures(1, &revealTexture); + glBindTexture(GL_TEXTURE_2D, revealTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, SCR_WIDTH, SCR_HEIGHT, 0, GL_RED, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + glBindFramebuffer(GL_FRAMEBUFFER, transparentFBO); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, accumTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, revealTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); // opaque framebuffer's depth texture + + const GLenum transparentDrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; + glDrawBuffers(2, transparentDrawBuffers); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + std::cout << "ERROR::FRAMEBUFFER:: Transparent framebuffer is not complete!" << std::endl; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + // set up transformation matrices + // ------------------------------------------------------------------ + glm::mat4 redModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 1.0f)); + glm::mat4 greenModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 0.0f)); + glm::mat4 blueModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 2.0f)); + + // set up intermediate variables + // ------------------------------------------------------------------ + glm::vec4 zeroFillerVec(0.0f); + glm::vec4 oneFillerVec(1.0f); + + // render loop + // ----------- + while (!glfwWindowShouldClose(window)) + { + // per-frame time logic + // -------------------- + float currentFrame = glfwGetTime(); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + + // camera matrices + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); + glm::mat4 view = camera.GetViewMatrix(); + glm::mat4 vp = projection * view; + + // input + // ----- + process_input(window); + + // render + // ------ + + // draw solid objects (solid pass) + // ------ + + // configure render states + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glDepthMask(GL_TRUE); + glDisable(GL_BLEND); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + // bind opaque framebuffer to render solid objects + glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // use solid shader + solidShader.use(); + + // draw red quad + solidShader.setMat4("mvp", vp * redModelMat); + solidShader.setVec3("color", glm::vec3(1.0f, 0.0f, 0.0f)); + glBindVertexArray(quadVAO); + glDrawArrays(GL_TRIANGLES, 0, 6); + + // draw transparent objects (transparent pass) + // ----- + + // configure render states + glDepthMask(GL_FALSE); + glEnable(GL_BLEND); + glBlendFunci(0, GL_ONE, GL_ONE); + glBlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_COLOR); + glBlendEquation(GL_FUNC_ADD); + + // bind transparent framebuffer to render transparent objects + glBindFramebuffer(GL_FRAMEBUFFER, transparentFBO); + glClearBufferfv(GL_COLOR, 0, &zeroFillerVec[0]); + glClearBufferfv(GL_COLOR, 1, &oneFillerVec[0]); + + // use transparent shader + transparentShader.use(); + + // draw green quad + transparentShader.setMat4("mvp", vp * greenModelMat); + transparentShader.setVec4("color", glm::vec4(0.0f, 1.0f, 0.0f, 0.5f)); + glBindVertexArray(quadVAO); + glDrawArrays(GL_TRIANGLES, 0, 6); + + // draw blue quad + transparentShader.setMat4("mvp", vp * blueModelMat); + transparentShader.setVec4("color", glm::vec4(0.0f, 0.0f, 1.0f, 0.5f)); + glBindVertexArray(quadVAO); + glDrawArrays(GL_TRIANGLES, 0, 6); + + // draw composite image (composite pass) + // ----- + + // set render states + glDepthFunc(GL_ALWAYS); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // bind opaque framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); + + // use composite shader + compositeShader.use(); + + // draw screen quad + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, accumTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, revealTexture); + glBindVertexArray(quadVAO); + glDrawArrays(GL_TRIANGLES, 0, 6); + + // draw to backbuffer (final pass) + // ----- + + // set render states + glDisable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); // enable depth writes so glClear won't ignore clearing the depth buffer + glDisable(GL_BLEND); + + // bind backbuffer + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + // use screen shader + screenShader.use(); + + // draw final screen quad + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, opaqueTexture); + glBindVertexArray(quadVAO); + glDrawArrays(GL_TRIANGLES, 0, 6); + + // 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, &quadVAO); + glDeleteBuffers(1, &quadVBO); + glDeleteTextures(1, &opaqueTexture); + glDeleteTextures(1, &depthTexture); + glDeleteTextures(1, &accumTexture); + glDeleteTextures(1, &revealTexture); + glDeleteFramebuffers(1, &opaqueFBO); + glDeleteFramebuffers(1, &transparentFBO); + + glfwTerminate(); + + return EXIT_SUCCESS; +} + +// 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); +} + +// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly +// --------------------------------------------------------------------------------------------------------- +void process_input(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); +} + +// generate a model matrix +// --------------------------------------------------------------------------------------------------------- +glm::mat4 calculate_model_matrix(const glm::vec3& position, const glm::vec3& rotation, const glm::vec3& scale) +{ + glm::mat4 trans = glm::mat4(1.0f); + + trans = glm::translate(trans, position); + trans = glm::rotate(trans, glm::radians(rotation.x), glm::vec3(1.0, 0.0, 0.0)); + trans = glm::rotate(trans, glm::radians(rotation.y), glm::vec3(0.0, 1.0, 0.0)); + trans = glm::rotate(trans, glm::radians(rotation.z), glm::vec3(0.0, 0.0, 1.0)); + trans = glm::scale(trans, scale); + + return trans; +} From 5e51f03c208a3d73ae35f0c2fc8833eb36a6ad60 Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei <34287983+jonassorgenfrei@users.noreply.github.com> Date: Mon, 4 Apr 2022 01:53:08 +0200 Subject: [PATCH 03/10] Delete composite.fs --- src/8.guest/2022/composite.fs | 51 ----------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 src/8.guest/2022/composite.fs diff --git a/src/8.guest/2022/composite.fs b/src/8.guest/2022/composite.fs deleted file mode 100644 index ab1c21f..0000000 --- a/src/8.guest/2022/composite.fs +++ /dev/null @@ -1,51 +0,0 @@ -#version 400 core - -// shader outputs -layout (location = 0) out vec4 frag; - -// color accumulation buffer -layout (binding = 0) uniform sampler2D accum; - -// revealage threshold buffer -layout (binding = 1) uniform sampler2D reveal; - -// epsilon number -const float EPSILON = 0.00001f; - -// caluclate floating point numbers equality accurately -bool isApproximatelyEqual(float a, float b) -{ - return abs(a - b) <= (abs(a) < abs(b) ? abs(b) : abs(a)) * EPSILON; -} - -// get the max value between three values -float max3(vec3 v) -{ - return max(max(v.x, v.y), v.z); -} - -void main() -{ - // fragment coordination - ivec2 coords = ivec2(gl_FragCoord.xy); - - // fragment revealage - float revealage = texelFetch(reveal, coords, 0).r; - - // save the blending and color texture fetch cost if there is not a transparent fragment - if (isApproximatelyEqual(revealage, 1.0f)) - discard; - - // fragment color - vec4 accumulation = texelFetch(accum, coords, 0); - - // suppress overflow - if (isinf(max3(abs(accumulation.rgb)))) - accumulation.rgb = vec3(accumulation.a); - - // prevent floating point precision bug - vec3 average_color = accumulation.rgb / max(accumulation.a, EPSILON); - - // blend pixels - frag = vec4(average_color, 1.0f - revealage); -} \ No newline at end of file From ace261e60ffbb81cd1b832e86fd35b0bd05e95d7 Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei <34287983+jonassorgenfrei@users.noreply.github.com> Date: Mon, 4 Apr 2022 01:53:15 +0200 Subject: [PATCH 04/10] Delete composite.vs --- src/8.guest/2022/composite.vs | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/8.guest/2022/composite.vs diff --git a/src/8.guest/2022/composite.vs b/src/8.guest/2022/composite.vs deleted file mode 100644 index 87fa99c..0000000 --- a/src/8.guest/2022/composite.vs +++ /dev/null @@ -1,9 +0,0 @@ -#version 400 core - -// shader inputs -layout (location = 0) in vec3 position; - -void main() -{ - gl_Position = vec4(position, 1.0f); -} \ No newline at end of file From 1751f341ecae0c09a9ec128645bdcc20c7e28e0f Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei <34287983+jonassorgenfrei@users.noreply.github.com> Date: Mon, 4 Apr 2022 01:53:34 +0200 Subject: [PATCH 05/10] Delete weighted_blended.cpp --- src/8.guest/2022/weighted_blended.cpp | 382 -------------------------- 1 file changed, 382 deletions(-) delete mode 100644 src/8.guest/2022/weighted_blended.cpp diff --git a/src/8.guest/2022/weighted_blended.cpp b/src/8.guest/2022/weighted_blended.cpp deleted file mode 100644 index b382f37..0000000 --- a/src/8.guest/2022/weighted_blended.cpp +++ /dev/null @@ -1,382 +0,0 @@ -#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 process_input(GLFWwindow *window); -glm::mat4 calculate_model_matrix(const glm::vec3& position, const glm::vec3& rotation = glm::vec3(0.0f), const glm::vec3& scale = glm::vec3(1.0f)); - -// settings -const unsigned int SCR_WIDTH = 800; -const unsigned int SCR_HEIGHT = 600; - -// camera -Camera camera(glm::vec3(0.0f, 0.0f, 5.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(int argc, char* argv[]) -{ - // glfw: initialize and configure - // ------------------------------ - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - 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; - } - - // build and compile shaders - // ------------------------- - Shader solidShader("solid.vs", "solid.fs"); - Shader transparentShader("transparent.vs", "transparent.fs"); - Shader compositeShader("composite.vs", "composite.fs"); - Shader screenShader("screen.vs", "screen.fs"); - - // set up vertex data (and buffer(s)) and configure vertex attributes - // ------------------------------------------------------------------ - float quadVertices[] = { - // positions // uv - -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, - 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, - - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, - -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 0.0f, 0.0f, 0.0f - }; - - // quad VAO - unsigned int quadVAO, quadVBO; - glGenVertexArrays(1, &quadVAO); - glGenBuffers(1, &quadVBO); - glBindVertexArray(quadVAO); - glBindBuffer(GL_ARRAY_BUFFER, quadVBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, 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); - - // set up framebuffers and their texture attachments - // ------------------------------------------------------------------ - unsigned int opaqueFBO, transparentFBO; - glGenFramebuffers(1, &opaqueFBO); - glGenFramebuffers(1, &transparentFBO); - - // set up attachments for opaque framebuffer - unsigned int opaqueTexture; - glGenTextures(1, &opaqueTexture); - glBindTexture(GL_TEXTURE_2D, opaqueTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_HALF_FLOAT, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, 0); - - unsigned int depthTexture; - glGenTextures(1, &depthTexture); - glBindTexture(GL_TEXTURE_2D, depthTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); - glBindTexture(GL_TEXTURE_2D, 0); - - glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, opaqueTexture, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - std::cout << "ERROR::FRAMEBUFFER:: Opaque framebuffer is not complete!" << std::endl; - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - // set up attachments for transparent framebuffer - unsigned int accumTexture; - glGenTextures(1, &accumTexture); - glBindTexture(GL_TEXTURE_2D, accumTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_HALF_FLOAT, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, 0); - - unsigned int revealTexture; - glGenTextures(1, &revealTexture); - glBindTexture(GL_TEXTURE_2D, revealTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, SCR_WIDTH, SCR_HEIGHT, 0, GL_RED, GL_FLOAT, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, 0); - - glBindFramebuffer(GL_FRAMEBUFFER, transparentFBO); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, accumTexture, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, revealTexture, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); // opaque framebuffer's depth texture - - const GLenum transparentDrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; - glDrawBuffers(2, transparentDrawBuffers); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - std::cout << "ERROR::FRAMEBUFFER:: Transparent framebuffer is not complete!" << std::endl; - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - // set up transformation matrices - // ------------------------------------------------------------------ - glm::mat4 redModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 1.0f)); - glm::mat4 greenModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 0.0f)); - glm::mat4 blueModelMat = calculate_model_matrix(glm::vec3(0.0f, 0.0f, 2.0f)); - - // set up intermediate variables - // ------------------------------------------------------------------ - glm::vec4 zeroFillerVec(0.0f); - glm::vec4 oneFillerVec(1.0f); - - // render loop - // ----------- - while (!glfwWindowShouldClose(window)) - { - // per-frame time logic - // -------------------- - float currentFrame = glfwGetTime(); - deltaTime = currentFrame - lastFrame; - lastFrame = currentFrame; - - // camera matrices - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); - glm::mat4 view = camera.GetViewMatrix(); - glm::mat4 vp = projection * view; - - // input - // ----- - process_input(window); - - // render - // ------ - - // draw solid objects (solid pass) - // ------ - - // configure render states - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); - glDepthMask(GL_TRUE); - glDisable(GL_BLEND); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - - // bind opaque framebuffer to render solid objects - glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // use solid shader - solidShader.use(); - - // draw red quad - solidShader.setMat4("mvp", vp * redModelMat); - solidShader.setVec3("color", glm::vec3(1.0f, 0.0f, 0.0f)); - glBindVertexArray(quadVAO); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // draw transparent objects (transparent pass) - // ----- - - // configure render states - glDepthMask(GL_FALSE); - glEnable(GL_BLEND); - glBlendFunci(0, GL_ONE, GL_ONE); - glBlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_COLOR); - glBlendEquation(GL_FUNC_ADD); - - // bind transparent framebuffer to render transparent objects - glBindFramebuffer(GL_FRAMEBUFFER, transparentFBO); - glClearBufferfv(GL_COLOR, 0, &zeroFillerVec[0]); - glClearBufferfv(GL_COLOR, 1, &oneFillerVec[0]); - - // use transparent shader - transparentShader.use(); - - // draw green quad - transparentShader.setMat4("mvp", vp * greenModelMat); - transparentShader.setVec4("color", glm::vec4(0.0f, 1.0f, 0.0f, 0.5f)); - glBindVertexArray(quadVAO); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // draw blue quad - transparentShader.setMat4("mvp", vp * blueModelMat); - transparentShader.setVec4("color", glm::vec4(0.0f, 0.0f, 1.0f, 0.5f)); - glBindVertexArray(quadVAO); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // draw composite image (composite pass) - // ----- - - // set render states - glDepthFunc(GL_ALWAYS); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - // bind opaque framebuffer - glBindFramebuffer(GL_FRAMEBUFFER, opaqueFBO); - - // use composite shader - compositeShader.use(); - - // draw screen quad - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, accumTexture); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, revealTexture); - glBindVertexArray(quadVAO); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // draw to backbuffer (final pass) - // ----- - - // set render states - glDisable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); // enable depth writes so glClear won't ignore clearing the depth buffer - glDisable(GL_BLEND); - - // bind backbuffer - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - - // use screen shader - screenShader.use(); - - // draw final screen quad - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, opaqueTexture); - glBindVertexArray(quadVAO); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // 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, &quadVAO); - glDeleteBuffers(1, &quadVBO); - glDeleteTextures(1, &opaqueTexture); - glDeleteTextures(1, &depthTexture); - glDeleteTextures(1, &accumTexture); - glDeleteTextures(1, &revealTexture); - glDeleteFramebuffers(1, &opaqueFBO); - glDeleteFramebuffers(1, &transparentFBO); - - glfwTerminate(); - - return EXIT_SUCCESS; -} - -// 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); -} - -// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly -// --------------------------------------------------------------------------------------------------------- -void process_input(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); -} - -// generate a model matrix -// --------------------------------------------------------------------------------------------------------- -glm::mat4 calculate_model_matrix(const glm::vec3& position, const glm::vec3& rotation, const glm::vec3& scale) -{ - glm::mat4 trans = glm::mat4(1.0f); - - trans = glm::translate(trans, position); - trans = glm::rotate(trans, glm::radians(rotation.x), glm::vec3(1.0, 0.0, 0.0)); - trans = glm::rotate(trans, glm::radians(rotation.y), glm::vec3(0.0, 1.0, 0.0)); - trans = glm::rotate(trans, glm::radians(rotation.z), glm::vec3(0.0, 0.0, 1.0)); - trans = glm::scale(trans, scale); - - return trans; -} From 36e0cc9ec33fe4e7966724861b46702e6a140f69 Mon Sep 17 00:00:00 2001 From: "Jonas_sorgenfrei@yahoo.de" Date: Mon, 4 Apr 2022 14:34:41 +0200 Subject: [PATCH 06/10] create seperate shader --- includes/learnopengl/shader_c.h | 151 ++++++++++++++++++ includes/learnopengl/shader_m.h | 5 +- .../computeShader.h | 49 ------ .../computer_shader_hello_world.cpp | 4 +- 4 files changed, 154 insertions(+), 55 deletions(-) create mode 100644 includes/learnopengl/shader_c.h delete mode 100644 src/8.guest/2022/5.computeshader_helloworld/computeShader.h diff --git a/includes/learnopengl/shader_c.h b/includes/learnopengl/shader_c.h new file mode 100644 index 0000000..5415548 --- /dev/null +++ b/includes/learnopengl/shader_c.h @@ -0,0 +1,151 @@ +#ifndef COMPUTE_SHADER_H +#define COMPUTE_SHADER_H + +#include +#include + +#include +#include +#include +#include + +class ComputeShader +{ +public: + unsigned int ID; + // constructor generates the shader on the fly + // ------------------------------------------------------------------------ + ComputeShader(const char* computePath) + { + // 1. retrieve the vertex/fragment source code from filePath + std::string computeCode; + std::ifstream cShaderFile; + // ensure ifstream objects can throw exceptions: + cShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try + { + // open files + cShaderFile.open(computePath); + + std::stringstream cShaderStream; + // read file's buffer contents into streams + cShaderStream << cShaderFile.rdbuf(); + // close file handlers + cShaderFile.close(); + // convert stream into string + computeCode = cShaderStream.str(); + } + catch (std::ifstream::failure& e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; + } + const char* cShaderCode = computeCode.c_str(); + // 2. compile shaders + unsigned int compute; + // compute shader + compute = glCreateShader(GL_COMPUTE_SHADER); + glShaderSource(compute, 1, &cShaderCode, NULL); + glCompileShader(compute); + checkCompileErrors(compute, "COMPUTE"); + + // shader Program + ID = glCreateProgram(); + glAttachShader(ID, compute); + glLinkProgram(ID); + checkCompileErrors(ID, "PROGRAM"); + // delete the shaders as they're linked into our program now and no longer necessery + glDeleteShader(compute); + } + // activate the shader + // ------------------------------------------------------------------------ + void use() + { + glUseProgram(ID); + } + // utility uniform functions + // ------------------------------------------------------------------------ + void setBool(const std::string &name, bool value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); + } + // ------------------------------------------------------------------------ + void setInt(const std::string &name, int value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + } + // ------------------------------------------------------------------------ + void setFloat(const std::string &name, float value) const + { + glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + } + // ------------------------------------------------------------------------ + void setVec2(const std::string &name, const glm::vec2 &value) const + { + glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec2(const std::string &name, float x, float y) const + { + glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y); + } + // ------------------------------------------------------------------------ + void setVec3(const std::string &name, const glm::vec3 &value) const + { + glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec3(const std::string &name, float x, float y, float z) const + { + glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z); + } + // ------------------------------------------------------------------------ + void setVec4(const std::string &name, const glm::vec4 &value) const + { + glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec4(const std::string &name, float x, float y, float z, float w) + { + glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w); + } + // ------------------------------------------------------------------------ + void setMat2(const std::string &name, const glm::mat2 &mat) const + { + glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + // ------------------------------------------------------------------------ + void setMat3(const std::string &name, const glm::mat3 &mat) const + { + glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + // ------------------------------------------------------------------------ + void setMat4(const std::string &name, const glm::mat4 &mat) const + { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + +private: + // utility function for checking shader compilation/linking errors. + // ------------------------------------------------------------------------ + void checkCompileErrors(GLuint shader, std::string type) + { + GLint success; + GLchar infoLog[1024]; + if(type != "PROGRAM") + { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if(!success) + { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + else + { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if(!success) + { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; + } + } + } +}; +#endif \ No newline at end of file diff --git a/includes/learnopengl/shader_m.h b/includes/learnopengl/shader_m.h index b8c96e0..7acdb55 100644 --- a/includes/learnopengl/shader_m.h +++ b/includes/learnopengl/shader_m.h @@ -13,9 +13,6 @@ class Shader { public: unsigned int ID; - Shader() { - ID = -1; - } // constructor generates the shader on the fly // ------------------------------------------------------------------------ @@ -139,7 +136,7 @@ public: glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); } -protected: +private: // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ void checkCompileErrors(GLuint shader, std::string type) diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.h b/src/8.guest/2022/5.computeshader_helloworld/computeShader.h deleted file mode 100644 index d1a4ecb..0000000 --- a/src/8.guest/2022/5.computeshader_helloworld/computeShader.h +++ /dev/null @@ -1,49 +0,0 @@ -#include - - -class ComputeShader : public Shader -{ - public: - ComputeShader(const char* computePath) - { - // 1. retrieve the vertex/fragment source code from filePath - std::string computeCode; - std::ifstream cShaderFile; - // ensure ifstream objects can throw exceptions: - cShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); - try - { - // open files - cShaderFile.open(computePath); - - std::stringstream cShaderStream; - // read file's buffer contents into streams - cShaderStream << cShaderFile.rdbuf(); - // close file handlers - cShaderFile.close(); - // convert stream into string - computeCode = cShaderStream.str(); - } - catch (std::ifstream::failure& e) - { - std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; - } - const char* cShaderCode = computeCode.c_str(); - // 2. compile shaders - unsigned int compute; - // compute shader - compute = glCreateShader(GL_COMPUTE_SHADER); - glShaderSource(compute, 1, &cShaderCode, NULL); - glCompileShader(compute); - checkCompileErrors(compute, "COMPUTE"); - - // shader Program - ID = glCreateProgram(); - glAttachShader(ID, compute); - glLinkProgram(ID); - checkCompileErrors(ID, "PROGRAM"); - // delete the shaders as they're linked into our program now and no longer necessery - glDeleteShader(compute); - - } -}; diff --git a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp index 44aa433..82abf60 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp +++ b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp @@ -5,13 +5,13 @@ #include #include + #include +#include #include #include -#include "computeShader.h" - void framebuffer_size_callback(GLFWwindow* window, int width, int height); void renderQuad(); From 25d692253fb30b6b1748309139da230272176475 Mon Sep 17 00:00:00 2001 From: "Jonas_sorgenfrei@yahoo.de" Date: Mon, 4 Apr 2022 14:36:57 +0200 Subject: [PATCH 07/10] reverted shader_m --- includes/learnopengl/shader_m.h | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/learnopengl/shader_m.h b/includes/learnopengl/shader_m.h index 7acdb55..b554064 100644 --- a/includes/learnopengl/shader_m.h +++ b/includes/learnopengl/shader_m.h @@ -13,7 +13,6 @@ class Shader { public: unsigned int ID; - // constructor generates the shader on the fly // ------------------------------------------------------------------------ Shader(const char* vertexPath, const char* fragmentPath) From b0e8a4c990c9bedfeff4a6a0d9b26ae28d6dbf3d Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei Date: Sat, 23 Apr 2022 20:19:04 +0200 Subject: [PATCH 08/10] fix version of Fragment and Vertex Shader --- src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs | 2 +- src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs index 736e174..dc6f9e3 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.fs @@ -1,4 +1,4 @@ -#version 330 core +#version 430 core out vec4 FragColor; in vec2 TexCoords; diff --git a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs index 38324be..bba53e5 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs +++ b/src/8.guest/2022/5.computeshader_helloworld/screenQuad.vs @@ -1,4 +1,4 @@ -#version 330 core +#version 430 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoords; From 2024ac1aee43e26d927b67e3d11cb39b4ee8e592 Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei Date: Sun, 24 Apr 2022 14:25:54 +0200 Subject: [PATCH 09/10] changed variable naming --- .../5.computeshader_helloworld/computeShader.comp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp index c7f11d6..a8df60b 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp @@ -19,10 +19,10 @@ layout (location = 0) uniform float t; /**< Time */ // ---------------------------------------------------------------------------- void main() { - vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0); - ivec2 pixelCoord = ivec2(gl_GlobalInvocationID.xy); - float speed = 0.5; - pixel.x = float(int((float(pixelCoord.x)/(gl_NumWorkGroups.x*gl_WorkGroupSize.x)+t*speed)*100)%100)/100; - pixel.y = float(pixelCoord.y)/(gl_NumWorkGroups.y*gl_WorkGroupSize.y); - imageStore(imgOutput, pixelCoord, pixel); -} + vec4 value = vec4(0.0, 0.0, 0.0, 1.0); + ivec2 texelCoord = ivec2(gl_GlobalInvocationID.xy); + float speed = 0.5; + value.x = float(int((float(texelCoord.x)/(gl_NumWorkGroups.x)+t*speed)*100)%100)/100; + value.y = float(texelCoord.y)/(gl_NumWorkGroups.y); + imageStore(imgOutput, texelCoord, value); +} \ No newline at end of file From f9ab4337fc94501d1d80b9c4f49a121697804b1e Mon Sep 17 00:00:00 2001 From: Jonas Sorgenfrei Date: Sun, 15 May 2022 15:56:10 +0200 Subject: [PATCH 10/10] adjusted comments from article feedback --- .../2022/5.computeshader_helloworld/computeShader.comp | 6 +++--- .../computer_shader_hello_world.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp index a8df60b..b4e3954 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp +++ b/src/8.guest/2022/5.computeshader_helloworld/computeShader.comp @@ -10,7 +10,7 @@ layout (local_size_x = 10, local_size_y = 10, local_size_z = 1) in; layout(rgba32f, binding = 0) uniform image2D imgOutput; -layout (location = 0) uniform float t; /**< Time */ +layout (location = 0) uniform float t; /** Time */ // ---------------------------------------------------------------------------- // @@ -22,7 +22,7 @@ void main() { vec4 value = vec4(0.0, 0.0, 0.0, 1.0); ivec2 texelCoord = ivec2(gl_GlobalInvocationID.xy); float speed = 0.5; - value.x = float(int((float(texelCoord.x)/(gl_NumWorkGroups.x)+t*speed)*100)%100)/100; - value.y = float(texelCoord.y)/(gl_NumWorkGroups.y); + value.x = float(int((float(texelCoord.x)/(gl_NumWorkGroups.x*gl_WorkGroupSize.x)+t*speed)*100)%100)/100; + value.y = float(texelCoord.y)/(gl_NumWorkGroups.y*gl_WorkGroupSize.y); imageStore(imgOutput, texelCoord, value); } \ No newline at end of file diff --git a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp index 82abf60..bf11d92 100644 --- a/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp +++ b/src/8.guest/2022/5.computeshader_helloworld/computer_shader_hello_world.cpp @@ -93,7 +93,7 @@ int main(int argc, char* argv[]) // Create texture for opengl operation // ----------------------------------- - GLuint texture; + unsigned int texture; glGenTextures(1, &texture); glActiveTexture(GL_TEXTURE0); @@ -115,7 +115,7 @@ int main(int argc, char* argv[]) while (!glfwWindowShouldClose(window)) { // Set frame time - GLfloat currentFrame = glfwGetTime(); + float currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; if(fCounter > 500) { @@ -127,7 +127,7 @@ int main(int argc, char* argv[]) computeShader.use(); computeShader.setFloat("t", currentFrame); - glDispatchCompute((GLuint)TEXTURE_WIDTH/10, (GLuint)TEXTURE_HEIGHT/10, 1); + glDispatchCompute((unsigned int)TEXTURE_WIDTH/10, (unsigned int)TEXTURE_HEIGHT/10, 1); // make sure writing to image has finished before read glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);