Skip to content

Commit

Permalink
Subsystem structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fszewczyk committed Oct 22, 2024
1 parent 25c4e7c commit 54c3874
Show file tree
Hide file tree
Showing 41 changed files with 1,083 additions and 446 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/Rendering)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/Runtime)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/UI)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/InputManager)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/Systems)

add_executable(ShkyeraEngine src/main.cpp)

Expand All @@ -47,5 +48,4 @@ target_link_libraries(

UI
Runtime
ECS
)
9 changes: 9 additions & 0 deletions resources/shaders/fragment/color.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 330 core

out vec4 FragColor;

uniform vec3 fixedColor;

void main() {
FragColor = vec4(fixedColor, 1.0);
}
File renamed without changes.
13 changes: 13 additions & 0 deletions resources/shaders/vertex/wireframe.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330 core

layout(location = 0) in vec3 position; // Vertex position

uniform mat4 modelMatrix; // Model matrix
uniform mat4 viewMatrix; // View matrix
uniform mat4 projectionMatrix; // Projection matrix

void main() {
// Transform the vertex position to clip space
gl_Position =
projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
14 changes: 10 additions & 4 deletions src/AssetManager/AssetManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ class AssetManager {
static AssetManager& getInstance();

template<typename AssetType, typename... Args>
std::shared_ptr<AssetType> getAsset(const std::filesystem::path& path, Args... args) {
if (_assets.contains(path.string())) {
if (auto asset = _assets.at(path.string()).lock()) {
std::shared_ptr<AssetType> getAsset(const std::string& path, Args... args) {
if (_assets.contains(path)) {
if (auto asset = _assets.at(path).lock()) {
return std::static_pointer_cast<AssetType>(asset);
}
}

auto asset = std::make_shared<AssetType>(path, args...);
_assetFilePaths[asset.get()] = path;
_assets[path.string()] = asset;
_assets[path] = asset;
return asset;
}

template<typename AssetType>
void addAsset(const std::string& path, std::shared_ptr<AssetType> asset) {
_assets[path] = asset;
_assetFilePaths[(void *)asset.get()] = path;
}

template<typename AssetType>
std::optional<std::string> getFilePath(AssetType* asset) {
void* ptr = (void *)(asset);
Expand Down
1 change: 1 addition & 0 deletions src/AssetManager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(
${CMAKE_CURRENT_LIST_DIR}/AssetManager.cpp
${CMAKE_CURRENT_LIST_DIR}/Shader.cpp
${CMAKE_CURRENT_LIST_DIR}/Mesh.cpp
${CMAKE_CURRENT_LIST_DIR}/Wireframe.cpp
${CMAKE_CURRENT_LIST_DIR}/Material.cpp
)

Expand Down
140 changes: 134 additions & 6 deletions src/AssetManager/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <functional>

#include <AssetManager/Mesh.hpp>
// Assuming you are using a simple OBJ loader. You could integrate a library like Assimp for more advanced formats.
#include <tiny_obj_loader.h>

namespace std {
Expand All @@ -19,11 +18,14 @@ namespace std {

namespace shkyera {

// Constructor that loads the model from a file
Mesh::Mesh(const std::string& filepath) {
loadFromFile(filepath);
}

Mesh::Mesh(const std::vector<Vertex>& vertices, std::vector<uint32_t> indices) {
uploadToGPU(vertices, indices);
}

// Destructor to clean up OpenGL resources
Mesh::~Mesh() {
glDeleteVertexArrays(1, &_vao);
Expand All @@ -45,9 +47,7 @@ static std::vector<glm::vec3> calculateNormals(const std::vector<Mesh::Vertex>&
glm::vec3 v1 = vertices[idx1].position;
glm::vec3 v2 = vertices[idx2].position;

glm::vec3 edge1 = v1 - v0;
glm::vec3 edge2 = v2 - v0;
glm::vec3 faceNormal = glm::normalize(glm::cross(edge1, edge2));
glm::vec3 faceNormal = glm::normalize(glm::cross(v1 - v0, v2 - v0));

faceNormals[v0].push_back(faceNormal);
faceNormals[v1].push_back(faceNormal);
Expand All @@ -60,7 +60,6 @@ static std::vector<glm::vec3> calculateNormals(const std::vector<Mesh::Vertex>&
const glm::vec3& position = entry.first;
const std::vector<glm::vec3>& normals = entry.second;

// Average the face normals for each position
glm::vec3 averagedNormal = glm::vec3(0.0f);
for (const glm::vec3& normal : normals) {
averagedNormal += normal;
Expand Down Expand Up @@ -169,4 +168,133 @@ void Mesh::uploadToGPU(const std::vector<Vertex>& vertices, const std::vector<un
glBindVertexArray(0);
}

Mesh* Mesh::Factory::createCube() {
std::vector<Vertex> vertices = {
// Front face
{ { -1.0f, -1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 0.0f } }, // 0
{ { 1.0f, -1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 1.0f, 0.0f } }, // 1
{ { 1.0f, 1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 1.0f, 1.0f } }, // 2
{ { -1.0f, 1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 1.0f } }, // 3

// Back face
{ { -1.0f, -1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f } }, // 4
{ { 1.0f, -1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 0.0f } }, // 5
{ { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } }, // 6
{ { -1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } }, // 7

// Left face
{ { -1.0f, -1.0f, 1.0f }, { -1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } }, // 8
{ { -1.0f, 1.0f, 1.0f }, { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } }, // 9
{ { -1.0f, 1.0f, -1.0f }, { -1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f } }, // 10
{ { -1.0f, -1.0f, -1.0f }, { -1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f } }, // 11

// Right face
{ { 1.0f, -1.0f, -1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f } }, // 12
{ { 1.0f, 1.0f, -1.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f } }, // 13
{ { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } }, // 14
{ { 1.0f, -1.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } }, // 15

// Top face
{ { -1.0f, 1.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } }, // 16
{ { 1.0f, 1.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } }, // 17
{ { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } }, // 18
{ { -1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } }, // 19

// Bottom face
{ { -1.0f, -1.0f, -1.0f }, { 0.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } }, // 20
{ { 1.0f, -1.0f, -1.0f }, { 0.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } }, // 21
{ { 1.0f, -1.0f, 1.0f }, { 0.0f, -1.0f, 0.0f }, { 1.0f, 1.0f } }, // 22
{ { -1.0f, -1.0f, 1.0f }, { 0.0f, -1.0f, 0.0f }, { 0.0f, 1.0f } }, // 23
};

std::vector<unsigned int> indices = {
// Front face
0, 1, 2, 2, 3, 0,
// Back face
4, 5, 6, 6, 7, 4,
// Left face
8, 11, 10, 10, 9, 8,
// Right face
12, 13, 14, 14, 15, 12,
// Top face
16, 17, 18, 18, 19, 16,
// Bottom face
20, 23, 22, 22, 21, 20,
};

return new Mesh(vertices, indices);
}

Mesh* Mesh::Factory::createCylinder() {
const int sectors = 36;
const float radius = 1.0f;
const float height = 2.0f;

std::vector<Vertex> vertices;
std::vector<unsigned int> indices;

// Generate vertices
for (int i = 0; i <= sectors; ++i) {
float theta = 2.0f * M_PI * float(i) / float(sectors);
float x = radius * cos(theta);
float z = radius * sin(theta);

vertices.push_back({ { x, -height / 2, z }, { x, 0.0f, z }, { float(i) / sectors, 0.0f } });
vertices.push_back({ { x, height / 2, z }, { x, 0.0f, z }, { float(i) / sectors, 1.0f } });
}

// Generate indices for the sides
for (int i = 0; i < sectors; ++i) {
indices.push_back(i * 2);
indices.push_back(i * 2 + 1);
indices.push_back((i * 2 + 2) % (sectors * 2));

indices.push_back(i * 2 + 1);
indices.push_back((i * 2 + 3) % (sectors * 2));
indices.push_back((i * 2 + 2) % (sectors * 2));
}

return new Mesh(vertices, indices);
}

Mesh* Mesh::Factory::createSphere() {
const int stacks = 18;
const int sectors = 36;
const float radius = 1.0f;

std::vector<Vertex> vertices;
std::vector<unsigned int> indices;

for (int i = 0; i <= stacks; ++i) {
float stackAngle = M_PI / 2.0f - i * (M_PI / stacks);
float xy = radius * cosf(stackAngle);
float z = radius * sinf(stackAngle);

for (int j = 0; j <= sectors; ++j) {
float sectorAngle = j * (2.0f * M_PI / sectors);
float x = xy * cosf(sectorAngle);
float y = xy * sinf(sectorAngle);

vertices.push_back({ { x, y, z }, { x / radius, y / radius, z / radius }, { float(j) / sectors, float(i) / stacks } });
}
}

for (int i = 0; i < stacks; ++i) {
for (int j = 0; j < sectors; ++j) {
unsigned int first = (i * (sectors + 1)) + j;
unsigned int second = first + sectors + 1;

indices.push_back(first);
indices.push_back(second);
indices.push_back(first + 1);

indices.push_back(second);
indices.push_back(second + 1);
indices.push_back(first + 1);
}
}

return new Mesh(vertices, indices);
}

}
9 changes: 7 additions & 2 deletions src/AssetManager/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Mesh : public Asset {
};

Mesh(const std::string& filepath);
Mesh(const std::vector<Vertex>& vertices, std::vector<uint32_t> indices);
~Mesh();

void bind() const { glBindVertexArray(_vao); }
Expand All @@ -30,8 +31,12 @@ class Mesh : public Asset {
GLuint getEBO() const { return _ebo; }
GLsizei getMeshSize() const { return _meshSize; }

std::vector<float> _vertices;
std::vector<unsigned int> _indices;
class Factory {
public:
static Mesh* createCube();
static Mesh* createCylinder();
static Mesh* createSphere();
};

private:
void loadFromFile(const std::string& filepath);
Expand Down
Loading

0 comments on commit 54c3874

Please sign in to comment.