From 12829892b262a0c7fcccba9198e5b6b31b2a8015 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 9 Jul 2020 13:02:31 +0200 Subject: Cleaning code --- resources/shaders/algorithms/normal.frag | 24 ++++ resources/shaders/algorithms/ray_marching.glsl | 5 - resources/shaders/algorithms/raymarching.frag | 26 ++++ resources/shaders/julia.frag | 48 ++++++++ resources/shaders/main.frag | 59 +++++++++ resources/shaders/main.glsl | 93 -------------- resources/shaders/objects/sphere.frag | 7 ++ resources/shaders/objects/sphere.glsl | 4 - src/opengl/shaders.cpp | 164 +++++++------------------ src/rms.cpp | 9 +- 10 files changed, 218 insertions(+), 221 deletions(-) create mode 100644 resources/shaders/algorithms/normal.frag delete mode 100644 resources/shaders/algorithms/ray_marching.glsl create mode 100644 resources/shaders/algorithms/raymarching.frag create mode 100644 resources/shaders/julia.frag create mode 100644 resources/shaders/main.frag delete mode 100644 resources/shaders/main.glsl create mode 100644 resources/shaders/objects/sphere.frag delete mode 100644 resources/shaders/objects/sphere.glsl diff --git a/resources/shaders/algorithms/normal.frag b/resources/shaders/algorithms/normal.frag new file mode 100644 index 0000000..0f61544 --- /dev/null +++ b/resources/shaders/algorithms/normal.frag @@ -0,0 +1,24 @@ +// Requirements: +// #define NORMAL_ACCURACY 0.01 +// GetDist: (vec3 position) => float +// GetDist should return the closest object point distance from position (in the whole scene) + +/** + * Compute the normal of an object at a given surface point + * this can be usefull for lighting etc.. + * This function exploit the fact that the gradient on a given surface point in 3D space + * given the normal direction (source: https://fr.wikipedia.org/wiki/Gradient#Dimension_3_:_gradient_normal_%C3%A0_une_surface_en_un_point,_plan_tangent) + * + * + */ +vec3 GetNormal(vec3 point_position) { + float point_distance = GetDist(point_position); + vec2 epsilon = vec2(NORMAL_ACCURACY, 0); // Just a convenient way to use NORMAL_ACCURACY and 0 + // Compute the gradient + vec3 gradient = point_distance - vec3( + GetDist(point_position-epsilon.xyy), + GetDist(point_position-epsilon.yxy), + GetDist(point_position-epsilon.yyx)); + + return normalize(gradient); +} \ No newline at end of file diff --git a/resources/shaders/algorithms/ray_marching.glsl b/resources/shaders/algorithms/ray_marching.glsl deleted file mode 100644 index d984c0e..0000000 --- a/resources/shaders/algorithms/ray_marching.glsl +++ /dev/null @@ -1,5 +0,0 @@ - - -RayMarching(vec3 ro, vec3 rd){ - -} \ No newline at end of file diff --git a/resources/shaders/algorithms/raymarching.frag b/resources/shaders/algorithms/raymarching.frag new file mode 100644 index 0000000..b8de515 --- /dev/null +++ b/resources/shaders/algorithms/raymarching.frag @@ -0,0 +1,26 @@ +// Requirements: +// #define MAX_STEPS 100 +// #define MAX_DIST 100. +// GetDist: (vec3 position) => float +// GetDist should return the closest object point distance from position (in the whole scene) + +/** + * Ray Marching algorithm + * Please take a look at: https://www.youtube.com/watch?v=PGtv-dBi2wE&t=1367s + * This function return the distance reached by the ray: + * - If dist=MAX_STEPS) + // We move the ray: + vec3 ray_position = ray_origin + ray_direction*dist_origin; + // We find the next closest point: + float closest_point = GetDist(ray_position); + // Increase the ray distance + dist_origin += closest_point; + // Check if we went to far or we are too close from a point (we hit a surface) + if(dist_origin>MAX_DIST || closest_point LIMIT) + break; + + } + return(z); +} + + +void main() +{ + vec2 coord=gl_FragCoord.xy/resolution.xy; + coord-=0.5; + coord/=(time/10); + float d=length(IsDiverging(coord)); + color=vec3(LIMIT/(d*2),0,0); +} diff --git a/resources/shaders/main.frag b/resources/shaders/main.frag new file mode 100644 index 0000000..590b849 --- /dev/null +++ b/resources/shaders/main.frag @@ -0,0 +1,59 @@ +#version 330 core + +uniform vec2 resolution; +uniform float time; + +out vec3 color; + +#define MAX_STEPS 100 +#define MAX_DIST 100. +#define SURF_DIST .01 +#define NORMAL_ACCURACY 0.01 + +#include "objects/sphere.frag" +float GetDist(vec3 p) { + float sphereDist = SphereSDF(p,vec3(0, 1, 6),1); + float planeDist = p.y; + + float d = min(sphereDist, planeDist); + return d; +} + +#include "algorithms/raymarching.frag" +#include "algorithms/normal.frag" + + + +float GetLight(vec3 p) { + vec3 lightPos = vec3(0, 5, 6); + lightPos.xz += vec2(sin(time), cos(time))*2.; + vec3 l = normalize(lightPos-p); + vec3 n = GetNormal(p); + + float dif = clamp(dot(n, l), 0., 1.); + float d = RayMarch(p+n*SURF_DIST*2., l); + if(dMAX_DIST || dS 0 ){ - std::vector VertexShaderErrorMessage(InfoLogLength+1); - glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); - printf("%s\n", &VertexShaderErrorMessage[0]); - } - - - - // Compile Fragment Shader - printf("Compiling shader : %s\n", fragment_file_path); - char const * FragmentSourcePointer = FragmentShaderCode.c_str(); - glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); - glCompileShader(FragmentShaderID); - - // Check Fragment Shader - glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); - glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ){ - std::vector FragmentShaderErrorMessage(InfoLogLength+1); - glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); - printf("%s\n", &FragmentShaderErrorMessage[0]); - } - - // Link the program - printf("Linking program\n"); - GLuint ProgramID = glCreateProgram(); - glAttachShader(ProgramID, VertexShaderID); - glAttachShader(ProgramID, FragmentShaderID); - glLinkProgram(ProgramID); - - // Check the program - glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); - glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ){ - std::vector ProgramErrorMessage(InfoLogLength+1); - glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); - printf("%s\n", &ProgramErrorMessage[0]); - } - - - glDetachShader(ProgramID, VertexShaderID); - glDetachShader(ProgramID, FragmentShaderID); - - glDeleteShader(VertexShaderID); - glDeleteShader(FragmentShaderID); - - return ProgramID; -} diff --git a/src/rms.cpp b/src/rms.cpp index b7cf93b..a9bb166 100644 --- a/src/rms.cpp +++ b/src/rms.cpp @@ -8,6 +8,12 @@ void OnWindowResize(GLFWwindow* window, int width, int height){ AppContext->renderer.AjustViewport(width,height); } +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + if (key == GLFW_KEY_R && action == GLFW_PRESS) + AppContext->renderer.RefreshShader(); +} + int main(int argc, char *argv[]) { GLFWwindow* window; @@ -29,7 +35,7 @@ int main(int argc, char *argv[]) // Init Renderer/OpenGL APP_CONTEXT InitContext={ - Renderer(WIDTH,HEIGHT,"main.glsl"), + Renderer(WIDTH,HEIGHT,"main.frag"), HUD(window) }; AppContext=&InitContext; @@ -37,6 +43,7 @@ int main(int argc, char *argv[]) glfwSetWindowSizeCallback(window, OnWindowResize); + glfwSetKeyCallback(window, key_callback); /* Loop until the user closes the window */ double InitTime = glfwGetTime(); -- cgit v1.2.3