demotool/raymarcher.cc

99 lines
2.6 KiB
C++
Raw Normal View History

2017-09-30 21:53:45 +02:00
#include "externals.hh"
#include "utilities.hh"
#include "texture.hh"
#include "rendertarget.hh"
#include "raymarcher.hh"
2017-10-01 11:37:04 +02:00
#include "profiling.hh"
2017-09-30 21:53:45 +02:00
2017-10-01 11:37:04 +02:00
namespace {
static const std::string Name_( "Raymarcher" );
}
#define PSTART() T_Profiler::Profiler.start( Name_ )
#define PEND() do { glFinish( ); T_Profiler::Profiler.end( Name_ ); } while ( 0 )
2017-09-30 21:53:45 +02:00
T_Raymarcher::T_Raymarcher(
__rw__ T_FilesWatcher& watcher ,
__rd__ const uint32_t width ,
__rd__ const uint32_t height )
: camera_( ) , program_( GL_FRAGMENT_SHADER , watcher ) ,
txOutput_( width , height , E_TexType::RGB16F ) ,
2017-10-01 18:51:02 +02:00
txDepth_( width , height , E_TexType::R16F ) ,
rtOutput_( T_RendertargetSetup( )
.add( txOutput_ )
.add( txDepth_ )
.create( ) )
2017-09-30 21:53:45 +02:00
{
2017-10-01 18:51:02 +02:00
glGenProgramPipelines( 1 , &pipeline_ );
2017-09-30 21:53:45 +02:00
program_.addFile( "raymarch-header.glsl" );
program_.addFile( "map.glsl" );
program_.addFile( "raymarcher.glsl" );
program_.load( );
2017-10-01 18:51:02 +02:00
auto& fsq( T_ShaderProgram::FullscreenQuad( watcher ) );
glUseProgramStages( pipeline_ , GL_VERTEX_SHADER_BIT , fsq.id( ) );
glUseProgramStages( pipeline_ , GL_FRAGMENT_SHADER_BIT , program_.id( ) );
assert( glGetError( ) == GL_NO_ERROR );
2017-09-30 21:53:45 +02:00
}
void T_Raymarcher::render( )
{
2017-10-01 11:37:04 +02:00
PSTART( );
2017-10-01 18:51:02 +02:00
if ( ! rtOutput_.activate( ) ) {
2017-10-01 11:37:04 +02:00
PEND( );
2017-09-30 21:53:45 +02:00
return;
}
2017-10-01 11:37:04 +02:00
2017-10-01 18:51:02 +02:00
glBindProgramPipeline( pipeline_ );
glClearColor( 0 , 0 , 0 , 1 );
2017-09-30 21:53:45 +02:00
glClear( GL_COLOR_BUFFER_BIT );
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_RAYMARCHER = 7 ,
};
2017-10-01 18:51:02 +02:00
glProgramUniform1f( program_.id( ) , U_TIME , 0 );
glProgramUniform2f( program_.id( ) , U_RESOLUTION , rtOutput_.width( ) , rtOutput_.height( ) );
2017-09-30 21:53:45 +02:00
2017-10-01 18:51:02 +02:00
glProgramUniform3fv( program_.id( ) , U_CAM_POS , 1 , &camera_.pos.x );
glProgramUniform3fv( program_.id( ) , U_LOOK_AT , 1 , &camera_.lookAt.x );
glProgramUniform3fv( program_.id( ) , U_CAM_UP , 1 , &camera_.up.x );
glProgramUniform1f( program_.id( ) , U_NEAR_PLANE , camera_.np );
2017-09-30 21:53:45 +02:00
2017-10-01 18:51:02 +02:00
glProgramUniform3f( program_.id( ) , U_LIGHT_DIR , 0 , 1 , 1 );
2017-09-30 21:53:45 +02:00
2017-10-01 18:51:02 +02:00
glProgramUniform4f( program_.id( ) , U_RAYMARCHER , rmIterations , rmStep ,
2017-09-30 21:53:45 +02:00
rmEpsilon , rmMaxDist );
2017-10-01 18:51:02 +02:00
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
glBindProgramPipeline( 0 );
2017-10-01 11:37:04 +02:00
PEND( );
2017-09-30 21:53:45 +02:00
}
void T_Raymarcher::makeUI( )
{
camera_.makeUI( );
if ( !ImGui::CollapsingHeader( "Raymarcher" ) ) {
return;
}
ImGui::DragInt( "Iterations" , &rmIterations , .1 , 1 , 512 );
ImGui::DragFloat( "Step" , &rmStep , .005 , .1 , 2 );
ImGui::DragFloat( "Max. dist." , &rmMaxDist , .1f ,
0.01 , 1000.0 , "%.2f" );
if ( ImGui::DragFloat( "Epsilon" , &epsLog , .01f ,
-10 , -0.5 , "10 ^ %.2f" ) ) {
rmEpsilon = pow( 10 , epsLog );
}
}