2017-09-30 21:53:45 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "raymarcher.hh"
|
2017-10-01 11:37:04 +02:00
|
|
|
#include "profiling.hh"
|
2017-10-04 17:29:48 +02:00
|
|
|
#include "globals.hh"
|
2017-10-06 14:29:01 +02:00
|
|
|
#include "odbg.hh"
|
2017-09-30 21:53:45 +02:00
|
|
|
|
2017-10-02 09:40:36 +02:00
|
|
|
|
2017-10-01 11:37:04 +02:00
|
|
|
namespace {
|
2017-11-03 11:55:26 +01:00
|
|
|
static char const* const Name_( "Raymarcher" );
|
2017-10-01 11:37:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 18:08:37 +02:00
|
|
|
#define PSTART() Globals::Profiler( ).start( Name_ )
|
|
|
|
#define PEND() do { glFinish( ); Globals::Profiler( ).end( Name_ ); } while ( 0 )
|
2017-09-30 21:53:45 +02:00
|
|
|
|
2017-10-02 09:40:36 +02:00
|
|
|
|
2017-09-30 21:53:45 +02:00
|
|
|
T_Raymarcher::T_Raymarcher(
|
|
|
|
__rd__ const uint32_t width ,
|
|
|
|
__rd__ const uint32_t height )
|
2017-10-04 17:29:48 +02:00
|
|
|
: camera_( ) ,
|
2017-09-30 21:53:45 +02:00
|
|
|
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-06 14:29:01 +02:00
|
|
|
Globals::ODbg( ).registerTexture( txOutput_ , E_ODbgMode::HDR , "Raymarched image" );
|
|
|
|
Globals::ODbg( ).registerTexture( txDepth_ , E_ODbgMode::DEPTH , "Raymarched distance" );
|
2017-10-04 17:29:48 +02:00
|
|
|
program_ = Globals::Shaders( ).pipeline({
|
|
|
|
"fullscreen.v.glsl" , "scene.f.glsl" });
|
2017-09-30 21:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void T_Raymarcher::render( )
|
|
|
|
{
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( !rtOutput_.activate( ) || !program_.valid( ) ) {
|
|
|
|
glClearColor( 0 , 0 , 0 , 1 );
|
|
|
|
glClear( GL_COLOR_BUFFER_BIT );
|
2017-09-30 21:53:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-04 18:08:37 +02:00
|
|
|
|
|
|
|
PSTART( );
|
2017-10-04 17:29:48 +02:00
|
|
|
program_.enable( );
|
2017-10-01 11:37:04 +02:00
|
|
|
|
2017-10-01 18:51:02 +02:00
|
|
|
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 ,
|
2017-10-05 15:05:05 +02:00
|
|
|
U_RAYMARCHER = 6 ,
|
|
|
|
U_FOG = 7 ,
|
2017-10-05 17:13:58 +02:00
|
|
|
U_CORRECTION = 8
|
2017-09-30 21:53:45 +02:00
|
|
|
};
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
const auto id( program_.program( E_ShaderType::FRAGMENT ) );
|
|
|
|
glProgramUniform1f( id , U_TIME , 0 );
|
|
|
|
glProgramUniform2f( id , U_RESOLUTION , rtOutput_.width( ) , rtOutput_.height( ) );
|
2017-09-30 21:53:45 +02:00
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
glProgramUniform3fv( id , U_CAM_POS , 1 , &camera_.pos.x );
|
|
|
|
glProgramUniform3fv( id , U_LOOK_AT , 1 , &camera_.lookAt.x );
|
|
|
|
glProgramUniform3fv( id , U_CAM_UP , 1 , &camera_.up.x );
|
|
|
|
glProgramUniform1f( id , U_NEAR_PLANE , camera_.np );
|
2017-09-30 21:53:45 +02:00
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
glProgramUniform4f( id , U_RAYMARCHER , rmIterations , rmStep ,
|
2017-09-30 21:53:45 +02:00
|
|
|
rmEpsilon , rmMaxDist );
|
2017-10-05 09:07:37 +02:00
|
|
|
glProgramUniform1f( id , U_FOG , fog );
|
2017-10-05 17:13:58 +02:00
|
|
|
glProgramUniform1i( id , U_CORRECTION , correction_ );
|
2017-10-05 09:07:37 +02:00
|
|
|
|
2017-10-01 18:51:02 +02:00
|
|
|
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
|
2017-10-01 11:37:04 +02:00
|
|
|
|
|
|
|
PEND( );
|
2017-09-30 21:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Raymarcher::makeUI( )
|
|
|
|
{
|
|
|
|
camera_.makeUI( );
|
|
|
|
|
2017-10-05 09:07:37 +02:00
|
|
|
if ( ImGui::CollapsingHeader( "Raymarcher" ) ) {
|
|
|
|
ImGui::DragInt( "Iterations" , &rmIterations , .01 , 1 , 512 );
|
|
|
|
ImGui::DragFloat( "Step" , &rmStep , .00005 , .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 );
|
|
|
|
}
|
2017-10-05 17:13:58 +02:00
|
|
|
ImGui::DragInt( "Correction" , &correction_ , .001 , 0 , 50 );
|
2017-10-05 09:07:37 +02:00
|
|
|
ImGui::DragFloat( "Fog" , &fog , .000001 , 0 , 1 , "%.5f" );
|
2017-09-30 21:53:45 +02:00
|
|
|
}
|
|
|
|
}
|