2 Commits
v0.2 ... v0.4

Author SHA1 Message Date
5a65f57ad7 Refactor & Readme 2024-05-07 23:00:16 -04:00
c294f6e50a Update obj.cpp 2024-05-07 18:44:42 -04:00
14 changed files with 57 additions and 83 deletions

15
README.md Normal file
View File

@@ -0,0 +1,15 @@
## Collage
#### [![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](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
```

View File

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

View File

@@ -1,6 +1,6 @@
#pragma once
#include <vector>
#include <types/bone.h>
#include <Collage/types/bone.h>
struct KeyFrame {
uint index;

View File

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

View File

@@ -5,7 +5,7 @@
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include<J3ML/Geometry.h>
#include <types/textureInfo.h>
#include <Collage/types/textureInfo.h>
typedef Vector3 Vertex;
@@ -27,14 +27,12 @@ class SkeletalVertex : public Vertex {
};
class Model {
private:
void loadOBJ(const std::string& filename);
void load(const std::string& file);
protected:
void load(const std::string& file);
void loadOBJ(const std::string& filename);
std::vector<Vertex> vertices;
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();
@@ -48,5 +46,5 @@ class SkeletalModel : public Model {
protected:
std::vector<SkeletalVertex> vertices;
public:
std::vector<SkeletalVertex>& getSkeletalertices();
std::vector<SkeletalVertex>& getSkeletalVertices();
};

View File

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

View File

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

View File

@@ -1,84 +1,45 @@
#include <iostream>
#include <types/model.h>
#include <Collage/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") {
auto result = ParseVertexCoordinatesFromOBJ(stream);
positions.emplace_back(result);
float x, y, z;
stream >> x >> y >> z;
positions.emplace_back(x, y, z);
} else if (prefix == "vt") {
auto result = ParseVertexTextureCoordinatesFromOBJ(stream);
uvs.emplace_back(result);
float u, v;
stream >> u >> v;
uvs.emplace_back(u, v);
} else if (prefix == "f") {
auto result = ParseFaceIndicesFromOBJ(stream);
mapFaces(result, positions, uvs);
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()));
}
}
}
textureInfo.push_back(tInfo);
file.close();
textureInfo.push_back({"", uvs});
};
// 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) {
std::vector<Vector2> textureCoordinates;
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);
indices.push_back(static_cast<unsigned int>(indices.size()));
//textureCoordinates.push_back(textureCoordinate);
}
textureInfo.push_back({"", textureCoordinates});
}
};

View File

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

View File

@@ -1,4 +1,4 @@
#include <types/bone.h>
#include <Collage/types/bone.h>
const Matrix4x4& Bone::getMatrix() {
return matrix;

View File

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

View File

@@ -1,4 +1,4 @@
#include <types/model.h>
#include <Collage/types/model.h>
void Model::load(const std::string& file) {
if (file.ends_with(".obj"))
@@ -21,6 +21,6 @@ Model::Model(const std::string &file) {
load(file);
}
std::vector<SkeletalVertex> &SkeletalModel::getSkeletalertices() {
std::vector<SkeletalVertex>& SkeletalModel::getSkeletalVertices() {
return vertices;
}