Autoformat everything + minor include fixes
This commit is contained in:
parent
8e4eacf48f
commit
9c1417a8f9
@ -9,13 +9,14 @@
|
|||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
struct Camera {
|
struct Camera {
|
||||||
/// Create a camera with a field of view (in degrees), width and height (in any unit) and near and far distances
|
/// Create a camera with a field of view (in degrees), width and height (in any unit) and near
|
||||||
Camera(float fov, float width, float height, float near, float far) : projection(
|
/// and far distances
|
||||||
glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
Camera(float fov, float width, float height, float near, float far)
|
||||||
|
: projection(glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
||||||
|
|
||||||
glm::mat4 projection;
|
glm::mat4 projection;
|
||||||
|
|
||||||
glm::mat4 view;
|
glm::mat4 view;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_CAMERA_H
|
#endif // ECSGAME_CAMERA_H
|
||||||
|
@ -5,10 +5,12 @@
|
|||||||
#ifndef ECSGAME_DIRECTIONALLIGHT_H
|
#ifndef ECSGAME_DIRECTIONALLIGHT_H
|
||||||
#define ECSGAME_DIRECTIONALLIGHT_H
|
#define ECSGAME_DIRECTIONALLIGHT_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
struct DirectionalLight {
|
struct DirectionalLight {
|
||||||
explicit DirectionalLight(const glm::vec3 &direction) : direction(direction) {}
|
explicit DirectionalLight(const glm::vec3 &direction) : direction(direction) {}
|
||||||
|
|
||||||
glm::vec3 direction;
|
glm::vec3 direction;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_DIRECTIONALLIGHT_H
|
#endif // ECSGAME_DIRECTIONALLIGHT_H
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
#ifndef ECSGAME_LODOBJMESH_H
|
#ifndef ECSGAME_LODOBJMESH_H
|
||||||
#define ECSGAME_LODOBJMESH_H
|
#define ECSGAME_LODOBJMESH_H
|
||||||
|
|
||||||
|
#include "ObjMesh.h"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "ObjMesh.h"
|
|
||||||
|
|
||||||
struct LODObjMesh {
|
struct LODObjMesh {
|
||||||
explicit LODObjMesh(std::vector<ObjMesh> meshes) : meshes(std::move(meshes)) {}
|
explicit LODObjMesh(std::vector<ObjMesh> meshes) : meshes(std::move(meshes)) {}
|
||||||
@ -15,4 +15,4 @@ struct LODObjMesh {
|
|||||||
std::vector<ObjMesh> meshes;
|
std::vector<ObjMesh> meshes;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_LODOBJMESH_H
|
#endif // ECSGAME_LODOBJMESH_H
|
||||||
|
@ -5,13 +5,15 @@
|
|||||||
#ifndef ECSGAME_MESH_H
|
#ifndef ECSGAME_MESH_H
|
||||||
#define ECSGAME_MESH_H
|
#define ECSGAME_MESH_H
|
||||||
|
|
||||||
#include <glad/glad.h>
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct Mesh {
|
struct Mesh {
|
||||||
explicit Mesh(const std::vector<float> &_vertices, const std::vector<unsigned int> &_indices) : vertex_count(_indices.size()) {
|
explicit Mesh(const std::vector<float> &_vertices, const std::vector<unsigned int> &_indices)
|
||||||
// Copy the vertices into a local classic float array. Nothing was displayed without this, maybe
|
: vertex_count(_indices.size()) {
|
||||||
|
// Copy the vertices into a local classic float array. Nothing was displayed without this,
|
||||||
|
// maybe
|
||||||
// due to weird hidden type incompatibility or out of scope issues?
|
// due to weird hidden type incompatibility or out of scope issues?
|
||||||
float vertices[_vertices.size()];
|
float vertices[_vertices.size()];
|
||||||
std::copy(_vertices.begin(), _vertices.end(), vertices);
|
std::copy(_vertices.begin(), _vertices.end(), vertices);
|
||||||
@ -23,7 +25,8 @@ struct Mesh {
|
|||||||
glGenBuffers(1, &VBO);
|
glGenBuffers(1, &VBO);
|
||||||
glGenBuffers(1, &EBO);
|
glGenBuffers(1, &EBO);
|
||||||
|
|
||||||
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
|
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then
|
||||||
|
// configure vertex attributes(s).
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
@ -33,23 +36,27 @@ struct Mesh {
|
|||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// position attribute
|
// position attribute
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// Normal attribute
|
// Normal attribute
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(3 * sizeof(float)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
||||||
|
(void *)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
// texture coord attribute
|
// texture coord attribute
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(6 * sizeof(float)));
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
||||||
|
(void *)(6 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
// Tangent attribute
|
// Tangent attribute
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(8 * sizeof(float)));
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
||||||
|
(void *)(8 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(3);
|
glEnableVertexAttribArray(3);
|
||||||
|
|
||||||
// Bitangent attribute
|
// Bitangent attribute
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(11 * sizeof(float)));
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
||||||
|
(void *)(11 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(4);
|
glEnableVertexAttribArray(4);
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@ -61,11 +68,11 @@ struct Mesh {
|
|||||||
glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int EBO;
|
unsigned int EBO;
|
||||||
unsigned int VBO;
|
unsigned int VBO;
|
||||||
unsigned int VAO;
|
unsigned int VAO;
|
||||||
unsigned int vertex_count;
|
unsigned int vertex_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MESH_H
|
#endif // ECSGAME_MESH_H
|
||||||
|
@ -5,6 +5,9 @@
|
|||||||
#ifndef ECSGAME_MOUSELOOK_H
|
#ifndef ECSGAME_MOUSELOOK_H
|
||||||
#define ECSGAME_MOUSELOOK_H
|
#define ECSGAME_MOUSELOOK_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/quaternion.hpp>
|
||||||
|
|
||||||
struct MouseLook {
|
struct MouseLook {
|
||||||
explicit MouseLook(float sensitivity) : sensitivity(sensitivity) {}
|
explicit MouseLook(float sensitivity) : sensitivity(sensitivity) {}
|
||||||
|
|
||||||
@ -18,4 +21,4 @@ struct MouseLook {
|
|||||||
bool is_active = true;
|
bool is_active = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MOUSELOOK_H
|
#endif // ECSGAME_MOUSELOOK_H
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#ifndef ECSGAME_MOVEMENT_H
|
#ifndef ECSGAME_MOVEMENT_H
|
||||||
#define ECSGAME_MOVEMENT_H
|
#define ECSGAME_MOVEMENT_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
struct Movement {
|
struct Movement {
|
||||||
Movement(glm::vec3 speed) : speed(speed) {}
|
Movement(glm::vec3 speed) : speed(speed) {}
|
||||||
|
|
||||||
@ -17,4 +19,4 @@ struct Movement {
|
|||||||
bool is_active = true;
|
bool is_active = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MOVEMENT_H
|
#endif // ECSGAME_MOVEMENT_H
|
||||||
|
@ -5,29 +5,31 @@
|
|||||||
#ifndef ECSGAME_OBJMESH_H
|
#ifndef ECSGAME_OBJMESH_H
|
||||||
#define ECSGAME_OBJMESH_H
|
#define ECSGAME_OBJMESH_H
|
||||||
|
|
||||||
#include "Mesh.h"
|
|
||||||
#include "../../Util/OBJ_Loader.h"
|
#include "../../Util/OBJ_Loader.h"
|
||||||
|
#include "Mesh.h"
|
||||||
|
|
||||||
struct ObjMesh : public Mesh {
|
struct ObjMesh : public Mesh {
|
||||||
struct Settings {
|
struct Settings {
|
||||||
Settings() = default;
|
Settings() = default;
|
||||||
|
|
||||||
Settings(float minDistanceForRender, float maxDistanceForRender, float diffuse, float specular)
|
Settings(float minDistanceForRender, float maxDistanceForRender, float diffuse,
|
||||||
: minDistanceForRender(minDistanceForRender), maxDistanceForRender(maxDistanceForRender) {}
|
float specular)
|
||||||
|
: minDistanceForRender(minDistanceForRender),
|
||||||
|
maxDistanceForRender(maxDistanceForRender) {}
|
||||||
|
|
||||||
float minDistanceForRender = 0.0;
|
float minDistanceForRender = 0.0;
|
||||||
float maxDistanceForRender = 1000.0;
|
float maxDistanceForRender = 1000.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit ObjMesh(const std::string &path, const Settings &settings) : Mesh(getVerticesFromFile(path), getIndicesFromFile(path)),
|
explicit ObjMesh(const std::string &path, const Settings &settings)
|
||||||
minDistance(settings.minDistanceForRender),
|
: Mesh(getVerticesFromFile(path), getIndicesFromFile(path)),
|
||||||
maxDistance(settings.maxDistanceForRender) {}
|
minDistance(settings.minDistanceForRender), maxDistance(settings.maxDistanceForRender) {}
|
||||||
|
|
||||||
float minDistance;
|
float minDistance;
|
||||||
|
|
||||||
float maxDistance;
|
float maxDistance;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::vector<float> getVerticesFromFile(const std::string &path) {
|
static std::vector<float> getVerticesFromFile(const std::string &path) {
|
||||||
objl::Loader loader;
|
objl::Loader loader;
|
||||||
|
|
||||||
@ -65,13 +67,16 @@ private:
|
|||||||
return vertexData;
|
return vertexData;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
std::cout
|
||||||
|
<< "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<std::vector<glm::vec3>, std::vector<glm::vec3>> getTangentsFromVertices(const std::vector<objl::Vertex> &vertices, const std::vector<unsigned int> indices) {
|
static std::pair<std::vector<glm::vec3>, std::vector<glm::vec3>>
|
||||||
// These vectors hold tangents and bitangents in the same way in which the vertices vectors holds vertices:
|
getTangentsFromVertices(const std::vector<objl::Vertex> &vertices,
|
||||||
// Their index in the vector corresponds to indices in the indices vector.
|
const std::vector<unsigned int> indices) {
|
||||||
|
// These vectors hold tangents and bitangents in the same way in which the vertices vectors
|
||||||
|
// holds vertices: Their index in the vector corresponds to indices in the indices vector.
|
||||||
std::vector<glm::vec3> tangents(vertices.size());
|
std::vector<glm::vec3> tangents(vertices.size());
|
||||||
std::vector<glm::vec3> bitangents(vertices.size());
|
std::vector<glm::vec3> bitangents(vertices.size());
|
||||||
|
|
||||||
@ -83,17 +88,26 @@ private:
|
|||||||
uint i2 = indices[index + 2];
|
uint i2 = indices[index + 2];
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
glm::vec3 vertex0 = glm::vec3(vertices[i0].Position.X, vertices[i0].Position.Y, vertices[i0].Position.Z);
|
glm::vec3 vertex0 = glm::vec3(vertices[i0].Position.X, vertices[i0].Position.Y,
|
||||||
glm::vec3 vertex1 = glm::vec3(vertices[i1].Position.X, vertices[i1].Position.Y, vertices[i1].Position.Z);
|
vertices[i0].Position.Z);
|
||||||
glm::vec3 vertex2 = glm::vec3(vertices[i2].Position.X, vertices[i2].Position.Y, vertices[i2].Position.Z);
|
glm::vec3 vertex1 = glm::vec3(vertices[i1].Position.X, vertices[i1].Position.Y,
|
||||||
|
vertices[i1].Position.Z);
|
||||||
|
glm::vec3 vertex2 = glm::vec3(vertices[i2].Position.X, vertices[i2].Position.Y,
|
||||||
|
vertices[i2].Position.Z);
|
||||||
|
|
||||||
glm::vec2 uv0 = glm::vec2(vertices[i0].TextureCoordinate.X, vertices[i0].TextureCoordinate.Y);
|
glm::vec2 uv0 =
|
||||||
glm::vec2 uv1 = glm::vec2(vertices[i1].TextureCoordinate.X, vertices[i1].TextureCoordinate.Y);
|
glm::vec2(vertices[i0].TextureCoordinate.X, vertices[i0].TextureCoordinate.Y);
|
||||||
glm::vec2 uv2 = glm::vec2(vertices[i2].TextureCoordinate.X, vertices[i2].TextureCoordinate.Y);
|
glm::vec2 uv1 =
|
||||||
|
glm::vec2(vertices[i1].TextureCoordinate.X, vertices[i1].TextureCoordinate.Y);
|
||||||
|
glm::vec2 uv2 =
|
||||||
|
glm::vec2(vertices[i2].TextureCoordinate.X, vertices[i2].TextureCoordinate.Y);
|
||||||
|
|
||||||
glm::vec3 normal0 = glm::vec3(vertices[i0].Normal.X, vertices[i0].Normal.Y, vertices[i0].Normal.Z);
|
glm::vec3 normal0 =
|
||||||
glm::vec3 normal1 = glm::vec3(vertices[i1].Normal.X, vertices[i1].Normal.Y, vertices[i1].Normal.Z);
|
glm::vec3(vertices[i0].Normal.X, vertices[i0].Normal.Y, vertices[i0].Normal.Z);
|
||||||
glm::vec3 normal2 = glm::vec3(vertices[i2].Normal.X, vertices[i2].Normal.Y, vertices[i2].Normal.Z);
|
glm::vec3 normal1 =
|
||||||
|
glm::vec3(vertices[i1].Normal.X, vertices[i1].Normal.Y, vertices[i1].Normal.Z);
|
||||||
|
glm::vec3 normal2 =
|
||||||
|
glm::vec3(vertices[i2].Normal.X, vertices[i2].Normal.Y, vertices[i2].Normal.Z);
|
||||||
|
|
||||||
// Edges of the triangle : position delta
|
// Edges of the triangle : position delta
|
||||||
glm::vec3 deltaPos1 = vertex1 - vertex0;
|
glm::vec3 deltaPos1 = vertex1 - vertex0;
|
||||||
@ -104,8 +118,8 @@ private:
|
|||||||
glm::vec2 deltaUV2 = uv2 - uv0;
|
glm::vec2 deltaUV2 = uv2 - uv0;
|
||||||
|
|
||||||
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
||||||
glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y)*r;
|
glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
|
||||||
glm::vec3 bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x)*r;
|
glm::vec3 bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;
|
||||||
|
|
||||||
// Add the tangent and bitangent to the current value in the array.
|
// Add the tangent and bitangent to the current value in the array.
|
||||||
// This is done to get an average in the end.
|
// This is done to get an average in the end.
|
||||||
@ -140,7 +154,8 @@ private:
|
|||||||
return indices;
|
return indices;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
std::cout
|
||||||
|
<< "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,12 +173,12 @@ private:
|
|||||||
|
|
||||||
// Print Material
|
// Print Material
|
||||||
std::cout << "Material: " << curMesh.MeshMaterial.name << "\n";
|
std::cout << "Material: " << curMesh.MeshMaterial.name << "\n";
|
||||||
std::cout << "Ambient Color: " << curMesh.MeshMaterial.Ka.X << ", " << curMesh.MeshMaterial.Ka.Y << ", "
|
std::cout << "Ambient Color: " << curMesh.MeshMaterial.Ka.X << ", "
|
||||||
<< curMesh.MeshMaterial.Ka.Z << "\n";
|
<< curMesh.MeshMaterial.Ka.Y << ", " << curMesh.MeshMaterial.Ka.Z << "\n";
|
||||||
std::cout << "Diffuse Color: " << curMesh.MeshMaterial.Kd.X << ", " << curMesh.MeshMaterial.Kd.Y << ", "
|
std::cout << "Diffuse Color: " << curMesh.MeshMaterial.Kd.X << ", "
|
||||||
<< curMesh.MeshMaterial.Kd.Z << "\n";
|
<< curMesh.MeshMaterial.Kd.Y << ", " << curMesh.MeshMaterial.Kd.Z << "\n";
|
||||||
std::cout << "Specular Color: " << curMesh.MeshMaterial.Ks.X << ", " << curMesh.MeshMaterial.Ks.Y << ", "
|
std::cout << "Specular Color: " << curMesh.MeshMaterial.Ks.X << ", "
|
||||||
<< curMesh.MeshMaterial.Ks.Z << "\n";
|
<< curMesh.MeshMaterial.Ks.Y << ", " << curMesh.MeshMaterial.Ks.Z << "\n";
|
||||||
std::cout << "Specular Exponent: " << curMesh.MeshMaterial.Ns << "\n";
|
std::cout << "Specular Exponent: " << curMesh.MeshMaterial.Ns << "\n";
|
||||||
std::cout << "Optical Density: " << curMesh.MeshMaterial.Ni << "\n";
|
std::cout << "Optical Density: " << curMesh.MeshMaterial.Ni << "\n";
|
||||||
std::cout << "Dissolve: " << curMesh.MeshMaterial.d << "\n";
|
std::cout << "Dissolve: " << curMesh.MeshMaterial.d << "\n";
|
||||||
@ -177,9 +192,10 @@ private:
|
|||||||
return vertexData;
|
return vertexData;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
std::cout
|
||||||
|
<< "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_OBJMESH_H
|
#endif // ECSGAME_OBJMESH_H
|
||||||
|
@ -26,9 +26,7 @@ struct PathMove {
|
|||||||
struct Views {
|
struct Views {
|
||||||
Views(std::vector<glm::quat> views) : views(views) {}
|
Views(std::vector<glm::quat> views) : views(views) {}
|
||||||
|
|
||||||
void add_view(glm::quat new_quat) {
|
void add_view(glm::quat new_quat) { views.emplace_back(new_quat); }
|
||||||
views.emplace_back(new_quat);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<glm::quat> views;
|
std::vector<glm::quat> views;
|
||||||
};
|
};
|
||||||
@ -40,7 +38,8 @@ struct PathMove {
|
|||||||
float time_passed = 0.0;
|
float time_passed = 0.0;
|
||||||
int current_point_index = 0;
|
int current_point_index = 0;
|
||||||
|
|
||||||
int speed_addition = 0; // 0, -1 or 1 depending on whether the speed should stay, decrease or increase
|
int speed_addition =
|
||||||
|
0; // 0, -1 or 1 depending on whether the speed should stay, decrease or increase
|
||||||
|
|
||||||
Path path;
|
Path path;
|
||||||
Views views;
|
Views views;
|
||||||
|
@ -5,11 +5,14 @@
|
|||||||
#ifndef ECSGAME_SINEANIMATION_H
|
#ifndef ECSGAME_SINEANIMATION_H
|
||||||
#define ECSGAME_SINEANIMATION_H
|
#define ECSGAME_SINEANIMATION_H
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
struct SineAnimation {
|
struct SineAnimation {
|
||||||
SineAnimation(const glm::vec3 &maxDistance, float speedScale) : maxDistance(maxDistance), speedScale(speedScale) {}
|
SineAnimation(const glm::vec3 &maxDistance, float speedScale)
|
||||||
|
: maxDistance(maxDistance), speedScale(speedScale) {}
|
||||||
|
|
||||||
glm::vec3 maxDistance;
|
glm::vec3 maxDistance;
|
||||||
float speedScale;
|
float speedScale;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_SINEANIMATION_H
|
#endif // ECSGAME_SINEANIMATION_H
|
||||||
|
@ -19,7 +19,7 @@ struct Texture {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Bind a texture from the given path and return its ID
|
// Bind a texture from the given path and return its ID
|
||||||
uint loadTexture(const std::string& path, Settings settings) {
|
uint loadTexture(const std::string &path, Settings settings) {
|
||||||
uint gl_id;
|
uint gl_id;
|
||||||
|
|
||||||
glGenTextures(1, &gl_id);
|
glGenTextures(1, &gl_id);
|
||||||
@ -41,15 +41,12 @@ struct Texture {
|
|||||||
if (data) {
|
if (data) {
|
||||||
// Alpha channel?
|
// Alpha channel?
|
||||||
unsigned int glChannels = GL_RGB;
|
unsigned int glChannels = GL_RGB;
|
||||||
if (nrChannels == 4) {
|
if (nrChannels == 4) { glChannels = GL_RGBA; }
|
||||||
glChannels = GL_RGBA;
|
|
||||||
}
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, data);
|
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels,
|
||||||
|
GL_UNSIGNED_BYTE, data);
|
||||||
|
|
||||||
if (settings.mipmaps) {
|
if (settings.mipmaps) { glGenerateMipmap(GL_TEXTURE_2D); }
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Failed to load texture" << std::endl;
|
std::cout << "Failed to load texture" << std::endl;
|
||||||
}
|
}
|
||||||
@ -59,11 +56,12 @@ struct Texture {
|
|||||||
return gl_id;
|
return gl_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit Texture(const std::string& path, Settings settings, bool transparent) : id(loadTexture(path, settings)), render_transparent(transparent) {}
|
explicit Texture(const std::string &path, Settings settings, bool transparent)
|
||||||
|
: id(loadTexture(path, settings)), render_transparent(transparent) {}
|
||||||
|
|
||||||
void addNormalmap(const std::string& path, Settings settings) {
|
void addNormalmap(const std::string &path, Settings settings) {
|
||||||
normal_id = loadTexture(path, settings);
|
normal_id = loadTexture(path, settings);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_TEXTURE_H
|
#endif // ECSGAME_TEXTURE_H
|
||||||
|
@ -17,33 +17,21 @@ struct Transform {
|
|||||||
glm::mat4 matrix = glm::mat4(1.0f); // Initialize as identity
|
glm::mat4 matrix = glm::mat4(1.0f); // Initialize as identity
|
||||||
glm::vec3 origin = glm::vec3(0.0f, 0.0f, 0.0f);
|
glm::vec3 origin = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
void translate(glm::vec3 offset) {
|
void translate(glm::vec3 offset) { matrix = glm::translate(matrix, offset); }
|
||||||
matrix = glm::translate(matrix, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 get_translation() {
|
glm::vec3 get_translation() { return glm::vec3(matrix[3]); }
|
||||||
return glm::vec3(matrix[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
void uniform_scale(float factor) {
|
void uniform_scale(float factor) { scale(glm::vec3(factor, factor, factor)); }
|
||||||
scale(glm::vec3(factor, factor, factor));
|
|
||||||
}
|
|
||||||
|
|
||||||
void scale(glm::vec3 factors) {
|
void scale(glm::vec3 factors) { matrix = glm::scale(matrix, factors); }
|
||||||
matrix = glm::scale(matrix, factors);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rotate(float degrees, glm::vec3 axis) {
|
void rotate(float degrees, glm::vec3 axis) {
|
||||||
matrix = glm::rotate(matrix, glm::radians(degrees), axis);
|
matrix = glm::rotate(matrix, glm::radians(degrees), axis);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_origin(glm::vec3 position) {
|
void set_origin(glm::vec3 position) { origin = position; }
|
||||||
origin = position;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_to_origin(glm::vec3 addition) {
|
void add_to_origin(glm::vec3 addition) { origin += addition; }
|
||||||
origin += addition;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_rotation_from_quat(glm::quat quaternion) {
|
void set_rotation_from_quat(glm::quat quaternion) {
|
||||||
// Remember translation
|
// Remember translation
|
||||||
@ -53,21 +41,13 @@ struct Transform {
|
|||||||
matrix[3] = save;
|
matrix[3] = save;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 get_origin() const {
|
glm::vec3 get_origin() const { return origin; }
|
||||||
return origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 forward() const {
|
glm::vec3 forward() const { return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0); }
|
||||||
return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 up() const {
|
glm::vec3 up() const { return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0); }
|
||||||
return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 right() const {
|
glm::vec3 right() const { return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0); }
|
||||||
return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_TRANSFORM_H
|
#endif // ECSGAME_TRANSFORM_H
|
||||||
|
@ -10,4 +10,4 @@ struct InputEvent {
|
|||||||
int action;
|
int action;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_INPUTEVENT_H
|
#endif // ECSGAME_INPUTEVENT_H
|
||||||
|
@ -10,4 +10,4 @@ struct MouseMoveEvent {
|
|||||||
double newY;
|
double newY;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_INPUTEVENT_H
|
#endif // ECSGAME_INPUTEVENT_H
|
||||||
|
@ -5,16 +5,14 @@
|
|||||||
#ifndef ECSGAME_GRAVITYSYSTEM_H
|
#ifndef ECSGAME_GRAVITYSYSTEM_H
|
||||||
#define ECSGAME_GRAVITYSYSTEM_H
|
#define ECSGAME_GRAVITYSYSTEM_H
|
||||||
|
|
||||||
#include "../ECS.h"
|
|
||||||
#include "../Components/Transform.h"
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
class GravitySystem : public EntitySystem {
|
class GravitySystem : public EntitySystem {
|
||||||
public:
|
public:
|
||||||
explicit GravitySystem(float amount) {
|
explicit GravitySystem(float amount) { gravityAmount = amount; }
|
||||||
gravityAmount = amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> position) {
|
pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> position) {
|
||||||
@ -22,8 +20,8 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float gravityAmount;
|
float gravityAmount;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_GRAVITYSYSTEM_H
|
#endif // ECSGAME_GRAVITYSYSTEM_H
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__
|
#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__
|
||||||
#define __PATHMOVEMENTSWITCHSYSTEM_H__
|
#define __PATHMOVEMENTSWITCHSYSTEM_H__
|
||||||
|
|
||||||
#include <glad/glad.h>
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../ECS.h"
|
#include "../Components/MouseLook.h"
|
||||||
|
#include "../Components/Movement.h"
|
||||||
#include "../Components/PathMove.h"
|
#include "../Components/PathMove.h"
|
||||||
|
#include "../ECS.h"
|
||||||
#include "../Events/InputEvent.h"
|
#include "../Events/InputEvent.h"
|
||||||
#include "../Components/Movement.h"
|
|
||||||
#include "../Components/MouseLook.h"
|
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -23,36 +23,39 @@ class InteractivePathSystem : public EntitySystem, public EventSubscriber<InputE
|
|||||||
|
|
||||||
void receive(World *pWorld, const InputEvent &event) override {
|
void receive(World *pWorld, const InputEvent &event) override {
|
||||||
if (event.key == GLFW_KEY_P) {
|
if (event.key == GLFW_KEY_P) {
|
||||||
myWorld->each<PathMove, Movement, MouseLook>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook) {
|
myWorld->each<PathMove, Movement, MouseLook>(
|
||||||
if (event.action == GLFW_PRESS) {
|
[&](Entity *ent, ComponentHandle<PathMove> pathmove,
|
||||||
// Switch between them
|
ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook) {
|
||||||
if (pathmove->is_active) {
|
if (event.action == GLFW_PRESS) {
|
||||||
pathmove->is_active = false;
|
// Switch between them
|
||||||
movement->is_active = true;
|
if (pathmove->is_active) {
|
||||||
mouselook->is_active = true;
|
pathmove->is_active = false;
|
||||||
} else {
|
movement->is_active = true;
|
||||||
pathmove->is_active = true;
|
mouselook->is_active = true;
|
||||||
movement->is_active = false;
|
} else {
|
||||||
mouselook->is_active = false;
|
pathmove->is_active = true;
|
||||||
|
movement->is_active = false;
|
||||||
|
mouselook->is_active = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
} else if (event.key == GLFW_KEY_R) {
|
} else if (event.key == GLFW_KEY_R) {
|
||||||
myWorld->each<PathMove, Movement, MouseLook, Transform>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook, ComponentHandle<Transform> transform) {
|
myWorld->each<PathMove, Movement, MouseLook, Transform>(
|
||||||
if (event.action == GLFW_PRESS) {
|
[&](Entity *ent, ComponentHandle<PathMove> pathmove,
|
||||||
// Add this point to the path
|
ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook,
|
||||||
pathmove->path.add_point(transform->origin);
|
ComponentHandle<Transform> transform) {
|
||||||
pathmove->views.add_view(mouselook->rotation);
|
if (event.action == GLFW_PRESS) {
|
||||||
}
|
// Add this point to the path
|
||||||
});
|
pathmove->path.add_point(transform->origin);
|
||||||
|
pathmove->views.add_view(mouselook->rotation);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override {
|
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
||||||
pWorld->unsubscribeAll(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
#ifndef ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
#ifndef ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
||||||
#define ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
#define ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
||||||
|
|
||||||
#include <glad/glad.h>
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../ECS.h"
|
#include "../Components/Movement.h"
|
||||||
#include "../Components/Transform.h"
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
#include "../Events/InputEvent.h"
|
#include "../Events/InputEvent.h"
|
||||||
#include "../Components/Movement.h"
|
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -61,20 +61,20 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform, Movement>(
|
pWorld->each<Transform, Movement>([&](Entity *ent, ComponentHandle<Transform> transform,
|
||||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
|
ComponentHandle<Movement> movement) {
|
||||||
if (!movement->is_active) return;
|
if (!movement->is_active) return;
|
||||||
|
|
||||||
transform->add_to_origin(transform->matrix * glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0));
|
transform->add_to_origin(
|
||||||
});
|
transform->matrix *
|
||||||
|
glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override {
|
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
||||||
pWorld->unsubscribeAll(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
#endif // ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
||||||
|
@ -5,15 +5,16 @@
|
|||||||
#ifndef ECSGAME_MOUSELOOKSYSTEM_H
|
#ifndef ECSGAME_MOUSELOOKSYSTEM_H
|
||||||
#define ECSGAME_MOUSELOOKSYSTEM_H
|
#define ECSGAME_MOUSELOOKSYSTEM_H
|
||||||
|
|
||||||
#include "../ECS.h"
|
#include "../Components/Camera.h"
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../Components/MouseLook.h"
|
#include "../Components/MouseLook.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
#include "../Events/MouseMoveEvent.h"
|
#include "../Events/MouseMoveEvent.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
class MouseLookSystem : public EntitySystem, public EventSubscriber<MouseMoveEvent> {
|
class MouseLookSystem : public EntitySystem, public EventSubscriber<MouseMoveEvent> {
|
||||||
public:
|
public:
|
||||||
explicit MouseLookSystem(int width, int height) : lastX(width / 2.0), lastY(height / 2.0) {}
|
explicit MouseLookSystem(int width, int height) : lastX(width / 2.0), lastY(height / 2.0) {}
|
||||||
|
|
||||||
void configure(World *pWorld) override {
|
void configure(World *pWorld) override {
|
||||||
@ -22,44 +23,46 @@ public:
|
|||||||
myWorld->subscribe<MouseMoveEvent>(this);
|
myWorld->subscribe<MouseMoveEvent>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override {
|
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
||||||
pWorld->unsubscribeAll(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void receive(World *pWorld, const MouseMoveEvent &event) override {
|
void receive(World *pWorld, const MouseMoveEvent &event) override {
|
||||||
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
pWorld->each<Transform, MouseLook, Camera>(
|
||||||
double xOffset = lastX - event.newX;
|
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse,
|
||||||
double yOffset = lastY - event.newY;
|
ComponentHandle<Camera> camera) {
|
||||||
|
double xOffset = lastX - event.newX;
|
||||||
|
double yOffset = lastY - event.newY;
|
||||||
|
|
||||||
lastX = event.newX;
|
lastX = event.newX;
|
||||||
lastY = event.newY;
|
lastY = event.newY;
|
||||||
|
|
||||||
mouse->yaw += xOffset * mouse->sensitivity;
|
mouse->yaw += xOffset * mouse->sensitivity;
|
||||||
mouse->pitch += yOffset * mouse->sensitivity;
|
mouse->pitch += yOffset * mouse->sensitivity;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
pWorld->each<Transform, MouseLook, Camera>(
|
||||||
if (!mouse->is_active) return;
|
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse,
|
||||||
|
ComponentHandle<Camera> camera) {
|
||||||
|
if (!mouse->is_active) return;
|
||||||
|
|
||||||
if(mouse->pitch > 80.0f)
|
if (mouse->pitch > 80.0f) mouse->pitch = 80.0f;
|
||||||
mouse->pitch = 80.0f;
|
if (mouse->pitch < -80.0f) mouse->pitch = -80.0f;
|
||||||
if(mouse->pitch < -80.0f)
|
|
||||||
mouse->pitch = -80.0f;
|
|
||||||
|
|
||||||
mouse->rotation = glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.f, 0.f));
|
mouse->rotation =
|
||||||
mouse->rotation *= glm::angleAxis(glm::radians((float)mouse->pitch), glm::vec3(1.f, 0.f, 0.f));
|
glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
mouse->rotation *=
|
||||||
|
glm::angleAxis(glm::radians((float)mouse->pitch), glm::vec3(1.f, 0.f, 0.f));
|
||||||
|
|
||||||
transform->set_rotation_from_quat(mouse->rotation);
|
transform->set_rotation_from_quat(mouse->rotation);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double lastX;
|
double lastX;
|
||||||
double lastY;
|
double lastY;
|
||||||
|
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MOUSELOOKSYSTEM_H
|
#endif // ECSGAME_MOUSELOOKSYSTEM_H
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
#ifndef __PATHMOVESYSTEM_H__
|
#ifndef __PATHMOVESYSTEM_H__
|
||||||
#define __PATHMOVESYSTEM_H__
|
#define __PATHMOVESYSTEM_H__
|
||||||
|
|
||||||
#include <glad/glad.h>
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../ECS.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../Events/InputEvent.h"
|
|
||||||
#include "../Components/PathMove.h"
|
#include "../Components/PathMove.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
|
#include "../Events/InputEvent.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ using namespace ECS;
|
|||||||
float get_t(float alpha, float t, glm::vec3 p0, glm::vec3 p1) {
|
float get_t(float alpha, float t, glm::vec3 p0, glm::vec3 p1) {
|
||||||
float a = pow((p1.x - p0.x), 2.0f) + pow((p1.y - p0.y), 2.0f) + pow((p1.z - p0.z), 2.0f);
|
float a = pow((p1.x - p0.x), 2.0f) + pow((p1.y - p0.y), 2.0f) + pow((p1.z - p0.z), 2.0f);
|
||||||
float b = pow(a, alpha * 0.5f);
|
float b = pow(a, alpha * 0.5f);
|
||||||
|
|
||||||
return (b + t);
|
return (b + t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,14 +34,14 @@ glm::vec3 catmul(float alpha, glm::vec3 p0, glm::vec3 p1, glm::vec3 p2, glm::vec
|
|||||||
// Lerp t to be between t1 and t2
|
// Lerp t to be between t1 and t2
|
||||||
t = t1 + t * (t2 - t1);
|
t = t1 + t * (t2 - t1);
|
||||||
|
|
||||||
glm::vec3 A1 = (t1-t)/(t1-t0)*p0 + (t-t0)/(t1-t0)*p1;
|
glm::vec3 A1 = (t1 - t) / (t1 - t0) * p0 + (t - t0) / (t1 - t0) * p1;
|
||||||
glm::vec3 A2 = (t2-t)/(t2-t1)*p1 + (t-t1)/(t2-t1)*p2;
|
glm::vec3 A2 = (t2 - t) / (t2 - t1) * p1 + (t - t1) / (t2 - t1) * p2;
|
||||||
glm::vec3 A3 = (t3-t)/(t3-t2)*p2 + (t-t2)/(t3-t2)*p3;
|
glm::vec3 A3 = (t3 - t) / (t3 - t2) * p2 + (t - t2) / (t3 - t2) * p3;
|
||||||
|
|
||||||
glm::vec3 B1 = (t2-t)/(t2-t0)*A1 + (t-t0)/(t2-t0)*A2;
|
glm::vec3 B1 = (t2 - t) / (t2 - t0) * A1 + (t - t0) / (t2 - t0) * A2;
|
||||||
glm::vec3 B2 = (t3-t)/(t3-t1)*A2 + (t-t1)/(t3-t1)*A3;
|
glm::vec3 B2 = (t3 - t) / (t3 - t1) * A2 + (t - t1) / (t3 - t1) * A3;
|
||||||
|
|
||||||
glm::vec3 C = (t2-t)/(t2-t1)*B1 + (t-t1)/(t2-t1)*B2;
|
glm::vec3 C = (t2 - t) / (t2 - t1) * B1 + (t - t1) / (t2 - t1) * B2;
|
||||||
|
|
||||||
return C;
|
return C;
|
||||||
}
|
}
|
||||||
@ -75,113 +75,113 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform, PathMove>(
|
pWorld->each<Transform, PathMove>([&](Entity *ent, ComponentHandle<Transform> transform,
|
||||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> pathmove) {
|
ComponentHandle<PathMove> pathmove) {
|
||||||
if (!pathmove->is_active) return;
|
if (!pathmove->is_active) return;
|
||||||
|
|
||||||
// Handle change in speed
|
// Handle change in speed
|
||||||
pathmove->speed += pathmove->speed_addition * deltaTime;
|
pathmove->speed += pathmove->speed_addition * deltaTime;
|
||||||
pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0);
|
pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0);
|
||||||
|
|
||||||
// Shorthand for the path (we'll use this a lot)
|
// Shorthand for the path (we'll use this a lot)
|
||||||
PathMove::Path path = pathmove->path;
|
PathMove::Path path = pathmove->path;
|
||||||
|
|
||||||
// Add the passed time
|
// Add the passed time
|
||||||
float desired_distance = deltaTime * pathmove->speed; // TODO
|
float desired_distance = deltaTime * pathmove->speed; // TODO
|
||||||
pathmove->time_passed += desired_distance / path.distances[pathmove->current_point_index];
|
pathmove->time_passed +=
|
||||||
|
desired_distance / path.distances[pathmove->current_point_index];
|
||||||
|
|
||||||
// Shorthand for number of points in the path
|
// Shorthand for number of points in the path
|
||||||
int num_points = path.points.size();
|
int num_points = path.points.size();
|
||||||
|
|
||||||
if (pathmove->time_passed >= 1.0) {
|
if (pathmove->time_passed >= 1.0) {
|
||||||
// If we passed the last target, set the current_point_index to that target
|
// If we passed the last target, set the current_point_index to that target
|
||||||
pathmove->time_passed -= 1.0;
|
pathmove->time_passed -= 1.0;
|
||||||
pathmove->current_point_index += 1;
|
pathmove->current_point_index += 1;
|
||||||
|
|
||||||
// If the point index is greater than the second to last one, reset
|
// If the point index is greater than the second to last one, reset
|
||||||
// (The point index specifies the point we're coming from, not the one we're moving towards)
|
// (The point index specifies the point we're coming from, not the one we're moving
|
||||||
if (pathmove->current_point_index >= num_points - 1) {
|
// towards)
|
||||||
pathmove->current_point_index = 0;
|
if (pathmove->current_point_index >= num_points - 1) {
|
||||||
}
|
pathmove->current_point_index = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The four points which are needed for the spline
|
// The four points which are needed for the spline
|
||||||
// p1 and p2 are always the same (the current origin and the current target), but the rest depends on edge cases
|
// p1 and p2 are always the same (the current origin and the current target), but the
|
||||||
glm::vec3 p0;
|
// rest depends on edge cases
|
||||||
glm::vec3 p1 = path.points[pathmove->current_point_index];
|
glm::vec3 p0;
|
||||||
glm::vec3 p2 = path.points[pathmove->current_point_index + 1];
|
glm::vec3 p1 = path.points[pathmove->current_point_index];
|
||||||
glm::vec3 p3;
|
glm::vec3 p2 = path.points[pathmove->current_point_index + 1];
|
||||||
|
glm::vec3 p3;
|
||||||
|
|
||||||
if (pathmove->current_point_index == num_points - 2) {
|
if (pathmove->current_point_index == num_points - 2) {
|
||||||
// We're moving towards the last point, so the point after that needs to be interpolated.
|
// We're moving towards the last point, so the point after that needs to be
|
||||||
// We interpolate linearly along the line from this point to the target point.
|
// interpolated. We interpolate linearly along the line from this point to the
|
||||||
glm::vec3 interp_direction = p2 - p1;
|
// target point.
|
||||||
p3 = p2 + interp_direction * 2.0f;
|
glm::vec3 interp_direction = p2 - p1;
|
||||||
} else {
|
p3 = p2 + interp_direction * 2.0f;
|
||||||
// We're fine - use the point after the target for p3
|
} else {
|
||||||
p3 = path.points[pathmove->current_point_index + 2];
|
// We're fine - use the point after the target for p3
|
||||||
}
|
p3 = path.points[pathmove->current_point_index + 2];
|
||||||
|
}
|
||||||
|
|
||||||
if (pathmove->current_point_index == 0) {
|
if (pathmove->current_point_index == 0) {
|
||||||
// We're at the first point, so the point before this needs to be interpolated.
|
// We're at the first point, so the point before this needs to be interpolated.
|
||||||
// We interpolate linearly along the line from this to the next point (backwards).
|
// We interpolate linearly along the line from this to the next point (backwards).
|
||||||
glm::vec3 interp_direction = path.points[pathmove->current_point_index] - path.points[pathmove->current_point_index + 1];
|
glm::vec3 interp_direction = path.points[pathmove->current_point_index] -
|
||||||
p0 = path.points[pathmove->current_point_index] + interp_direction;
|
path.points[pathmove->current_point_index + 1];
|
||||||
} else {
|
p0 = path.points[pathmove->current_point_index] + interp_direction;
|
||||||
// We're fine - use the point before the current point
|
} else {
|
||||||
p0 = path.points[pathmove->current_point_index - 1];
|
// We're fine - use the point before the current point
|
||||||
}
|
p0 = path.points[pathmove->current_point_index - 1];
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate the point on the spline
|
// Calculate the point on the spline
|
||||||
glm::vec3 point = catmul(1.f,
|
glm::vec3 point = catmul(1.f, p0, p1, p2, p3, pathmove->time_passed);
|
||||||
p0,
|
|
||||||
p1,
|
|
||||||
p2,
|
|
||||||
p3,
|
|
||||||
pathmove->time_passed);
|
|
||||||
|
|
||||||
// Apply
|
|
||||||
transform->set_origin(point);
|
|
||||||
|
|
||||||
// Rotation
|
// Apply
|
||||||
// https://www.3dgep.com/understanding-quaternions/#SQUAD
|
transform->set_origin(point);
|
||||||
PathMove::Views views = pathmove->views;
|
|
||||||
|
|
||||||
// Similar procedure as with position to get the relevant values
|
// Rotation
|
||||||
glm::quat q0;
|
// https://www.3dgep.com/understanding-quaternions/#SQUAD
|
||||||
glm::quat q1 = views.views[pathmove->current_point_index];
|
PathMove::Views views = pathmove->views;
|
||||||
glm::quat q2 = views.views[pathmove->current_point_index + 1];
|
|
||||||
glm::quat q3;
|
|
||||||
|
|
||||||
if (pathmove->current_point_index == num_points - 2) {
|
// Similar procedure as with position to get the relevant values
|
||||||
// Interpolate what q3 would be if the change from q1 to q2 continues
|
glm::quat q0;
|
||||||
q3 = glm::fastMix(q1, q2, 2.0f);
|
glm::quat q1 = views.views[pathmove->current_point_index];
|
||||||
} else {
|
glm::quat q2 = views.views[pathmove->current_point_index + 1];
|
||||||
// We're fine - use the point after the target for p3
|
glm::quat q3;
|
||||||
q3 = views.views[pathmove->current_point_index + 2];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pathmove->current_point_index == 0) {
|
if (pathmove->current_point_index == num_points - 2) {
|
||||||
// Interpolate what q0 would be if the same change happened from q0 to q1 as from q1 to q2
|
// Interpolate what q3 would be if the change from q1 to q2 continues
|
||||||
q0 = glm::fastMix(q1, q2, -1.0f);
|
q3 = glm::fastMix(q1, q2, 2.0f);
|
||||||
} else {
|
} else {
|
||||||
// We're fine - use the point before the current point
|
// We're fine - use the point after the target for p3
|
||||||
q0 = views.views[pathmove->current_point_index - 1];
|
q3 = views.views[pathmove->current_point_index + 2];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interpolate
|
if (pathmove->current_point_index == 0) {
|
||||||
glm::quat result = glm::squad(q1, q2, glm::intermediate(q0, q1, q2), glm::intermediate(q1, q2, q3), pathmove->time_passed);
|
// Interpolate what q0 would be if the same change happened from q0 to q1 as from q1
|
||||||
|
// to q2
|
||||||
|
q0 = glm::fastMix(q1, q2, -1.0f);
|
||||||
|
} else {
|
||||||
|
// We're fine - use the point before the current point
|
||||||
|
q0 = views.views[pathmove->current_point_index - 1];
|
||||||
|
}
|
||||||
|
|
||||||
// Apply
|
// Interpolate
|
||||||
transform->set_rotation_from_quat(result);
|
glm::quat result = glm::squad(q1, q2, glm::intermediate(q0, q1, q2),
|
||||||
});
|
glm::intermediate(q1, q2, q3), pathmove->time_passed);
|
||||||
|
|
||||||
|
// Apply
|
||||||
|
transform->set_rotation_from_quat(result);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override {
|
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
||||||
pWorld->unsubscribeAll(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,22 +7,20 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../ECS.h"
|
|
||||||
#include "../Components/Transform.h"
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
class PositionDebugOutputSystem : public EntitySystem {
|
class PositionDebugOutputSystem : public EntitySystem {
|
||||||
public:
|
public:
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> transform) {
|
pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> transform) {
|
||||||
std::cout << ent->getEntityId() << ": "
|
std::cout << ent->getEntityId() << ": " << transform->get_origin().x << ", "
|
||||||
<< transform->get_origin().x << ", "
|
<< transform->get_origin().y << ", " << transform->get_origin().z
|
||||||
<< transform->get_origin().y << ", "
|
|
||||||
<< transform->get_origin().z
|
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_POSITIONDEBUGSYSTEM_H
|
#endif // ECSGAME_POSITIONDEBUGSYSTEM_H
|
||||||
|
@ -5,32 +5,28 @@
|
|||||||
#ifndef ECSGAME_RENDERSYSTEM_H
|
#ifndef ECSGAME_RENDERSYSTEM_H
|
||||||
#define ECSGAME_RENDERSYSTEM_H
|
#define ECSGAME_RENDERSYSTEM_H
|
||||||
|
|
||||||
#include "../ECS.h"
|
#include "../../Rendering/Material.h"
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../Components/Mesh.h"
|
|
||||||
#include "../Components/Camera.h"
|
|
||||||
#include "../../Rendering/Shader.h"
|
#include "../../Rendering/Shader.h"
|
||||||
|
#include "../Components/Camera.h"
|
||||||
|
#include "../Components/DirectionalLight.h"
|
||||||
|
#include "../Components/LODObjMesh.h"
|
||||||
|
#include "../Components/Mesh.h"
|
||||||
#include "../Components/ObjMesh.h"
|
#include "../Components/ObjMesh.h"
|
||||||
#include "../Components/Texture.h"
|
#include "../Components/Texture.h"
|
||||||
#include "../Components/LODObjMesh.h"
|
#include "../Components/Transform.h"
|
||||||
#include "../Components/DirectionalLight.h"
|
#include "../ECS.h"
|
||||||
#include "../../Rendering/Material.h"
|
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
// For Debugging
|
// For Debugging
|
||||||
unsigned int quadVAO = 0;
|
unsigned int quadVAO = 0;
|
||||||
unsigned int quadVBO;
|
unsigned int quadVBO;
|
||||||
void renderQuad()
|
void renderQuad() {
|
||||||
{
|
if (quadVAO == 0) {
|
||||||
if (quadVAO == 0)
|
|
||||||
{
|
|
||||||
float quadVertices[] = {
|
float quadVertices[] = {
|
||||||
// positions // texture Coords
|
// positions // texture Coords
|
||||||
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
|
||||||
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
|
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
||||||
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
|
||||||
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
|
||||||
};
|
};
|
||||||
// setup plane VAO
|
// setup plane VAO
|
||||||
glGenVertexArrays(1, &quadVAO);
|
glGenVertexArrays(1, &quadVAO);
|
||||||
@ -39,9 +35,10 @@ void renderQuad()
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
||||||
|
(void *)(3 * sizeof(float)));
|
||||||
}
|
}
|
||||||
glBindVertexArray(quadVAO);
|
glBindVertexArray(quadVAO);
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
@ -49,16 +46,13 @@ void renderQuad()
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
||||||
public:
|
public:
|
||||||
struct RenderObject {
|
struct RenderObject {
|
||||||
RenderObject(const glm::mat4 &matrix, const glm::vec3 &origin, unsigned int textureId, unsigned int normalId, const Mesh &mesh, float distance, const Material &material)
|
RenderObject(const glm::mat4 &matrix, const glm::vec3 &origin, unsigned int textureId,
|
||||||
: matrix(matrix),
|
unsigned int normalId, const Mesh &mesh, float distance,
|
||||||
origin(origin),
|
const Material &material)
|
||||||
texture_id(textureId),
|
: matrix(matrix), origin(origin), texture_id(textureId), normal_id(normalId),
|
||||||
normal_id(normalId),
|
mesh(mesh), distance(distance), material(material) {}
|
||||||
mesh(mesh),
|
|
||||||
distance(distance),
|
|
||||||
material(material) {}
|
|
||||||
|
|
||||||
void render(Shader shader) const {
|
void render(Shader shader) const {
|
||||||
glm::mat4 model_matrix = matrix;
|
glm::mat4 model_matrix = matrix;
|
||||||
@ -95,26 +89,33 @@ public:
|
|||||||
|
|
||||||
// Fill the passed vector with render objects of the given world
|
// Fill the passed vector with render objects of the given world
|
||||||
std::vector<std::vector<RenderObject>> getRenderObjects(World *world, glm::vec3 cameraPos) {
|
std::vector<std::vector<RenderObject>> getRenderObjects(World *world, glm::vec3 cameraPos) {
|
||||||
std::vector<std::vector<RenderObject>> renderObjects(2); // First normal, then transparent
|
std::vector<std::vector<RenderObject>> renderObjects(2); // First normal, then transparent
|
||||||
|
|
||||||
/*pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
|
/*pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh,
|
||||||
|
ComponentHandle<Transform> transform) {
|
||||||
renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), 0));
|
renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), 0));
|
||||||
});*/
|
});*/
|
||||||
|
|
||||||
/*// TODO: Is it possible to do get ObjMeshes in the Mesh loop above implicitly via polymorphism?
|
/*// TODO: Is it possible to do get ObjMeshes in the Mesh loop above implicitly via
|
||||||
* // TODO: Commented out because of double rendering - we only want to get objects that explicitly DON'T have a texture here!
|
polymorphism?
|
||||||
pWorld->each<ObjMesh, Transform>([&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform) {
|
* // TODO: Commented out because of double rendering - we only want to get objects that
|
||||||
// Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh
|
explicitly DON'T have a texture here! pWorld->each<ObjMesh, Transform>([&](Entity *ent,
|
||||||
float distance = glm::distance(cameraPos, transform->getPosition());
|
ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform) {
|
||||||
|
// Add the object to the renderObjects to draw if the distance is within the min and max
|
||||||
|
distance of the mesh float distance = glm::distance(cameraPos, transform->getPosition());
|
||||||
|
|
||||||
if (distance > mesh->minDistance && distance < mesh->maxDistance) {
|
if (distance > mesh->minDistance && distance < mesh->maxDistance) {
|
||||||
renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), distance));
|
renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(),
|
||||||
|
distance));
|
||||||
}
|
}
|
||||||
});*/
|
});*/
|
||||||
|
|
||||||
// ObjMesh with textures
|
// ObjMesh with textures
|
||||||
world->each<ObjMesh, Transform, Texture>([&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform, ComponentHandle<Texture> texture) {
|
world->each<ObjMesh, Transform, Texture>([&](Entity *ent, ComponentHandle<ObjMesh> mesh,
|
||||||
// Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh
|
ComponentHandle<Transform> transform,
|
||||||
|
ComponentHandle<Texture> texture) {
|
||||||
|
// Add the object to the renderObjects to draw if the distance is within the min and max
|
||||||
|
// distance of the mesh
|
||||||
float distance = glm::distance(cameraPos, transform->get_origin());
|
float distance = glm::distance(cameraPos, transform->get_origin());
|
||||||
|
|
||||||
if (distance > mesh->minDistance && distance < mesh->maxDistance) {
|
if (distance > mesh->minDistance && distance < mesh->maxDistance) {
|
||||||
@ -122,21 +123,29 @@ public:
|
|||||||
ComponentHandle<Texture> textureComponent = ent->get<Texture>();
|
ComponentHandle<Texture> textureComponent = ent->get<Texture>();
|
||||||
ComponentHandle<Material> materialComponent = ent->get<Material>();
|
ComponentHandle<Material> materialComponent = ent->get<Material>();
|
||||||
|
|
||||||
Material material = materialComponent.isValid() ? materialComponent.get() : Material();
|
Material material =
|
||||||
|
materialComponent.isValid() ? materialComponent.get() : Material();
|
||||||
unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0;
|
unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0;
|
||||||
unsigned int normalID = textureComponent.isValid() ? textureComponent->normal_id : 0;
|
unsigned int normalID =
|
||||||
|
textureComponent.isValid() ? textureComponent->normal_id : 0;
|
||||||
|
|
||||||
// Put it into the list of transparent render objects if the texture wants to be rendered transparently
|
// Put it into the list of transparent render objects if the texture wants to be
|
||||||
|
// rendered transparently
|
||||||
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
||||||
renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh.get(), distance, material));
|
renderObjects[1].emplace_back(
|
||||||
|
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
||||||
|
normalID, mesh.get(), distance, material));
|
||||||
} else {
|
} else {
|
||||||
renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh.get(), distance, material));
|
renderObjects[0].emplace_back(
|
||||||
|
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
||||||
|
normalID, mesh.get(), distance, material));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// LODObjMesh with Texture
|
// LODObjMesh with Texture
|
||||||
world->each<LODObjMesh, Transform>([&](Entity *ent, ComponentHandle<LODObjMesh> lodMesh, ComponentHandle<Transform> transform) {
|
world->each<LODObjMesh, Transform>([&](Entity *ent, ComponentHandle<LODObjMesh> lodMesh,
|
||||||
|
ComponentHandle<Transform> transform) {
|
||||||
float distance = glm::distance(cameraPos, transform->get_origin());
|
float distance = glm::distance(cameraPos, transform->get_origin());
|
||||||
|
|
||||||
for (const auto &mesh : lodMesh->meshes) {
|
for (const auto &mesh : lodMesh->meshes) {
|
||||||
@ -145,31 +154,37 @@ public:
|
|||||||
ComponentHandle<Texture> textureComponent = ent->get<Texture>();
|
ComponentHandle<Texture> textureComponent = ent->get<Texture>();
|
||||||
ComponentHandle<Material> materialComponent = ent->get<Material>();
|
ComponentHandle<Material> materialComponent = ent->get<Material>();
|
||||||
|
|
||||||
Material material = materialComponent.isValid() ? materialComponent.get() : Material();
|
Material material =
|
||||||
|
materialComponent.isValid() ? materialComponent.get() : Material();
|
||||||
unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0;
|
unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0;
|
||||||
unsigned int normalID = textureComponent.isValid() ? textureComponent->normal_id : 0;
|
unsigned int normalID =
|
||||||
|
textureComponent.isValid() ? textureComponent->normal_id : 0;
|
||||||
|
|
||||||
// Put it into the list of transparent render objects if the texture wants to be rendered transparently
|
// Put it into the list of transparent render objects if the texture wants to be
|
||||||
|
// rendered transparently
|
||||||
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
||||||
renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh, distance, material));
|
renderObjects[1].emplace_back(
|
||||||
|
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
||||||
|
normalID, mesh, distance, material));
|
||||||
} else {
|
} else {
|
||||||
renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh, distance, material));
|
renderObjects[0].emplace_back(
|
||||||
|
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
||||||
|
normalID, mesh, distance, material));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort transparent objects
|
// Sort transparent objects
|
||||||
std::sort(renderObjects[1].begin(), renderObjects[1].end(), [](const RenderObject &first, const RenderObject &second) -> bool {
|
std::sort(renderObjects[1].begin(), renderObjects[1].end(),
|
||||||
return first.distance > second.distance;
|
[](const RenderObject &first, const RenderObject &second) -> bool {
|
||||||
});
|
return first.distance > second.distance;
|
||||||
|
});
|
||||||
|
|
||||||
return renderObjects;
|
return renderObjects;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderSystem() {
|
RenderSystem() { setup(); }
|
||||||
setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
void configure(World *pWorld) override {
|
void configure(World *pWorld) override {
|
||||||
myWorld = pWorld;
|
myWorld = pWorld;
|
||||||
@ -196,12 +211,13 @@ public:
|
|||||||
// Create depth texture
|
// Create depth texture
|
||||||
glGenTextures(1, &depthMap);
|
glGenTextures(1, &depthMap);
|
||||||
glBindTexture(GL_TEXTURE_2D, depthMap);
|
glBindTexture(GL_TEXTURE_2D, depthMap);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_width, shadow_height, 0,
|
||||||
|
GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
|
||||||
// Attach depth texture as FBO's depth buffer
|
// Attach depth texture as FBO's depth buffer
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
||||||
@ -210,11 +226,12 @@ public:
|
|||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void render(World *pWorld, Shader normalShader, Shader shadowShader, Shader debugShader) {
|
void render(World *pWorld, Shader normalShader, Shader shadowShader, Shader debugShader) {
|
||||||
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera, ComponentHandle<Transform> cameraTransform) {
|
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera,
|
||||||
|
ComponentHandle<Transform> cameraTransform) {
|
||||||
// Get render objects
|
// Get render objects
|
||||||
std::vector<std::vector<RenderObject>> allRenderObjects = getRenderObjects(pWorld, cameraTransform->get_origin());
|
std::vector<std::vector<RenderObject>> allRenderObjects =
|
||||||
|
getRenderObjects(pWorld, cameraTransform->get_origin());
|
||||||
std::vector<RenderObject> renderObjects = allRenderObjects[0];
|
std::vector<RenderObject> renderObjects = allRenderObjects[0];
|
||||||
std::vector<RenderObject> transparentRenderObjects = allRenderObjects[1];
|
std::vector<RenderObject> transparentRenderObjects = allRenderObjects[1];
|
||||||
|
|
||||||
@ -222,19 +239,22 @@ public:
|
|||||||
// Get light direction
|
// Get light direction
|
||||||
// TODO: Currently only the last light is used!
|
// TODO: Currently only the last light is used!
|
||||||
glm::vec3 lightDirection;
|
glm::vec3 lightDirection;
|
||||||
pWorld->each<DirectionalLight>([&](Entity *ent, ComponentHandle<DirectionalLight> light) {
|
pWorld->each<DirectionalLight>(
|
||||||
lightDirection = light->direction;
|
[&](Entity *ent, ComponentHandle<DirectionalLight> light) {
|
||||||
});
|
lightDirection = light->direction;
|
||||||
|
});
|
||||||
float near_plane = 1.0f, far_plane = 100.0f;
|
float near_plane = 1.0f, far_plane = 100.0f;
|
||||||
glm::mat4 lightProjection = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, near_plane, far_plane);
|
glm::mat4 lightProjection =
|
||||||
glm::mat4 lightView = glm::lookAt(lightDirection * 40.0f, -lightDirection, glm::vec3(0.0, 1.0, 0.0));
|
glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, near_plane, far_plane);
|
||||||
|
glm::mat4 lightView =
|
||||||
|
glm::lookAt(lightDirection * 40.0f, -lightDirection, glm::vec3(0.0, 1.0, 0.0));
|
||||||
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
||||||
|
|
||||||
// Render shadows
|
// Render shadows
|
||||||
shadowShader.use();
|
shadowShader.use();
|
||||||
|
|
||||||
shadowShader.setMat4("lightSpaceMatrix", lightSpaceMatrix);
|
shadowShader.setMat4("lightSpaceMatrix", lightSpaceMatrix);
|
||||||
|
|
||||||
glViewport(0, 0, shadow_width, shadow_height);
|
glViewport(0, 0, shadow_width, shadow_height);
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
@ -249,7 +269,7 @@ public:
|
|||||||
|
|
||||||
// Render Normal
|
// Render Normal
|
||||||
glViewport(0, 0, screen_width, screen_height);
|
glViewport(0, 0, screen_width, screen_height);
|
||||||
|
|
||||||
glClearColor(0.6f, 0.9f, 0.9f, 1.0f);
|
glClearColor(0.6f, 0.9f, 0.9f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
@ -301,4 +321,4 @@ public:
|
|||||||
World *myWorld;
|
World *myWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_RENDERSYSTEM_H
|
#endif // ECSGAME_RENDERSYSTEM_H
|
||||||
|
@ -5,24 +5,27 @@
|
|||||||
#ifndef ECSGAME_SINEANIMATIONSYSTEM_H
|
#ifndef ECSGAME_SINEANIMATIONSYSTEM_H
|
||||||
#define ECSGAME_SINEANIMATIONSYSTEM_H
|
#define ECSGAME_SINEANIMATIONSYSTEM_H
|
||||||
|
|
||||||
#include "../ECS.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../Components/SineAnimation.h"
|
#include "../Components/SineAnimation.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
#include "../ECS.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
class SineAnimationSystem : public EntitySystem {
|
class SineAnimationSystem : public EntitySystem {
|
||||||
public:
|
public:
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
passedTime += deltaTime;
|
passedTime += deltaTime;
|
||||||
|
|
||||||
pWorld->each<Transform, SineAnimation>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<SineAnimation> anim) {
|
pWorld->each<Transform, SineAnimation>([&](Entity *ent,
|
||||||
transform->translate(anim->maxDistance * glm::sin(passedTime * anim->speedScale) * deltaTime);
|
ComponentHandle<Transform> transform,
|
||||||
|
ComponentHandle<SineAnimation> anim) {
|
||||||
|
transform->translate(anim->maxDistance * glm::sin(passedTime * anim->speedScale) *
|
||||||
|
deltaTime);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float passedTime = 0.0;
|
float passedTime = 0.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_SINEANIMATIONSYSTEM_H
|
#endif // ECSGAME_SINEANIMATIONSYSTEM_H
|
||||||
|
@ -17,4 +17,4 @@ struct Material {
|
|||||||
float normal_scale = 3.0;
|
float normal_scale = 3.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MATERIAL_H
|
#endif // ECSGAME_MATERIAL_H
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
||||||
// 1. retrieve the vertex/fragment source code from filePath
|
// 1. retrieve the vertex/fragment source code from filePath
|
||||||
@ -36,8 +36,7 @@ Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
|||||||
// convert stream into string
|
// convert stream into string
|
||||||
vertexCode = vShaderStream.str();
|
vertexCode = vShaderStream.str();
|
||||||
fragmentCode = fShaderStream.str();
|
fragmentCode = fShaderStream.str();
|
||||||
}
|
} catch (std::ifstream::failure &e) {
|
||||||
catch (std::ifstream::failure &e) {
|
|
||||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +68,6 @@ Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
|||||||
// delete the shaders as they're linked into our program now and no longer necessary
|
// delete the shaders as they're linked into our program now and no longer necessary
|
||||||
glDeleteShader(vertex);
|
glDeleteShader(vertex);
|
||||||
glDeleteShader(fragment);
|
glDeleteShader(fragment);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::use() {
|
void Shader::use() {
|
||||||
@ -77,7 +75,7 @@ void Shader::use() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setBool(const std::string &name, bool value) const {
|
void Shader::setBool(const std::string &name, bool value) const {
|
||||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setInt(const std::string &name, int value) const {
|
void Shader::setInt(const std::string &name, int value) const {
|
||||||
@ -92,7 +90,6 @@ void Shader::setMat4(const std::string &name, glm::mat4 mat) const {
|
|||||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(mat));
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(mat));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Shader::setVec3(const std::string &name, glm::vec3 vec) const {
|
void Shader::setVec3(const std::string &name, glm::vec3 vec) const {
|
||||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), vec.x, vec.y, vec.z);
|
glUniform3f(glGetUniformLocation(ID, name.c_str()), vec.x, vec.y, vec.z);
|
||||||
}
|
}
|
||||||
@ -104,15 +101,17 @@ void Shader::checkCompileErrors(unsigned int shader, const std::string &type) {
|
|||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
|
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
|
||||||
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog
|
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n"
|
||||||
<< "\n -- --------------------------------------------------- -- " << std::endl;
|
<< infoLog << "\n -- --------------------------------------------------- -- "
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
glGetProgramInfoLog(shader, 1024, nullptr, infoLog);
|
glGetProgramInfoLog(shader, 1024, nullptr, infoLog);
|
||||||
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog
|
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n"
|
||||||
<< "\n -- --------------------------------------------------- -- " << std::endl;
|
<< infoLog << "\n -- --------------------------------------------------- -- "
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,17 @@
|
|||||||
#ifndef ECSGAME_SHADER_H
|
#ifndef ECSGAME_SHADER_H
|
||||||
#define ECSGAME_SHADER_H
|
#define ECSGAME_SHADER_H
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class Shader {
|
class Shader {
|
||||||
public:
|
public:
|
||||||
/// Program ID
|
/// Program ID
|
||||||
unsigned int ID;
|
unsigned int ID;
|
||||||
|
|
||||||
/// Read and build the shader from the files at vertexPath and fragmentPath.
|
/// Read and build the shader from the files at vertexPath and fragmentPath.
|
||||||
/// Note that an OpenGL context has to be initialized before calling this! Otherwise a SIGSEGV will be thrown.
|
/// Note that an OpenGL context has to be initialized before calling this! Otherwise a SIGSEGV
|
||||||
|
/// will be thrown.
|
||||||
Shader(const char *vertexPath, const char *fragmentPath);
|
Shader(const char *vertexPath, const char *fragmentPath);
|
||||||
|
|
||||||
/// Activate the shader - usually called before rendering.
|
/// Activate the shader - usually called before rendering.
|
||||||
@ -35,9 +36,8 @@ public:
|
|||||||
/// Set a uniform vec3 in the shader
|
/// Set a uniform vec3 in the shader
|
||||||
void setVec3(const std::string &name, glm::vec3 vec) const;
|
void setVec3(const std::string &name, glm::vec3 vec) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void checkCompileErrors(unsigned int shader, const std::string &type);
|
static void checkCompileErrors(unsigned int shader, const std::string &type);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif // ECSGAME_SHADER_H
|
||||||
#endif //ECSGAME_SHADER_H
|
|
||||||
|
2090
Util/OBJ_Loader.h
2090
Util/OBJ_Loader.h
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user