From 29339928d7a05ca11786885643631206a978655e Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 6 Jul 2020 07:17:09 +0200 Subject: [PATCH] Cleaning code --- CMakeLists.txt | 1 - resources/shaders/{square.glsl => main.glsl} | 3 -- src/opengl/renderer.cpp | 41 ++++++++++++-------- src/opengl/renderer.hpp | 28 +++++++++++-- src/opengl/shaders.hpp | 1 + src/rms.cpp | 7 ++-- tools/shader_watcher.sh | 6 ++- 7 files changed, 58 insertions(+), 29 deletions(-) rename resources/shaders/{square.glsl => main.glsl} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2032208..70f2a9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,6 @@ file(GLOB_RECURSE SHADERS_SRC_FILES CONFIGURE_DEPENDS resources/shaders/*) include_directories(${CMAKE_SOURCE_DIR}/src/) add_executable(rms ${SRC_FILES}) - # Shaders add_custom_target(shaders COMMAND ${CMAKE_COMMAND} -E copy_directory diff --git a/resources/shaders/square.glsl b/resources/shaders/main.glsl similarity index 99% rename from resources/shaders/square.glsl rename to resources/shaders/main.glsl index 200215c..7d4067a 100644 --- a/resources/shaders/square.glsl +++ b/resources/shaders/main.glsl @@ -5,11 +5,8 @@ layout(location = 0) in vec3 position; uniform mat4 projection; uniform mat4 model; - void main(){ - gl_Position = projection * model * vec4(position,1); - } // ----- Fragment Shader ----- diff --git a/src/opengl/renderer.cpp b/src/opengl/renderer.cpp index 1898753..1a7166d 100644 --- a/src/opengl/renderer.cpp +++ b/src/opengl/renderer.cpp @@ -18,7 +18,9 @@ void GLAPIENTRY MessageCallback( GLenum source, } -Renderer::Renderer(short width,short height): Width(width),Height(height), ClockStart(steady_clock::now()){ +Renderer::Renderer(short width,short height, std::string shader_name): + Width(width),Height(height), ClockStart(steady_clock::now()), + CurrentShader(shader_name){ // Init OpenGl glewInit(); glEnable(GL_DEPTH_TEST); @@ -33,24 +35,25 @@ Renderer::Renderer(short width,short height): Width(width),Height(height), Clock glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); float square[]{ - -1,1,0, - 1,1,0, - -1,-1,0, - - -1,-1,0, - 1,1,0, - 1,-1,0 - + -1,1,0,1,1,0,-1,-1,0, + -1,-1,0,1,1,0,1,-1,0 }; glBufferData(GL_ARRAY_BUFFER, 6*3*sizeof(float), square, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0); glBindVertexArray(0); - LoadShader("square.glsl"); + LoadShader(); +} + +Renderer::~Renderer(){ + glDeleteProgram(RayMarchingShader); + glDeleteVertexArrays(1,&VAO); } void Renderer::Render(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Setup shader glUseProgram(RayMarchingShader); MProjection=glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.f,100.0f); MModel=glm::scale(glm::mat4(1.0f),glm::vec3(10,10,1)); @@ -59,22 +62,28 @@ void Renderer::Render(){ ClockCurrent = std::chrono::steady_clock::now(); duration time_span = duration_cast>(ClockCurrent - ClockStart); glUniform1f(UTime, time_span.count()); - glUniform2f(UResolution,Width,Height); + + // Draw the actual shader onto the quad glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES,0,6); glBindVertexArray(0); glUseProgram(0); } -void Renderer::UpdateShader(std::string name){ +void Renderer::ChangeShader(std::string name){ + this->CurrentShader=name; glDeleteProgram(RayMarchingShader); - LoadShader(name); + LoadShader(); } -void Renderer::LoadShader(std::string name){ - RayMarchingShader = CompileShader("square.glsl"); - RayMarchingShader = CompileShader("square.glsl"); +void Renderer::RefreshShader(){ + glDeleteProgram(RayMarchingShader); + LoadShader(); +} + +void Renderer::LoadShader(){ + RayMarchingShader = CompileShader(CurrentShader); UProjection = glGetUniformLocation(RayMarchingShader, "projection"); UModel = glGetUniformLocation(RayMarchingShader, "model"); UResolution = glGetUniformLocation(RayMarchingShader, "resolution"); diff --git a/src/opengl/renderer.hpp b/src/opengl/renderer.hpp index 157ceb3..5cb7b3d 100644 --- a/src/opengl/renderer.hpp +++ b/src/opengl/renderer.hpp @@ -43,12 +43,34 @@ private: short Width,Height; steady_clock::time_point ClockStart; steady_clock::time_point ClockCurrent; + /// @brief Current loaded shader name + std::string CurrentShader; - void LoadShader(std::string name); + /** + * Compile send CurrentShader to the graphics card + */ + void LoadShader(); public: - Renderer(short width,short height); + Renderer(short width,short height,std::string shader_name); + ~Renderer(); + /** + * Draw current shader into the screen + */ void Render(); + /** + * Change current OpenGL viewport. + * @param with + * @param height + */ void AjustViewport(short with,short height); - void UpdateShader(std::string name); + /** + * Compile and load another shader + * @param name The name of the new shader + */ + void ChangeShader(std::string name); + /** + * Refresh CurrentShader code (recompile it etc..) + */ + void RefreshShader(); }; diff --git a/src/opengl/shaders.hpp b/src/opengl/shaders.hpp index ba7da09..9017222 100644 --- a/src/opengl/shaders.hpp +++ b/src/opengl/shaders.hpp @@ -10,6 +10,7 @@ #else #define SHADERS_RESOURCES "resources/shaders/" #endif + /** * Compile a shader: * - Shader should contains both vertex and fragment code diff --git a/src/rms.cpp b/src/rms.cpp index d2c6136..7238920 100644 --- a/src/rms.cpp +++ b/src/rms.cpp @@ -11,12 +11,12 @@ int main(int argc, char *argv[]) settings.depthBits = 24; settings.stencilBits = 8; settings.antialiasingLevel = 3; - settings.attributeFlags=sf::ContextSettings::Core; + settings.attributeFlags=sf::ContextSettings::Default; sf::Window window(sf::VideoMode(WIDTH, HEIGHT,32), "SFML/OpenGL Ray Marching", sf::Style::Close, settings); window.setFramerateLimit(60); // Limit to 60 frames per second // Init Renderer/OpenGL - Renderer renderer(WIDTH,HEIGHT); + Renderer renderer(WIDTH,HEIGHT,"main.glsl"); // Main loop bool running=true; @@ -31,9 +31,8 @@ int main(int argc, char *argv[]) else if (event.type == sf::Event::Resized) renderer.AjustViewport(event.size.width,event.size.height); else if (event.key.code == sf::Keyboard::R) - renderer.UpdateShader("square.glsl"); + renderer.RefreshShader(); } - renderer.Render(); window.display(); // Refresh screen } diff --git a/tools/shader_watcher.sh b/tools/shader_watcher.sh index d32285d..ca507ae 100755 --- a/tools/shader_watcher.sh +++ b/tools/shader_watcher.sh @@ -12,7 +12,8 @@ # \_/\_/_/ \_\_| \____|_| |_|_____|_| \_\ # # This program require xdotool to communicate -# with the window. +# with the window. It require entr (http://eradman.com/entrproject/) +# if you use the refresh on edit feature. wai=$(dirname $(readlink -f "$0")) # Current script directory @@ -33,6 +34,7 @@ usage() { if [ $# -eq 2 ] && [ $1 == "--every" ] then + ##### Periodically refresh shader shift time=$1 while [ true ] @@ -41,7 +43,7 @@ then refresh done else - + ##### Refresh on edit while [ true ] do ls $wai/../resources/shaders/*.glsl | entr -pd -s 'kill $PPID'