From da3f2ec6857d234c0cffc37bb6252b5b888b13ba Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Wed, 10 Feb 2021 00:36:43 +0100 Subject: [PATCH 1/2] refactor out boring code --- opengl_test35.cpp | 147 +++++++++++++++++++++++----------------------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/opengl_test35.cpp b/opengl_test35.cpp index 65d8ca6..06b4c49 100644 --- a/opengl_test35.cpp +++ b/opengl_test35.cpp @@ -13,6 +13,8 @@ #include +GLFWwindow *initialize_glfw_window(void); + 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); @@ -36,57 +38,14 @@ 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(); + if ((GLFWwindow *window = initialize_glfw_window()) == nullptr) 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); + // load objects and shaders + Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag"); + Model kniedeModel("../resources/objects/kniede/kniede.obj"); - // 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); - - - // build and compile shaders - // ------------------------- - Shader nanosuitShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag"); - //Model nanosuitModel("../resources/objects/nanosuit/nanosuit.obj"); - Model nanosuitModel("../resources/objects/kniede/kniede.obj"); - - - // build and compile shaders - // ------------------------- - Shader shader("6.2.cubemaps.vert", "6.2.cubemaps.frag"); + Shader cubemapShader("6.2.cubemaps.vert", "6.2.cubemaps.frag"); Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes @@ -191,6 +150,7 @@ int main() glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + // skybox VAO unsigned int skyboxVAO, skyboxVBO; glGenVertexArrays(1, &skyboxVAO); @@ -219,8 +179,8 @@ int main() // shader configuration // -------------------- - shader.use(); - shader.setInt("skybox", 0); + cubemapShader.use(); + cubemapShader.setInt("skybox", 0); skyboxShader.use(); skyboxShader.setInt("skybox", 0); @@ -234,39 +194,34 @@ int main() 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); // draw scene as normal + 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); - glm::mat4 model = glm::mat4(1.0f); + // model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene // model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); - nanosuitShader.use(); - nanosuitShader.setMat4("projection", projection); - nanosuitShader.setMat4("view", view); - nanosuitShader.setMat4("model", model); - nanosuitModel.Draw(nanosuitShader); + kniedeShader.use(); + kniedeShader.setMat4("model", model); + kniedeShader.setMat4("view", view); + kniedeShader.setMat4("projection", projection); + kniedeModel.Draw(kniedeShader); // draw scene as normal - shader.use(); model = glm::mat4(1.0f); - view = camera.GetViewMatrix(); - projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); - shader.setMat4("model", model); - shader.setMat4("view", view); - shader.setMat4("projection", projection); - shader.setVec3("cameraPos", camera.Position); + cubemapShader.use(); + cubemapShader.setMat4("model", model); + cubemapShader.setMat4("view", view); + cubemapShader.setMat4("projection", projection); + cubemapShader.setVec3("cameraPos", camera.Position); + // cubes glBindVertexArray(cubeVAO); glActiveTexture(GL_TEXTURE0); @@ -280,6 +235,7 @@ int main() view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix skyboxShader.setMat4("view", view); skyboxShader.setMat4("projection", projection); + // skybox cube glBindVertexArray(skyboxVAO); glActiveTexture(GL_TEXTURE0); @@ -294,17 +250,58 @@ int main() glfwPollEvents(); } - // optional: de-allocate all resources once they've outlived their purpose: - // ------------------------------------------------------------------------ - glDeleteVertexArrays(1, &cubeVAO); - glDeleteVertexArrays(1, &skyboxVAO); - glDeleteBuffers(1, &cubeVBO); - glDeleteBuffers(1, &skyboxVAO); - glfwTerminate(); return 0; } +// initialize GLFW window and OpenGL context with glew +// --------------------------------------------------------------------------------------------------------- +GLFWwindow *initialize_glfw_window(void) +{ + // 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 nullptr; + } + 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 nullptr; + } + + // configure global opengl state + // ----------------------------- + glEnable(GL_DEPTH_TEST); + + return window; +} + + // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly // --------------------------------------------------------------------------------------------------------- void processInput(GLFWwindow *window) From 8bf452cc7959ee6ab7f8fe6f2ad92ac1715d18f4 Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Tue, 16 Feb 2021 19:18:33 +0100 Subject: [PATCH 2/2] up --- opengl_test35.cpp | 174 +++++++++++++++++++++++----------------------- 1 file changed, 86 insertions(+), 88 deletions(-) diff --git a/opengl_test35.cpp b/opengl_test35.cpp index 06b4c49..100c94d 100644 --- a/opengl_test35.cpp +++ b/opengl_test35.cpp @@ -1,20 +1,18 @@ #include #include -//#include +#include #define STB_IMAGE_IMPLEMENTATION // dodane do pliku z kursu #include #include #include #include -#include +#include #include #include #include -GLFWwindow *initialize_glfw_window(void); - 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); @@ -36,16 +34,56 @@ bool firstMouse = true; float deltaTime = 0.0f; float lastFrame = 0.0f; +// + +float angle = 0; + + int main() { - if ((GLFWwindow *window = initialize_glfw_window()) == nullptr) + // 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); - // load objects and shaders - Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag"); - Model kniedeModel("../resources/objects/kniede/kniede.obj"); + // tell GLFW to capture our mouse + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - Shader cubemapShader("6.2.cubemaps.vert", "6.2.cubemaps.frag"); + // 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); + + // build and compile shaders + // ------------------------- + Shader shader("6.2.cubemaps.vert", "6.2.cubemaps.frag"); Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes @@ -150,7 +188,6 @@ int main() glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); - // skybox VAO unsigned int skyboxVAO, skyboxVBO; glGenVertexArrays(1, &skyboxVAO); @@ -163,24 +200,24 @@ int main() // load textures // ------------- - string dir = "mountain-skyboxes/Ryfjallet"; + string dir = "../resources/textures/SaintsPetersBasilica/"; - vector faces + vector faces { - "../resources/textures/skybox/" + dir + "/posx.jpg", - "../resources/textures/skybox/" + dir + "/negx.jpg", - "../resources/textures/skybox/" + dir + "/posy.jpg", - "../resources/textures/skybox/" + dir + "/negy.jpg", - "../resources/textures/skybox/" + dir + "/posz.jpg", - "../resources/textures/skybox/" + dir + "/negz.jpg", + dir + "posx.jpg", + dir + "negx.jpg", + dir + "posy.jpg", + dir + "negy.jpg", + dir + "posz.jpg", + dir + "negz.jpg" }; unsigned int cubemapTexture = loadCubemap(faces); // shader configuration // -------------------- - cubemapShader.use(); - cubemapShader.setInt("skybox", 0); + shader.use(); + shader.setInt("skybox", 0); skyboxShader.use(); skyboxShader.setInt("skybox", 0); @@ -194,39 +231,42 @@ int main() float currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; + angle += deltaTime; + + // input + // ----- processInput(window); + // render + // ------ glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw scene as normal + shader.use(); glm::mat4 model = glm::mat4(1.0f); + model = glm::rotate(model, angle/10.f, glm::vec3(0, 1, 0)); glm::mat4 view = camera.GetViewMatrix(); glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); - -// model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene -// model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down - model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); - kniedeShader.use(); - kniedeShader.setMat4("model", model); - kniedeShader.setMat4("view", view); - kniedeShader.setMat4("projection", projection); - kniedeModel.Draw(kniedeShader); - - - // draw scene as normal - model = glm::mat4(1.0f); - cubemapShader.use(); - cubemapShader.setMat4("model", model); - cubemapShader.setMat4("view", view); - cubemapShader.setMat4("projection", projection); - cubemapShader.setVec3("cameraPos", camera.Position); - + shader.setMat4("model", model); + shader.setMat4("view", view); + shader.setMat4("projection", projection); + shader.setVec3("cameraPos", camera.Position); // cubes glBindVertexArray(cubeVAO); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture); glDrawArrays(GL_TRIANGLES, 0, 36); + model = glm::mat4(1.0f); + model = glm::rotate(model, angle, glm::vec3(0, 1, 0)); + model = glm::translate(model, glm::vec3(5, 0, 0)); + model = glm::rotate(model, -angle, glm::vec3(0, 1, 0)); + model = glm::translate(model, glm::vec3(0, -5, 0)); + + shader.setMat4("model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); // draw skybox as last @@ -235,7 +275,6 @@ int main() view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix skyboxShader.setMat4("view", view); skyboxShader.setMat4("projection", projection); - // skybox cube glBindVertexArray(skyboxVAO); glActiveTexture(GL_TEXTURE0); @@ -250,58 +289,17 @@ int main() glfwPollEvents(); } + // optional: de-allocate all resources once they've outlived their purpose: + // ------------------------------------------------------------------------ + glDeleteVertexArrays(1, &cubeVAO); + glDeleteVertexArrays(1, &skyboxVAO); + glDeleteBuffers(1, &cubeVBO); + glDeleteBuffers(1, &skyboxVAO); + glfwTerminate(); return 0; } -// initialize GLFW window and OpenGL context with glew -// --------------------------------------------------------------------------------------------------------- -GLFWwindow *initialize_glfw_window(void) -{ - // 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 nullptr; - } - 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 nullptr; - } - - // configure global opengl state - // ----------------------------- - glEnable(GL_DEPTH_TEST); - - return window; -} - - // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly // --------------------------------------------------------------------------------------------------------- void processInput(GLFWwindow *window)