Cleaning code
This commit is contained in:
parent
e29a9115d1
commit
29339928d7
7 changed files with 58 additions and 29 deletions
|
@ -28,7 +28,6 @@ file(GLOB_RECURSE SHADERS_SRC_FILES CONFIGURE_DEPENDS resources/shaders/*)
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/src/)
|
include_directories(${CMAKE_SOURCE_DIR}/src/)
|
||||||
add_executable(rms ${SRC_FILES})
|
add_executable(rms ${SRC_FILES})
|
||||||
|
|
||||||
|
|
||||||
# Shaders
|
# Shaders
|
||||||
add_custom_target(shaders
|
add_custom_target(shaders
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
|
|
|
@ -5,11 +5,8 @@ layout(location = 0) in vec3 position;
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
|
|
||||||
gl_Position = projection * model * vec4(position,1);
|
gl_Position = projection * model * vec4(position,1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----- Fragment Shader -----
|
// ----- Fragment Shader -----
|
|
@ -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
|
// Init OpenGl
|
||||||
glewInit();
|
glewInit();
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
@ -33,24 +35,25 @@ Renderer::Renderer(short width,short height): Width(width),Height(height), Clock
|
||||||
glGenBuffers(1, &vertexbuffer);
|
glGenBuffers(1, &vertexbuffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||||
float square[]{
|
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);
|
glBufferData(GL_ARRAY_BUFFER, 6*3*sizeof(float), square, GL_STATIC_DRAW);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
LoadShader("square.glsl");
|
LoadShader();
|
||||||
|
}
|
||||||
|
|
||||||
|
Renderer::~Renderer(){
|
||||||
|
glDeleteProgram(RayMarchingShader);
|
||||||
|
glDeleteVertexArrays(1,&VAO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Render(){
|
void Renderer::Render(){
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// Setup shader
|
||||||
glUseProgram(RayMarchingShader);
|
glUseProgram(RayMarchingShader);
|
||||||
MProjection=glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.f,100.0f);
|
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));
|
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();
|
ClockCurrent = std::chrono::steady_clock::now();
|
||||||
duration<double> time_span = duration_cast<duration<double>>(ClockCurrent - ClockStart);
|
duration<double> time_span = duration_cast<duration<double>>(ClockCurrent - ClockStart);
|
||||||
glUniform1f(UTime, time_span.count());
|
glUniform1f(UTime, time_span.count());
|
||||||
|
|
||||||
glUniform2f(UResolution,Width,Height);
|
glUniform2f(UResolution,Width,Height);
|
||||||
|
|
||||||
|
// Draw the actual shader onto the quad
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawArrays(GL_TRIANGLES,0,6);
|
glDrawArrays(GL_TRIANGLES,0,6);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::UpdateShader(std::string name){
|
void Renderer::ChangeShader(std::string name){
|
||||||
|
this->CurrentShader=name;
|
||||||
glDeleteProgram(RayMarchingShader);
|
glDeleteProgram(RayMarchingShader);
|
||||||
LoadShader(name);
|
LoadShader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::LoadShader(std::string name){
|
void Renderer::RefreshShader(){
|
||||||
RayMarchingShader = CompileShader("square.glsl");
|
glDeleteProgram(RayMarchingShader);
|
||||||
RayMarchingShader = CompileShader("square.glsl");
|
LoadShader();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::LoadShader(){
|
||||||
|
RayMarchingShader = CompileShader(CurrentShader);
|
||||||
UProjection = glGetUniformLocation(RayMarchingShader, "projection");
|
UProjection = glGetUniformLocation(RayMarchingShader, "projection");
|
||||||
UModel = glGetUniformLocation(RayMarchingShader, "model");
|
UModel = glGetUniformLocation(RayMarchingShader, "model");
|
||||||
UResolution = glGetUniformLocation(RayMarchingShader, "resolution");
|
UResolution = glGetUniformLocation(RayMarchingShader, "resolution");
|
||||||
|
|
|
@ -43,12 +43,34 @@ private:
|
||||||
short Width,Height;
|
short Width,Height;
|
||||||
steady_clock::time_point ClockStart;
|
steady_clock::time_point ClockStart;
|
||||||
steady_clock::time_point ClockCurrent;
|
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:
|
public:
|
||||||
Renderer(short width,short height);
|
Renderer(short width,short height,std::string shader_name);
|
||||||
|
~Renderer();
|
||||||
|
/**
|
||||||
|
* Draw current shader into the screen
|
||||||
|
*/
|
||||||
void Render();
|
void Render();
|
||||||
|
/**
|
||||||
|
* Change current OpenGL viewport.
|
||||||
|
* @param with
|
||||||
|
* @param height
|
||||||
|
*/
|
||||||
void AjustViewport(short with,short 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();
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#else
|
#else
|
||||||
#define SHADERS_RESOURCES "resources/shaders/"
|
#define SHADERS_RESOURCES "resources/shaders/"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a shader:
|
* Compile a shader:
|
||||||
* - Shader should contains both vertex and fragment code
|
* - Shader should contains both vertex and fragment code
|
||||||
|
|
|
@ -11,12 +11,12 @@ int main(int argc, char *argv[])
|
||||||
settings.depthBits = 24;
|
settings.depthBits = 24;
|
||||||
settings.stencilBits = 8;
|
settings.stencilBits = 8;
|
||||||
settings.antialiasingLevel = 3;
|
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);
|
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
|
window.setFramerateLimit(60); // Limit to 60 frames per second
|
||||||
|
|
||||||
// Init Renderer/OpenGL
|
// Init Renderer/OpenGL
|
||||||
Renderer renderer(WIDTH,HEIGHT);
|
Renderer renderer(WIDTH,HEIGHT,"main.glsl");
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
bool running=true;
|
bool running=true;
|
||||||
|
@ -31,9 +31,8 @@ int main(int argc, char *argv[])
|
||||||
else if (event.type == sf::Event::Resized)
|
else if (event.type == sf::Event::Resized)
|
||||||
renderer.AjustViewport(event.size.width,event.size.height);
|
renderer.AjustViewport(event.size.width,event.size.height);
|
||||||
else if (event.key.code == sf::Keyboard::R)
|
else if (event.key.code == sf::Keyboard::R)
|
||||||
renderer.UpdateShader("square.glsl");
|
renderer.RefreshShader();
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.Render();
|
renderer.Render();
|
||||||
window.display(); // Refresh screen
|
window.display(); // Refresh screen
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,8 @@
|
||||||
# \_/\_/_/ \_\_| \____|_| |_|_____|_| \_\
|
# \_/\_/_/ \_\_| \____|_| |_|_____|_| \_\
|
||||||
#
|
#
|
||||||
# This program require xdotool to communicate
|
# 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
|
wai=$(dirname $(readlink -f "$0")) # Current script directory
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ usage() {
|
||||||
|
|
||||||
if [ $# -eq 2 ] && [ $1 == "--every" ]
|
if [ $# -eq 2 ] && [ $1 == "--every" ]
|
||||||
then
|
then
|
||||||
|
##### Periodically refresh shader
|
||||||
shift
|
shift
|
||||||
time=$1
|
time=$1
|
||||||
while [ true ]
|
while [ true ]
|
||||||
|
@ -41,7 +43,7 @@ then
|
||||||
refresh
|
refresh
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
|
##### Refresh on edit
|
||||||
while [ true ]
|
while [ true ]
|
||||||
do
|
do
|
||||||
ls $wai/../resources/shaders/*.glsl | entr -pd -s 'kill $PPID'
|
ls $wai/../resources/shaders/*.glsl | entr -pd -s 'kill $PPID'
|
||||||
|
|
Loading…
Add table
Reference in a new issue