Cleaning code

This commit is contained in:
Loic Guegan 2020-07-06 07:17:09 +02:00
parent e29a9115d1
commit 29339928d7
7 changed files with 58 additions and 29 deletions

View file

@ -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

View file

@ -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 -----

View file

@ -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<double> time_span = duration_cast<duration<double>>(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");

View file

@ -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();
};

View file

@ -10,6 +10,7 @@
#else
#define SHADERS_RESOURCES "resources/shaders/"
#endif
/**
* Compile a shader:
* - Shader should contains both vertex and fragment code

View file

@ -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
}

View file

@ -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'