Skip to content

Commit

Permalink
Added GLM camera
Browse files Browse the repository at this point in the history
  • Loading branch information
emeiri committed Oct 10, 2024
1 parent e9ca885 commit 08e9d6a
Show file tree
Hide file tree
Showing 433 changed files with 64,251 additions and 0 deletions.
105 changes: 105 additions & 0 deletions Common/ogldev_glm_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2024 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This camera implementation is based on the "3D Graphics Rendering Cookbook"
*/

#include <algorithm>

#include "ogldev_glm_camera.h"

CameraPositionerFirstPerson::CameraPositionerFirstPerson(const glm::vec3& Pos, const glm::vec3& Target, const glm::vec3& Up)
{
m_cameraPos = Pos;

if (CAMERA_LEFT_HANDED) {
m_cameraOrientation = glm::lookAtLH(Pos, Target, Up);
} else {
m_cameraOrientation = glm::lookAtRH(Pos, Target, Up);
}
}


void CameraPositionerFirstPerson::Update(float dt, const glm::vec2& MousePos, bool MousePressed)
{
if (MousePressed) {
glm::vec2 DeltaMouse = MousePos - m_mousePos;

glm::quat DeltaQuat = glm::quat(glm::vec3(m_mouseSpeed * DeltaMouse.y, m_mouseSpeed * DeltaMouse.x, 0.0f));

m_cameraOrientation = glm::normalize(DeltaQuat * m_cameraOrientation);
}

m_mousePos = MousePos;

glm::mat4 v = glm::mat4_cast(m_cameraOrientation);

glm::vec3 Forward = -glm::vec3(v[0][2], v[1][2], v[2][2]);
glm::vec3 Right = glm::vec3(v[0][0], v[1][0], v[2][0]);
glm::vec3 Up = glm::cross(Right, Forward);

glm::vec3 Acceleration = glm::vec3(0.0f);

//printf("2 %d\n", m_movement.Forward);

if (m_movement.Forward) { Acceleration += Forward; }
if (m_movement.Backward) { Acceleration -= Forward; }
if (m_movement.Left) { Acceleration -= Right; }
if (m_movement.Right) { Acceleration += Right; }
if (m_movement.Up) { Acceleration += Up; }
if (m_movement.Down) { Acceleration -= Up; }
if (m_movement.FastSpeed) { Acceleration *= m_fastCoef; }

if (Acceleration == glm::vec3(0.0f)) {
m_moveSpeed -= m_moveSpeed * std::min(dt * 1.0f / m_damping, 1.0f);
} else {
m_moveSpeed = Acceleration * m_acceleration * dt;
float MaxSpeed = m_movement.FastSpeed ? m_maxSpeed * m_fastCoef : m_maxSpeed;

if (glm::length(m_moveSpeed) > m_maxSpeed) {
m_moveSpeed = glm::normalize(m_moveSpeed) * m_maxSpeed;
}
}

m_cameraPos += m_moveSpeed * dt;
}


glm::mat4 CameraPositionerFirstPerson::GetViewMatrix() const
{
glm::mat4 t = glm::translate(glm::mat4(1.0), -m_cameraPos);

glm::mat4 r = glm::mat4_cast(m_cameraOrientation);

glm::mat4 res = r * t;

return res;
}


void CameraPositionerFirstPerson::SetUpVector(const glm::vec3& Up)
{
glm::mat4 View = GetViewMatrix();
glm::vec3 Dir = -glm::vec3(View[0][2], View[1][2], View[2][2]);

if (CAMERA_LEFT_HANDED) {
m_cameraOrientation = glm::lookAtLH(m_cameraPos, m_cameraPos + Dir, Up);
}
else {
m_cameraOrientation = glm::lookAtRH(m_cameraPos, m_cameraPos + Dir, Up);
}
}
69 changes: 69 additions & 0 deletions Include/glm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
file(GLOB ROOT_SOURCE *.cpp)
file(GLOB ROOT_INLINE *.inl)
file(GLOB ROOT_HEADER *.hpp)
file(GLOB ROOT_TEXT ../*.txt)
file(GLOB ROOT_MD ../*.md)
file(GLOB ROOT_NAT ../util/glm.natvis)

file(GLOB_RECURSE CORE_SOURCE ./detail/*.cpp)
file(GLOB_RECURSE CORE_INLINE ./detail/*.inl)
file(GLOB_RECURSE CORE_HEADER ./detail/*.hpp)

file(GLOB_RECURSE EXT_SOURCE ./ext/*.cpp)
file(GLOB_RECURSE EXT_INLINE ./ext/*.inl)
file(GLOB_RECURSE EXT_HEADER ./ext/*.hpp)

file(GLOB_RECURSE GTC_SOURCE ./gtc/*.cpp)
file(GLOB_RECURSE GTC_INLINE ./gtc/*.inl)
file(GLOB_RECURSE GTC_HEADER ./gtc/*.hpp)

file(GLOB_RECURSE GTX_SOURCE ./gtx/*.cpp)
file(GLOB_RECURSE GTX_INLINE ./gtx/*.inl)
file(GLOB_RECURSE GTX_HEADER ./gtx/*.hpp)

file(GLOB_RECURSE SIMD_SOURCE ./simd/*.cpp)
file(GLOB_RECURSE SIMD_INLINE ./simd/*.inl)
file(GLOB_RECURSE SIMD_HEADER ./simd/*.h)

source_group("Text Files" FILES ${ROOT_TEXT} ${ROOT_MD})
source_group("Core Files" FILES ${CORE_SOURCE})
source_group("Core Files" FILES ${CORE_INLINE})
source_group("Core Files" FILES ${CORE_HEADER})
source_group("EXT Files" FILES ${EXT_SOURCE})
source_group("EXT Files" FILES ${EXT_INLINE})
source_group("EXT Files" FILES ${EXT_HEADER})
source_group("GTC Files" FILES ${GTC_SOURCE})
source_group("GTC Files" FILES ${GTC_INLINE})
source_group("GTC Files" FILES ${GTC_HEADER})
source_group("GTX Files" FILES ${GTX_SOURCE})
source_group("GTX Files" FILES ${GTX_INLINE})
source_group("GTX Files" FILES ${GTX_HEADER})
source_group("SIMD Files" FILES ${SIMD_SOURCE})
source_group("SIMD Files" FILES ${SIMD_INLINE})
source_group("SIMD Files" FILES ${SIMD_HEADER})

add_library(glm-header-only INTERFACE)
add_library(glm::glm-header-only ALIAS glm-header-only)

target_include_directories(glm-header-only INTERFACE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

if (GLM_BUILD_LIBRARY)
add_library(glm
${ROOT_TEXT} ${ROOT_MD} ${ROOT_NAT}
${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HEADER}
${CORE_SOURCE} ${CORE_INLINE} ${CORE_HEADER}
${EXT_SOURCE} ${EXT_INLINE} ${EXT_HEADER}
${GTC_SOURCE} ${GTC_INLINE} ${GTC_HEADER}
${GTX_SOURCE} ${GTX_INLINE} ${GTX_HEADER}
${SIMD_SOURCE} ${SIMD_INLINE} ${SIMD_HEADER}
)
add_library(glm::glm ALIAS glm)
target_link_libraries(glm PUBLIC glm-header-only)
else()
add_library(glm INTERFACE)
add_library(glm::glm ALIAS glm)
target_link_libraries(glm INTERFACE glm-header-only)
endif()
Loading

0 comments on commit 08e9d6a

Please sign in to comment.