Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
c4f4cffbdc |
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.18..3.27)
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(Collage
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
@@ -11,7 +11,7 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.1.zip
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-1.zip
|
||||
)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
@@ -25,17 +25,10 @@ if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||
message(FATAL_ERROR "In-Source builds are not allowed")
|
||||
endif()
|
||||
|
||||
if (UNIX AND NOT APPLE)
|
||||
add_library(Collage SHARED ${SOURCES} ${HEADERS})
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
add_library(Collage STATIC ${SOURCES} ${HEADERS})
|
||||
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)
|
||||
set_target_properties(CollageTest PROPERTIES LINKER_LANGUAGE CXX)
|
15
README.md
15
README.md
@@ -1,15 +0,0 @@
|
||||
## Collage
|
||||
#### [](http://unlicense.org/)
|
||||
|
||||
Another model loader for Linux & Windows.
|
||||
|
||||
### Supported Formats
|
||||
- Wavefront OBJ
|
||||
- Animated Wavefront OBJ
|
||||
- Collada (maybe)
|
||||
- FBX
|
||||
|
||||
### Building The Demo
|
||||
```bash
|
||||
git clone https://git.redacted.cc/Redacted/Collage.git && cd Collage && mkdir build && cd build && cmake .. && make -j8
|
||||
```
|
@@ -1,2 +0,0 @@
|
||||
#pragma once
|
||||
#include <Collage/types/model.h>
|
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <Collage/types/bone.h>
|
||||
|
||||
struct KeyFrame {
|
||||
unsigned int index;
|
||||
Bone bone;
|
||||
};
|
||||
|
||||
typedef std::vector<KeyFrame> Animation;
|
@@ -1,2 +0,0 @@
|
||||
#pragma once
|
||||
#include <Collage/types/bone.h>
|
2
include/collage.h
Normal file
2
include/collage.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include <types/model.h>
|
10
include/types/animation.h
Normal file
10
include/types/animation.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <types/bone.h>
|
||||
|
||||
struct KeyFrame {
|
||||
uint index;
|
||||
Bone bone;
|
||||
};
|
||||
|
||||
typedef std::vector<KeyFrame> Animation;
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
class Bone {
|
||||
protected:
|
||||
std::string name;
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
const Matrix4x4& getMatrix();
|
||||
void setMatrix(const Matrix4x4& m);
|
||||
void appendChild(const Bone& bone);
|
||||
unsigned int getDepth(); //The "depth" refers to how far in the bone is on the bonemap. For ex, the fingers would be deeper than the elbow.
|
||||
uint getDepth(); //The "depth" refers to how far in the bone is on the bonemap. For ex, the fingers would be deeper than the elbow.
|
||||
Bone* getParent();
|
||||
Bone* getChildByName(const std::string& bName);
|
||||
Bone* getChildByNameRecursive(const std::string& bName);
|
2
include/types/keyFrame.h
Normal file
2
include/types/keyFrame.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include <types/bone.h>
|
@@ -2,10 +2,10 @@
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector3.hpp>
|
||||
#include<J3ML/Geometry.hpp>
|
||||
#include <Collage/types/textureInfo.h>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include<J3ML/Geometry.h>
|
||||
#include <types/textureInfo.h>
|
||||
|
||||
typedef Vector3 Vertex;
|
||||
|
||||
@@ -27,16 +27,18 @@ class SkeletalVertex : public Vertex {
|
||||
};
|
||||
|
||||
class Model {
|
||||
protected:
|
||||
void load(const std::string& file);
|
||||
private:
|
||||
void loadOBJ(const std::string& filename);
|
||||
void load(const std::string& file);
|
||||
protected:
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<uint> indices;
|
||||
std::vector<TextureInformation> textureInfo;
|
||||
void mapFaces(const FaceIndices& facedata, const std::vector<Vector3>& positions, const std::vector<Vector2>& uvs);
|
||||
public:
|
||||
ModelType type;
|
||||
const std::vector<Vertex>& getVertices();
|
||||
const std::vector<unsigned int>& getIndices();
|
||||
const std::vector<uint>& getIndices();
|
||||
const std::vector<TextureInformation>& getTextureInformation();
|
||||
Model() = default;
|
||||
explicit Model(const std::string& file);
|
||||
@@ -46,5 +48,5 @@ class SkeletalModel : public Model {
|
||||
protected:
|
||||
std::vector<SkeletalVertex> vertices;
|
||||
public:
|
||||
std::vector<SkeletalVertex>& getSkeletalVertices();
|
||||
std::vector<SkeletalVertex>& getSkeletalertices();
|
||||
};
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
|
||||
struct TextureInformation {
|
||||
std::string path;
|
98
src/obj.cpp
98
src/obj.cpp
@@ -1,45 +1,85 @@
|
||||
#include <iostream>
|
||||
#include <Collage/types/model.h>
|
||||
#include <types/model.h>
|
||||
|
||||
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;
|
||||
}
|
||||
void Model::loadOBJ(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open())
|
||||
std::cerr << filename << " not found." << std::endl;
|
||||
|
||||
std::vector<Vector3> positions;
|
||||
std::vector<Vector2> uvs;
|
||||
TextureInformation tInfo = {};
|
||||
std::string line;
|
||||
|
||||
// First: We read every (unique) vertex entry and textureCoord entry from the file.
|
||||
// Then: Use Face data to map the bulk data into correct locations.
|
||||
|
||||
// TODO: Check the assumption that we can **always** read all "v", "vt" etc. tokens first,
|
||||
// allowing us to perform a second-pass to reconstruct faces?
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
std::istringstream stream(line);
|
||||
std::string prefix;
|
||||
stream >> prefix;
|
||||
|
||||
if (prefix == "v") {
|
||||
float x, y, z;
|
||||
stream >> x >> y >> z;
|
||||
positions.emplace_back(x, y, z);
|
||||
} else if (prefix == "vt") {
|
||||
float u, v;
|
||||
stream >> u >> v;
|
||||
uvs.emplace_back(u, v);
|
||||
} else if (prefix == "f") {
|
||||
unsigned int vertexIndex[3], texCoordIndex[3];
|
||||
char slash;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
stream >> vertexIndex[i] >> slash >> texCoordIndex[i];
|
||||
vertexIndex[i]--;
|
||||
texCoordIndex[i]--;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
Vertex vertex;
|
||||
Vector2 textureCoordinate;
|
||||
vertex = positions[vertexIndex[i]];
|
||||
textureCoordinate = uvs[texCoordIndex[i]];
|
||||
vertices.push_back(vertex);
|
||||
tInfo.textureCoordinates.push_back(textureCoordinate);
|
||||
indices.push_back(static_cast<unsigned int>(indices.size()));
|
||||
}
|
||||
if (prefix == "v") {
|
||||
auto result = ParseVertexCoordinatesFromOBJ(stream);
|
||||
positions.emplace_back(result);
|
||||
} else if (prefix == "vt") {
|
||||
auto result = ParseVertexTextureCoordinatesFromOBJ(stream);
|
||||
uvs.emplace_back(result);
|
||||
} else if (prefix == "f") {
|
||||
auto result = ParseFaceIndicesFromOBJ(stream);
|
||||
mapFaces(result, positions, uvs);
|
||||
}
|
||||
}
|
||||
textureInfo.push_back(tInfo);
|
||||
file.close();
|
||||
};
|
||||
};
|
||||
|
||||
// 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) {
|
||||
TextureInformation tInfo{};
|
||||
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);
|
||||
tInfo.path = "default"; //Placeholder
|
||||
tInfo.textureCoordinates.push_back(textureCoordinate);
|
||||
indices.push_back(static_cast<unsigned int>(indices.size()));
|
||||
}
|
||||
textureInfo.push_back(tInfo);
|
||||
}
|
@@ -1,2 +1,2 @@
|
||||
#include <Collage/types/animation.h>
|
||||
#include <Collage/types/keyFrame.h>
|
||||
#include <types/animation.h>
|
||||
#include <types/keyFrame.h>
|
@@ -1,4 +1,4 @@
|
||||
#include <Collage/types/bone.h>
|
||||
#include <types/bone.h>
|
||||
|
||||
const Matrix4x4& Bone::getMatrix() {
|
||||
return matrix;
|
||||
@@ -54,12 +54,12 @@ Bone* Bone::getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
unsigned int Bone::getDepth() {
|
||||
uint Bone::getDepth() {
|
||||
if (isRootBone())
|
||||
return 0;
|
||||
|
||||
Bone* b = parent;
|
||||
unsigned int depth = 1;
|
||||
uint depth = 1;
|
||||
while (b != nullptr && !b->isRootBone()) {
|
||||
b = b->getParent();
|
||||
depth++;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include <Collage/types/bone.h>
|
||||
#include <types/bone.h>
|
||||
struct BoneMap {
|
||||
std::vector<Bone> bones;
|
||||
};
|
@@ -1,4 +1,4 @@
|
||||
#include <Collage/types/model.h>
|
||||
#include <types/model.h>
|
||||
|
||||
void Model::load(const std::string& file) {
|
||||
if (file.ends_with(".obj"))
|
||||
@@ -9,7 +9,7 @@ const std::vector<Vector3>& Model::getVertices() {
|
||||
return vertices;
|
||||
}
|
||||
|
||||
const std::vector<unsigned int>& Model::getIndices() {
|
||||
const std::vector<uint>& Model::getIndices() {
|
||||
return indices;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,6 @@ Model::Model(const std::string &file) {
|
||||
load(file);
|
||||
}
|
||||
|
||||
std::vector<SkeletalVertex>& SkeletalModel::getSkeletalVertices() {
|
||||
std::vector<SkeletalVertex> &SkeletalModel::getSkeletalertices() {
|
||||
return vertices;
|
||||
}
|
||||
|
Reference in New Issue
Block a user