demotool/utilities.cc

450 lines
10 KiB
C++

#include "externals.hh"
#include "utilities.hh"
void disableButton( )
{
ImGui::PushStyleColor( ImGuiCol_Button , ImColor( .3f , .3f , .3f ) );
ImGui::PushStyleColor( ImGuiCol_ButtonHovered , ImColor( .3f , .3f , .3f ) );
ImGui::PushStyleColor( ImGuiCol_ButtonActive , ImColor( .3f , .3f , .3f ) );
}
/*----------------------------------------------------------------------------*/
void updateAngle(
__rw__ float& initial ,
__rd__ const float delta
)
{
initial = fmod( initial + delta + 540 , 360 ) - 180;
}
void anglesToMatrix(
__rd__ float const* angles ,
__wr__ float* matrix )
{
float c[3] , s[3];
for ( int i = 0 ; i < 3 ; i ++ ) {
const float a = M_PI * angles[ i ] / 180;
c[i] = cos( a );
s[i] = sin( a );
}
matrix[0] = c[1]*c[2];
matrix[1] = s[0]*s[1]*c[2] - c[0]*s[2];
matrix[2] = s[0]*s[2] + c[0]*s[1]*c[2];
matrix[3] = c[1]*s[2];
matrix[4] = c[0]*c[2] + s[0]*s[1]*s[2];
matrix[5] = c[0]*s[1]*s[2] - s[0]*c[2];
matrix[6] = -s[1];
matrix[7] = s[0]*c[1];
matrix[8] = c[0]*c[1];
}
/*= T_FilesWatcher ===========================================================*/
T_FilesWatcher::T_FilesWatcher( )
: fd( inotify_init1( O_NONBLOCK ) )
{ }
T_FilesWatcher::T_FilesWatcher( T_FilesWatcher&& other ) noexcept
: fd( 0 ) , watched( std::move( other.watched ) )
{
std::swap( fd , other.fd );
other.watched.clear( );
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->watcher = this;
}
}
}
T_FilesWatcher::~T_FilesWatcher( )
{
if ( fd ) {
close( fd );
}
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->watcher = nullptr;
}
}
}
void T_FilesWatcher::check( )
{
for ( T_WatchedFiles* wf : watched ) {
if ( wf ) {
wf->triggered = false;
}
}
inotify_event ie;
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
continue;
}
for ( T_WatchedFiles* wf : watched ) {
if ( !wf || wf->triggered ) {
continue;
}
auto const& idl( wf->identifiers );
if ( find( idl , ie.wd ) != idl.end( ) ) {
wf->triggered = true;
wf->callback( );
}
}
}
}
/*= T_WatchedFiles ===========================================================*/
T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
: watcher( other.watcher ) , callback( other.callback ) ,
triggered( other.triggered ) ,
identifiers( std::move( other.identifiers ) )
{
if ( watcher ) {
other.watcher = nullptr;
*( find( watcher->watched , &other ) ) = this;
}
}
T_WatchedFiles::T_WatchedFiles(
__rw__ T_FilesWatcher& watcher ,
__rd__ const F_OnFileChanges callback )
: watcher( &watcher ) , callback( callback ) , triggered( false )
{
watcher.watched.push_back( this );
}
T_WatchedFiles::~T_WatchedFiles( )
{
clear( );
if ( watcher ) {
watcher->watched.erase( find( watcher->watched , this ) );
}
}
void T_WatchedFiles::clear( )
{
if ( watcher ) {
const auto fd( watcher->fd );
for ( int wd : identifiers ) {
inotify_rm_watch( fd , wd );
}
}
identifiers.clear( );
}
bool T_WatchedFiles::watch(
__rd__ std::string const& file )
{
static constexpr auto inFlags( IN_CLOSE_WRITE | IN_DELETE_SELF );
if ( watcher ) {
const auto wd( inotify_add_watch( watcher->fd ,
file.c_str( ) , inFlags ) );
if ( wd == -1 ) {
return false;
}
if ( find( identifiers , wd ) == identifiers.end( ) ) {
identifiers.push_back( wd );
}
return true;
}
return false;
}
/*= T_ShaderCode =============================================================*/
T_ShaderCode::T_ShaderCode(
__rd__ const int nparts )
: code( nparts , nullptr )
{ }
T_ShaderCode::~T_ShaderCode( )
{
for ( char* str : code ) {
delete[] str;
}
}
/*----------------------------------------------------------------------------*/
void T_ShaderCode::setPart(
__rd__ const int index ,
__rd__ char const* const string )
{
assert( code[ index ] == nullptr );
const int len( strlen( string ) + 1 );
char buffer[ 32 ];
const int extraLen( index == 0 ? 0
: snprintf( buffer , sizeof( buffer ) ,
"\n#line 0 %d\n" , index ) );
char* const output( new char[ extraLen + len ] );
if ( index != 0 ) {
memcpy( output , buffer , extraLen );
}
strcpy( output + extraLen , string );
code[ index ] = output;
}
void T_ShaderCode::setPart(
__rd__ const int index ,
__rd__ void const* const data ,
__rd__ const int size )
{
assert( code[ index ] == nullptr );
char buffer[ 32 ];
const int extraLen( index == 0 ? 0
: snprintf( buffer , sizeof( buffer ) ,
"\n#line 0 %d\n" , index ) );
char* const output( new char[ extraLen + size + 1 ] );
if ( index != 0 ) {
memcpy( output , buffer , extraLen );
}
memcpy( output + extraLen , data , size );
output[ extraLen + size ] = 0;
code[ index ] = output;
}
bool T_ShaderCode::loadPart(
__rd__ const int index ,
__rd__ std::string const& source ,
__rw__ std::vector< std::string >& errors )
{
assert( code[ index ] == nullptr );
FILE * f = fopen( source.c_str( ) , "r" );
if ( !f ) {
std::string error( "File not found: " );
error += source;
errors.push_back( error );
return false;
}
char buffer[ 32 ];
const int extraLen( index == 0 ? 0
: snprintf( buffer , sizeof( buffer ) ,
"\n#line 0 %d\n" , index ) );
fseek( f , 0 , SEEK_END );
const size_t size( ftell( f ) );
fseek( f , 0 , SEEK_SET );
char* const output( new char[ extraLen + size + 1 ] );
if ( index != 0 ) {
memcpy( output , buffer , extraLen );
}
if ( fread( output + extraLen , 1 , size , f ) != size ) {
fclose( f );
delete[] output;
std::string error( "Could not read file: " );
error += source;
errors.push_back( error );
return false;
}
output[ extraLen + size ] = 0;
fclose( f );
code[ index ] = output;
return true;
}
/*----------------------------------------------------------------------------*/
GLuint T_ShaderCode::createProgram(
__rd__ GLenum type ,
__rw__ std::vector< std::string >& errors ) const
{
GLenum sid = glCreateShaderProgramv( type , code.size( ) , &code[ 0 ] );
if ( sid == 0 ) {
errors.push_back( "Failed to create GL program" );
return sid;
}
int infoLogLength;
glGetProgramiv( sid , GL_INFO_LOG_LENGTH , &infoLogLength );
if ( infoLogLength ) {
char buffer[ infoLogLength + 1 ];
glGetProgramInfoLog( sid , infoLogLength , nullptr , buffer );
char* start( buffer );
char* found( strchr( buffer , '\n' ) );
while ( found ) {
*found = 0;
errors.push_back( start );
start = found + 1;
found = strchr( start , '\n' );
}
if ( start < &buffer[ infoLogLength - 1 ] ) {
errors.push_back( start );
}
}
int lnk;
glGetProgramiv( sid , GL_LINK_STATUS , &lnk );
if ( !lnk ) {
glDeleteProgram( sid );
return 0;
}
return sid;
}
/*= T_ShaderProgram ==========================================================*/
T_ShaderProgram::T_ShaderProgram(
__rd__ const GLenum programType ,
__rw__ T_FilesWatcher& watcher )
: files_( watcher , [this] { load( ); } ) ,
programType_( programType ) , program_( 0 )
{
}
T_ShaderProgram::~T_ShaderProgram( )
{
if ( program_ != 0 ) {
glDeleteProgram( program_ );
}
}
void T_ShaderProgram::addChunk(
__rd__ std::string const& string )
{
chunksOrFiles_.push_back( true );
parts_.push_back( string );
}
void T_ShaderProgram::addFile(
__rd__ std::string const& source )
{
chunksOrFiles_.push_back( false );
parts_.push_back( source );
}
void T_ShaderProgram::load( )
{
const auto n( parts_.size( ) );
errors_.clear( );
files_.clear( );
if ( program_ != 0 ) {
glDeleteProgram( program_ );
program_ = 0;
}
T_ShaderCode sc( n );
for ( auto i = 0u ; i < n ; i ++ ) {
if ( chunksOrFiles_[ i ] ) {
sc.setPart( i , parts_[ i ].c_str( ) );
} else if ( sc.loadPart( i , parts_[ i ].c_str( ) , errors_ ) ) {
files_.watch( parts_[ i ] );
}
}
if ( errors_.size( ) == 0 ) {
program_ = sc.createProgram( programType_ , errors_ );
}
if ( errors_.size( ) ) {
printf( "\n--------------------------------------------------------------------------------\n\n" );
for ( auto const& str : errors_ ) {
printf( "ERR: %s\n" , str.c_str( ) );
}
}
}
bool T_ShaderProgram::activate( ) const
{
if ( program_ != 0 ) {
glUseProgram( program_ );
}
return program_ != 0;
}
/*= T_Camera =================================================================*/
void T_Camera::handleDND(
__rd__ ImVec2 const& move ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift ,
__rd__ const bool lmb // Left mouse button
)
{
if ( move.x == 0 || move.y == 0 ) {
return;
}
const float fdx( move.x * .1f * ( hasCtrl ? 1.f : .1f ) );
const float fdy( move.y * .1f * ( hasCtrl ? 1.f : .1f ) );
if ( lmb && hasShift ) {
// Left mouse button, shift - move camera
const auto side( glm::normalize( glm::cross( up , dir ) ) );
lookAt += .01f * ( side * fdx + up * fdy );
} else if ( lmb ) {
// Left mouse button, no shift - change yaw/pitch
updateAngle( angles.y , fdx );
updateAngle( angles.x , fdy );
} else {
// Right mouse button - change roll
updateAngle( angles.z , fdx );
}
update( );
}
void T_Camera::handleWheel(
__rd__ const float wheel ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift
)
{
const float delta( wheel * ( hasCtrl ? 1.f : .1f) );
if ( hasShift ) {
fov = std::max( 1.f , std::min( 179.f , fov + delta ) );
} else {
distance = std::max( .01f , distance - delta );
}
update( );
}
/*----------------------------------------------------------------------------*/
void T_Camera::makeUI( )
{
if ( !ImGui::CollapsingHeader( "Camera" ) ) {
return;
}
const bool changed[] = {
ImGui::DragFloat3( "Look at" , &lookAt.x ) ,
ImGui::DragFloat( "Distance" , &distance , .1f ,
.1f , 1e8 , "%.1f" ) ,
ImGui::DragFloat3( "Angles" , &angles.x , .01f , -180 , 180 ) ,
ImGui::DragFloat( "FoV" , &fov , .01f , .01f , 179.9f )
};
for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) {
if ( changed[ i ] ) {
update( );
break;
}
}
}
/*----------------------------------------------------------------------------*/
void T_Camera::update( )
{
anglesToMatrix( &angles.x , &rotMat[ 0 ].x );
dir = glm::vec3( 0 , 0 , -distance ) * rotMat;
up = glm::vec3( 0 , 1 , 0 ) * rotMat;
pos = lookAt - dir;
np = 2 * tan( M_PI * ( 180. - fov ) / 360. );
}