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)