build 181030. basic external views system

This commit is contained in:
tmj-fstate
2018-10-30 14:29:02 +01:00
parent 83f56b07f8
commit c59b53067a
12 changed files with 593 additions and 451 deletions

View File

@@ -14,44 +14,30 @@ http://mozilla.org/MPL/2.0/.
#include "utilities.h"
#include "Console.h"
#include "Timer.h"
#include "DynObj.h"
#include "MOVER.h"
//---------------------------------------------------------------------------
void TCamera::Init( Math3D::vector3 const &NPos, Math3D::vector3 const &NAngle, TCameraType const NType ) {
void TCamera::Init( Math3D::vector3 const &NPos, Math3D::vector3 const &NAngle/*, TCameraType const NType*/, TDynamicObject *Owner ) {
vUp = { 0, 1, 0 };
Velocity = { 0, 0, 0 };
Pitch = NAngle.x;
Yaw = NAngle.y;
Roll = NAngle.z;
Angle = NAngle;
Pos = NPos;
Type = NType;
m_owner = Owner;
};
void TCamera::Reset() {
Pitch = Yaw = Roll = 0;
Angle = {};
m_rotationoffsets = {};
};
void TCamera::OnCursorMove(double x, double y) {
/*
Yaw -= x;
while( Yaw > M_PI ) {
Yaw -= 2 * M_PI;
}
while( Yaw < -M_PI ) {
Yaw += 2 * M_PI;
}
Pitch -= y;
if (Type == tp_Follow) {
// jeżeli jazda z pojazdem ograniczenie kąta spoglądania w dół i w górę
Pitch = clamp( Pitch, -M_PI_4, M_PI_4 );
}
*/
m_rotationoffsets += glm::dvec3 { y, x, 0.0 };
}
@@ -76,19 +62,21 @@ TCamera::OnCommand( command_data const &Command ) {
case user_command::movehorizontalfast: {
auto const movespeed = (
Type == TCameraType::tp_Free ? runspeed :
Type == TCameraType::tp_Follow ? walkspeed :
0.0 );
m_owner == nullptr ? runspeed : // free roam
false == FreeFlyModeFlag ? walkspeed : // vehicle cab
0.0 ); // vehicle external
// if( movespeed == 0.0 ) { break; } // enable to fix external cameras in place
auto const speedmultiplier = (
( ( Type == TCameraType::tp_Free ) && ( Command.command == user_command::movehorizontalfast ) ) ?
( ( m_owner == nullptr ) && ( Command.command == user_command::movehorizontalfast ) ) ?
30.0 :
1.0 );
// left-right
auto const movexparam { Command.param1 };
// 2/3rd of the stick range enables walk speed, past that we lerp between walk and run speed
auto const movex { walkspeed + ( std::max( 0.0, std::abs( movexparam ) - 0.65 ) / 0.35 ) * ( movespeed - walkspeed ) };
auto const movex { walkspeed + ( std::max( 0.0, std::abs( movexparam ) - 0.65 ) / 0.35 ) * std::max( 0.0, movespeed - walkspeed ) };
m_moverate.x = (
movexparam > 0.0 ? movex * speedmultiplier :
@@ -97,7 +85,7 @@ TCamera::OnCommand( command_data const &Command ) {
// forward-back
double const movezparam { Command.param2 };
auto const movez { walkspeed + ( std::max( 0.0, std::abs( movezparam ) - 0.65 ) / 0.35 ) * ( movespeed - walkspeed ) };
auto const movez { walkspeed + ( std::max( 0.0, std::abs( movezparam ) - 0.65 ) / 0.35 ) * std::max( 0.0, movespeed - walkspeed ) };
// NOTE: z-axis is flipped given world coordinate system
m_moverate.z = (
movezparam > 0.0 ? -movez * speedmultiplier :
@@ -111,15 +99,16 @@ TCamera::OnCommand( command_data const &Command ) {
case user_command::moveverticalfast: {
auto const movespeed = (
Type == TCameraType::tp_Free ? runspeed * 0.5 :
Type == TCameraType::tp_Follow ? walkspeed :
0.0 );
m_owner == nullptr ? runspeed * 0.5 : // free roam
false == FreeFlyModeFlag ? walkspeed : // vehicle cab
0.0 ); // vehicle external
// if( movespeed == 0.0 ) { break; } // enable to fix external cameras in place
auto const speedmultiplier = (
( ( Type == TCameraType::tp_Free ) && ( Command.command == user_command::moveverticalfast ) ) ?
( ( m_owner == nullptr ) && ( Command.command == user_command::moveverticalfast ) ) ?
10.0 :
1.0 );
// up-down
auto const moveyparam { Command.param1 };
// 2/3rd of the stick range enables walk speed, past that we lerp between walk and run speed
@@ -145,9 +134,6 @@ TCamera::OnCommand( command_data const &Command ) {
void TCamera::Update()
{
if( FreeFlyModeFlag == true ) { Type = TCameraType::tp_Free; }
else { Type = TCameraType::tp_Follow; }
// check for sent user commands
// NOTE: this is a temporary arrangement, for the transition period from old command setup to the new one
// ultimately we'll need to track position of camera/driver for all human entities present in the scenario
@@ -161,8 +147,28 @@ void TCamera::Update()
auto const deltatime { Timer::GetDeltaRenderTime() }; // czas bez pauzy
// update rotation
auto const rotationfactor { std::min( 1.0, 20 * deltatime ) };
Angle.y -= m_rotationoffsets.y * rotationfactor;
m_rotationoffsets.y *= ( 1.0 - rotationfactor );
while( Angle.y > M_PI ) {
Angle.y -= 2 * M_PI;
}
while( Angle.y < -M_PI ) {
Angle.y += 2 * M_PI;
}
Angle.x -= m_rotationoffsets.x * rotationfactor;
m_rotationoffsets.x *= ( 1.0 - rotationfactor );
if( m_owner != nullptr ) {
// jeżeli jazda z pojazdem ograniczenie kąta spoglądania w dół i w górę
Angle.x = clamp( Angle.x, -M_PI_4, M_PI_4 );
}
// update position
if( ( Type == TCameraType::tp_Free )
if( ( m_owner == nullptr )
|| ( false == Global.ctrlState )
|| ( true == DebugCameraFlag ) ) {
// ctrl is used for mirror view, so we ignore the controls when in vehicle if ctrl is pressed
@@ -171,46 +177,34 @@ void TCamera::Update()
Velocity.z = clamp( Velocity.z + m_moverate.z * 10.0 * deltatime, -std::abs( m_moverate.z ), std::abs( m_moverate.z ) );
Velocity.y = clamp( Velocity.y + m_moverate.y * 10.0 * deltatime, -std::abs( m_moverate.y ), std::abs( m_moverate.y ) );
}
if( ( Type == TCameraType::tp_Free )
if( ( m_owner == nullptr )
|| ( true == DebugCameraFlag ) ) {
// free movement position update is handled here, movement while in vehicle is handled by train update
Math3D::vector3 Vec = Velocity;
Vec.RotateY( Yaw );
Pos += Vec * 5.0 * deltatime;
// free movement position update
auto movement { Velocity };
movement.RotateY( Angle.y );
Pos += movement * 5.0 * deltatime;
}
// update rotation
auto const rotationfactor { std::min( 1.0, 20 * deltatime ) };
else {
// attached movement position update
auto movement { Velocity * -2.0 };
movement.y = -movement.y;
if( m_owner->MoverParameters->ActiveCab < 0 ) {
movement *= -1.f;
movement.y = -movement.y;
}
movement.RotateY( Angle.y );
Yaw -= rotationfactor * m_rotationoffsets.y;
m_rotationoffsets.y *= ( 1.0 - rotationfactor );
while( Yaw > M_PI ) {
Yaw -= 2 * M_PI;
m_owneroffset += movement * deltatime;
}
while( Yaw < -M_PI ) {
Yaw += 2 * M_PI;
}
Pitch -= rotationfactor * m_rotationoffsets.x;
m_rotationoffsets.x *= ( 1.0 - rotationfactor );
if( Type == TCameraType::tp_Follow ) {
// jeżeli jazda z pojazdem ograniczenie kąta spoglądania w dół i w górę
Pitch = clamp( Pitch, -M_PI_4, M_PI_4 );
}
}
Math3D::vector3 TCamera::GetDirection() {
return glm::normalize( glm::rotateY<float>( glm::vec3{ 0.f, 0.f, 1.f }, Yaw ) );
}
bool TCamera::SetMatrix( glm::dmat4 &Matrix ) {
Matrix = glm::rotate( Matrix, -Roll, glm::dvec3( 0.0, 0.0, 1.0 ) ); // po wyłączeniu tego kręci się pojazd, a sceneria nie
Matrix = glm::rotate( Matrix, -Pitch, glm::dvec3( 1.0, 0.0, 0.0 ) );
Matrix = glm::rotate( Matrix, -Yaw, glm::dvec3( 0.0, 1.0, 0.0 ) ); // w zewnętrznym widoku: kierunek patrzenia
Matrix = glm::rotate( Matrix, -Angle.z, glm::dvec3( 0.0, 0.0, 1.0 ) ); // po wyłączeniu tego kręci się pojazd, a sceneria nie
Matrix = glm::rotate( Matrix, -Angle.x, glm::dvec3( 1.0, 0.0, 0.0 ) );
Matrix = glm::rotate( Matrix, -Angle.y, glm::dvec3( 0.0, 1.0, 0.0 ) ); // w zewnętrznym widoku: kierunek patrzenia
if( ( Type == TCameraType::tp_Follow ) && ( false == DebugCameraFlag ) ) {
if( ( m_owner != nullptr ) && ( false == DebugCameraFlag ) ) {
Matrix *= glm::lookAt(
glm::dvec3{ Pos },
@@ -226,14 +220,14 @@ bool TCamera::SetMatrix( glm::dmat4 &Matrix ) {
void TCamera::RaLook()
{ // zmiana kierunku patrzenia - przelicza Yaw
Math3D::vector3 where = LookAt - Pos + Math3D::vector3(0, 3, 0); // trochę w górę od szyn
Math3D::vector3 where = LookAt - Pos /*+ Math3D::vector3(0, 3, 0)*/; // trochę w górę od szyn
if( ( where.x != 0.0 ) || ( where.z != 0.0 ) ) {
Yaw = atan2( -where.x, -where.z ); // kąt horyzontalny
Angle.y = atan2( -where.x, -where.z ); // kąt horyzontalny
m_rotationoffsets.y = 0.0;
}
double l = Math3D::Length3(where);
if( l > 0.0 ) {
Pitch = asin( where.y / l ); // kąt w pionie
Angle.x = asin( where.y / l ); // kąt w pionie
m_rotationoffsets.x = 0.0;
}
};