Skip to content

Commit

Permalink
beginning support for physics
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Dec 10, 2023
1 parent 569e867 commit 020dbaa
Show file tree
Hide file tree
Showing 15 changed files with 541 additions and 70 deletions.
162 changes: 134 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# CMake entry point
cmake_minimum_required (VERSION 3.0)
project (engine)
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)

set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
if(MSVC)
set(_GLFW_WIN32, true)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
endif(MSVC)

set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)
project(engine)

if( CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR )
message( FATAL_ERROR "Please select another Build Directory" )
Expand All @@ -22,8 +13,45 @@ if( CMAKE_BINARY_DIR MATCHES " " )
message( "Your Build Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()

# configure compiler
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
set(_GLFW_WIN32, true)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
endif(MSVC)

#
# configuration flags
#
set(GENERATE_DEBUG_SYMBOLS ON)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
set(DOUBLE_PRECISION OFF)

# opengl
set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)

# glm
add_compile_definitions(GLM_FORCE_SSE2)

# Jolt physics
set(CROSS_PLATFORM_DETERMINISTIC OFF) # deterministic across platforms
set(FLOATING_POINT_EXCEPTIONS_ENABLED OFF) # only on msvc
set(OBJECT_LAYER_BITS 16)
set(USE_SSE4_1 ON)
set(USE_SSE4_2 ON)
set(USE_AVX ON)
set(USE_AVX2 ON)
set(USE_AVX512 OFF)
set(USE_LZCNT ON)
set(USE_TZCNT ON)
set(USE_F16C ON)
set(USE_FMADD ON)

include_directories(
external/glfw-3.3.7/include/
external/glm-0.9.7.1/
Expand All @@ -39,26 +67,26 @@ include_directories(
)

# Compile external dependencies
add_subdirectory (external)
add_subdirectory(external)

set(ALL_LIBS
${OPENGL_LIBRARY}
glfw
GLEW_210
assimp
SOLOUD
Jolt
)


add_definitions(
-DTW_STATIC
-DTW_NO_LIB_PRAGMA
-DTW_NO_DIRECT3D
-DGLEW_STATIC
-D_CRT_SECURE_NO_WARNINGS
-DUSE_ASSIMP
)


set(SOURCES
src/controls/core.cpp
src/controls/behaviour.cpp
Expand All @@ -85,29 +113,106 @@ set(SOURCES
src/game_behaviour.cpp
src/lightmapper.cpp
src/graphics.cpp
src/physics.cpp

external/imgui/imgui_draw.cpp
external/imgui/imgui_impl_glfw.cpp
external/imgui/imgui_impl_opengl3.cpp
external/imgui/imgui_widgets.cpp
external/imgui/imgui_draw.cpp
external/imgui/imgui_impl_glfw.cpp
external/imgui/imgui_impl_opengl3.cpp
external/imgui/imgui_widgets.cpp
external/imgui/imgui_tables.cpp
external/imgui/imgui_stdlib.cpp
external/imgui/imgui.cpp
external/imgui/imgui_stdlib.cpp
external/imgui/imgui.cpp
external/xatlas/xatlas.cpp

src/main.cpp
)

# YCM generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable (engine ${SOURCES})
if (MSVC)
# 64 bit architecture
set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")

# Set runtime library
if (USE_STATIC_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

# Set general compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /Wall /WX /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")

# Optionally generate debug symbols
if (GENERATE_DEBUG_SYMBOLS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
endif()

# Remove any existing compiler flag that enables RTTI
string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

# Set compiler flag for disabling RTTI
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")

if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
# On ARM the exception handling flag is missing which causes warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
endif()

# Set compiler flags for various configurations
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /GS /Od /Ob0 /RTC1")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GS- /Gy /O2 /Oi /Ot")
set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_DISTRIBUTION} /GS- /Gy /O2 /Oi /Ot")

# Set linker flags
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
if (CROSS_PLATFORM_DETERMINISTIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:fast") # Clang doesn't use fast math because it cannot be turned off inside a single compilation unit
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
endif()
else()
# Set general compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Optionally generate debug symbols
if (GENERATE_DEBUG_SYMBOLS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
endif()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
# Also turn off automatic fused multiply add contractions, there doesn't seem to be a way to do this selectively through the macro JPH_PRECISE_MATH_OFF
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -ffp-contract=off")
else()
# Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")

# On clang 14 and later we can turn off float contraction through a pragma, older versions and deterministic versions need it off always, see Core.h
if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
endif()
endif()

# Set compiler flags for various configurations
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_DISTRIBUTION} -O3")

# Set linker flags
if (NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows"))
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
endif()
endif()
set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") # Set linker flags

target_link_libraries(engine
${ALL_LIBS}
)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # generate compile_commands.json

set_target_properties(engine PROPERTIES COMPILE_DEFINITIONS "USE_ASSIMP")
add_executable (engine ${SOURCES})
target_include_directories(engine PUBLIC ${JoltPhysics_SOURCE_DIR}/..)
target_link_libraries(engine ${ALL_LIBS})

# Copy data files, better to just simlink or copy manually
#if(MSVC)
Expand All @@ -117,6 +222,7 @@ set_target_properties(engine PROPERTIES COMPILE_DEFINITIONS "USE_ASSIMP")
#add_custom_target(copy-runtime-files ALL
# COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/data)
#endif()
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "engine")

# Copy compile_commands.json
if( EXISTS "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json" )
Expand Down
16 changes: 15 additions & 1 deletion external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ target_link_libraries(GLEW_210


### ASSIMP ###
# AssImp already has a CMakeLists.txt so let's use these
# AssImp already has a CMakeLists.txt so let's use theirs

set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF)
set(ASSIMP_BUILD_GLTF_IMPORTER ON)
Expand Down Expand Up @@ -145,3 +145,17 @@ if(UNIX)
endif(ALSA_FOUND)
add_compile_definitions(WITH_ALSA)
endif()

## Jolt
include (FetchContent)

set(INTERPROCEDURAL_OPTIMIZATION ON)

FetchContent_Declare(
JoltPhysics
GIT_REPOSITORY "https://github.com/jrouwe/JoltPhysics"
GIT_TAG "v4.0.1"
SOURCE_SUBDIR "Build"
)
FetchContent_MakeAvailable(JoltPhysics)
SET_INTERPROCEDURAL_OPTIMIZATION() # Enable link time optimization in Release and Distribution mode if requested and available
2 changes: 1 addition & 1 deletion external/assimp-5.1.5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ ELSEIF(MSVC)
ENDIF()
# disable "elements of array '' will be default initialized" warning on MSVC2013
IF(MSVC12)
ADD_COMPILE_OPTIONS(/wd4351)
ADD_COMPILE_OPTIONS(/wd4351)
ENDIF()
ADD_COMPILE_OPTIONS(/wd4244) #supress warning for double to float conversion if Double precission is activated
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D_DEBUG /Zi /Od")
Expand Down
2 changes: 1 addition & 1 deletion include/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ namespace Editor {
extern CopySelection copy_selection;
}

#endif // EDITOR_HPP
#endif // EDITOR_HPP
35 changes: 21 additions & 14 deletions include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

#include <glm/gtc/quaternion.hpp>

#include <Jolt/Jolt.h>
#include "Jolt/Physics/Body/BodyCreationSettings.h"

#include "globals.hpp"
#include "assets.hpp"
#include "physics.hpp"

#define NULLID Id(-1, -1)

Expand All @@ -22,7 +26,12 @@ struct Id {
return (i == other.i) && (v == other.v);
}
Id(int _i, int _v) : i(_i), v(_v) {}
Id(uint64_t phys_id) : i(phys_id >> 16), v(phys_id & 0xffff) {}
Id() : i(-1), v(-1) {}

constexpr uint64_t to_phys() {
return (i << 16) | i;
}
};

enum EntityType : uint64_t {
Expand All @@ -42,6 +51,9 @@ struct Entity {
};

struct MeshEntity : Entity {
JPH::BodyCreationSettings* body_settings = nullptr;
JPH::BodyID body_id = JPH::BodyID();

glm::vec3 position = glm::vec3(0.0);
glm::quat rotation = glm::quat();
glm::mat3 scale = glm::mat3(1.0);
Expand All @@ -58,6 +70,10 @@ struct MeshEntity : Entity {
MeshEntity(Id _id = NULLID) : Entity(_id) {
type = EntityType::MESH_ENTITY;
}
~MeshEntity() {
if (body_settings != nullptr)
delete body_settings;
}
};

struct PointLightEntity : Entity {
Expand Down Expand Up @@ -335,7 +351,7 @@ struct EntityManager {
inline void clear() {
// Delete entities
for (uint64_t i = 0; i < ENTITY_COUNT; i++) {
if (entities[i] != nullptr) free(entities[i]);
if (entities[i] != nullptr) delete entities[i];
entities[i] = nullptr;
}
memset(versions, 0, sizeof(versions));
Expand Down Expand Up @@ -387,7 +403,7 @@ struct EntityManager {
int i = delete_entity_stack.top();
delete_entity_stack.pop();
free_entity_stack.push(i);
if (entities[i] != nullptr) free(entities[i]);
if (entities[i] != nullptr) delete entities[i];
entities[i] = nullptr;
}
}
Expand All @@ -397,17 +413,8 @@ struct EntityManager {
setEntity(id.i, e);
return e;
}

void tickAnimatedMeshes(float dt, bool paused) {
for (int i = 0; i < ENTITY_COUNT; ++i) {
auto e = reinterpret_cast<AnimatedMeshEntity*>(entities[i]);

if (e != nullptr && entityInherits(e->type, EntityType::ANIMATED_MESH_ENTITY) && (!paused || e->draw_animated)) {
e->tick(dt);
continue;
}
}
}
};

#endif // ENTITIES_CORE_HPP
void tickEntities(EntityManager& entities, float dt, bool is_playing);

#endif // ENTITIES_CORE_HPP
5 changes: 4 additions & 1 deletion include/game_behaviour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
void pauseGame();
void playGame();
void resetGameState();
void updateGameEntities(float dt, EntityManager& entity_manager);

void initCameraMove(glm::vec3 origin, glm::vec3 target, float duration);
void updateCameraMove(float dt);
void updatePlayerEntity(EntityManager& entity_manager, float dt, PlayerEntity &player);

struct GameState {
Level level;
Expand Down
8 changes: 6 additions & 2 deletions include/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include "graphics.hpp"
#include "entities.hpp"
#include "assets.hpp"
#include "physics.hpp"

#include <Jolt/Jolt.h>
#include "Jolt/Physics/PhysicsSystem.h"
#include "Jolt/Physics/PhysicsScene.h"

struct FogProperties {
float anisotropy = 0.2;
Expand All @@ -28,12 +33,11 @@ struct Level {

EntityManager entities;
Camera camera;
Environment environment;

enum class Type : uint64_t {
BASIC = 0,
} type = Type::BASIC;

Environment environment;
};

extern Level loaded_level;
Expand Down
Loading

0 comments on commit 020dbaa

Please sign in to comment.