Raymarcher + automatic load

This commit is contained in:
Emmanuel BENOîT 2017-09-30 11:23:14 +02:00
parent dab7bcaa74
commit f0b4f2d46e
4 changed files with 84 additions and 13 deletions

View file

@ -4,9 +4,7 @@ CPPFLAGS += -I. -Iimgui -Iglm \
-DREAL_BUILD -DGLM_ENABLE_EXPERIMENTAL -DREAL_BUILD -DGLM_ENABLE_EXPERIMENTAL
LIBS += $(shell sdl2-config --libs) -lGL -lGLEW -ldl LIBS += $(shell sdl2-config --libs) -lGL -lGLEW -ldl
FILEDUMPS = \ FILEDUMPS =
fd-raymarch-header.glsl.h \
fd-raymarcher.glsl.h
IMGUI = imgui.o imgui_demo.o imgui_draw.o IMGUI = imgui.o imgui_demo.o imgui_draw.o
DEMO = \ DEMO = \

66
main.cc
View file

@ -18,6 +18,8 @@ struct T_Main
SDL_Window * window; SDL_Window * window;
SDL_GLContext gl; SDL_GLContext gl;
T_FilesWatcher watcher; T_FilesWatcher watcher;
T_WatchedFiles shaderFiles;
GLuint prog;
std::string loadError; std::string loadError;
bool done = false; bool done = false;
@ -29,11 +31,16 @@ struct T_Main
void handleCapture( ); void handleCapture( );
void makeUI( ); void makeUI( );
void render( ); void render( );
void loadProgram( );
}; };
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
T_Main::T_Main( ) T_Main::T_Main( )
: shaderFiles( watcher , [this] {
this->loadProgram( );
} )
{ {
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ); SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
@ -52,6 +59,8 @@ T_Main::T_Main( )
gl = SDL_GL_CreateContext( window ); gl = SDL_GL_CreateContext( window );
glewInit(); glewInit();
ImGui_ImplSdl_Init( window ); ImGui_ImplSdl_Init( window );
loadProgram( );
} }
void T_Main::mainLoop( ) void T_Main::mainLoop( )
@ -148,6 +157,34 @@ void T_Main::render( )
// FIXME draw the fuck // FIXME draw the fuck
//project->render( ); //project->render( );
if ( prog != 0 ) {
glUseProgram( prog );
enum {
U_TIME = 0 ,
U_RESOLUTION = 1 ,
U_CAM_POS = 2 ,
U_LOOK_AT = 3 ,
U_CAM_UP = 4 ,
U_NEAR_PLANE = 5 ,
U_LIGHT_DIR = 6 ,
U_RENDER = 7 ,
};
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
glUniform1f( U_TIME , 0 );
glUniform2f( U_RESOLUTION , dspSize.x , dspSize.y );
glUniform3f( U_CAM_POS , 0 , 0 , -15 );
glUniform3f( U_LOOK_AT , 0 , 0 , 0 );
glUniform3f( U_CAM_UP , 0 , 1 , 0 );
glUniform1f( U_NEAR_PLANE , 1 );
glUniform3f( U_LIGHT_DIR , 0 , 1 , 1 );
glUniform4f( U_RENDER , 128 , 0.9 , 0.001 , 100 );
glRectf( -1, -1 , 1 , 1 );
}
glUseProgram( 0 ); glUseProgram( 0 );
ImGui::Render( ); ImGui::Render( );
@ -155,6 +192,35 @@ void T_Main::render( )
} }
void T_Main::loadProgram( )
{
std::vector< std::string > errors;
if ( prog != 0 ) {
glDeleteProgram( prog );
prog = 0;
}
shaderFiles.clear( );
shaderFiles.watch( "raymarch-header.glsl" );
shaderFiles.watch( "map.glsl" );
shaderFiles.watch( "raymarcher.glsl" );
T_ShaderCode shader( 3 );
shader.loadPart( 0 , "raymarch-header.glsl" , errors );
shader.loadPart( 1 , "map.glsl" , errors );
shader.loadPart( 2 , "raymarcher.glsl" , errors );
prog = shader.createProgram( GL_FRAGMENT_SHADER , errors );
if ( errors.size( ) != 0 ) {
for ( auto const& str : errors ) {
printf( "ERR: %s\n" , str.c_str( ) );
}
}
}
/*============================================================================*/ /*============================================================================*/
int main( int , char** ) int main( int , char** )

4
map.glsl Normal file
View file

@ -0,0 +1,4 @@
vec2 map( vec3 pos )
{
return vec2( length( pos ) - 1. , 0. );
}

View file

@ -49,8 +49,6 @@ void main( )
{ {
setCamFromUniforms( ); setCamFromUniforms( );
int dispMode = ( u_DebugFeatures.x >> 3 ) & 3;
vec2 uv = ( gl_FragCoord.xy / u_Resolution ) * 2 - 1; vec2 uv = ( gl_FragCoord.xy / u_Resolution ) * 2 - 1;
vec3 camDir = normalize( lookAt - camPos ); vec3 camDir = normalize( lookAt - camPos );
vec3 side = normalize( cross( camUp , camDir ) ); vec3 side = normalize( cross( camUp , camDir ) );
@ -62,7 +60,9 @@ void main( )
vec3 r = march( camPos , rayDir , int( u_Render.x ) , u_Render.y ); vec3 r = march( camPos , rayDir , int( u_Render.x ) , u_Render.y );
vec3 hitPos = camPos + rayDir * r.x; vec3 hitPos = camPos + rayDir * r.x;
vec3 bc = vec3( 1. ); vec3 bc;
if ( r.y >= 0. ) {
bc = vec3( 1. , 1. , 0. );
vec3 hvec = normalize( rayDir - u_LightDir ) , vec3 hvec = normalize( rayDir - u_LightDir ) ,
norm = getNormal( hitPos ); norm = getNormal( hitPos );
float ndotl = dot( norm , -normalize( u_LightDir ) ) , float ndotl = dot( norm , -normalize( u_LightDir ) ) ,
@ -70,6 +70,9 @@ void main( )
float si = pow( clamp( ndoth , 0 , 1 ) , 4 ) , float si = pow( clamp( ndoth , 0 , 1 ) , 4 ) ,
di = .6 + .3 * clamp( ndotl , 0 , 1 ); di = .6 + .3 * clamp( ndotl , 0 , 1 );
bc = mix( bc * di , vec3( 1. ) , si ); bc = mix( bc * di , vec3( 1. ) , si );
} else {
bc = vec3( 0. );
}
color = vec4( bc , 1 ); color = vec4( bc , 1 );