2017-09-30 10:37:45 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
|
|
|
|
#include "imgui_impl_sdl.h"
|
|
|
|
#include "utilities.hh"
|
2017-09-30 12:59:04 +02:00
|
|
|
#include "texture.hh"
|
2017-09-30 14:59:15 +02:00
|
|
|
#include "rendertarget.hh"
|
2017-09-30 19:13:06 +02:00
|
|
|
#include "bloom.hh"
|
2017-09-30 10:37:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*= T_Main ===================================================================*/
|
|
|
|
|
|
|
|
struct T_Main
|
|
|
|
{
|
|
|
|
T_Main( );
|
|
|
|
~T_Main( );
|
|
|
|
|
|
|
|
void mainLoop( );
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string projectFile;
|
|
|
|
SDL_Window * window;
|
|
|
|
SDL_GLContext gl;
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
bool capture = false;
|
|
|
|
ImVec2 mouseInitial;
|
|
|
|
ImVec2 mouseMove;
|
|
|
|
|
2017-09-30 11:47:28 +02:00
|
|
|
T_Camera camera;
|
|
|
|
|
|
|
|
T_FilesWatcher watcher;
|
2017-09-30 14:59:15 +02:00
|
|
|
std::unique_ptr< T_ShaderProgram > spRaymarch;
|
|
|
|
std::unique_ptr< T_ShaderProgram > spCopy;
|
|
|
|
|
|
|
|
std::unique_ptr< T_Texture > txRaymarchOutput;
|
|
|
|
std::unique_ptr< T_Rendertarget > rtRaymarchOutput;
|
2017-09-30 11:47:28 +02:00
|
|
|
|
2017-09-30 19:13:06 +02:00
|
|
|
std::unique_ptr< T_BloomPass > bloomPass;
|
2017-09-30 17:58:48 +02:00
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
void startIteration( );
|
|
|
|
void handleCapture( );
|
|
|
|
void makeUI( );
|
|
|
|
void render( );
|
2017-09-30 11:23:14 +02:00
|
|
|
|
2017-09-30 12:08:18 +02:00
|
|
|
void initProgram( );
|
2017-09-30 21:37:35 +02:00
|
|
|
|
|
|
|
// FIXME: move
|
|
|
|
int rmIterations = 128;
|
|
|
|
float rmStep = .9;
|
|
|
|
float rmEpsilon = .000001;
|
|
|
|
float rmMaxDist = 50;
|
|
|
|
float epsLog = log( rmEpsilon ) / log( 10 );
|
2017-09-30 10:37:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
T_Main::T_Main( )
|
|
|
|
{
|
|
|
|
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
|
|
|
|
|
|
|
|
// Setup window
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE , 24 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE , 8 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION , 2 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION , 2 );
|
|
|
|
SDL_DisplayMode current;
|
|
|
|
SDL_GetCurrentDisplayMode( 0 , ¤t );
|
|
|
|
window = SDL_CreateWindow( "DEMO",
|
|
|
|
SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED ,
|
|
|
|
1280 , 720 ,
|
|
|
|
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
|
|
|
|
gl = SDL_GL_CreateContext( window );
|
|
|
|
glewInit();
|
|
|
|
ImGui_ImplSdl_Init( window );
|
2017-09-30 11:23:14 +02:00
|
|
|
|
2017-09-30 12:08:18 +02:00
|
|
|
initProgram( );
|
2017-09-30 17:58:48 +02:00
|
|
|
|
2017-09-30 14:59:15 +02:00
|
|
|
txRaymarchOutput = std::make_unique< T_Texture >(
|
2017-09-30 17:58:48 +02:00
|
|
|
1280 , 720 , E_TexType::RGB16F );
|
2017-09-30 14:59:15 +02:00
|
|
|
rtRaymarchOutput = std::make_unique < T_Rendertarget >(
|
|
|
|
T_RendertargetSetup( ).add( *txRaymarchOutput ).create( ) );
|
2017-09-30 17:58:48 +02:00
|
|
|
|
2017-09-30 21:30:31 +02:00
|
|
|
bloomPass = std::make_unique< T_BloomPass >( watcher , *txRaymarchOutput );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::mainLoop( )
|
|
|
|
{
|
|
|
|
while ( !done ) {
|
|
|
|
startIteration( );
|
|
|
|
if ( !done ) {
|
|
|
|
handleCapture( );
|
|
|
|
makeUI( );
|
|
|
|
watcher.check( );
|
|
|
|
render( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_Main::~T_Main( )
|
|
|
|
{
|
|
|
|
ImGui_ImplSdl_Shutdown( );
|
|
|
|
SDL_GL_DeleteContext( gl );
|
|
|
|
SDL_DestroyWindow( window );
|
|
|
|
SDL_Quit( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void T_Main::startIteration( )
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
mouseMove = ImVec2( );
|
|
|
|
while ( SDL_PollEvent( &event ) ) {
|
|
|
|
ImGui_ImplSdl_ProcessEvent( &event );
|
|
|
|
if ( event.type == SDL_QUIT ) {
|
|
|
|
done = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( capture && event.type == SDL_MOUSEMOTION ) {
|
|
|
|
mouseMove.x += event.motion.xrel;
|
|
|
|
mouseMove.y += event.motion.yrel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplSdl_NewFrame( window , capture , mouseInitial );
|
|
|
|
ImGui::GetIO( ).MouseDrawCursor = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::handleCapture( )
|
|
|
|
{
|
|
|
|
auto const& io( ImGui::GetIO( ) );
|
|
|
|
const bool lmb( ImGui::IsMouseDown( 0 ) );
|
|
|
|
const bool mb( lmb || ImGui::IsMouseDown( 1 ) );
|
|
|
|
const bool appCanGrab( !( ImGui::IsMouseHoveringAnyWindow( )
|
|
|
|
|| io.WantCaptureMouse
|
|
|
|
|| io.WantCaptureKeyboard ) );
|
|
|
|
const bool shift( io.KeyShift );
|
|
|
|
const bool ctrl( io.KeyCtrl );
|
|
|
|
|
|
|
|
if ( capture && !mb ) {
|
|
|
|
capture = false;
|
|
|
|
ImGui::CaptureMouseFromApp( false );
|
|
|
|
SDL_SetRelativeMouseMode( SDL_FALSE );
|
|
|
|
SDL_WarpMouseInWindow( window ,
|
|
|
|
int( mouseInitial.x ) ,
|
|
|
|
int( mouseInitial.y ) );
|
|
|
|
ImGui::SetMouseCursor( ImGuiMouseCursor_Arrow );
|
|
|
|
} else if ( capture ) {
|
|
|
|
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
|
2017-09-30 11:47:28 +02:00
|
|
|
camera.handleDND( mouseMove , ctrl , shift , lmb );
|
2017-09-30 10:37:45 +02:00
|
|
|
} else if ( appCanGrab && mb ) {
|
|
|
|
capture = true;
|
|
|
|
mouseInitial = ImGui::GetMousePos( );
|
|
|
|
ImGui::CaptureMouseFromApp( true );
|
|
|
|
SDL_SetRelativeMouseMode( SDL_TRUE );
|
|
|
|
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( appCanGrab || capture ) && io.MouseWheel ) {
|
2017-09-30 11:47:28 +02:00
|
|
|
camera.handleWheel( io.MouseWheel , ctrl , shift );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::makeUI( )
|
|
|
|
{
|
2017-09-30 12:25:24 +02:00
|
|
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
|
|
|
ImGui::SetNextWindowSize( ImVec2( 300 , dspSize.y ) ,
|
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::SetNextWindowPos( ImVec2( ) , ImGuiSetCond_Once );
|
|
|
|
ImGui::Begin( "Yay! Demo!" );
|
|
|
|
|
|
|
|
camera.makeUI( );
|
2017-09-30 21:37:35 +02:00
|
|
|
|
|
|
|
if ( ImGui::CollapsingHeader( "Raymarcher" ) ) {
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 19:41:45 +02:00
|
|
|
bloomPass->makeUI( );
|
2017-09-30 12:25:24 +02:00
|
|
|
|
|
|
|
ImGui::End( );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::render( )
|
|
|
|
{
|
2017-09-30 17:58:48 +02:00
|
|
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
|
|
|
|
2017-09-30 14:59:15 +02:00
|
|
|
if ( spRaymarch->activate( ) && rtRaymarchOutput->activate( ) ) {
|
|
|
|
glClearColor( 0 , 1 , 1 , 1 );
|
|
|
|
glClear( GL_COLOR_BUFFER_BIT );
|
2017-09-30 11:23:14 +02:00
|
|
|
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 ,
|
2017-09-30 21:30:31 +02:00
|
|
|
U_RAYMARCHER = 7 ,
|
2017-09-30 11:23:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
glUniform1f( U_TIME , 0 );
|
|
|
|
glUniform2f( U_RESOLUTION , dspSize.x , dspSize.y );
|
|
|
|
|
2017-09-30 11:47:28 +02:00
|
|
|
glUniform3fv( U_CAM_POS , 1 , &camera.pos.x );
|
|
|
|
glUniform3fv( U_LOOK_AT , 1 , &camera.lookAt.x );
|
|
|
|
glUniform3fv( U_CAM_UP , 1 , &camera.up.x );
|
|
|
|
glUniform1f( U_NEAR_PLANE , camera.np );
|
2017-09-30 11:23:14 +02:00
|
|
|
|
|
|
|
glUniform3f( U_LIGHT_DIR , 0 , 1 , 1 );
|
2017-09-30 21:30:31 +02:00
|
|
|
|
2017-09-30 21:37:35 +02:00
|
|
|
glUniform4f( U_RAYMARCHER , rmIterations , rmStep ,
|
|
|
|
rmEpsilon , rmMaxDist );
|
2017-09-30 11:23:14 +02:00
|
|
|
|
|
|
|
glRectf( -1, -1 , 1 , 1 );
|
|
|
|
}
|
2017-09-30 19:13:06 +02:00
|
|
|
bloomPass->render( );
|
2017-09-30 14:59:15 +02:00
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
glUseProgram( 0 );
|
|
|
|
ImGui::Render( );
|
|
|
|
SDL_GL_SwapWindow( window );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-30 12:08:18 +02:00
|
|
|
void T_Main::initProgram( )
|
2017-09-30 11:23:14 +02:00
|
|
|
{
|
2017-09-30 14:59:15 +02:00
|
|
|
spRaymarch = std::make_unique< T_ShaderProgram >(
|
2017-09-30 12:08:18 +02:00
|
|
|
GL_FRAGMENT_SHADER , watcher );
|
2017-09-30 14:59:15 +02:00
|
|
|
spRaymarch->addFile( "raymarch-header.glsl" );
|
|
|
|
spRaymarch->addFile( "map.glsl" );
|
|
|
|
spRaymarch->addFile( "raymarcher.glsl" );
|
|
|
|
spRaymarch->load( );
|
|
|
|
|
2017-09-30 17:58:48 +02:00
|
|
|
spCopy = std::make_unique< T_ShaderProgram >( GL_FRAGMENT_SHADER , watcher );
|
2017-09-30 14:59:15 +02:00
|
|
|
spCopy->addFile( "copy.glsl" );
|
|
|
|
spCopy->load( );
|
2017-09-30 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
int main( int , char** )
|
|
|
|
{
|
|
|
|
T_Main m;
|
|
|
|
m.mainLoop( );
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Frame time history
|
|
|
|
const int nFrameTimes = 200;
|
|
|
|
float frameTimes[ nFrameTimes ];
|
|
|
|
memset( frameTimes , 0 , sizeof( float ) * nFrameTimes );
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
// Update frame time history
|
|
|
|
memmove( frameTimes , &frameTimes[ 1 ] ,
|
|
|
|
( nFrameTimes - 1 ) * sizeof( float ) );
|
|
|
|
frameTimes[ nFrameTimes - 1 ] = 1000.0f / ImGui::GetIO( ).Framerate;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|