Initial Commit

This commit is contained in:
2024-05-05 10:46:01 -04:00
commit 9f1359aa3e
14 changed files with 295 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/cmake-build-debug
/.idea

34
CMakeLists.txt Normal file
View File

@@ -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)

24
cmake/CPM.cmake Normal file
View File

@@ -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})

2
include/collage.h Normal file
View File

@@ -0,0 +1,2 @@
#pragma once
#include <types/model.h>

View File

@@ -0,0 +1,5 @@
#pragma once
#include <vector>
#include <types/keyFrame.h>
typedef std::vector<KeyFrame> Animation;

21
include/types/bone.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <vector>
#include <J3ML/LinearAlgebra.h>
class Bone {
protected:
std::string name;
Matrix4x4 matrix;
float weight;
Bone* parent;
std::vector<Bone*> 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();
};

7
include/types/keyFrame.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#include <types/bone.h>
struct KeyFrame {
uint index;
Bone bone;
};

45
include/types/model.h Normal file
View File

@@ -0,0 +1,45 @@
#pragma once
#include <vector>
#include <sstream>
#include <fstream>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include<J3ML/Geometry.h>
#include <types/textureInfo.h>
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<Vertex> vertices;
std::vector<uint> indices;
std::vector<TextureInformation> textureInfo;
void load(const std::string& file);
void load(const std::string& file, const ModelType& type);
public:
ModelType type;
const std::vector<Vertex>& getVertices();
const std::vector<uint>& getIndices();
const std::vector<TextureInformation>& getTextureInformation();
Model() = default;
Model(const std::string& file);
};
class SkeletalModel : public Model {
protected:
std::vector<SkeletalVertex> vertices;
public:
std::vector<SkeletalVertex>& getSkeletalertices();
};

View File

@@ -0,0 +1,8 @@
#pragma once
#include <vector>
#include <J3ML/LinearAlgebra.h>
struct TextureInformation {
std::string name;
std::vector<Vector2> textureCoordinates;
};

4
main.cpp Normal file
View File

@@ -0,0 +1,4 @@
#include <iostream>
int main() {
}

2
src/types/animation.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include <types/animation.h>
#include <types/keyFrame.h>

55
src/types/bone.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <types/bone.h>
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;
}

4
src/types/boneMap.cpp Normal file
View File

@@ -0,0 +1,4 @@
#include <types/bone.h>
struct BoneMap {
std::vector<Bone> bones;
};

82
src/types/model.cpp Normal file
View File

@@ -0,0 +1,82 @@
#include <types/model.h>
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<Vector3>& positions, const std::vector<Vector2>& 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<unsigned int>(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<Vector3>& Model::getVertices() {
return vertices;
}
const std::vector<uint>& Model::getIndices() {
return indices;
}
const std::vector<TextureInformation>& Model::getTextureInformation() {
return textureInfo;
}
Model::Model(const std::string &file) {
load(file);
}
std::vector<SkeletalVertex> &SkeletalModel::getSkeletalertices() {
return vertices;
}