From 9f1359aa3e3a6a98993f74ee0b3576ffb1ee5d57 Mon Sep 17 00:00:00 2001 From: Redacted Date: Sun, 5 May 2024 10:46:01 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 2 + CMakeLists.txt | 34 +++++++++++++++ cmake/CPM.cmake | 24 +++++++++++ include/collage.h | 2 + include/types/animation.h | 5 +++ include/types/bone.h | 21 ++++++++++ include/types/keyFrame.h | 7 ++++ include/types/model.h | 45 ++++++++++++++++++++ include/types/textureInfo.h | 8 ++++ main.cpp | 4 ++ src/types/animation.cpp | 2 + src/types/bone.cpp | 55 +++++++++++++++++++++++++ src/types/boneMap.cpp | 4 ++ src/types/model.cpp | 82 +++++++++++++++++++++++++++++++++++++ 14 files changed, 295 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 cmake/CPM.cmake create mode 100644 include/collage.h create mode 100644 include/types/animation.h create mode 100644 include/types/bone.h create mode 100644 include/types/keyFrame.h create mode 100644 include/types/model.h create mode 100644 include/types/textureInfo.h create mode 100644 main.cpp create mode 100644 src/types/animation.cpp create mode 100644 src/types/bone.cpp create mode 100644 src/types/boneMap.cpp create mode 100644 src/types/model.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f88b18 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/cmake-build-debug +/.idea diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7f7ca62 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.20) +project(Collage + VERSION 1.0 + LANGUAGES CXX +) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +# Enable Package Managers +include(cmake/CPM.cmake) + +CPMAddPackage( + NAME J3ML + URL https://git.redacted.cc/josh/j3ml/archive/Release-1.zip +) + +set(CMAKE_CXX_STANDARD 20) + +file(GLOB_RECURSE HEADERS "include/*.h") +file(GLOB_RECURSE SOURCES "src/*.cpp") + +include_directories("include" ${J3ML_SOURCE_DIR}/include) + +if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message(FATAL_ERROR "In-Source builds are not allowed") +endif() + +add_library(Collage SHARED ${SOURCES} ${HEADERS}) +add_executable(CollageTest main.cpp) + +set_target_properties(Collage PROPERTIES LINKER_LANGUAGE CXX) +target_link_libraries(Collage PUBLIC J3ML) +target_link_libraries(CollageTest PUBLIC Collage) +set_target_properties(CollageTest PROPERTIES LINKER_LANGUAGE CXX) \ No newline at end of file diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..cc25ec2 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: MIT +# +# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors + +set(CPM_DOWNLOAD_VERSION 0.38.7) +set(CPM_HASH_SUM "83e5eb71b2bbb8b1f2ad38f1950287a057624e385c238f6087f94cdfc44af9c5") + +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) + +file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM} +) + +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/include/collage.h b/include/collage.h new file mode 100644 index 0000000..0de35c9 --- /dev/null +++ b/include/collage.h @@ -0,0 +1,2 @@ +#pragma once +#include diff --git a/include/types/animation.h b/include/types/animation.h new file mode 100644 index 0000000..dc3c52f --- /dev/null +++ b/include/types/animation.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +typedef std::vector Animation; \ No newline at end of file diff --git a/include/types/bone.h b/include/types/bone.h new file mode 100644 index 0000000..1519c1c --- /dev/null +++ b/include/types/bone.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +class Bone { +protected: + std::string name; + Matrix4x4 matrix; + float weight; + Bone* parent; + std::vector children; +public: + bool isRootBone(); + bool hasChildren(); + const Matrix4x4& getMatrix(); + void setMatrix(const Matrix4x4& m); + void appendChild(const Bone& bone); + Bone* getParent(); + Bone* getChildByName(const std::string& bName); + Bone* getChildByNameRecursive(const std::string& bName); + Bone* getFirstChild(); +}; \ No newline at end of file diff --git a/include/types/keyFrame.h b/include/types/keyFrame.h new file mode 100644 index 0000000..0135bc4 --- /dev/null +++ b/include/types/keyFrame.h @@ -0,0 +1,7 @@ +#pragma once +#include + +struct KeyFrame { + uint index; + Bone bone; +}; diff --git a/include/types/model.h b/include/types/model.h new file mode 100644 index 0000000..b234959 --- /dev/null +++ b/include/types/model.h @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +typedef Vector3 Vertex; + +enum struct ModelType : uint8_t { + WAVEFRONT_OBJ = 0, + ANIMATED_WAVEFRONT_OBJ = 1, + FBX = 2, //More complicated. + COLLADA = 3, //*Very* complicated. +}; + +class SkeletalVertex : public Vertex { + int8_t joints[3]; //The index of 4 bones that can effect the vertex. 0 represents an empty slot. -1 represents the root bone. + Vector4 weights; //The 4 weights must total to 1.0 +}; + +class Model { +protected: + std::vector vertices; + std::vector indices; + std::vector textureInfo; + void load(const std::string& file); + void load(const std::string& file, const ModelType& type); +public: + ModelType type; + const std::vector& getVertices(); + const std::vector& getIndices(); + const std::vector& getTextureInformation(); + Model() = default; + Model(const std::string& file); +}; + +class SkeletalModel : public Model { +protected: + std::vector vertices; +public: + std::vector& getSkeletalertices(); +}; \ No newline at end of file diff --git a/include/types/textureInfo.h b/include/types/textureInfo.h new file mode 100644 index 0000000..eb09cad --- /dev/null +++ b/include/types/textureInfo.h @@ -0,0 +1,8 @@ +#pragma once +#include +#include + +struct TextureInformation { + std::string name; + std::vector textureCoordinates; +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..f4be74a --- /dev/null +++ b/main.cpp @@ -0,0 +1,4 @@ +#include + +int main() { +} diff --git a/src/types/animation.cpp b/src/types/animation.cpp new file mode 100644 index 0000000..e2e67ee --- /dev/null +++ b/src/types/animation.cpp @@ -0,0 +1,2 @@ +#include +#include \ No newline at end of file diff --git a/src/types/bone.cpp b/src/types/bone.cpp new file mode 100644 index 0000000..696378b --- /dev/null +++ b/src/types/bone.cpp @@ -0,0 +1,55 @@ +#include + +const Matrix4x4& Bone::getMatrix() { + return matrix; +} + +void Bone::setMatrix(const Matrix4x4& m) { + matrix = m; +} + +void Bone::appendChild(const Bone& bone) { + Bone* b = new(Bone); *b = bone; + b->parent = this; + children.push_back(b); +} + +bool Bone::isRootBone() { + if (parent == nullptr) + return true; + return false; +} + +Bone* Bone::getFirstChild() { + if (children.empty()) + return nullptr; + return children[0]; +} + +Bone* Bone::getChildByName(const std::string& bName) { + if (children.empty()) + return nullptr; + + for (auto& bone : children) + if (bone->name == bName) + return bone; +} + +Bone *Bone::getChildByNameRecursive(const std::string &bName) { + if (children.empty()) + return nullptr; + +} + +bool Bone::hasChildren() { + if (children.empty()) + return false; + return true; +} + +Bone* Bone::getParent() { + //If we're trying to get the parent of the root bone just return the root bone. + if (parent == nullptr) + return this; + return parent; +} diff --git a/src/types/boneMap.cpp b/src/types/boneMap.cpp new file mode 100644 index 0000000..db0bf22 --- /dev/null +++ b/src/types/boneMap.cpp @@ -0,0 +1,4 @@ +#include +struct BoneMap { + std::vector bones; +}; \ No newline at end of file diff --git a/src/types/model.cpp b/src/types/model.cpp new file mode 100644 index 0000000..1836c93 --- /dev/null +++ b/src/types/model.cpp @@ -0,0 +1,82 @@ +#include + +struct FaceIndices { + unsigned int vertexIndex[3]; + unsigned int texCoordIndex[3]; +}; + +Vector3 ParseVertexCoordinatesFromOBJ(std::istringstream& stream) { + float x, y, z; + stream >> x >> y >> z; + return {x,y,z}; +} + +Vector2 ParseVertexTextureCoordinatesFromOBJ(std::istringstream& stream) { + float u, v; + stream >> u >> v; + return {u,v}; +} + +FaceIndices ParseFaceIndicesFromOBJ(std::istringstream& stream) { + FaceIndices face{}; + char slash; + for (int i = 0; i < 3; ++i) { + unsigned int vI; + unsigned int tCI; + + stream >> vI >> slash >> tCI; + + face.vertexIndex[i] = vI; + face.texCoordIndex[i] = tCI; + + // Shift vertexindex by 1 + face.vertexIndex[i]--; + // shift texcoordindex by 1 + face.texCoordIndex[i]--; + } + return face; +} + +/* +// map face of model to positions and texture coords +void Model::mapfaces(const FaceIndices& facedata, const std::vector& positions, const std::vector& uvs) { + for (int i = 0; i < 3; ++i) { + Vector3 vertex; + Vector2 textureCoordinate; + vertex = positions[facedata.vertexIndex[i]]; + textureCoordinate = uvs[facedata.texCoordIndex[i]]; + + vertices.push_back(vertex); + textureCoor.push_back(textureCoordinate); + indices.push_back(static_cast(indices.size())); + } +} +*/ + +void Model::load(const std::string& file, const ModelType& modelType) { + //TODO: Call correct func. +} + +void Model::load(const std::string& file) { +//TODO: Determine model type and call correct func. +} + +const std::vector& Model::getVertices() { + return vertices; +} + +const std::vector& Model::getIndices() { + return indices; +} + +const std::vector& Model::getTextureInformation() { + return textureInfo; +} + +Model::Model(const std::string &file) { + load(file); +} + +std::vector &SkeletalModel::getSkeletalertices() { + return vertices; +}