Moved camera to separate file

This commit is contained in:
Emmanuel BENOîT 2017-10-02 10:12:27 +02:00
parent e6e8b2c935
commit 080eb3b29b
7 changed files with 130 additions and 125 deletions

View file

@ -17,6 +17,7 @@ DEMO = \
texture.cc \
rendertarget.cc \
programs.cc \
camera.cc \
\
raymarcher.cc \
\

85
camera.cc Normal file
View file

@ -0,0 +1,85 @@
#include "externals.hh"
#include "camera.hh"
#include "utilities.hh"
/*= 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. );
}

43
camera.hh Normal file
View file

@ -0,0 +1,43 @@
#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
/*= T_Camera =================================================================*/
struct T_Camera
{
glm::vec3 lookAt;
glm::vec3 angles;
float distance = 10;
float fov = 90;
// Everything below is updated by update()
glm::vec3 dir;
glm::vec3 up;
glm::vec3 pos;
float np;
T_Camera()
{ update( ); }
void handleDND(
__rd__ ImVec2 const& move ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift ,
__rd__ const bool lmb // Left mouse button
);
void handleWheel(
__rd__ const float wheel ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift
);
void makeUI( );
private:
glm::mat3x3 rotMat;
float rotationMatrix[ 9 ];
void update( );
};

View file

@ -1,10 +1,6 @@
#include "externals.hh"
#include "imgui_impl_sdl.h"
#include "utilities.hh"
#include "programs.hh"
#include "texture.hh"
#include "rendertarget.hh"
#include "raymarcher.hh"
#include "dof.hh"
#include "bloom.hh"

View file

@ -2,6 +2,7 @@
#include "rendertarget.hh"
#include "programs.hh"
#include "camera.hh"
struct T_Raymarcher

View file

@ -155,85 +155,3 @@ bool T_WatchedFiles::watch(
}
return false;
}
/*= 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. );
}

View file

@ -105,42 +105,3 @@ struct T_WatchedFiles
bool triggered;
std::vector< int > identifiers;
};
/*= T_Camera =================================================================*/
struct T_Camera
{
glm::vec3 lookAt;
glm::vec3 angles;
float distance = 10;
float fov = 90;
// Everything below is updated by update()
glm::vec3 dir;
glm::vec3 up;
glm::vec3 pos;
float np;
T_Camera()
{ update( ); }
void handleDND(
__rd__ ImVec2 const& move ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift ,
__rd__ const bool lmb // Left mouse button
);
void handleWheel(
__rd__ const float wheel ,
__rd__ const bool hasCtrl ,
__rd__ const bool hasShift
);
void makeUI( );
private:
glm::mat3x3 rotMat;
float rotationMatrix[ 9 ];
void update( );
};