From 276a5481a69f9df8ce1602510ed42db0fc884bec Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 5 Feb 2023 20:15:18 +0100 Subject: [PATCH] allow to rebuild shader on the fly --- src/main.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8b01276..f46eca6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ void mouse_callback(GLFWwindow *window, double xpos, double ypos); void processInput(GLFWwindow *); Camera camera; +Shader* theShader; int main() { @@ -90,8 +91,8 @@ int main() glEnableVertexAttribArray(0); // Shader with dummy vertex shader - Shader theShader("shaders/shader.vs", "shaders/shader.fs"); - theShader.use(); + theShader = new Shader("shaders/shader.vs", "shaders/shader.fs"); + theShader->use(); float lastFrame = glfwGetTime(), deltaTime = 0; int width, height; @@ -116,16 +117,16 @@ int main() glBindVertexArray(VAO); camera.update(window, deltaTime); - theShader.use(); + theShader->use(); // std::cout << width << "*" << height << "\n"; - theShader.setVec2("u_resolution", (float)width, (float)height); - theShader.setFloat("u_time", currentFrame); - theShader.setFloat("u_deltatime", deltaTime); - theShader.setVec3("u_camorigin", camera.getPos()); - theShader.setVec3("u_camdir", camera.getFront()); - theShader.setVec3("u_camup", camera.getUp()); - theShader.setMat4("u_projection", camera.getProjection()); - theShader.setMat4("u_view", camera.getView()); + theShader->setVec2("u_resolution", (float)width, (float)height); + theShader->setFloat("u_time", currentFrame); + theShader->setFloat("u_deltatime", deltaTime); + theShader->setVec3("u_camorigin", camera.getPos()); + theShader->setVec3("u_camdir", camera.getFront()); + theShader->setVec3("u_camup", camera.getUp()); + theShader->setMat4("u_projection", camera.getProjection()); + theShader->setMat4("u_view", camera.getView()); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); @@ -153,4 +154,9 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); + if( glfwGetKey(window, GLFW_KEY_0) == GLFW_PRESS){ + std::cout << "Rebuilding main shader" << std::endl; + delete(theShader); + theShader = new Shader("shaders/shader.vs", "shaders/shader.fs"); + } } \ No newline at end of file