From bb6eab5fdbdd185f306c4748beb757e587c59cc6 Mon Sep 17 00:00:00 2001 From: det-fys Date: Fri, 8 Dec 2023 18:28:01 +0100 Subject: [PATCH] Add project files. --- .gitattributes | 63 +++++ .gitignore | 368 ++++++++++++++++++++++++ README.md | 1 + src/tsr/assets.cpp | 2 + src/tsr/assets.hpp | 49 ++++ src/tsr/camera.cpp | 81 ++++++ src/tsr/camera.hpp | 33 +++ src/tsr/filesystem.cpp | 115 ++++++++ src/tsr/filesystem.hpp | 32 +++ src/tsr/ia.cpp | 159 +++++++++++ src/tsr/ia.hpp | 57 ++++ src/tsr/model.cpp | 128 +++++++++ src/tsr/model.hpp | 65 +++++ src/tsr/renderer.cpp | 585 +++++++++++++++++++++++++++++++++++++++ src/tsr/renderer.hpp | 317 +++++++++++++++++++++ src/tsr/rendersystem.cpp | 26 ++ src/tsr/rendersystem.hpp | 25 ++ src/tsr/systems.hpp | 3 + src/tsr/tsr.hpp | 45 +++ src/tsr/window.cpp | 90 ++++++ src/tsr/window.hpp | 72 +++++ src/tsr/world.cpp | 6 + src/tsr/world.hpp | 33 +++ src/tsrecs.cpp | 143 ++++++++++ tsrecs.sln | 31 +++ tsrecs.vcxproj | 159 +++++++++++ tsrecs.vcxproj.filters | 84 ++++++ 27 files changed, 2772 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 src/tsr/assets.cpp create mode 100644 src/tsr/assets.hpp create mode 100644 src/tsr/camera.cpp create mode 100644 src/tsr/camera.hpp create mode 100644 src/tsr/filesystem.cpp create mode 100644 src/tsr/filesystem.hpp create mode 100644 src/tsr/ia.cpp create mode 100644 src/tsr/ia.hpp create mode 100644 src/tsr/model.cpp create mode 100644 src/tsr/model.hpp create mode 100644 src/tsr/renderer.cpp create mode 100644 src/tsr/renderer.hpp create mode 100644 src/tsr/rendersystem.cpp create mode 100644 src/tsr/rendersystem.hpp create mode 100644 src/tsr/systems.hpp create mode 100644 src/tsr/tsr.hpp create mode 100644 src/tsr/window.cpp create mode 100644 src/tsr/window.hpp create mode 100644 src/tsr/world.cpp create mode 100644 src/tsr/world.hpp create mode 100644 src/tsrecs.cpp create mode 100644 tsrecs.sln create mode 100644 tsrecs.vcxproj create mode 100644 tsrecs.vcxproj.filters diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c643b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,368 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + + +#TSR + +main/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f006d8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# TSR_ECS \ No newline at end of file diff --git a/src/tsr/assets.cpp b/src/tsr/assets.cpp new file mode 100644 index 0000000..a4f756e --- /dev/null +++ b/src/tsr/assets.cpp @@ -0,0 +1,2 @@ +#include "assets.hpp" +std::map> TSR::AssetMap::s_assets; diff --git a/src/tsr/assets.hpp b/src/tsr/assets.hpp new file mode 100644 index 0000000..d3beb37 --- /dev/null +++ b/src/tsr/assets.hpp @@ -0,0 +1,49 @@ +#pragma once +#include +#include +#include +#include +#include "tsr.hpp" + +namespace TSR { + template + using AssetPtr = std::shared_ptr; + + class Asset { + public: + virtual ~Asset() {} + }; + + class AssetMap { + static std::map> s_assets; + + public: + //AssetMap(Game& game_ref) : m_game_ref(game_ref) {} + + template + static AssetPtr Get(const std::string& name) { + try { + AssetPtr asset_ptr; + + auto asset_it = s_assets.find(name); + if (asset_it == s_assets.end() || asset_it->second.expired()) { + asset_ptr = AssetPtr(new T(name)); //std::make_shared(m_fs_ref, name); + s_assets[name] = asset_ptr; + } + else { + asset_ptr = asset_it->second.lock(); + } + + auto t_ptr = std::dynamic_pointer_cast(asset_ptr); + if (!t_ptr) Throw("(AM) Asset type mismatch"); + + return t_ptr; + } + catch (std::exception ex) { + Throw(std::string("(AM) Error loading \"") + name + std::string("\":\n") + std::string(ex.what())); + } + + } + }; + +} diff --git a/src/tsr/camera.cpp b/src/tsr/camera.cpp new file mode 100644 index 0000000..86adfdd --- /dev/null +++ b/src/tsr/camera.cpp @@ -0,0 +1,81 @@ +#include "camera.hpp" +#include + +using namespace TSR; + +void TSR::Camera::UpdateVectors() { + auto yaw_rad = glm::radians(yaw); + auto pitch_rad = glm::radians(pitch); + front_vector = glm::normalize(glm::vec3(cos(yaw_rad) * glm::cos(pitch_rad), glm::sin(pitch_rad), glm::sin(yaw_rad) * glm::cos(pitch_rad))); + forward_backward_vector = glm::normalize(glm::vec3(front_vector.x, 0.0f, front_vector.z)); + right_vector = glm::normalize(glm::cross(front_vector, world_up)); + up_vector = glm::normalize(glm::cross(right_vector, front_vector)); +} + +TSR::Camera::Camera(glm::vec3 i_position, float i_movement_speed, float i_mouse_sensitivity, float i_third_person_distance, float i_yaw, float i_pitch) : + position(i_position), + movement_speed(i_movement_speed), + mouse_sensitivity(i_mouse_sensitivity), + third_person_distance(i_third_person_distance), + yaw(i_yaw), + pitch(i_pitch), + world_up(glm::vec3(0.0f, 1.0f, 0.0f)) +{ + UpdateVectors(); +} + +glm::mat4 TSR::Camera::GetViewMatrix(bool firstperson) { + float space = 0.3f; + auto dist = firstperson ? 0.0f : third_person_distance; + + //if (world) { + // auto to_glm = position - front_vector * (third_person_distance + space); + // auto position_bt = btVector3(position.x, position.y, position.z); + // + // auto to_bt = btVector3(to_glm.x, to_glm.y, to_glm.z); + // btCollisionWorld::ClosestRayResultCallback ray_callback(position_bt, to_bt); + // world->rayTest(position_bt, to_bt, ray_callback); + + // if (ray_callback.hasHit()) { + // to_glm.x = ray_callback.m_hitPointWorld.x(); + // to_glm.y = ray_callback.m_hitPointWorld.y(); + // to_glm.z = ray_callback.m_hitPointWorld.z(); + + // dist = glm::distance(position, to_glm) - space; + // } + //} + + return glm::lookAt(position - front_vector * dist, position + front_vector, up_vector); +} + +void TSR::Camera::ProcessMovement(Movement direction, float time) { + float diff = movement_speed * time; + switch (direction) { + case Movement::FORWARD: + position += forward_backward_vector * diff; + break; + case Movement::BACKWARD: + position -= forward_backward_vector * diff; + break; + case Movement::RIGHT: + position += right_vector * diff; + break; + case Movement::LEFT: + position -= right_vector * diff; + break; + case Movement::UP: + position += world_up * diff; + break; + case Movement::DOWN: + position -= world_up * diff; + break; + } +} + +void TSR::Camera::ProcessMouse(float x_offset, float y_offset) { + yaw += x_offset * mouse_sensitivity; + //pitch = std::min(89.0f, std::max(-89.0f, pitch + y_offset * mouse_sensitivity)); + pitch = std::min(89.9f, std::max(-89.9f, pitch + y_offset * mouse_sensitivity)); + + UpdateVectors(); +} diff --git a/src/tsr/camera.hpp b/src/tsr/camera.hpp new file mode 100644 index 0000000..f89203a --- /dev/null +++ b/src/tsr/camera.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +namespace TSR { + + class Camera { + glm::vec3 forward_backward_vector; + glm::vec3 world_up; + void UpdateVectors(); + + public: + glm::vec3 front_vector; + glm::vec3 up_vector; + glm::vec3 right_vector; + + enum class Movement { FORWARD, BACKWARD, LEFT, RIGHT, UP, DOWN }; + + glm::vec3 position; + float yaw; + float pitch; + float movement_speed; + float mouse_sensitivity; + float third_person_distance; + + Camera(glm::vec3 i_position = glm::vec3(0.0f), float i_movement_speed = 20.0f, float i_mouse_sensitivity = 0.1f, float i_third_person_distance = 0.0f, float i_yaw = 0.0f, float i_pitch = 0.0f); + glm::mat4 GetViewMatrix(bool firstperson = false); + void ProcessMovement(Movement direction, float time); + void ProcessMouse(float x_offset, float y_offset); + + }; +} \ No newline at end of file diff --git a/src/tsr/filesystem.cpp b/src/tsr/filesystem.cpp new file mode 100644 index 0000000..1a03318 --- /dev/null +++ b/src/tsr/filesystem.cpp @@ -0,0 +1,115 @@ +#include "filesystem.hpp" +#include +#include + +using namespace TSR; + +std::filesystem::path Filesystem::s_main_dir; +Filesystem::FileIndex Filesystem::s_file_index; + +void Filesystem::NormalizeFileName(std::string& file_name) { + std::transform(file_name.begin(), file_name.end(), file_name.begin(), [](unsigned char c) -> unsigned char { + if (c == '\\') return '/'; + return std::tolower(c); + }); +} + +void Filesystem::Init(const std::string& directory) { + if (!std::filesystem::is_directory(directory)) throw std::runtime_error("(FS) Main directory does not exist!"); + s_main_dir = std::filesystem::absolute(directory); + s_file_index.clear(); + + TsrPrintf("(FS) ---------- INIT ----------\n"); + + for (auto& p : std::filesystem::recursive_directory_iterator(directory)) { + if (!p.is_regular_file()) continue; + + if (p.path().extension() == ".tsrp") { + auto abs_path = std::filesystem::absolute(p.path()); + auto abs_path_str = abs_path.string(); + + try { + auto archive = std::make_shared(abs_path_str); + + archive->open(); + + auto entries = archive->getEntries(); + for (auto& entry : entries) { + if (!entry.isFile()) continue; + std::string name = entry.getName(); + NormalizeFileName(name); + if (s_file_index.count(name) > 0 && s_file_index[name] == nullptr) continue; + s_file_index[name] = archive; + } + + TsrPrintf("(FS) A %s\n", std::filesystem::relative(p.path(), s_main_dir).string().c_str()); + + } + catch (...) { + TsrPrintf("Could not open \"%s\"\n", abs_path_str.c_str()); + } + } + else { + auto path_str = std::filesystem::relative(p.path(), s_main_dir).string(); + NormalizeFileName(path_str); + s_file_index[path_str] = nullptr; + TsrPrintf("(FS) F %s\n", path_str.c_str()); + } + } + + TsrPrintf("(FS) ---------- FINISHED ----------\n"); +} + +std::string Filesystem::Read(std::string file_name, bool allow_empty) { + NormalizeFileName(file_name); + if (s_file_index.count(file_name) < 1) { + if (allow_empty) + return std::string(); + else + throw std::runtime_error(std::string("(FS) File not found: ") + file_name); + } + + auto& archive = s_file_index.at(file_name); + if (archive) { // File is in an archive + auto file = archive->getEntry(file_name, false, false); + if (file.isNull()) throw std::runtime_error("(FS) File is null"); + return file.readAsText(); + } + else { // File is physical + auto path = s_main_dir / std::filesystem::path(file_name); + std::ifstream t(path, std::ios::binary); + std::stringstream buffer; + buffer << t.rdbuf(); + return buffer.str(); + } +} + +//void TSR::Filesystem::Write(const std::string& name, const std::string& contents) { +// // TODO: implement +//} + +void Filesystem::Write(const std::string& name, const std::string& contents) { + std::filesystem::path path = s_main_dir / std::filesystem::path(name); + std::ofstream file(path, std::ios::binary | std::ios::trunc); + if (!file) { + Throw("(FS) Unable to open file for writing: " + name); + } + file << contents; + file.close(); +} + +std::string Filesystem::CheckFileName(std::string file_name) { + NormalizeFileName(file_name); + + auto entry = s_file_index.find(file_name); + + if (entry == s_file_index.end() || entry->second != nullptr) + throw std::runtime_error(std::string("(FS) File not found: ") + file_name); + + return (s_main_dir / std::filesystem::path(file_name)).string(); +} + +bool Filesystem::Exists(std::string file_name) { + NormalizeFileName(file_name); + return s_file_index.contains(file_name); +} diff --git a/src/tsr/filesystem.hpp b/src/tsr/filesystem.hpp new file mode 100644 index 0000000..0eebd56 --- /dev/null +++ b/src/tsr/filesystem.hpp @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include +#include "tsr.hpp" + +namespace TSR { + + class Filesystem { + using FileIndex = std::map>; + + static std::filesystem::path s_main_dir; + static FileIndex s_file_index; + + static void NormalizeFileName(std::string& file_name); + + public: + static void Init(const std::string& directory); + + static const FileIndex& GetFileIndex() { + return s_file_index; + } + + static std::string Read(std::string file_name, bool allow_empty = false); + static void Write(const std::string& name, const std::string& contents); + + static std::string CheckFileName(std::string file_name); + + static bool Exists(std::string file_name); + }; +} + diff --git a/src/tsr/ia.cpp b/src/tsr/ia.cpp new file mode 100644 index 0000000..0d45a02 --- /dev/null +++ b/src/tsr/ia.cpp @@ -0,0 +1,159 @@ +#include "ia.hpp" + +using namespace TSR; + +class ReadStream { + const char* const m_data; + const char* m_pos; + const char* const m_end; + +public: + ReadStream(const char* data, size_t size) : m_data(data), m_pos(data), m_end(m_pos + size) {} + + void Seek(size_t pos) { + m_pos = m_data + pos; + if (m_pos > m_end) + throw ParseException("Out of range"); + } + + template + const T& Read() { + if (m_pos + sizeof(T) > m_end) + throw ParseException("Out of range"); + + auto ret_pos = m_pos; + m_pos += sizeof(T); + + return *(T*)ret_pos; + } + + const void* ReadBlock(size_t size) { + auto start = m_pos; + m_pos += size; + + if (m_pos > m_end) + throw ParseException("Out of range"); + + return start; + } + + const char* ReadStr() { + auto start = m_pos; + + while (m_pos < m_end) { + if (*(m_pos++) == '\0') + return start; + } + + throw ParseException("String not terminated"); + } +}; + + +IAData TSR::ParseIA(const std::string& str, std::vector& bone_list) { + IAData data; + + ReadStream rs(str.data(), str.length()); + + data.is_skeletal = false; + int additional_vertex_size = 0; + + auto magic = rs.ReadStr(); + + if (!strcmp(magic, "IA8")) + data.stride = 8; + else if (!strcmp(magic, "IA2")) + data.stride = 2; + else if (!strcmp(magic, "IA3")) + data.stride = 3; + else if (!strcmp(magic, "SKIA84")) { + data.stride = 8; + data.is_skeletal = true; + additional_vertex_size = 20; + } + else + throw ParseException("Unsupported IA format"); + + // skip reserved header + rs.Seek(16); + + if (data.is_skeletal) { + auto num_bones = rs.Read(); + if (num_bones >= TSR_MAX_BONES) + throw ParseException("Max bones exceeded"); + + bone_list.resize(num_bones); + + for (auto& bone : bone_list) { + bone.name = rs.ReadStr(); + bone.offset = rs.Read(); + } + } + + data.num_vertices = rs.Read(); + data.vertices_size = data.num_vertices * (sizeof(float) * data.stride + additional_vertex_size); + data.vertices_ptr = rs.ReadBlock(data.vertices_size); + + data.num_indices = rs.Read(); + data.indices_size = data.num_indices * sizeof(uint32_t); + data.indices_ptr = rs.ReadBlock(data.indices_size); + + return data; +} + +void TSR::ParseSK(const std::string& str, std::vector& bone_list) { + ReadStream rs(str.data(), str.length()); + + auto magic = rs.ReadStr(); + + if (strcmp(magic, "SK") != 0) + throw ParseException("Unsupported SK format"); + + // header + rs.Seek(16); + + auto num_bones = rs.Read(); + if (num_bones >= TSR_MAX_BONES) + throw ParseException("Max bones exceeded"); + + bone_list.resize(num_bones); + + for (auto& bone : bone_list) { + bone.name = rs.ReadStr(); + bone.parent_idx = rs.Read(); + bone.transform = rs.Read(); + } +} + +void TSR::ParseSKAN(const std::string& str, std::vector& chan_list, size_t& duration, float& tps) { + ReadStream rs(str.data(), str.length()); + + auto magic = rs.ReadStr(); + + if (strcmp(magic, "SKAN") != 0) + throw ParseException("Unsupported SKAN format"); + + // header + rs.Seek(16); + + duration = rs.Read(); + tps = rs.Read(); + + auto num_channels = rs.Read(); + if (num_channels >= TSR_MAX_BONES) + throw ParseException("Max channels exceeded"); + + chan_list.resize(num_channels); + + for (auto& chan : chan_list) { + chan.name = rs.ReadStr(); + chan.frames.resize(duration + 1); + + for (auto& frame : chan.frames) { + frame.pos = rs.Read(); + frame.rot = rs.Read(); + frame.scl = rs.Read(); + } + } +} + diff --git a/src/tsr/ia.hpp b/src/tsr/ia.hpp new file mode 100644 index 0000000..8e55192 --- /dev/null +++ b/src/tsr/ia.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include "tsr.hpp" + +// PARSERS + +namespace TSR { + + class ParseException : public std::exception { + public: + ParseException(const char* what) : std::exception(what) {} + }; + + struct BoneInfo { + std::string name; + glm::mat4 transform; + int parent_idx; + }; + + struct BoneRef { + std::string name; + glm::mat4 offset; + }; + + struct SkAnimFrame { + glm::vec3 pos; + glm::quat rot; + glm::vec3 scl; + }; + + struct SkAnimChannel { + std::string name; + std::vector frames; + }; + + //struct SkAnim { + // std::vector channels; + // size_t duration; + // float tps; + //}; + + + struct IAData { + size_t stride; + const void* vertices_ptr; + size_t vertices_size; + size_t num_vertices; + const void* indices_ptr; + size_t indices_size; + size_t num_indices; + bool is_skeletal; + }; + + IAData ParseIA(const std::string& str, std::vector& bone_list); + void ParseSK(const std::string& str, std::vector& bone_list); + void ParseSKAN(const std::string& str, std::vector& chan_list, size_t& duration, float& tps); +} diff --git a/src/tsr/model.cpp b/src/tsr/model.cpp new file mode 100644 index 0000000..98e62de --- /dev/null +++ b/src/tsr/model.cpp @@ -0,0 +1,128 @@ +#include "model.hpp" + +using namespace TSR; + +Skeleton::Skeleton(const std::string& name) { + auto sk_str = Filesystem::Read(name); + ParseSK(sk_str, m_bones); +} + +int TSR::Skeleton::GetBoneIdByName(const std::string& name) const { + for (int i = 0; i < m_bones.size(); ++i) { + if (m_bones[i].name == name) + return i; + } + + return TSR_NO_BONE; +} + +SkAnim::SkAnim(const std::string& name) { + auto skan_str = Filesystem::Read(name); + ParseSKAN(skan_str, m_channels, m_duration, m_tps); +} + +Model::Model(const std::string& name) { + + auto model_str = Filesystem::Read(name); + + try { + auto model_json = nlohmann::json::parse(model_str); + + if (model_json.contains("skeleton")) + m_skeleton = AssetMap::Get(model_json.at("skeleton").get()); + + const auto& parts_json = model_json.at("parts"); // .get>(); + for (const auto& part_json : parts_json) { + std::string type_str("basic"); + + if (part_json.count("type")) { + const auto& type_json = part_json.at("type"); + if (type_json.is_string()) type_json.get_to(type_str); + } + + Drawable d; + d.type = RT_BASIC; + d.texture = AssetMap::Get(part_json.at("texture").get()); + d.mesh = AssetMap::Get(part_json.at("mesh").get()); + d.color = glm::vec3(1.0f); + d.uv_mult = 1.0f; + d.xz_mult = glm::vec2(1.0f); + + if (part_json.contains("texture1")) + d.texture1 = AssetMap::Get(part_json.at("texture1").get()); + + if (part_json.contains("texture2")) + d.texture2 = AssetMap::Get(part_json.at("texture2").get()); + + if (part_json.contains("uv_mult")) + part_json.at("uv_mult").get_to(d.uv_mult); + + if (part_json.contains("xz_mult1")) + part_json.at("xz_mult1").get_to(d.xz_mult[0]); + + if (part_json.contains("xz_mult2")) + part_json.at("xz_mult2").get_to(d.xz_mult[1]); + + // TYPE + if (type_str == "B_DOUBLESIDED") { + d.type = RT_BASIC_DOUBLESIDED; + } + else if (type_str == "UV_TINT_UV") { + d.type = RT_UV_TINT_UV; + } + else if (type_str == "XZ") { + d.type = RT_XZ; + } + else if (type_str == "XZ_XZ_MASK_UV") { + d.type = RT_XZ_XZ_MASK_UV; + } + else if (type_str == "SKELETAL") { + d.type = RT_SKELETAL; + } + + // BONE + if (part_json.contains("bone")) { + if (!m_skeleton) Throw("Part is bound to a bone but the model has no skeleton"); + d.bone_id = m_skeleton->GetBoneIdByName(part_json.at("bone").get()); + } + else { + d.bone_id = TSR_NO_BONE; + } + + // SKELETAL BONES + if (d.mesh->IsSkeletal()) { + if (!m_skeleton) Throw("Skeletal mesh in a model with no skeleton"); + const auto& mesh_bones = d.mesh->GetBones(); + for (const auto& bone : mesh_bones) { + d.bone_ids.push_back(m_skeleton->GetBoneIdByName(bone.name)); + } + } + + m_parts.push_back(d); + + if (part_json.contains("name")) + m_part_names.insert({ part_json.at("name").get(), m_parts.size() - 1 }); + } + + const auto& anims_json = model_json.at("animations"); + for (const auto& anim_json : anims_json) { + m_animations.push_back(Animation()); + auto idx = m_animations.size() - 1; + auto& anim = m_animations[idx]; + m_anim_names.insert({ anim_json.at("name").get(), idx }); + + if (anim_json.contains("skel")) { + if (!m_skeleton) Throw("Skeletal animation in a model with no skeleton"); + anim.skel = AssetMap::Get(anim_json.at("skel").get()); + for (const auto& chan : anim.skel->Channels()) + anim.bone_ids.push_back(m_skeleton->GetBoneIdByName(chan.name)); + } + + // events + } + } + catch (nlohmann::json::exception ex) { + Throw(ex.what()); + } + +} diff --git a/src/tsr/model.hpp b/src/tsr/model.hpp new file mode 100644 index 0000000..31be706 --- /dev/null +++ b/src/tsr/model.hpp @@ -0,0 +1,65 @@ +#pragma once +#include "assets.hpp" +#include "renderer.hpp" + +namespace TSR { + + class Skeleton : public Asset { + std::vector m_bones; + + public: + Skeleton(const std::string& name); + const std::vector& Bones() const { return m_bones; } + int GetBoneIdByName(const std::string& name) const; + }; + + class SkAnim : public Asset { + std::vector m_channels; + size_t m_duration; + float m_tps; + + public: + SkAnim(const std::string& name); + const std::vector& Channels() const { return m_channels; } + size_t Duration() const { return m_duration; } + float TPS() const { return m_tps; } + + }; + + struct Animation { + AssetPtr skel; + std::vector bone_ids; + /* events */ + }; + + class Model : public Asset { + AssetPtr m_skeleton; + + std::vector m_parts; + std::map m_part_names; + + std::vector m_animations; + std::map m_anim_names; + + public: + + Model(const std::string& name); + Model(const Model&) = delete; + Model(Model&&) = delete; + + void Draw(Renderer& renderer_ref, const DrawCtx* draw_context_ptr) const { + for (const auto& part : m_parts) + renderer_ref.Add(DrawRef(&part, draw_context_ptr)); + } + + void DrawInstanced(Renderer& renderer_ref, const DrawCtx* draw_context_ptr) const { + for (const auto& part : m_parts) + renderer_ref.AddInstanced(DrawRef(&part, draw_context_ptr)); + } + + const Skeleton& GetSkeleton() const { return *m_skeleton; } + int GetAnimId(const std::string& name) { return m_anim_names.at(name); } + const Animation& GetAnim(int id) { return m_animations[id]; } + }; + +} \ No newline at end of file diff --git a/src/tsr/renderer.cpp b/src/tsr/renderer.cpp new file mode 100644 index 0000000..bdc8a34 --- /dev/null +++ b/src/tsr/renderer.cpp @@ -0,0 +1,585 @@ +#include "renderer.hpp" +#include +#include +#include +#include "window.hpp" + +using namespace TSR; + +RenderType Renderer::s_rt_instanced_map[RT_COUNT] = { + RT_NONE, //RT_NONE + RT_BASIC_INSTANCED, //RT_BASIC + RT_BASIC_DOUBLESIDED_INSTANCED, //RT_BASIC_DOUBLESIDED + RT_NONE, //RT_BASIC_INSTANTIATED + RT_NONE, //RT_BASIC_DOUBLESIDED_INSTANTIATED + RT_UV_TINT_UV_INSTANCED, //RT_UV_TINT_UV + RT_NONE, //RT_UV_TINT_UV_INSTANCED + RT_NONE, //RT_XZ + RT_NONE, //RT_XZ_XZ_MASK_UV + RT_NONE, //RT_SKELETAL + RT_NONE, //RT_TRANSLUCENT +}; + +const char* Shader::s_uniform_names[SU_COUNT] = { + "u_tex", //SU_TEX + "u_tex1", //SU_TEX1 + "u_tex2", //SU_TEX2 + "u_tex3", //SU_TEX3 + "u_tex_pos", //SU_TEX_POS + "u_tex_normal", //SU_TEX_NORMAL + "u_tex_light", //SU_TEX_LIGHT + "u_vp", //SU_VP + "u_mvp", //SU_MVP + "u_model", //SU_MODEL + "u_no_light", //SU_NO_LIGHT + "u_alpha", //SU_ALPHA + "u_sun_color", //SU_SUN_COLOR + "u_sun_direction", //SU_SUN_DIRECTION + "u_ambient_color", //SU_AMBIENT_COLOR + "u_uv_mult", //SU_UV_MULT + "u_xz_mult", //SU_XZ_MULT + "u_bone_transforms",//SU_BONE_TRANSFORMS +}; + +Texture::Texture(const std::string& name) { + + GLint filter_min = GL_LINEAR_MIPMAP_LINEAR; + GLint filter_mag = GL_LINEAR; + GLint wrap_s = GL_REPEAT; + GLint wrap_t = GL_REPEAT; + bool compress = true; + + auto config_file = name + ".json"; + auto config_string = Filesystem::Read(config_file, true); + + if (!config_string.empty()) { + auto j_config = json::parse(config_string); + + auto ParseFilterType = [](const std::string& name) { + if (name == "LINEAR") + return GL_LINEAR; + else if (name == "NEAREST") + return GL_NEAREST; + else if (name == "LINEAR_MIPMAP_LINEAR") + return GL_LINEAR_MIPMAP_LINEAR; + else if (name == "LINEAR_MIPMAP_NEAREST") + return GL_LINEAR_MIPMAP_NEAREST; + else if (name == "NEAREST_MIPMAP_LINEAR") + return GL_NEAREST_MIPMAP_LINEAR; + else if (name == "NEAREST_MIPMAP_NEAREST") + return GL_NEAREST_MIPMAP_NEAREST; + else + return GL_LINEAR; + }; + + if (j_config.contains("filter_min")) + filter_min = ParseFilterType(j_config.at("filter_min").get()); + + if (j_config.contains("filter_mag")) + filter_mag = ParseFilterType(j_config.at("filter_mag").get()); + + if (j_config.contains("dxt_compress")) + j_config.at("dxt_compress").get_to(compress); + + if (j_config.contains("repeat_s")) + wrap_s = j_config.at("repeat_s").get() ? GL_REPEAT : GL_CLAMP_TO_EDGE; + + if (j_config.contains("repeat_t")) + wrap_t = j_config.at("repeat_t").get() ? GL_REPEAT : GL_CLAMP_TO_EDGE; + } + + auto image_string = Filesystem::Read(name); + + m_id = SOIL_load_OGL_texture_from_memory((const unsigned char*)image_string.c_str(), image_string.size(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, compress ? SOIL_FLAG_COMPRESS_TO_DXT : 0); + if (!m_id) throw std::runtime_error("Invalid image format"); + + glBindTexture(GL_TEXTURE_2D, m_id); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_min); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_mag); + + glGenerateMipmap(GL_TEXTURE_2D); + +} + +Texture::~Texture() { + glDeleteTextures(1, &m_id); +} + +template +void SetAttribPointer(unsigned int index, int size, size_t stride, const void* pointer) {} + +template <> +void SetAttribPointer(unsigned int index, int size, size_t stride, const void* pointer) { + glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride, pointer); +} + +template <> +void SetAttribPointer(unsigned int index, int size, size_t stride, const void* pointer) { + glVertexAttribIPointer(index, size, GL_UNSIGNED_BYTE, stride, pointer); +} + +template +static inline void SetAttribPointersImpl(size_t, size_t, size_t) {} + +template +static inline void SetAttribPointersImpl(size_t index, size_t offset, size_t stride, size_t size, Size... sizes) { + glEnableVertexAttribArray(index); + SetAttribPointer(index, size, stride, (void*)(offset)); + SetAttribPointersImpl(index + 1, offset + size * sizeof(T), stride, sizes...); +} + +template +static inline void SetAttribPointers(Size... sizes) { + size_t stride = 0U; + ((stride += sizes * sizeof(T)), ...); + SetAttribPointersImpl(0, 0, stride, sizes...); +} + +Mesh::Mesh(const std::string& name) { + auto file_str = Filesystem::Read(name); + + auto ia = ParseIA(file_str, m_bones); + + glGenVertexArrays(1, &m_vao); + glBindVertexArray(m_vao); + + glGenBuffers(1, &m_vbo); // Generate 1 buffer + glBindBuffer(GL_ARRAY_BUFFER, m_vbo); + glBufferData(GL_ARRAY_BUFFER, ia.vertices_size, ia.vertices_ptr, GL_STATIC_DRAW); + + glGenBuffers(1, &m_ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, ia.indices_size, ia.indices_ptr, GL_STATIC_DRAW); + + m_is_skeletal = ia.is_skeletal; + if (m_is_skeletal) { + if (ia.stride != 8) + Throw("Unsupported SKIA stride"); + + SetAttribPointers(3, 2, 3, 4, 4); + } + else { + if (ia.stride == 8) + SetAttribPointers(3, 2, 3); + else if (ia.stride == 3) + SetAttribPointers(3); + else if (ia.stride == 2) + SetAttribPointers(2); + else + Throw("Unsupported IA stride"); + } + + m_size = ia.num_indices; +} + +Mesh::~Mesh() { + glDeleteBuffers(1, &m_vbo); + glDeleteBuffers(1, &m_ebo); + glDeleteVertexArrays(1, &m_vao); +} + +InstancedSSBO::InstancedSSBO(const std::vector& data) : m_size(data.size()) { + glBindVertexArray(0); + + glGenBuffers(1, &m_id); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_id); + glBufferData(GL_SHADER_STORAGE_BUFFER, data.size() * sizeof(BasicDrawCtx), &data[0], GL_STATIC_DRAW); +} + +TSR::InstancedSSBO::~InstancedSSBO() { + glDeleteBuffers(1, &m_id); +} + +static GLuint CreateShader(const std::string& source_code, GLenum type) { + GLuint id = glCreateShader(type); + + const char* c_str = source_code.c_str(); + glShaderSource(id, 1, &c_str, NULL); + + glCompileShader(id); + + GLint is_compiled = 0; + glGetShaderiv(id, GL_COMPILE_STATUS, &is_compiled); + if (is_compiled == GL_FALSE) { + GLint max_length = 0; + glGetShaderiv(id, GL_INFO_LOG_LENGTH, &max_length); + + std::vector error_log(max_length); + glGetShaderInfoLog(id, max_length, &max_length, &error_log[0]); + + glDeleteShader(id); + + Throw(std::string("Could not compile shader: " + std::string(error_log.begin(), error_log.end()))); + } + + return id; +} + +static GLuint CreateProgram(GLuint vertex, GLuint fragment) { + GLuint id = glCreateProgram(); + glAttachShader(id, vertex); + glAttachShader(id, fragment); + glLinkProgram(id); + + GLint is_linked = 0; + glGetProgramiv(id, GL_LINK_STATUS, &is_linked); + if (is_linked == GL_FALSE) { + GLint max_length = 0; + glGetProgramiv(id, GL_INFO_LOG_LENGTH, &max_length); + + std::vector error_log(max_length); + glGetProgramInfoLog(id, max_length, &max_length, &error_log[0]); + + glDeleteProgram(id); + + Throw(std::string("Could not link program: " + std::string(error_log.begin(), error_log.end()))); + } + + return id; +} + +Shader::Shader(const std::string& vertex_code, const std::string& fragment_code, const char* nametag) { + GLuint vertex, fragment; + vertex = CreateShader(vertex_code, GL_VERTEX_SHADER); + fragment = CreateShader(fragment_code, GL_FRAGMENT_SHADER); + + try { + m_id = CreateProgram(vertex, fragment); + glDeleteShader(vertex); + glDeleteShader(fragment); + } + catch (std::runtime_error& e) { + glDeleteShader(vertex); + glDeleteShader(fragment); + throw; + } + + for (int i = 0; i < SU_COUNT; ++i) + m_uniforms[i] = GetUniformLocation(s_uniform_names[i]); + + //glBindFragDataLocation(m_id, 0, "FragColor"); + TsrPrintf("%s is program %u\n", nametag, m_id); + + glUseProgram(0); +} + +Shader::~Shader() { + glDeleteProgram(m_id); +} + + +void FramebuffersWrapper::CreateFramebufferTexture(GLuint& texture, GLint internalformat, GLenum format, GLenum type, GLenum attachment) { + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, m_width, m_height, 0, format, type, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, texture, 0); +} + +FramebuffersWrapper::FramebuffersWrapper(GLuint width, GLuint height) : m_width(width), m_height(height) { + // G-BUFFER + glGenFramebuffers(1, &m_fb_gb); + glBindFramebuffer(GL_FRAMEBUFFER, m_fb_gb); + CreateFramebufferTexture(m_tex_gb_color, GL_RGBA, GL_RGBA, GL_FLOAT, GL_COLOR_ATTACHMENT0); + CreateFramebufferTexture(m_tex_gb_normal, GL_RGBA16F, GL_RGBA, GL_FLOAT, GL_COLOR_ATTACHMENT1); + CreateFramebufferTexture(m_tex_gb_depth, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL_ATTACHMENT); + const GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; + glDrawBuffers(2, buffers); + + // Light framebuffer + glGenFramebuffers(1, &m_fb_lights); + glBindFramebuffer(GL_FRAMEBUFFER, m_fb_lights); + CreateFramebufferTexture(m_tex_light_mult, GL_RGBA, GL_RGBA, GL_FLOAT, GL_COLOR_ATTACHMENT0); + //CreateFramebufferTexture(m_tex_light_add, GL_RGBA, GL_RGBA, GL_FLOAT, GL_COLOR_ATTACHMENT1); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_tex_gb_depth, 0); +} + +FramebuffersWrapper::~FramebuffersWrapper() { + glDeleteTextures(1, &m_tex_gb_color); + glDeleteTextures(1, &m_tex_gb_normal); + glDeleteTextures(1, &m_tex_gb_depth); + + glDeleteTextures(1, &m_tex_light_mult); + //glDeleteTextures(1, &m_tex_light_add); + + glDeleteFramebuffers(1, &m_fb_gb); + glDeleteFramebuffers(1, &m_fb_lights); +} + +Renderer::Renderer() : + m_shader_basic(Filesystem::Read("shaders/basic.vs"), Filesystem::Read("shaders/basic.fs"), "BASIC"), + m_shader_basic_instanced(Filesystem::Read("shaders/basic_instanced.vs"), Filesystem::Read("shaders/basic.fs"), "BASIC_INSTANCED"), + m_shader_uv_tint_uv(Filesystem::Read("shaders/basic.vs"), Filesystem::Read("shaders/uv_tint_uv.fs"), "UV_TINT_UV"), + m_shader_uv_tint_uv_instanced(Filesystem::Read("shaders/basic_instanced.vs"), Filesystem::Read("shaders/uv_tint_uv.fs"), "UV_TINT_UV_INSTANCED"), + m_shader_screen(Filesystem::Read("shaders/deferred_quad.vs"), Filesystem::Read("shaders/deferred_quad.fs"), "SCREEN"), + m_shader_xz(Filesystem::Read("shaders/basic.vs"), Filesystem::Read("shaders/xz.fs"), "XZ"), + m_shader_xz_xz_mask_uv(Filesystem::Read("shaders/basic.vs"), Filesystem::Read("shaders/xz_xz_mask_uv.fs"), "XZ_XZ_MASK_UV"), + m_shader_skeletal(Filesystem::Read("shaders/skeletal.vs"), Filesystem::Read("shaders/skeletal.fs"), "SKELETAL"), + m_proj_matrix(1.0f), + m_view_matrix(1.0f), + m_viewport_buf(AssetMap::Get("viewport.ia2")), + m_last_width(0), + m_last_height(0) +{} + +void Renderer::Render(int width, int height) { + if (width && height) { + + if (m_last_width != width || m_last_height != height) { + m_fbs.reset(); + m_fbs = std::make_unique(width, height); + m_last_width = width; + m_last_height = height; + } + + glViewport(0, 0, width, height); + //auto aspect_ratio = (float)width / (float)height; + + //glm::vec3 sun_color(0.3f, 0.3f, 0.4f); + //glm::vec3 sun_direction(0.0f, 1.0f, 0.0f); + //glm::vec3 ambient_color = sun_color * 0.6f;//(1.0f, 1.0f, 0.8f); + //glm::vec3 farplane_color(0.1f, 0.1f, 0.2f); + + glm::vec3 sun_color(1.0f, 1.0f, 0.8f); + glm::vec3 sun_direction(0.0f, 1.0f, 0.0f); + glm::vec3 ambient_color = sun_color * 0.6f;//(1.0f, 1.0f, 0.8f); + glm::vec3 farplane_color(0.7f, 0.8f, 0.9f); + + sun_direction = glm::normalize(glm::vec3(0.5f, 1.0f, 0.7f)); + + m_fbs->BindGBuffer(); + glStencilMask(0xFF); + glClearStencil(0); + glClearColor(farplane_color.r, farplane_color.g, farplane_color.b, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + glEnable(GL_STENCIL_TEST); + + glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + glStencilFunc(GL_ALWAYS, 1, 0xFF); + glStencilMask(0xFF); + + auto mat_vp = m_proj_matrix * m_view_matrix; + + + // BASIC & DOUBLESIDED + m_shader_basic.Use(); + glUniformMatrix4fv(m_shader_basic.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_basic.U(SU_TEX), 0); + + auto DrawListBasic = [&](TSR::RenderType t) { + TSR::Texture* last_texture = nullptr; + + glm::mat4 model; + for (const auto& d : m_draw_lists[t]) { + model = d.context->model_matrix; + if (d.drawable->bone_id != TSR_NO_BONE) + model *= d.context->bone_transforms[d.drawable->bone_id]; + + glUniformMatrix4fv(m_shader_basic.U(SU_MODEL), 1, GL_FALSE, glm::value_ptr(model)); + + auto texture = d.drawable->texture.get(); + if (last_texture != texture) { + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + last_texture = texture; + } + + d.drawable->mesh->Draw(); + } + }; + + glEnable(GL_CULL_FACE); + DrawListBasic(RT_BASIC); + glDisable(GL_CULL_FACE); + DrawListBasic(RT_BASIC_DOUBLESIDED); + + glEnable(GL_CULL_FACE); + + // SKELETAL + if (!m_draw_lists[RT_SKELETAL].empty()) { + m_shader_skeletal.Use(); + glUniformMatrix4fv(m_shader_skeletal.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_skeletal.U(SU_TEX), 0); + + for (const auto& d : m_draw_lists[RT_SKELETAL]) { + glUniformMatrix4fv(m_shader_skeletal.U(SU_MODEL), 1, GL_FALSE, glm::value_ptr(d.context->model_matrix)); + + const auto& bone_ids = d.drawable->bone_ids; + auto num_bones = bone_ids.size(); + const auto& mesh_bones = d.drawable->mesh->GetBones(); + m_bone_transforms.resize(num_bones); + for (int i = 0; i < num_bones; ++i) { + m_bone_transforms[i] = d.context->bone_transforms[bone_ids[i]] * mesh_bones[i].offset; + } + + glUniformMatrix4fv(m_shader_skeletal.U(SU_BONE_TRANSFORMS), num_bones, GL_FALSE, glm::value_ptr(m_bone_transforms[0])); + + auto texture = d.drawable->texture.get(); + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + + d.drawable->mesh->Draw(); + } + } + + + // BASIC & DOUBLESIDED INSTANCED + if (!m_draw_lists[RT_BASIC_INSTANCED].empty() || !m_draw_lists[RT_BASIC_DOUBLESIDED_INSTANCED].empty()) { + m_shader_basic_instanced.Use(); + glUniformMatrix4fv(m_shader_basic_instanced.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_basic_instanced.U(SU_TEX), 0); + + auto DrawListBasicInstanced = [&](TSR::RenderType t) { + + for (const auto& d : m_draw_lists[t]) { + d.context->ssbo->Bind(); + + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + + d.drawable->mesh->DrawInstanced(d.context->ssbo->Size()); + } + }; + + glEnable(GL_CULL_FACE); + DrawListBasicInstanced(RT_BASIC_INSTANCED); + glDisable(GL_CULL_FACE); + DrawListBasicInstanced(RT_BASIC_DOUBLESIDED_INSTANCED); + } + + glEnable(GL_CULL_FACE); + + // UV_TINT_UV + if (!m_draw_lists[RT_UV_TINT_UV].empty()) { + m_shader_uv_tint_uv.Use(); + glUniformMatrix4fv(m_shader_uv_tint_uv.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_uv_tint_uv.U(SU_TEX), 0); + glUniform1i(m_shader_uv_tint_uv.U(SU_TEX1), 1); + + for (const auto& d : m_draw_lists[RT_UV_TINT_UV]) { + glUniformMatrix4fv(m_shader_basic.U(SU_MODEL), 1, GL_FALSE, glm::value_ptr(d.context->model_matrix)); + glUniform1f(m_shader_uv_tint_uv.U(SU_UV_MULT), d.drawable->uv_mult); + + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + glActiveTexture(GL_TEXTURE1); + d.drawable->texture1->Bind(); + + d.drawable->mesh->Draw(); + } + + } + + // UV_TINT_UV_INSTANCED + if (!m_draw_lists[RT_UV_TINT_UV_INSTANCED].empty()) { + m_shader_uv_tint_uv_instanced.Use(); + glUniformMatrix4fv(m_shader_uv_tint_uv_instanced.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_uv_tint_uv_instanced.U(SU_TEX), 0); + glUniform1i(m_shader_uv_tint_uv_instanced.U(SU_TEX1), 1); + + for (const auto& d : m_draw_lists[RT_UV_TINT_UV_INSTANCED]) { + d.context->ssbo->Bind(); + glUniform1f(m_shader_uv_tint_uv_instanced.U(SU_UV_MULT), d.drawable->uv_mult); + + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + glActiveTexture(GL_TEXTURE1); + d.drawable->texture1->Bind(); + + d.drawable->mesh->DrawInstanced(d.context->ssbo->Size()); + + } + + } + + // XZ + if (!m_draw_lists[RT_XZ].empty()) { + m_shader_xz.Use(); + glUniformMatrix4fv(m_shader_xz.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_xz.U(SU_TEX), 0); + + for (const auto& d : m_draw_lists[RT_XZ]) { + glUniformMatrix4fv(m_shader_basic.U(SU_MODEL), 1, GL_FALSE, glm::value_ptr(d.context->model_matrix)); + glUniform2fv(m_shader_xz.U(SU_XZ_MULT), 1, glm::value_ptr(d.drawable->xz_mult)); + + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + + d.drawable->mesh->Draw(); + } + + } + + // XZ_XZ_MASK_UV + if (!m_draw_lists[RT_XZ_XZ_MASK_UV].empty()) { + m_shader_xz_xz_mask_uv.Use(); + glUniformMatrix4fv(m_shader_xz_xz_mask_uv.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); + glUniform1i(m_shader_xz_xz_mask_uv.U(SU_TEX), 0); + glUniform1i(m_shader_xz_xz_mask_uv.U(SU_TEX1), 1); + glUniform1i(m_shader_xz_xz_mask_uv.U(SU_TEX2), 2); + + for (const auto& d : m_draw_lists[RT_XZ_XZ_MASK_UV]) { + glUniformMatrix4fv(m_shader_basic.U(SU_MODEL), 1, GL_FALSE, glm::value_ptr(d.context->model_matrix)); + glUniform2fv(m_shader_xz_xz_mask_uv.U(SU_XZ_MULT), 1, glm::value_ptr(d.drawable->xz_mult)); + + glActiveTexture(GL_TEXTURE0); + d.drawable->texture->Bind(); + glActiveTexture(GL_TEXTURE1); + d.drawable->texture1->Bind(); + glActiveTexture(GL_TEXTURE2); + d.drawable->texture2->Bind(); + + d.drawable->mesh->Draw(); + } + + } + + //glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + //glStencilFunc(GL_EQUAL, 0, 0xFF); + //glStencilMask(0x00); + + // draw skybox + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClear(GL_COLOR_BUFFER_BIT); + + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_STENCIL_TEST); + + m_shader_screen.Use(); + + glUniform3f(m_shader_screen.U(SU_AMBIENT_COLOR), ambient_color.r, ambient_color.g, ambient_color.b); + glUniform3f(m_shader_screen.U(SU_SUN_COLOR), sun_color.r, sun_color.g, sun_color.b); + glUniform3f(m_shader_screen.U(SU_SUN_DIRECTION), sun_direction.x, sun_direction.y, sun_direction.z); + + glUniform1i(m_shader_screen.U(SU_TEX), 0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fbs->GetGBColor()); + + glUniform1i(m_shader_screen.U(SU_TEX_POS), 1); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fbs->GetGBDepth()); + + glUniform1i(m_shader_screen.U(SU_TEX_NORMAL), 2); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fbs->GetGBNormal()); + + glUniform1i(m_shader_screen.U(SU_TEX_LIGHT), 3); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, m_fbs->GetLightMult()); + + m_viewport_buf->Draw(); + + } + + for (int i = 0; i < RT_COUNT; ++i) + m_draw_lists[i].clear(); +} diff --git a/src/tsr/renderer.hpp b/src/tsr/renderer.hpp new file mode 100644 index 0000000..eecdaa3 --- /dev/null +++ b/src/tsr/renderer.hpp @@ -0,0 +1,317 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include "filesystem.hpp" +#include "assets.hpp" +#include "ia.hpp" + +namespace TSR { + + enum RenderType { + RT_NONE, + RT_BASIC, + RT_BASIC_DOUBLESIDED, + RT_BASIC_INSTANCED, + RT_BASIC_DOUBLESIDED_INSTANCED, + RT_UV_TINT_UV, + RT_UV_TINT_UV_INSTANCED, + RT_XZ, + RT_XZ_XZ_MASK_UV, + RT_SKELETAL, + RT_TRANSLUCENT, + RT_COUNT + }; + + class Texture : public Asset { + GLuint m_id; + public: + Texture(const std::string& name); + Texture(const Texture&) = delete; + Texture(Texture&&) = delete; + ~Texture(); + + void Bind() const { glBindTexture(GL_TEXTURE_2D, m_id); } + GLuint GetRawId() const { return m_id; } + + }; + + class Mesh : public Asset { + GLuint m_vao; + GLuint m_vbo; + GLuint m_ebo; + GLuint m_size; + + bool m_is_skeletal; + std::vector m_bones; + + public: + Mesh(const std::string& name); + Mesh(const Mesh&) = delete; + Mesh(Mesh&&) = delete; + ~Mesh(); + + void Draw() const { + glBindVertexArray(m_vao); + glDrawElements(GL_TRIANGLES, m_size, GL_UNSIGNED_INT, 0); + } + + void DrawInstanced(size_t num) const { + glBindVertexArray(m_vao); + glDrawElementsInstanced(GL_TRIANGLES, m_size, GL_UNSIGNED_INT, 0, num); + } + + bool IsSkeletal() const { + return m_is_skeletal; + } + + const std::vector& GetBones() const { + return m_bones; + } + }; + + struct Drawable { + RenderType type; + + AssetPtr mesh; + + AssetPtr texture; + AssetPtr texture1; + AssetPtr texture2; + + glm::vec3 color; + + float uv_mult; + glm::vec2 xz_mult; + + int bone_id; + std::vector bone_ids; + }; + + struct BasicDrawCtx { + glm::mat4 model_matrix; + glm::vec4 colors[4]; + }; + + class InstancedSSBO { + GLuint m_id; + size_t m_size; + + public: + InstancedSSBO(const std::vector& data); + + void Bind() const { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_id); + } + + size_t Size() const { return m_size; } + + ~InstancedSSBO(); + }; + + struct DrawCtx : public BasicDrawCtx { + std::unique_ptr ssbo; + glm::mat4* bone_transforms; + //std::vector bone_transforms; + }; + + struct DrawRef { + const Drawable* drawable; + const DrawCtx* context; + + DrawRef(const Drawable* drawable, const DrawCtx* context) : drawable(drawable), context(context) {} + }; + + enum ShaderUniform { + SU_TEX, + SU_TEX1, + SU_TEX2, + SU_TEX3, + SU_TEX_POS, + SU_TEX_NORMAL, + SU_TEX_LIGHT, + SU_VP, + SU_MVP, + SU_MODEL, + SU_NO_LIGHT, + SU_ALPHA, + SU_SUN_COLOR, + SU_SUN_DIRECTION, + SU_AMBIENT_COLOR, + SU_UV_MULT, + SU_XZ_MULT, + SU_BONE_TRANSFORMS, + SU_COUNT + }; + + class Shader { + protected: + GLuint m_id; + std::string m_nametag; + + static const char* s_uniform_names[SU_COUNT]; + GLuint m_uniforms[SU_COUNT]; + + GLuint GetUniformLocation(const char* name) const { + return glGetUniformLocation(m_id, name); + } + + public: + Shader(const std::string& vertex_code, const std::string& fragment_code, const char* nametag = ""); + Shader(const Shader&) = delete; + Shader(Shader&&) = delete; + ~Shader(); + + void Use() const { + glUseProgram(m_id); + } + + GLuint U(ShaderUniform uni) const { + return m_uniforms[uni]; + } + + }; + + //class SceneShader : public Shader { + //public: + // const GLuint u_tex; + // const GLuint u_model; + // const GLuint u_view; + // const GLuint u_proj; + // const GLuint u_no_light; + // const GLuint u_alpha; + + // SceneShader(const std::string& vertex_code, const std::string& fragment_code, const char* nametag) : + // Shader(vertex_code, fragment_code, nametag), + // u_tex(GetUniformLocation("u_tex")), + // u_model(GetUniformLocation("u_model")), + // u_view(GetUniformLocation("u_view")), + // u_proj(GetUniformLocation("u_proj")), + // u_no_light(GetUniformLocation("u_no_light")), + // u_alpha(GetUniformLocation("u_alpha")) + // {} + //}; + + //class ScreenShader : public Shader { + //public: + // const GLuint u_tex; + // const GLuint u_tex_pos; + // const GLuint u_tex_normal; + // const GLuint u_tex_light; + // const GLuint u_sun_color; + // const GLuint u_sun_direction; + // const GLuint u_ambient_color; + + // ScreenShader(const std::string& vertex_code, const std::string& fragment_code, const char* nametag) : + // Shader(vertex_code, fragment_code, nametag), + // u_tex(GetUniformLocation("u_tex")), + // u_tex_pos(GetUniformLocation("u_tex_pos")), + // u_tex_normal(GetUniformLocation("u_tex_normal")), + // u_tex_light(GetUniformLocation("u_tex_light")), + // u_sun_color(GetUniformLocation("u_sun_color")), + // u_sun_direction(GetUniformLocation("u_sun_direction")), + // u_ambient_color(GetUniformLocation("u_ambient_color")) + // {} + //}; + + + class FramebuffersWrapper { + const GLuint m_width; + const GLuint m_height; + + GLuint m_fb_gb; + GLuint m_tex_gb_color; + GLuint m_tex_gb_normal; + GLuint m_tex_gb_depth; + + GLuint m_fb_lights; + GLuint m_tex_light_mult; + GLuint m_tex_light_add; + + void CreateFramebufferTexture(GLuint& texture, GLint internalformat, GLenum format, GLenum type, GLenum attachment); + + public: + + FramebuffersWrapper(GLuint width, GLuint height); + ~FramebuffersWrapper(); + + void BindGBuffer() const { + glBindFramebuffer(GL_FRAMEBUFFER, m_fb_gb); + } + + void BindLightBuffer() const { + glBindFramebuffer(GL_FRAMEBUFFER, m_fb_lights); + } + + GLuint GetGBColor() const { + return m_tex_gb_color; + } + + GLuint GetGBNormal() const { + return m_tex_gb_normal; + } + + GLuint GetGBDepth() const { + return m_tex_gb_depth; + } + + GLuint GetLightMult() const { + return m_tex_light_mult; + } + }; + + class Renderer { + static RenderType s_rt_instanced_map[RT_COUNT]; + + std::vector m_draw_lists[RT_COUNT]; + + Shader m_shader_basic; + Shader m_shader_basic_instanced; + Shader m_shader_uv_tint_uv; + Shader m_shader_uv_tint_uv_instanced; + Shader m_shader_xz; + Shader m_shader_xz_xz_mask_uv; + Shader m_shader_screen; + Shader m_shader_skeletal; + //float m_aspect_ratio; + std::unique_ptr m_fbs; + + std::shared_ptr m_viewport_buf; + + int m_last_width, m_last_height; + + std::vector m_bone_transforms; + + public: + + glm::mat4 m_proj_matrix; + glm::mat4 m_view_matrix; + Renderer(); + + void Add(const DrawRef& draw_ref) { + m_draw_lists[draw_ref.drawable->type].push_back(draw_ref); + } + + void AddInstanced(const DrawRef& draw_ref) { + m_draw_lists[s_rt_instanced_map[draw_ref.drawable->type]].push_back(draw_ref); + } + + void SetViewMatrix(const glm::mat4& view_matrix) { + m_view_matrix = view_matrix; + } + + void SetProjectionMatrix(const glm::mat4& proj_matrix) { + m_proj_matrix = proj_matrix; + } + + void SetProjection(float aspect_ratio, float fov, float nearplane, float farplane) { + SetProjectionMatrix(glm::perspective(glm::radians(fov * 0.5f), aspect_ratio, nearplane, farplane)); + } + + void Render(int width, int height); + }; + +} \ No newline at end of file diff --git a/src/tsr/rendersystem.cpp b/src/tsr/rendersystem.cpp new file mode 100644 index 0000000..2ea9449 --- /dev/null +++ b/src/tsr/rendersystem.cpp @@ -0,0 +1,26 @@ +#include "rendersystem.hpp" + +using namespace TSR; + +void RenderSystem::Render(TSR::Renderer& renderer) { + + //auto v = World::Registry().view(); + //for (auto [ent, mesh, render, trans] : v.each()) { + // render.ctx.m_model_matrix = glm::toMat4(trans.rot) * glm::translate(glm::mat4(1.0f), trans.pos); + // + // for (auto& drawable : mesh.parts) + // renderer.Add(TSR::DrawRef(&drawable, &render.ctx)); + + + //} + + + + + + //entt::view v(World::Get().Reg()); + + + //World::Get().Reg() + +} diff --git a/src/tsr/rendersystem.hpp b/src/tsr/rendersystem.hpp new file mode 100644 index 0000000..346fbd8 --- /dev/null +++ b/src/tsr/rendersystem.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "systems.hpp" +#include +#include "../tsr/renderer.hpp" + +namespace TSR { + + struct ModelComponent { + TSR::DrawCtx ctx; + + std::vector bone_transforms; + + + }; + + class RenderSystem { + public: + + static void Render(TSR::Renderer& renderer); + + + + }; + +} diff --git a/src/tsr/systems.hpp b/src/tsr/systems.hpp new file mode 100644 index 0000000..2b9a3b2 --- /dev/null +++ b/src/tsr/systems.hpp @@ -0,0 +1,3 @@ +#pragma once +#include +#include "world.hpp" \ No newline at end of file diff --git a/src/tsr/tsr.hpp b/src/tsr/tsr.hpp new file mode 100644 index 0000000..aa87688 --- /dev/null +++ b/src/tsr/tsr.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +// log + +#ifdef TSR_ADVANCED_LOG +#define TsrFPrintf(file, format, ...) fprintf(file, "[%s] " format, __func__, __VA_ARGS__) +#define TsrErrorf(format, ...) TsrFPrintf(stderr, format, __VA_ARGS__) +#define TsrPrintf(format, ...) TsrFPrintf(stdout, format, __VA_ARGS__) +#else +#define TsrErrorf printf +#define TsrPrintf printf +#endif + +#define TSR_NO_BONE 255 +#define TSR_MAX_BONES TSR_NO_BONE + +#define U8(_S) (const char*)u8##_S + +namespace TSR { + using std::string_literals::operator""s; + using namespace nlohmann; + + inline void Throw(const std::string& msg) { + throw std::runtime_error(msg); + } + + template + inline glm::vec JsonGetVec(json j) { + glm::vec ret; + + for (int i = 0; i < size; ++i) + ret[i] = j[i]; + + return ret; + } + +} \ No newline at end of file diff --git a/src/tsr/window.cpp b/src/tsr/window.cpp new file mode 100644 index 0000000..ae44b88 --- /dev/null +++ b/src/tsr/window.cpp @@ -0,0 +1,90 @@ +#include "window.hpp" +#include +#include "tsr.hpp" + +GLFWwindow* TSR::Window::s_window = nullptr; +int TSR::Window::s_window_dimensions[4]; + +void TSR::Window::Init(const char* title, int width, int height) { + s_window_dimensions[0] = 0; + s_window_dimensions[1] = 0; + s_window_dimensions[2] = width; + s_window_dimensions[3] = height; + + TsrPrintf("GLFW init\n"); + + if (!glfwInit()) Throw("Could not init GLFW!"); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); + glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE); + glfwWindowHint(GLFW_DEPTH_BITS, 0); + //glfwWindowHint(GLFW_DEPTH_BITS, 0); + glfwWindowHint(GLFW_STENCIL_BITS, 0); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + s_window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (!s_window) { + glfwTerminate(); + Throw("Could not create GLFW window!"); + } + + glfwMakeContextCurrent(s_window); + + glewExperimental = GL_TRUE; + if (glewInit() != GLEW_OK) { + glfwDestroyWindow(s_window); + glfwTerminate(); + Throw("Could not init GLEW!"); + } + + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* user_param) { + if (type == GL_DEBUG_TYPE_OTHER) return; + printf("GL DEBUG: %s\n", message); + }, nullptr); + + glfwSwapInterval(1); + + //glfwSetWindowUserPointer(s_window, this); + + //glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scancode, int action, int mods) { + // ((Window*)glfwGetWindowUserPointer(window))->m_key_cb(key, scancode, action, mods); + //}); + + //glfwSetCursorPosCallback(m_window, [](GLFWwindow* window, double x, double y) { + // ((Window*)glfwGetWindowUserPointer(window))->m_mouse_cb(x, y); + //}); + + //glfwSetFramebufferSizeCallback(m_window, [](GLFWwindow* window, int width, int height) { + // ((Window*)glfwGetWindowUserPointer(window))->m_size_cb(width, height); + //}); +} + +void TSR::Window::Close() { + if (s_window) { + glfwDestroyWindow(s_window); + glfwTerminate(); + } +} + +void TSR::Window::SwitchFullscreen() { + if (glfwGetWindowMonitor(s_window) != nullptr) { + glfwSetWindowMonitor(s_window, nullptr, s_window_dimensions[0], s_window_dimensions[1], s_window_dimensions[2], s_window_dimensions[3], 0); + } + else { + glfwGetWindowPos(s_window, &s_window_dimensions[0], &s_window_dimensions[1]); + glfwGetWindowSize(s_window, &s_window_dimensions[2], &s_window_dimensions[3]); + + const GLFWvidmode* vm = glfwGetVideoMode(glfwGetPrimaryMonitor()); + glfwSetWindowMonitor(s_window, glfwGetPrimaryMonitor(), 0, 0, vm->width, vm->height, vm->refreshRate); + } +} +// +//void GLAPIENTRY TSR::Window::DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* user_param) { +// Tsr()->OnWindowMessage(std::string(message)); +//} + diff --git a/src/tsr/window.hpp b/src/tsr/window.hpp new file mode 100644 index 0000000..b8b7bbd --- /dev/null +++ b/src/tsr/window.hpp @@ -0,0 +1,72 @@ +#pragma once +#include +#include +#include + +namespace TSR { + + class Window { + //using KeyCallback = std::function; + //using MouseCallback = std::function; + //using SizeCallback = std::function; + + static GLFWwindow* s_window; + static int s_window_dimensions[4]; + + public: + //KeyCallback m_key_cb; + //MouseCallback m_mouse_cb; + //SizeCallback m_size_cb; + + //Window(const char* title, int width, int height); + static void Init(const char* title, int width, int height); + static void Close(); + + static void Show() { glfwShowWindow(s_window); } + static void Hide() { glfwHideWindow(s_window); } + static void SwitchFullscreen(); + static bool ShouldClose() { return glfwWindowShouldClose(s_window); } + static void SetShouldClose(int value) { glfwSetWindowShouldClose(s_window, value); } + static bool KeyDown(int key) { return glfwGetKey(s_window, key) == GLFW_PRESS; } + static void SetInputMode(int mode, int value) { glfwSetInputMode(s_window, mode, value); } + static void GetFramebufferSize(int& width, int& height) { glfwGetFramebufferSize(s_window, &width, &height); } + static void GetMousePos(double& x, double& y) { glfwGetCursorPos(s_window, &x, &y); } + static void PollEvents() { glfwPollEvents(); } + static void SwapBuffers() { glfwSwapBuffers(s_window); } + static GLFWwindow* Ptr() { return s_window; } + + static GLFWkeyfun SetKeyCallback(GLFWkeyfun callback) { return glfwSetKeyCallback(s_window, callback); } + static GLFWmousebuttonfun SetMouseButtonCallback(GLFWmousebuttonfun callback) { return glfwSetMouseButtonCallback(s_window, callback); } + static GLFWcursorposfun SetCursorPosCallback(GLFWcursorposfun callback) { return glfwSetCursorPosCallback(s_window, callback); } + + //static void SetKeyCallback(KeyCallback cb) { + // m_key_cb = cb; + //} + + //static void SetMouseCallback(MouseCallback cb) { + // m_mouse_cb = cb; + //} + + //static void SetSizeCallback(SizeCallback cb) { + // m_size_cb = cb; + //} + + //static void GLAPIENTRY DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* user_param); + //static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); + //static void MouseMoveCallback(GLFWwindow* window, double x, double y); + //static void SizeCallback(GLFWwindow* window, int width, int height); + + //Window(Window const&) = delete; + //Window(Window&&) = delete; + + //~Window(); + }; + + class WindowWrapper { + + public: + WindowWrapper(const char* title, int width, int height) { Window::Init(title, width, height); } + ~WindowWrapper() { Window::Close(); } + }; + +} \ No newline at end of file diff --git a/src/tsr/world.cpp b/src/tsr/world.cpp new file mode 100644 index 0000000..c100111 --- /dev/null +++ b/src/tsr/world.cpp @@ -0,0 +1,6 @@ +#include "world.hpp" + +using namespace TSR; + +//std::unique_ptr World::s_world; +entt::registry World::s_registry; \ No newline at end of file diff --git a/src/tsr/world.hpp b/src/tsr/world.hpp new file mode 100644 index 0000000..3f9f8d6 --- /dev/null +++ b/src/tsr/world.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace TSR { + + struct TransformComponent { + glm::vec3 pos; + glm::quat rot; + }; + + class World { + static entt::registry s_registry; + + //entt::registry m_reg; + + + + public: + + static entt::registry& Registry() { + return s_registry; + } + + }; + +} + + diff --git a/src/tsrecs.cpp b/src/tsrecs.cpp new file mode 100644 index 0000000..1999b3d --- /dev/null +++ b/src/tsrecs.cpp @@ -0,0 +1,143 @@ +#include +#include +#include "tsr/filesystem.hpp" +#include "tsr/window.hpp" +#include "tsr/renderer.hpp" +#include "tsr/model.hpp" +#include "tsr/camera.hpp" +#include + +using namespace TSR; +Camera cam; + +int main() { + TsrPrintf("Hello (client)\n"); + + try { + Filesystem::Init("main"); + WindowWrapper ww("TSR test", 640, 480); + ////Audio::Init(); + + Renderer r; + + auto model = AssetMap::Get("models/test_skeletal.mdl.json"); + + DrawCtx draw_ctx; + draw_ctx.model_matrix = glm::mat4(1.0f); + + std::vector bone_transforms; + bone_transforms.resize(model->GetSkeleton().Bones().size()); + + draw_ctx.bone_transforms = &bone_transforms[0]; + + auto anim_id = model->GetAnimId("ruce"); + const auto& anim = model->GetAnim(anim_id); + + const auto& skel = model->GetSkeleton(); + const auto& sk_bones = skel.Bones(); + + //glfwSwapInterval(0); + + Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED); + Window::SetCursorPosCallback([](GLFWwindow* window, double xpos, double ypos) { + + static float last_mouse_x = 0.0f, last_mouse_y = 0.0f; + + + double xoffset = xpos - last_mouse_x; + double yoffset = last_mouse_y - ypos; // reversed since y-coordinates range from bottom to top + + cam.ProcessMouse(xoffset, yoffset); + + last_mouse_x = xpos; + last_mouse_y = ypos; + }); + + cam.movement_speed *= 0.1f; + + Window::Show(); + while (!Window::ShouldClose()) { + float time = 1.0f / 75.0f; + + if (Window::KeyDown(GLFW_KEY_W)) + cam.ProcessMovement(Camera::Movement::FORWARD, time); + if (Window::KeyDown(GLFW_KEY_S)) + cam.ProcessMovement(Camera::Movement::BACKWARD, time); + if (Window::KeyDown(GLFW_KEY_A)) + cam.ProcessMovement(Camera::Movement::LEFT, time); + if (Window::KeyDown(GLFW_KEY_D)) + cam.ProcessMovement(Camera::Movement::RIGHT, time); + if (Window::KeyDown(GLFW_KEY_SPACE)) + cam.ProcessMovement(Camera::Movement::UP, time); + if (Window::KeyDown(GLFW_KEY_LEFT_SHIFT)) + cam.ProcessMovement(Camera::Movement::DOWN, time); + + + int w, h; + Window::GetFramebufferSize(w, h); + r.SetProjection((float)w / (float)h, 90.0f, 0.1f, 1000.0f); + //r.SetViewMatrix(glm::lookAt(glm::vec3(-1.0f, 2.0f, 2.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f))); + r.SetViewMatrix(cam.GetViewMatrix()); + //r.SetViewMatrix(glm::lookAt(glm::vec3(10.0f, 5.0f, 3.0f), glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))); + + + // reset bones + for (int i = 0; i < bone_transforms.size(); ++i) { + bone_transforms[i] = sk_bones[i].transform; + } + + // animation + float cur_frame = glm::mod((float)(glfwGetTime() * anim.skel->TPS()), (float)anim.skel->Duration()); + //float cur_frame = glm::mod(1.0f, 3.0f); + + for (int i = 0; i < anim.skel->Channels().size(); ++i) { + + int fr1 = glm::floor(cur_frame); + int fr2 = glm::ceil(cur_frame); + float t = glm::fract(cur_frame); + + const auto& frame1 = anim.skel->Channels()[i].frames[fr1]; + const auto& frame2 = anim.skel->Channels()[i].frames[fr2]; + + auto translation = glm::translate(glm::mat4(1.0f), glm::mix(frame1.pos, frame2.pos, t)); + auto rotation = glm::toMat4(glm::normalize(glm::slerp(frame1.rot, frame2.rot, t))); + auto scale = glm::scale(glm::mat4(1.0f), glm::mix(frame1.scl, frame2.scl, t)); + + bone_transforms[anim.bone_ids[i]] = translation * rotation * scale; + + } + + // apply parents + for (int i = 0; i < bone_transforms.size(); ++i) { + if (sk_bones[i].parent_idx != TSR_NO_BONE) + bone_transforms[i] = bone_transforms[sk_bones[i].parent_idx] * bone_transforms[i]; + } + + + + model->Draw(r, &draw_ctx); + + r.Render(w, h); + + Window::PollEvents(); + Window::SwapBuffers(); + + + + + } + + //Client::Run(); + + + + } + catch (std::exception ex) { + + TsrPrintf("ERROR: %s\n", ex.what()); + MessageBoxA(NULL, ex.what(), "ERROR", MB_ICONERROR); + } + //Audio::Close(); + + return 0; +} diff --git a/tsrecs.sln b/tsrecs.sln new file mode 100644 index 0000000..42c6b06 --- /dev/null +++ b/tsrecs.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tsrecs", "tsrecs.vcxproj", "{636143B5-5296-4E48-813E-6FE5EB883870}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {636143B5-5296-4E48-813E-6FE5EB883870}.Debug|x64.ActiveCfg = Debug|x64 + {636143B5-5296-4E48-813E-6FE5EB883870}.Debug|x64.Build.0 = Debug|x64 + {636143B5-5296-4E48-813E-6FE5EB883870}.Debug|x86.ActiveCfg = Debug|Win32 + {636143B5-5296-4E48-813E-6FE5EB883870}.Debug|x86.Build.0 = Debug|Win32 + {636143B5-5296-4E48-813E-6FE5EB883870}.Release|x64.ActiveCfg = Release|x64 + {636143B5-5296-4E48-813E-6FE5EB883870}.Release|x64.Build.0 = Release|x64 + {636143B5-5296-4E48-813E-6FE5EB883870}.Release|x86.ActiveCfg = Release|Win32 + {636143B5-5296-4E48-813E-6FE5EB883870}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {96A7D133-9A25-473C-B402-A0DB0C48E308} + EndGlobalSection +EndGlobal diff --git a/tsrecs.vcxproj b/tsrecs.vcxproj new file mode 100644 index 0000000..4f643c5 --- /dev/null +++ b/tsrecs.vcxproj @@ -0,0 +1,159 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {636143b5-5296-4e48-813e-6fe5eb883870} + tsrecs + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp20 + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp20 + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tsrecs.vcxproj.filters b/tsrecs.vcxproj.filters new file mode 100644 index 0000000..3d11d62 --- /dev/null +++ b/tsrecs.vcxproj.filters @@ -0,0 +1,84 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file