Compare commits
No commits in common. "c2d733db22a970e7099258117155fcbda5a6f2a0" and "3a93f2410fdb27149044c4e643bcdda85ebeede3" have entirely different histories.
c2d733db22
...
3a93f2410f
@ -1,13 +0,0 @@
|
|||||||
---
|
|
||||||
BasedOnStyle: LLVM
|
|
||||||
AllowShortBlocksOnASingleLine: 'true'
|
|
||||||
AllowShortCaseLabelsOnASingleLine: 'true'
|
|
||||||
AllowShortFunctionsOnASingleLine: Inline
|
|
||||||
AllowShortIfStatementsOnASingleLine: WithoutElse
|
|
||||||
AllowShortLambdasOnASingleLine: Inline
|
|
||||||
AllowShortLoopsOnASingleLine: 'false'
|
|
||||||
AlwaysBreakBeforeMultilineStrings: 'true'
|
|
||||||
IndentWidth: '4'
|
|
||||||
ColumnLimit: 100
|
|
||||||
|
|
||||||
...
|
|
@ -9,10 +9,9 @@
|
|||||||
#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
|
/// Create a camera with a field of view (in degrees), width and height (in any unit) and near and far distances
|
||||||
/// and far distances
|
Camera(float fov, float width, float height, float near, float far) : projection(
|
||||||
Camera(float fov, float width, float height, float near, float far)
|
glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
||||||
: projection(glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
|
||||||
|
|
||||||
glm::mat4 projection;
|
glm::mat4 projection;
|
||||||
|
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
#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) {}
|
||||||
|
|
||||||
|
@ -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)) {}
|
||||||
|
@ -5,15 +5,13 @@
|
|||||||
#ifndef ECSGAME_MESH_H
|
#ifndef ECSGAME_MESH_H
|
||||||
#define ECSGAME_MESH_H
|
#define ECSGAME_MESH_H
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct Mesh {
|
struct Mesh {
|
||||||
explicit Mesh(const std::vector<float> &_vertices, const std::vector<unsigned int> &_indices)
|
explicit Mesh(const std::vector<float> &_vertices, const std::vector<unsigned int> &_indices) : vertex_count(_indices.size()) {
|
||||||
: vertex_count(_indices.size()), vertices(_vertices), indices(_indices) {
|
// Copy the vertices into a local classic float array. Nothing was displayed without this, maybe
|
||||||
// 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);
|
||||||
@ -25,8 +23,7 @@ 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
|
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
|
||||||
// configure vertex attributes(s).
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
@ -40,23 +37,19 @@ struct Mesh {
|
|||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// Normal attribute
|
// Normal attribute
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(3 * 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),
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
(void *)(6 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
// Tangent attribute
|
// Tangent attribute
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(8 * sizeof(float)));
|
||||||
(void *)(8 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(3);
|
glEnableVertexAttribArray(3);
|
||||||
|
|
||||||
// Bitangent attribute
|
// Bitangent attribute
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float),
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(11 * sizeof(float)));
|
||||||
(void *)(11 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(4);
|
glEnableVertexAttribArray(4);
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@ -68,14 +61,11 @@ struct Mesh {
|
|||||||
glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float> vertices;
|
|
||||||
std::vector<unsigned int> indices;
|
|
||||||
unsigned int vertex_count;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int EBO;
|
unsigned int EBO;
|
||||||
unsigned int VBO;
|
unsigned int VBO;
|
||||||
unsigned int VAO;
|
unsigned int VAO;
|
||||||
|
unsigned int vertex_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_MESH_H
|
#endif //ECSGAME_MESH_H
|
||||||
|
@ -5,14 +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) {}
|
||||||
|
|
||||||
glm::vec3 get_look_direction() { return glm::mat3_cast(rotation) * glm::vec3(0, 0, -1); }
|
|
||||||
|
|
||||||
float sensitivity;
|
float sensitivity;
|
||||||
|
|
||||||
double yaw = 0.0;
|
double yaw = 0.0;
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
#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) {}
|
||||||
|
|
||||||
|
@ -5,25 +5,23 @@
|
|||||||
#ifndef ECSGAME_OBJMESH_H
|
#ifndef ECSGAME_OBJMESH_H
|
||||||
#define ECSGAME_OBJMESH_H
|
#define ECSGAME_OBJMESH_H
|
||||||
|
|
||||||
#include "../../Util/OBJ_Loader.h"
|
|
||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
|
#include "../../Util/OBJ_Loader.h"
|
||||||
|
|
||||||
struct ObjMesh : public Mesh {
|
struct ObjMesh : public Mesh {
|
||||||
struct Settings {
|
struct Settings {
|
||||||
Settings() = default;
|
Settings() = default;
|
||||||
|
|
||||||
Settings(float minDistanceForRender, float maxDistanceForRender, float diffuse,
|
Settings(float minDistanceForRender, float maxDistanceForRender, float diffuse, float specular)
|
||||||
float specular)
|
: minDistanceForRender(minDistanceForRender), maxDistanceForRender(maxDistanceForRender) {}
|
||||||
: 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)
|
explicit ObjMesh(const std::string &path, const Settings &settings) : Mesh(getVerticesFromFile(path), getIndicesFromFile(path)),
|
||||||
: Mesh(getVerticesFromFile(path), getIndicesFromFile(path)),
|
minDistance(settings.minDistanceForRender),
|
||||||
minDistance(settings.minDistanceForRender), maxDistance(settings.maxDistanceForRender) {}
|
maxDistance(settings.maxDistanceForRender) {}
|
||||||
|
|
||||||
float minDistance;
|
float minDistance;
|
||||||
|
|
||||||
@ -67,16 +65,13 @@ struct ObjMesh : public Mesh {
|
|||||||
return vertexData;
|
return vertexData;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout
|
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
<< "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>>
|
static std::pair<std::vector<glm::vec3>, std::vector<glm::vec3>> getTangentsFromVertices(const std::vector<objl::Vertex> &vertices, const std::vector<unsigned int> indices) {
|
||||||
getTangentsFromVertices(const std::vector<objl::Vertex> &vertices,
|
// These vectors hold tangents and bitangents in the same way in which the vertices vectors holds vertices:
|
||||||
const std::vector<unsigned int> indices) {
|
// Their index in the vector corresponds to indices in the indices vector.
|
||||||
// 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());
|
||||||
|
|
||||||
@ -88,26 +83,17 @@ struct ObjMesh : public Mesh {
|
|||||||
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,
|
glm::vec3 vertex0 = glm::vec3(vertices[i0].Position.X, vertices[i0].Position.Y, vertices[i0].Position.Z);
|
||||||
vertices[i0].Position.Z);
|
glm::vec3 vertex1 = glm::vec3(vertices[i1].Position.X, vertices[i1].Position.Y, vertices[i1].Position.Z);
|
||||||
glm::vec3 vertex1 = glm::vec3(vertices[i1].Position.X, vertices[i1].Position.Y,
|
glm::vec3 vertex2 = glm::vec3(vertices[i2].Position.X, vertices[i2].Position.Y, vertices[i2].Position.Z);
|
||||||
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 uv0 = glm::vec2(vertices[i0].TextureCoordinate.X, vertices[i0].TextureCoordinate.Y);
|
||||||
glm::vec2(vertices[i0].TextureCoordinate.X, vertices[i0].TextureCoordinate.Y);
|
glm::vec2 uv1 = glm::vec2(vertices[i1].TextureCoordinate.X, vertices[i1].TextureCoordinate.Y);
|
||||||
glm::vec2 uv1 =
|
glm::vec2 uv2 = glm::vec2(vertices[i2].TextureCoordinate.X, vertices[i2].TextureCoordinate.Y);
|
||||||
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 normal0 = glm::vec3(vertices[i0].Normal.X, vertices[i0].Normal.Y, vertices[i0].Normal.Z);
|
||||||
glm::vec3(vertices[i0].Normal.X, vertices[i0].Normal.Y, vertices[i0].Normal.Z);
|
glm::vec3 normal1 = glm::vec3(vertices[i1].Normal.X, vertices[i1].Normal.Y, vertices[i1].Normal.Z);
|
||||||
glm::vec3 normal1 =
|
glm::vec3 normal2 = glm::vec3(vertices[i2].Normal.X, vertices[i2].Normal.Y, vertices[i2].Normal.Z);
|
||||||
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;
|
||||||
@ -154,8 +140,7 @@ struct ObjMesh : public Mesh {
|
|||||||
return indices;
|
return indices;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout
|
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
<< "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,12 +158,12 @@ struct ObjMesh : public Mesh {
|
|||||||
|
|
||||||
// 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 << ", "
|
std::cout << "Ambient Color: " << curMesh.MeshMaterial.Ka.X << ", " << curMesh.MeshMaterial.Ka.Y << ", "
|
||||||
<< curMesh.MeshMaterial.Ka.Y << ", " << curMesh.MeshMaterial.Ka.Z << "\n";
|
<< curMesh.MeshMaterial.Ka.Z << "\n";
|
||||||
std::cout << "Diffuse Color: " << curMesh.MeshMaterial.Kd.X << ", "
|
std::cout << "Diffuse Color: " << curMesh.MeshMaterial.Kd.X << ", " << curMesh.MeshMaterial.Kd.Y << ", "
|
||||||
<< curMesh.MeshMaterial.Kd.Y << ", " << curMesh.MeshMaterial.Kd.Z << "\n";
|
<< curMesh.MeshMaterial.Kd.Z << "\n";
|
||||||
std::cout << "Specular Color: " << curMesh.MeshMaterial.Ks.X << ", "
|
std::cout << "Specular Color: " << curMesh.MeshMaterial.Ks.X << ", " << curMesh.MeshMaterial.Ks.Y << ", "
|
||||||
<< curMesh.MeshMaterial.Ks.Y << ", " << curMesh.MeshMaterial.Ks.Z << "\n";
|
<< 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";
|
||||||
@ -192,8 +177,7 @@ struct ObjMesh : public Mesh {
|
|||||||
return vertexData;
|
return vertexData;
|
||||||
} else {
|
} else {
|
||||||
// Output Error
|
// Output Error
|
||||||
std::cout
|
std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
||||||
<< "Failed to Load File. May have failed to find it or it was not an .obj file.\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -26,7 +26,9 @@ 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) { views.emplace_back(new_quat); }
|
void add_view(glm::quat new_quat) {
|
||||||
|
views.emplace_back(new_quat);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<glm::quat> views;
|
std::vector<glm::quat> views;
|
||||||
};
|
};
|
||||||
@ -38,8 +40,7 @@ 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 =
|
int speed_addition = 0; // 0, -1 or 1 depending on whether the speed should stay, decrease or increase
|
||||||
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,8 @@
|
|||||||
#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)
|
SineAnimation(const glm::vec3 &maxDistance, float speedScale) : maxDistance(maxDistance), speedScale(speedScale) {}
|
||||||
: maxDistance(maxDistance), speedScale(speedScale) {}
|
|
||||||
|
|
||||||
glm::vec3 maxDistance;
|
glm::vec3 maxDistance;
|
||||||
float speedScale;
|
float speedScale;
|
||||||
|
@ -41,12 +41,15 @@ struct Texture {
|
|||||||
if (data) {
|
if (data) {
|
||||||
// Alpha channel?
|
// Alpha channel?
|
||||||
unsigned int glChannels = GL_RGB;
|
unsigned int glChannels = GL_RGB;
|
||||||
if (nrChannels == 4) { glChannels = GL_RGBA; }
|
if (nrChannels == 4) {
|
||||||
|
glChannels = GL_RGBA;
|
||||||
|
}
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels,
|
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, data);
|
||||||
GL_UNSIGNED_BYTE, data);
|
|
||||||
|
|
||||||
if (settings.mipmaps) { glGenerateMipmap(GL_TEXTURE_2D); }
|
if (settings.mipmaps) {
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Failed to load texture" << std::endl;
|
std::cout << "Failed to load texture" << std::endl;
|
||||||
}
|
}
|
||||||
@ -56,8 +59,7 @@ struct Texture {
|
|||||||
return gl_id;
|
return gl_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit Texture(const std::string &path, Settings settings, bool transparent)
|
explicit Texture(const std::string& path, Settings settings, bool transparent) : id(loadTexture(path, settings)), render_transparent(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);
|
||||||
|
@ -17,21 +17,33 @@ 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) { matrix = glm::translate(matrix, offset); }
|
void translate(glm::vec3 offset) {
|
||||||
|
matrix = glm::translate(matrix, offset);
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec3 get_translation() { return glm::vec3(matrix[3]); }
|
glm::vec3 get_translation() {
|
||||||
|
return glm::vec3(matrix[3]);
|
||||||
|
}
|
||||||
|
|
||||||
void uniform_scale(float factor) { scale(glm::vec3(factor, factor, factor)); }
|
void uniform_scale(float factor) {
|
||||||
|
scale(glm::vec3(factor, factor, factor));
|
||||||
|
}
|
||||||
|
|
||||||
void scale(glm::vec3 factors) { matrix = glm::scale(matrix, factors); }
|
void scale(glm::vec3 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) { origin = position; }
|
void set_origin(glm::vec3 position) {
|
||||||
|
origin = position;
|
||||||
|
}
|
||||||
|
|
||||||
void add_to_origin(glm::vec3 addition) { origin += addition; }
|
void add_to_origin(glm::vec3 addition) {
|
||||||
|
origin += addition;
|
||||||
|
}
|
||||||
|
|
||||||
void set_rotation_from_quat(glm::quat quaternion) {
|
void set_rotation_from_quat(glm::quat quaternion) {
|
||||||
// Remember translation
|
// Remember translation
|
||||||
@ -41,13 +53,21 @@ struct Transform {
|
|||||||
matrix[3] = save;
|
matrix[3] = save;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 get_origin() const { return origin; }
|
glm::vec3 get_origin() const {
|
||||||
|
return origin;
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec3 forward() const { return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0); }
|
glm::vec3 forward() const {
|
||||||
|
return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec3 up() const { return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0); }
|
glm::vec3 up() const {
|
||||||
|
return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec3 right() const { return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0); }
|
glm::vec3 right() const {
|
||||||
|
return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //ECSGAME_TRANSFORM_H
|
#endif //ECSGAME_TRANSFORM_H
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../../Util/kdtree.h"
|
|
||||||
#include "../Components/LODObjMesh.h"
|
|
||||||
#include "../Components/Mesh.h"
|
|
||||||
#include "../Components/MouseLook.h"
|
|
||||||
#include "../Components/ObjMesh.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
|
||||||
|
|
||||||
using namespace ECS;
|
|
||||||
|
|
||||||
class CollisionSystem : public EntitySystem {
|
|
||||||
public:
|
|
||||||
// Initialize the kdtree
|
|
||||||
void build() {
|
|
||||||
std::vector<Triangle *> triangles;
|
|
||||||
std::vector<Point *> points;
|
|
||||||
|
|
||||||
// ObjMesh
|
|
||||||
myWorld->each<ObjMesh, Transform>(
|
|
||||||
[&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform) {
|
|
||||||
std::vector<unsigned int> indices = mesh->indices;
|
|
||||||
std::vector<float> vertices = mesh->vertices;
|
|
||||||
|
|
||||||
for (int i = 0; i < mesh->vertex_count; i += 3) {
|
|
||||||
float v0p0 = vertices[indices[i + 0] * 14 + 0];
|
|
||||||
float v0p1 = vertices[indices[i + 0] * 14 + 1];
|
|
||||||
float v0p2 = vertices[indices[i + 0] * 14 + 2];
|
|
||||||
|
|
||||||
float v1p0 = vertices[indices[i + 1] * 14 + 0];
|
|
||||||
float v1p1 = vertices[indices[i + 1] * 14 + 1];
|
|
||||||
float v1p2 = vertices[indices[i + 1] * 14 + 2];
|
|
||||||
|
|
||||||
float v2p0 = vertices[indices[i + 2] * 14 + 0];
|
|
||||||
float v2p1 = vertices[indices[i + 2] * 14 + 1];
|
|
||||||
float v2p2 = vertices[indices[i + 2] * 14 + 2];
|
|
||||||
|
|
||||||
glm::vec4 v1glm(v0p0, v0p1, v0p2, 1.0);
|
|
||||||
glm::vec4 v2glm(v1p0, v1p1, v1p2, 1.0);
|
|
||||||
glm::vec4 v3glm(v2p0, v2p1, v2p2, 1.0);
|
|
||||||
|
|
||||||
// Transform to World Position -- these are local coordinates with
|
|
||||||
// individual mesh origins
|
|
||||||
v1glm = transform->matrix * v1glm + glm::vec4(transform->get_origin(), 0.0);
|
|
||||||
v2glm = transform->matrix * v2glm + glm::vec4(transform->get_origin(), 0.0);
|
|
||||||
v3glm = transform->matrix * v3glm + glm::vec4(transform->get_origin(), 0.0);
|
|
||||||
|
|
||||||
Vector v1(v1glm.x, v1glm.y, v1glm.z);
|
|
||||||
Vector v2(v2glm.x, v2glm.y, v2glm.z);
|
|
||||||
Vector v3(v3glm.x, v3glm.y, v3glm.z);
|
|
||||||
|
|
||||||
Triangle *triangle = new Triangle(v1, v2, v3);
|
|
||||||
triangles.emplace_back(triangle);
|
|
||||||
|
|
||||||
points.emplace_back(new Point(v1, triangle));
|
|
||||||
points.emplace_back(new Point(v2, triangle));
|
|
||||||
points.emplace_back(new Point(v3, triangle));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// LODObjMesh
|
|
||||||
myWorld->each<LODObjMesh, Transform>([&](Entity *ent, ComponentHandle<LODObjMesh> lodMesh,
|
|
||||||
ComponentHandle<Transform> transform) {
|
|
||||||
// TODO
|
|
||||||
});
|
|
||||||
|
|
||||||
std::cout << "Start building kdtree with " << points.size() << " points" << std::endl;
|
|
||||||
kdtree = new KDTree(points);
|
|
||||||
std::cout << "Done" << std::endl;
|
|
||||||
|
|
||||||
std::cout << kdtree->to_string() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
|
||||||
pWorld->each<Transform, MouseLook>([&](Entity *ent, ComponentHandle<Transform> transform,
|
|
||||||
ComponentHandle<MouseLook> mouse_look) {
|
|
||||||
glm::vec3 origin_glm = transform->get_origin();
|
|
||||||
Vector origin = Vector(origin_glm.x, origin_glm.y, origin_glm.z);
|
|
||||||
|
|
||||||
glm::vec3 direction_glm = mouse_look->get_look_direction();
|
|
||||||
Vector direction = Vector(direction_glm.x, direction_glm.y, direction_glm.z);
|
|
||||||
|
|
||||||
// TODO: Get mouse look direction
|
|
||||||
Ray ray(origin, direction * 5.0);
|
|
||||||
|
|
||||||
Triangle *result = kdtree->intersect_ray(ray);
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
std::cout << result->p1[0] << ", " << result->p1[1] << ", " << result->p1[2]
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
// TODO: Also output visually
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void configure(World *pWorld) override { myWorld = pWorld; }
|
|
||||||
|
|
||||||
World *myWorld;
|
|
||||||
|
|
||||||
KDTree *kdtree;
|
|
||||||
};
|
|
@ -5,14 +5,16 @@
|
|||||||
#ifndef ECSGAME_GRAVITYSYSTEM_H
|
#ifndef ECSGAME_GRAVITYSYSTEM_H
|
||||||
#define ECSGAME_GRAVITYSYSTEM_H
|
#define ECSGAME_GRAVITYSYSTEM_H
|
||||||
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
class GravitySystem : public EntitySystem {
|
class GravitySystem : public EntitySystem {
|
||||||
public:
|
public:
|
||||||
explicit GravitySystem(float amount) { gravityAmount = amount; }
|
explicit GravitySystem(float 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) {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__
|
#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__
|
||||||
#define __PATHMOVEMENTSWITCHSYSTEM_H__
|
#define __PATHMOVEMENTSWITCHSYSTEM_H__
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../Components/MouseLook.h"
|
|
||||||
#include "../Components/Movement.h"
|
|
||||||
#include "../Components/PathMove.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/PathMove.h"
|
||||||
#include "../Events/InputEvent.h"
|
#include "../Events/InputEvent.h"
|
||||||
|
#include "../Components/Movement.h"
|
||||||
|
#include "../Components/MouseLook.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -23,9 +23,7 @@ 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>(
|
myWorld->each<PathMove, Movement, MouseLook>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook) {
|
||||||
[&](Entity *ent, ComponentHandle<PathMove> pathmove,
|
|
||||||
ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook) {
|
|
||||||
if (event.action == GLFW_PRESS) {
|
if (event.action == GLFW_PRESS) {
|
||||||
// Switch between them
|
// Switch between them
|
||||||
if (pathmove->is_active) {
|
if (pathmove->is_active) {
|
||||||
@ -40,10 +38,7 @@ class InteractivePathSystem : public EntitySystem, public EventSubscriber<InputE
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (event.key == GLFW_KEY_R) {
|
} else if (event.key == GLFW_KEY_R) {
|
||||||
myWorld->each<PathMove, Movement, MouseLook, Transform>(
|
myWorld->each<PathMove, Movement, MouseLook, Transform>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook, ComponentHandle<Transform> transform) {
|
||||||
[&](Entity *ent, ComponentHandle<PathMove> pathmove,
|
|
||||||
ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook,
|
|
||||||
ComponentHandle<Transform> transform) {
|
|
||||||
if (event.action == GLFW_PRESS) {
|
if (event.action == GLFW_PRESS) {
|
||||||
// Add this point to the path
|
// Add this point to the path
|
||||||
pathmove->path.add_point(transform->origin);
|
pathmove->path.add_point(transform->origin);
|
||||||
@ -53,7 +48,9 @@ class InteractivePathSystem : public EntitySystem, public EventSubscriber<InputE
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
void unconfigure(World *pWorld) override {
|
||||||
|
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 <GLFW/glfw3.h>
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../Components/Movement.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
#include "../Events/InputEvent.h"
|
#include "../Events/InputEvent.h"
|
||||||
|
#include "../Components/Movement.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -61,17 +61,17 @@ 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>([&](Entity *ent, ComponentHandle<Transform> transform,
|
pWorld->each<Transform, Movement>(
|
||||||
ComponentHandle<Movement> movement) {
|
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
|
||||||
if (!movement->is_active) return;
|
if (!movement->is_active) return;
|
||||||
|
|
||||||
transform->add_to_origin(
|
transform->add_to_origin(transform->matrix * glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0));
|
||||||
transform->matrix *
|
|
||||||
glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
void unconfigure(World *pWorld) override {
|
||||||
|
pWorld->unsubscribeAll(this);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
|
@ -5,10 +5,9 @@
|
|||||||
#ifndef ECSGAME_MOUSELOOKSYSTEM_H
|
#ifndef ECSGAME_MOUSELOOKSYSTEM_H
|
||||||
#define ECSGAME_MOUSELOOKSYSTEM_H
|
#define ECSGAME_MOUSELOOKSYSTEM_H
|
||||||
|
|
||||||
#include "../Components/Camera.h"
|
|
||||||
#include "../Components/MouseLook.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
#include "../Components/MouseLook.h"
|
||||||
#include "../Events/MouseMoveEvent.h"
|
#include "../Events/MouseMoveEvent.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
@ -23,12 +22,12 @@ class MouseLookSystem : public EntitySystem, public EventSubscriber<MouseMoveEve
|
|||||||
myWorld->subscribe<MouseMoveEvent>(this);
|
myWorld->subscribe<MouseMoveEvent>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
void unconfigure(World *pWorld) override {
|
||||||
|
pWorld->unsubscribeAll(this);
|
||||||
|
}
|
||||||
|
|
||||||
void receive(World *pWorld, const MouseMoveEvent &event) override {
|
void receive(World *pWorld, const MouseMoveEvent &event) override {
|
||||||
pWorld->each<Transform, MouseLook, Camera>(
|
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
||||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse,
|
|
||||||
ComponentHandle<Camera> camera) {
|
|
||||||
double xOffset = lastX - event.newX;
|
double xOffset = lastX - event.newX;
|
||||||
double yOffset = lastY - event.newY;
|
double yOffset = lastY - event.newY;
|
||||||
|
|
||||||
@ -41,18 +40,16 @@ class MouseLookSystem : public EntitySystem, public EventSubscriber<MouseMoveEve
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
pWorld->each<Transform, MouseLook, Camera>(
|
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
|
||||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse,
|
|
||||||
ComponentHandle<Camera> camera) {
|
|
||||||
if (!mouse->is_active) return;
|
if (!mouse->is_active) return;
|
||||||
|
|
||||||
if (mouse->pitch > 80.0f) mouse->pitch = 80.0f;
|
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;
|
||||||
|
|
||||||
mouse->rotation =
|
mouse->rotation = glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.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));
|
||||||
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);
|
||||||
});
|
});
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
#ifndef __PATHMOVESYSTEM_H__
|
#ifndef __PATHMOVESYSTEM_H__
|
||||||
#define __PATHMOVESYSTEM_H__
|
#define __PATHMOVESYSTEM_H__
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../Components/PathMove.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
#include "../Events/InputEvent.h"
|
#include "../Events/InputEvent.h"
|
||||||
|
#include "../Components/PathMove.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -75,8 +75,8 @@ 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>([&](Entity *ent, ComponentHandle<Transform> transform,
|
pWorld->each<Transform, PathMove>(
|
||||||
ComponentHandle<PathMove> pathmove) {
|
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> pathmove) {
|
||||||
if (!pathmove->is_active) return;
|
if (!pathmove->is_active) return;
|
||||||
|
|
||||||
// Handle change in speed
|
// Handle change in speed
|
||||||
@ -88,8 +88,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
|
|
||||||
// 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 +=
|
pathmove->time_passed += desired_distance / path.distances[pathmove->current_point_index];
|
||||||
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();
|
||||||
@ -100,25 +99,22 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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
|
// (The point index specifies the point we're coming from, not the one we're moving towards)
|
||||||
// towards)
|
|
||||||
if (pathmove->current_point_index >= num_points - 1) {
|
if (pathmove->current_point_index >= num_points - 1) {
|
||||||
pathmove->current_point_index = 0;
|
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
|
// p1 and p2 are always the same (the current origin and the current target), but the rest depends on edge cases
|
||||||
// rest depends on edge cases
|
|
||||||
glm::vec3 p0;
|
glm::vec3 p0;
|
||||||
glm::vec3 p1 = path.points[pathmove->current_point_index];
|
glm::vec3 p1 = path.points[pathmove->current_point_index];
|
||||||
glm::vec3 p2 = path.points[pathmove->current_point_index + 1];
|
glm::vec3 p2 = path.points[pathmove->current_point_index + 1];
|
||||||
glm::vec3 p3;
|
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
|
// We're moving towards the last point, so the point after that needs to be interpolated.
|
||||||
// interpolated. We interpolate linearly along the line from this point to the
|
// We interpolate linearly along the line from this point to the target point.
|
||||||
// target point.
|
|
||||||
glm::vec3 interp_direction = p2 - p1;
|
glm::vec3 interp_direction = p2 - p1;
|
||||||
p3 = p2 + interp_direction * 2.0f;
|
p3 = p2 + interp_direction * 2.0f;
|
||||||
} else {
|
} else {
|
||||||
@ -129,8 +125,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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] -
|
glm::vec3 interp_direction = path.points[pathmove->current_point_index] - path.points[pathmove->current_point_index + 1];
|
||||||
path.points[pathmove->current_point_index + 1];
|
|
||||||
p0 = path.points[pathmove->current_point_index] + interp_direction;
|
p0 = path.points[pathmove->current_point_index] + interp_direction;
|
||||||
} else {
|
} else {
|
||||||
// We're fine - use the point before the current point
|
// We're fine - use the point before the current point
|
||||||
@ -138,7 +133,12 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the point on the spline
|
// Calculate the point on the spline
|
||||||
glm::vec3 point = catmul(1.f, p0, p1, p2, p3, pathmove->time_passed);
|
glm::vec3 point = catmul(1.f,
|
||||||
|
p0,
|
||||||
|
p1,
|
||||||
|
p2,
|
||||||
|
p3,
|
||||||
|
pathmove->time_passed);
|
||||||
|
|
||||||
// Apply
|
// Apply
|
||||||
transform->set_origin(point);
|
transform->set_origin(point);
|
||||||
@ -162,8 +162,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pathmove->current_point_index == 0) {
|
if (pathmove->current_point_index == 0) {
|
||||||
// Interpolate what q0 would be if the same change happened from q0 to q1 as from q1
|
// Interpolate what q0 would be if the same change happened from q0 to q1 as from q1 to q2
|
||||||
// to q2
|
|
||||||
q0 = glm::fastMix(q1, q2, -1.0f);
|
q0 = glm::fastMix(q1, q2, -1.0f);
|
||||||
} else {
|
} else {
|
||||||
// We're fine - use the point before the current point
|
// We're fine - use the point before the current point
|
||||||
@ -171,15 +170,16 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Interpolate
|
// Interpolate
|
||||||
glm::quat result = glm::squad(q1, q2, glm::intermediate(q0, q1, q2),
|
glm::quat result = glm::squad(q1, q2, glm::intermediate(q0, q1, q2), glm::intermediate(q1, q2, q3), pathmove->time_passed);
|
||||||
glm::intermediate(q1, q2, q3), pathmove->time_passed);
|
|
||||||
|
|
||||||
// Apply
|
// Apply
|
||||||
transform->set_rotation_from_quat(result);
|
transform->set_rotation_from_quat(result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void unconfigure(World *pWorld) override { pWorld->unsubscribeAll(this); }
|
void unconfigure(World *pWorld) override {
|
||||||
|
pWorld->unsubscribeAll(this);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *myWorld;
|
World *myWorld;
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -16,8 +16,10 @@ 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() << ": " << transform->get_origin().x << ", "
|
std::cout << ent->getEntityId() << ": "
|
||||||
<< transform->get_origin().y << ", " << transform->get_origin().z
|
<< transform->get_origin().x << ", "
|
||||||
|
<< transform->get_origin().y << ", "
|
||||||
|
<< transform->get_origin().z
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5,28 +5,32 @@
|
|||||||
#ifndef ECSGAME_RENDERSYSTEM_H
|
#ifndef ECSGAME_RENDERSYSTEM_H
|
||||||
#define ECSGAME_RENDERSYSTEM_H
|
#define ECSGAME_RENDERSYSTEM_H
|
||||||
|
|
||||||
#include "../../Rendering/Material.h"
|
#include "../ECS.h"
|
||||||
#include "../../Rendering/Shader.h"
|
#include "../Components/Transform.h"
|
||||||
#include "../Components/Camera.h"
|
|
||||||
#include "../Components/DirectionalLight.h"
|
|
||||||
#include "../Components/LODObjMesh.h"
|
|
||||||
#include "../Components/Mesh.h"
|
#include "../Components/Mesh.h"
|
||||||
|
#include "../Components/Camera.h"
|
||||||
|
#include "../../Rendering/Shader.h"
|
||||||
#include "../Components/ObjMesh.h"
|
#include "../Components/ObjMesh.h"
|
||||||
#include "../Components/Texture.h"
|
#include "../Components/Texture.h"
|
||||||
#include "../Components/Transform.h"
|
#include "../Components/LODObjMesh.h"
|
||||||
#include "../ECS.h"
|
#include "../Components/DirectionalLight.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, 0.0f,
|
-1.0f, 1.0f, 0.0f, 0.0f, 1.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, 0.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);
|
||||||
@ -37,8 +41,7 @@ void renderQuad() {
|
|||||||
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),
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
(void *)(3 * sizeof(float)));
|
|
||||||
}
|
}
|
||||||
glBindVertexArray(quadVAO);
|
glBindVertexArray(quadVAO);
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
@ -48,11 +51,14 @@ 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,
|
RenderObject(const glm::mat4 &matrix, const glm::vec3 &origin, unsigned int textureId, unsigned int normalId, const Mesh &mesh, float distance, const Material &material)
|
||||||
unsigned int normalId, const Mesh &mesh, float distance,
|
: matrix(matrix),
|
||||||
const Material &material)
|
origin(origin),
|
||||||
: matrix(matrix), origin(origin), texture_id(textureId), normal_id(normalId),
|
texture_id(textureId),
|
||||||
mesh(mesh), distance(distance), material(material) {}
|
normal_id(normalId),
|
||||||
|
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;
|
||||||
@ -91,31 +97,24 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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,
|
/*pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
|
||||||
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
|
/*// TODO: Is it possible to do get ObjMeshes in the Mesh loop above implicitly via polymorphism?
|
||||||
polymorphism?
|
* // TODO: Commented out because of double rendering - we only want to get objects that explicitly DON'T have a texture here!
|
||||||
* // TODO: Commented out because of double rendering - we only want to get objects that
|
pWorld->each<ObjMesh, Transform>([&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform) {
|
||||||
explicitly DON'T have a texture here! pWorld->each<ObjMesh, Transform>([&](Entity *ent,
|
// Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh
|
||||||
ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform) {
|
float distance = glm::distance(cameraPos, transform->getPosition());
|
||||||
// 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(),
|
renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), distance));
|
||||||
distance));
|
|
||||||
}
|
}
|
||||||
});*/
|
});*/
|
||||||
|
|
||||||
// ObjMesh with textures
|
// ObjMesh with textures
|
||||||
world->each<ObjMesh, Transform, Texture>([&](Entity *ent, ComponentHandle<ObjMesh> mesh,
|
world->each<ObjMesh, Transform, Texture>([&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform, ComponentHandle<Texture> texture) {
|
||||||
ComponentHandle<Transform> transform,
|
// Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh
|
||||||
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) {
|
||||||
@ -123,29 +122,21 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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 =
|
Material material = materialComponent.isValid() ? materialComponent.get() : 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 =
|
unsigned int normalID = textureComponent.isValid() ? textureComponent->normal_id : 0;
|
||||||
textureComponent.isValid() ? textureComponent->normal_id : 0;
|
|
||||||
|
|
||||||
// Put it into the list of transparent render objects if the texture wants to be
|
// Put it into the list of transparent render objects if the texture wants to be rendered transparently
|
||||||
// rendered transparently
|
|
||||||
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
||||||
renderObjects[1].emplace_back(
|
renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh.get(), distance, material));
|
||||||
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
|
||||||
normalID, mesh.get(), distance, material));
|
|
||||||
} else {
|
} else {
|
||||||
renderObjects[0].emplace_back(
|
renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh.get(), distance, material));
|
||||||
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,
|
world->each<LODObjMesh, Transform>([&](Entity *ent, ComponentHandle<LODObjMesh> lodMesh, ComponentHandle<Transform> transform) {
|
||||||
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) {
|
||||||
@ -154,37 +145,31 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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 =
|
Material material = materialComponent.isValid() ? materialComponent.get() : 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 =
|
unsigned int normalID = textureComponent.isValid() ? textureComponent->normal_id : 0;
|
||||||
textureComponent.isValid() ? textureComponent->normal_id : 0;
|
|
||||||
|
|
||||||
// Put it into the list of transparent render objects if the texture wants to be
|
// Put it into the list of transparent render objects if the texture wants to be rendered transparently
|
||||||
// rendered transparently
|
|
||||||
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
if (textureComponent.isValid() && textureComponent->render_transparent) {
|
||||||
renderObjects[1].emplace_back(
|
renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh, distance, material));
|
||||||
RenderObject(transform->matrix, transform->get_origin(), textureID,
|
|
||||||
normalID, mesh, distance, material));
|
|
||||||
} else {
|
} else {
|
||||||
renderObjects[0].emplace_back(
|
renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, normalID, mesh, distance, material));
|
||||||
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(),
|
std::sort(renderObjects[1].begin(), renderObjects[1].end(), [](const RenderObject &first, const RenderObject &second) -> bool {
|
||||||
[](const RenderObject &first, const RenderObject &second) -> bool {
|
|
||||||
return first.distance > second.distance;
|
return first.distance > second.distance;
|
||||||
});
|
});
|
||||||
|
|
||||||
return renderObjects;
|
return renderObjects;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderSystem() { setup(); }
|
RenderSystem() {
|
||||||
|
setup();
|
||||||
|
}
|
||||||
|
|
||||||
void configure(World *pWorld) override {
|
void configure(World *pWorld) override {
|
||||||
myWorld = pWorld;
|
myWorld = pWorld;
|
||||||
@ -211,8 +196,7 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
// 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,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||||
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);
|
||||||
@ -226,12 +210,11 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
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,
|
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera, ComponentHandle<Transform> cameraTransform) {
|
||||||
ComponentHandle<Transform> cameraTransform) {
|
|
||||||
// Get render objects
|
// Get render objects
|
||||||
std::vector<std::vector<RenderObject>> allRenderObjects =
|
std::vector<std::vector<RenderObject>> allRenderObjects = getRenderObjects(pWorld, cameraTransform->get_origin());
|
||||||
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];
|
||||||
|
|
||||||
@ -239,15 +222,12 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
|||||||
// 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>(
|
pWorld->each<DirectionalLight>([&](Entity *ent, ComponentHandle<DirectionalLight> light) {
|
||||||
[&](Entity *ent, ComponentHandle<DirectionalLight> light) {
|
|
||||||
lightDirection = light->direction;
|
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::mat4 lightProjection = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, near_plane, far_plane);
|
||||||
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 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
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
#ifndef ECSGAME_SINEANIMATIONSYSTEM_H
|
#ifndef ECSGAME_SINEANIMATIONSYSTEM_H
|
||||||
#define ECSGAME_SINEANIMATIONSYSTEM_H
|
#define ECSGAME_SINEANIMATIONSYSTEM_H
|
||||||
|
|
||||||
#include "../Components/SineAnimation.h"
|
|
||||||
#include "../Components/Transform.h"
|
|
||||||
#include "../ECS.h"
|
#include "../ECS.h"
|
||||||
|
#include "../Components/Transform.h"
|
||||||
|
#include "../Components/SineAnimation.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
@ -16,11 +16,8 @@ class SineAnimationSystem : public EntitySystem {
|
|||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
passedTime += deltaTime;
|
passedTime += deltaTime;
|
||||||
|
|
||||||
pWorld->each<Transform, SineAnimation>([&](Entity *ent,
|
pWorld->each<Transform, SineAnimation>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<SineAnimation> anim) {
|
||||||
ComponentHandle<Transform> transform,
|
transform->translate(anim->maxDistance * glm::sin(passedTime * anim->speedScale) * deltaTime);
|
||||||
ComponentHandle<SineAnimation> anim) {
|
|
||||||
transform->translate(anim->maxDistance * glm::sin(passedTime * anim->speedScale) *
|
|
||||||
deltaTime);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
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,7 +36,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +69,7 @@ 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() {
|
||||||
@ -90,6 +92,7 @@ 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);
|
||||||
}
|
}
|
||||||
@ -101,17 +104,15 @@ 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"
|
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog
|
||||||
<< infoLog << "\n -- --------------------------------------------------- -- "
|
<< "\n -- --------------------------------------------------- -- " << std::endl;
|
||||||
<< 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"
|
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog
|
||||||
<< infoLog << "\n -- --------------------------------------------------- -- "
|
<< "\n -- --------------------------------------------------- -- " << std::endl;
|
||||||
<< std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
#ifndef ECSGAME_SHADER_H
|
#ifndef ECSGAME_SHADER_H
|
||||||
#define ECSGAME_SHADER_H
|
#define ECSGAME_SHADER_H
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Shader {
|
class Shader {
|
||||||
public:
|
public:
|
||||||
@ -14,8 +14,7 @@ class Shader {
|
|||||||
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
|
/// Note that an OpenGL context has to be initialized before calling this! Otherwise a SIGSEGV will be thrown.
|
||||||
/// 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.
|
||||||
@ -40,4 +39,5 @@ class Shader {
|
|||||||
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
|
||||||
|
File diff suppressed because it is too large
Load Diff
112
Util/geometry.h
112
Util/geometry.h
@ -1,112 +0,0 @@
|
|||||||
#include <vector>
|
|
||||||
|
|
||||||
// Forward declarations
|
|
||||||
struct Triangle;
|
|
||||||
|
|
||||||
struct Vector {
|
|
||||||
Vector(float coordinates[3]) : c(coordinates) {}
|
|
||||||
Vector(float x, float y, float z) : c(new float[3]{x, y, z}) {}
|
|
||||||
|
|
||||||
// Avoid having to write vector.c[index], instead allow vector[index]
|
|
||||||
float operator[](int i) const { return c[i]; }
|
|
||||||
float &operator[](int i) { return c[i]; }
|
|
||||||
|
|
||||||
Vector operator+(const Vector &other) const {
|
|
||||||
return Vector(c[0] + other.c[0], c[1] + other.c[1], c[2] + other.c[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector operator-(const Vector &other) const {
|
|
||||||
return Vector(c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector operator*(float scalar) const {
|
|
||||||
return Vector(c[0] * scalar, c[1] * scalar, c[2] * scalar);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector cross(const Vector &other) {
|
|
||||||
return Vector(c[1] * other[2] - c[2] - other[1], c[2] * other[0] - c[0] * other[2],
|
|
||||||
c[0] * other[1] - c[1] * other[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
float dot(const Vector &other) { return c[0] * other[0] + c[1] * other[1] + c[2] * other[2]; }
|
|
||||||
|
|
||||||
float *c;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Point {
|
|
||||||
Point(Vector pos, Triangle *triangle) : pos(pos), triangle(triangle) {}
|
|
||||||
|
|
||||||
Vector pos;
|
|
||||||
|
|
||||||
Triangle *triangle;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Triangle {
|
|
||||||
Triangle(Vector p1, Vector p2, Vector p3) : p1(p1), p2(p2), p3(p3) {}
|
|
||||||
|
|
||||||
std::vector<Point *> create_point_objects() {
|
|
||||||
return std::vector<Point *>{new Point(p1, this), new Point(p2, this), new Point(p3, this)};
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector p1;
|
|
||||||
Vector p2;
|
|
||||||
Vector p3;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Node {
|
|
||||||
Node(int axis, Point *point, Node *left, Node *right)
|
|
||||||
: axis(axis), point(point), left(left), right(right) {}
|
|
||||||
|
|
||||||
int axis;
|
|
||||||
|
|
||||||
Point *point;
|
|
||||||
|
|
||||||
Node *left;
|
|
||||||
Node *right;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Ray {
|
|
||||||
Ray(Vector origin, Vector direction) : origin(origin), direction(direction) {}
|
|
||||||
|
|
||||||
Vector origin;
|
|
||||||
|
|
||||||
Vector direction;
|
|
||||||
|
|
||||||
bool intersects_triangle(Triangle *triangle) {
|
|
||||||
// Ray-triangle-intersection with the Möller–Trumbore algorithm
|
|
||||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
|
||||||
const float EPSILON = 0.0000001;
|
|
||||||
|
|
||||||
Vector p1 = triangle->p1;
|
|
||||||
Vector p2 = triangle->p2;
|
|
||||||
Vector p3 = triangle->p3;
|
|
||||||
|
|
||||||
Vector edge1 = p2 - p1;
|
|
||||||
Vector edge2 = p3 - p1;
|
|
||||||
|
|
||||||
Vector h = direction.cross(edge2);
|
|
||||||
float a = edge1.dot(h);
|
|
||||||
|
|
||||||
if (a > -EPSILON && a < EPSILON) return false; // This ray is parallel to this triangle.
|
|
||||||
|
|
||||||
float f = 1.0 / a;
|
|
||||||
Vector s = origin - p1;
|
|
||||||
float u = f * s.dot(h);
|
|
||||||
|
|
||||||
if (u < 0.0 || u > 1.0) return false;
|
|
||||||
|
|
||||||
Vector q = s.cross(edge1);
|
|
||||||
float v = f * direction.dot(q);
|
|
||||||
if (v < 0.0 || u + v > 1.0) return false;
|
|
||||||
|
|
||||||
// At this stage we can compute t to find out where the intersection point is on the
|
|
||||||
// line.
|
|
||||||
float t = f * edge2.dot(q);
|
|
||||||
if (t > EPSILON) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
// This means that there is a line intersection but not a ray intersection.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
139
Util/kdtree.h
139
Util/kdtree.h
@ -1,139 +0,0 @@
|
|||||||
#include "geometry.h"
|
|
||||||
#include <algorithm>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class KDTree {
|
|
||||||
public:
|
|
||||||
KDTree(std::vector<Point *> points) { root = build(points, 0); }
|
|
||||||
|
|
||||||
~KDTree() = default; // TODO: Delete all allocated Nodes
|
|
||||||
|
|
||||||
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); }
|
|
||||||
|
|
||||||
std::string to_string() {
|
|
||||||
std::string str = "";
|
|
||||||
to_string_recurse(str, root, 0);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Node *root;
|
|
||||||
|
|
||||||
int MAX_DEPTH = 500;
|
|
||||||
|
|
||||||
// Returns a comparator lambda for assessing which of the two points has a
|
|
||||||
// greater coordinate in the given axis.
|
|
||||||
auto get_point_comparator(int axis) {
|
|
||||||
return [axis](Point *p1, Point *p2) {
|
|
||||||
return p1->pos[axis] < p2->pos[axis];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *build(std::vector<Point *> points, int depth) {
|
|
||||||
// Exit conditions
|
|
||||||
if (points.empty() || depth > MAX_DEPTH) { return nullptr; }
|
|
||||||
|
|
||||||
// Select axis by choosing the one with maximal extent
|
|
||||||
float max_extent = 0;
|
|
||||||
int axis = 0;
|
|
||||||
|
|
||||||
for (int it_axis = 0; it_axis < 3; it_axis++) {
|
|
||||||
// Get extent along this axis
|
|
||||||
auto comparator = get_point_comparator(it_axis);
|
|
||||||
|
|
||||||
Point *min = *std::min_element(points.begin(), points.end(), comparator);
|
|
||||||
Point *max = *std::max_element(points.begin(), points.end(), comparator);
|
|
||||||
|
|
||||||
float extent = max->pos[it_axis] - min->pos[it_axis];
|
|
||||||
|
|
||||||
// Is it greater than max_extent?
|
|
||||||
if (extent > max_extent) {
|
|
||||||
// If so, make this the splitting axis
|
|
||||||
max_extent = extent;
|
|
||||||
axis = it_axis;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Choose the median as the pivot and sort the points into
|
|
||||||
// left-of-median and right-of-median using nth_element
|
|
||||||
int middle = points.size() / 2;
|
|
||||||
|
|
||||||
std::nth_element(points.begin(), points.begin() + middle, points.end(),
|
|
||||||
get_point_comparator(axis));
|
|
||||||
|
|
||||||
Point *median = points[middle];
|
|
||||||
|
|
||||||
// TODO: This copies. Can we split the vector into two without copying?
|
|
||||||
std::vector<Point *> left_of_median(points.begin(), points.begin() + middle);
|
|
||||||
std::vector<Point *> right_of_median(points.begin() + middle + 1, points.end());
|
|
||||||
|
|
||||||
// Create node, recursively call to construct subtree
|
|
||||||
return new Node(axis, median, build(left_of_median, depth + 1),
|
|
||||||
build(right_of_median, depth + 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance) {
|
|
||||||
// Exit condition: There was no collision
|
|
||||||
if (node == nullptr) { return nullptr; }
|
|
||||||
|
|
||||||
// Is the left or right child node closer to this point?
|
|
||||||
Node *near =
|
|
||||||
ray.origin[node->axis] > node->point->pos[node->axis] ? node->right : node->left;
|
|
||||||
Node *far = near == node->right ? node->left : node->right;
|
|
||||||
|
|
||||||
std::cout << "Checking " << node->point->pos[0] << ", " << node->point->pos[1] << ", "
|
|
||||||
<< node->point->pos[2] << std::endl;
|
|
||||||
|
|
||||||
// Check for collisions in this order (stopping if an intersection is found):
|
|
||||||
// 1. In the nearer section
|
|
||||||
// 2. With the point in this current node
|
|
||||||
// 3. In the further section
|
|
||||||
|
|
||||||
// If the axes are not parallel, our max_distance decreases, since we've already covered
|
|
||||||
// some area. `t` represents the distance from this node to the splitting plane.
|
|
||||||
float t = ray.direction[node->axis] != 0.0
|
|
||||||
? (node->point->pos[node->axis] - ray.origin[node->axis]) /
|
|
||||||
ray.direction[node->axis]
|
|
||||||
: max_distance;
|
|
||||||
Triangle *near_result = intersect_ray_recurse(ray, near, t);
|
|
||||||
|
|
||||||
// If the nearer segment had a collision, we're done! We're only interested in the closest
|
|
||||||
// collision.
|
|
||||||
if (near_result != nullptr) { return near_result; }
|
|
||||||
|
|
||||||
// No collision in the nearer side, so check for a collision directly here
|
|
||||||
if (ray.intersects_triangle(node->point->triangle)) {
|
|
||||||
// We do have a collision here, so we're done and can return this point!
|
|
||||||
return node->point->triangle;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No collision here either. Does it make sense to also check the far node?
|
|
||||||
// Only if the axes are not parallel and if that area is not behind us
|
|
||||||
if (ray.direction[node->axis] != 0.0 && t >= 0.0) {
|
|
||||||
// It does make sense to check the far node.
|
|
||||||
// For this, calculate a new ray origin and continue towards that direction, but with
|
|
||||||
// the new origin (we can leave behind what we already checked)
|
|
||||||
return intersect_ray_recurse(Ray(ray.origin + ray.direction * t, ray.direction), far,
|
|
||||||
max_distance - t);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If nothing worked, return a nullptr
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void to_string_recurse(std::string &str, Node *node, int depth) {
|
|
||||||
if (node == nullptr) { return; }
|
|
||||||
|
|
||||||
Point *point = node->point;
|
|
||||||
|
|
||||||
str += std::string(depth * 2, ' ') + std::to_string(point->pos[0]) + ", " +
|
|
||||||
std::to_string(point->pos[1]) + ", " + std::to_string(point->pos[2]) +
|
|
||||||
" with axis " + std::to_string(node->axis) + "\n";
|
|
||||||
|
|
||||||
to_string_recurse(str, node->left, depth + 1);
|
|
||||||
to_string_recurse(str, node->right, depth + 1);
|
|
||||||
}
|
|
||||||
};
|
|
85
main.cpp
85
main.cpp
@ -1,32 +1,35 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "ECS/Components/DirectionalLight.h"
|
#include "Rendering/Shader.h"
|
||||||
#include "ECS/Components/ObjMesh.h"
|
|
||||||
#include "ECS/Components/PathMove.h"
|
|
||||||
#include "ECS/Components/SineAnimation.h"
|
|
||||||
#include "ECS/Components/Texture.h"
|
|
||||||
#include "ECS/ECS.h"
|
#include "ECS/ECS.h"
|
||||||
#include "ECS/Events/InputEvent.h"
|
#include "ECS/Events/InputEvent.h"
|
||||||
#include "ECS/Events/MouseMoveEvent.h"
|
#include "ECS/Events/MouseMoveEvent.h"
|
||||||
#include "ECS/Systems/CollisionSystem.h"
|
|
||||||
#include "ECS/Systems/GravitySystem.h"
|
#include "ECS/Systems/GravitySystem.h"
|
||||||
#include "ECS/Systems/InteractivePathSystem.h"
|
#include "ECS/Systems/PositionDebugSystem.h"
|
||||||
#include "ECS/Systems/KeyboardMovementSystem.h"
|
#include "ECS/Systems/KeyboardMovementSystem.h"
|
||||||
|
#include "ECS/Systems/RenderSystem.h"
|
||||||
#include "ECS/Systems/MouseLookSystem.h"
|
#include "ECS/Systems/MouseLookSystem.h"
|
||||||
#include "ECS/Systems/PathMoveSystem.h"
|
#include "ECS/Systems/PathMoveSystem.h"
|
||||||
#include "ECS/Systems/PositionDebugSystem.h"
|
#include "ECS/Components/ObjMesh.h"
|
||||||
#include "ECS/Systems/RenderSystem.h"
|
#include "ECS/Components/Texture.h"
|
||||||
|
#include "ECS/Components/SineAnimation.h"
|
||||||
#include "ECS/Systems/SineAnimationSystem.h"
|
#include "ECS/Systems/SineAnimationSystem.h"
|
||||||
#include "Rendering/Shader.h"
|
#include "ECS/Components/DirectionalLight.h"
|
||||||
|
#include "ECS/Components/PathMove.h"
|
||||||
|
#include "ECS/Systems/InteractivePathSystem.h"
|
||||||
|
|
||||||
using namespace ECS;
|
using namespace ECS;
|
||||||
|
|
||||||
|
|
||||||
World *world = World::createWorld();
|
World *world = World::createWorld();
|
||||||
|
|
||||||
|
|
||||||
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||||
|
|
||||||
world->emit<InputEvent>({key, action});
|
world->emit<InputEvent>({key, action});
|
||||||
}
|
}
|
||||||
@ -47,7 +50,8 @@ int main(int argc, char **argv) {
|
|||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
|
|
||||||
/* Initialize the library */
|
/* Initialize the library */
|
||||||
if (!glfwInit()) return -1;
|
if (!glfwInit())
|
||||||
|
return -1;
|
||||||
|
|
||||||
// Anti Aliasing
|
// Anti Aliasing
|
||||||
glfwWindowHint(GLFW_SAMPLES, aa_samples);
|
glfwWindowHint(GLFW_SAMPLES, aa_samples);
|
||||||
@ -70,7 +74,8 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
// glad: load all OpenGL function pointers
|
// glad: load all OpenGL function pointers
|
||||||
// ---------------------------------------
|
// ---------------------------------------
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
|
{
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -90,9 +95,6 @@ int main(int argc, char **argv) {
|
|||||||
world->registerSystem(new SineAnimationSystem());
|
world->registerSystem(new SineAnimationSystem());
|
||||||
world->registerSystem(new InteractivePathSystem());
|
world->registerSystem(new InteractivePathSystem());
|
||||||
|
|
||||||
CollisionSystem *collision_system = new CollisionSystem();
|
|
||||||
world->registerSystem(collision_system);
|
|
||||||
|
|
||||||
RenderSystem* renderSystem = new RenderSystem();
|
RenderSystem* renderSystem = new RenderSystem();
|
||||||
world->registerSystem(renderSystem);
|
world->registerSystem(renderSystem);
|
||||||
|
|
||||||
@ -101,30 +103,33 @@ int main(int argc, char **argv) {
|
|||||||
player->assign<Movement>(glm::vec3(5.f, 5.f, 5.f));
|
player->assign<Movement>(glm::vec3(5.f, 5.f, 5.f));
|
||||||
player->assign<MouseLook>(0.1);
|
player->assign<MouseLook>(0.1);
|
||||||
player->assign<Camera>(70.0f, 900, 600, 0.1f, 100.0f);
|
player->assign<Camera>(70.0f, 900, 600, 0.1f, 100.0f);
|
||||||
player->assign<PathMove>(
|
player->assign<PathMove>(3.0, PathMove::Path(std::vector<glm::vec3>{
|
||||||
3.0,
|
glm::vec3(0.0, 2.0, 0.0),
|
||||||
PathMove::Path(std::vector<glm::vec3>{glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 2.5, -1.0),
|
glm::vec3(0.0, 2.5, -1.0),
|
||||||
glm::vec3(0.0, 2.5, -2.0), glm::vec3(3.0, 3.0, -3.0),
|
glm::vec3(0.0, 2.5, -2.0),
|
||||||
glm::vec3(4.0, 2.0, -4.0), glm::vec3(3.0, 2.0, -5.0),
|
glm::vec3(3.0, 3.0, -3.0),
|
||||||
glm::vec3(0.0, 2.0, -10.0)}),
|
glm::vec3(4.0, 2.0, -4.0),
|
||||||
PathMove::Views(
|
glm::vec3(3.0, 2.0, -5.0),
|
||||||
std::vector<glm::quat>{glm::angleAxis(glm::radians(0.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::vec3(0.0, 2.0, -10.0)
|
||||||
|
}),
|
||||||
|
PathMove::Views(std::vector<glm::quat>{
|
||||||
|
glm::angleAxis(glm::radians(0.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(10.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::angleAxis(glm::radians(10.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(20.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::angleAxis(glm::radians(20.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(40.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::angleAxis(glm::radians(40.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(120.f), glm::vec3(0.f, 1.f, 0.f)),
|
glm::angleAxis(glm::radians(120.f), glm::vec3(0.f, 1.f, 0.f)),
|
||||||
glm::angleAxis(glm::radians(180.f), glm::vec3(0.f, 1.f, 0.f))}));
|
glm::angleAxis(glm::radians(180.f), glm::vec3(0.f, 1.f, 0.f))
|
||||||
|
})
|
||||||
|
);
|
||||||
player->get<Transform>()->set_origin(glm::vec3(0.0, 3.0, 4.0));
|
player->get<Transform>()->set_origin(glm::vec3(0.0, 3.0, 4.0));
|
||||||
|
|
||||||
Entity *monkey = world->create();
|
Entity *monkey = world->create();
|
||||||
monkey->assign<Transform>();
|
monkey->assign<Transform>();
|
||||||
monkey->assign<LODObjMesh>(std::vector<ObjMesh>{
|
monkey->assign<LODObjMesh>(std::vector<ObjMesh>{ObjMesh("Resources/Monkey.obj",ObjMesh::Settings(0.0, 8.0, 0.4, 0.6)),
|
||||||
ObjMesh("Resources/Monkey.obj", ObjMesh::Settings(0.0, 8.0, 0.4, 0.6)),
|
|
||||||
ObjMesh("Resources/MonkeySimple.obj", ObjMesh::Settings(8.0, 100.0, 0.4, 0.6))});
|
ObjMesh("Resources/MonkeySimple.obj", ObjMesh::Settings(8.0, 100.0, 0.4, 0.6))});
|
||||||
monkey->assign<Texture>("Resources/Marble010_2K_Color.jpg", Texture::Settings(true), false);
|
monkey->assign<Texture>("Resources/Marble010_2K_Color.jpg", Texture::Settings(true), false);
|
||||||
monkey->get<Texture>()->addNormalmap("Resources/Marble010_2K_Normal.jpg",
|
monkey->get<Texture>()->addNormalmap("Resources/Marble010_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
monkey->assign<SineAnimation>(glm::vec3(0.0, 0.3, 0.0), 0.5);
|
monkey->assign<SineAnimation>(glm::vec3(0.0, 0.3, 0.0), 0.5);
|
||||||
monkey->assign<Material>(0.6, 0.6);
|
monkey->assign<Material>(0.6, 0.6);
|
||||||
monkey->get<Transform>()->set_origin(glm::vec3(0.0f, 2.0f, -6.0f));
|
monkey->get<Transform>()->set_origin(glm::vec3(0.0f, 2.0f, -6.0f));
|
||||||
@ -133,8 +138,7 @@ int main(int argc, char **argv) {
|
|||||||
wall1->assign<Transform>();
|
wall1->assign<Transform>();
|
||||||
wall1->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
wall1->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
||||||
wall1->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
wall1->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
||||||
wall1->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg",
|
wall1->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
wall1->assign<Material>(0.2, 0.8);
|
wall1->assign<Material>(0.2, 0.8);
|
||||||
wall1->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, -2.0f));
|
wall1->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, -2.0f));
|
||||||
|
|
||||||
@ -142,8 +146,7 @@ int main(int argc, char **argv) {
|
|||||||
wall2->assign<Transform>();
|
wall2->assign<Transform>();
|
||||||
wall2->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
wall2->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
||||||
wall2->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
wall2->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
||||||
wall2->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg",
|
wall2->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
wall2->assign<Material>(0.2, 0.8);
|
wall2->assign<Material>(0.2, 0.8);
|
||||||
wall2->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, -10.0f));
|
wall2->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, -10.0f));
|
||||||
|
|
||||||
@ -151,8 +154,7 @@ int main(int argc, char **argv) {
|
|||||||
wall3->assign<Transform>();
|
wall3->assign<Transform>();
|
||||||
wall3->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
wall3->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
||||||
wall3->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
wall3->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
||||||
wall3->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg",
|
wall3->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
wall3->assign<Material>(0.2, 0.8);
|
wall3->assign<Material>(0.2, 0.8);
|
||||||
wall3->get<Transform>()->set_origin(glm::vec3(4.0f, 0.0f, -6.0f));
|
wall3->get<Transform>()->set_origin(glm::vec3(4.0f, 0.0f, -6.0f));
|
||||||
wall3->get<Transform>()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0));
|
wall3->get<Transform>()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0));
|
||||||
@ -161,8 +163,7 @@ int main(int argc, char **argv) {
|
|||||||
wall4->assign<Transform>();
|
wall4->assign<Transform>();
|
||||||
wall4->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
wall4->assign<ObjMesh>(ObjMesh("Resources/Wall.obj", ObjMesh::Settings()));
|
||||||
wall4->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
wall4->assign<Texture>("Resources/Facade001_2K_Color.png", Texture::Settings(true), true);
|
||||||
wall4->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg",
|
wall4->get<Texture>()->addNormalmap("Resources/Facade001_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
wall4->assign<Material>(0.2, 0.8);
|
wall4->assign<Material>(0.2, 0.8);
|
||||||
wall4->get<Transform>()->set_origin(glm::vec3(-4.0f, 0.0f, -6.0f));
|
wall4->get<Transform>()->set_origin(glm::vec3(-4.0f, 0.0f, -6.0f));
|
||||||
wall4->get<Transform>()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0));
|
wall4->get<Transform>()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0));
|
||||||
@ -171,8 +172,7 @@ int main(int argc, char **argv) {
|
|||||||
ground->assign<Transform>();
|
ground->assign<Transform>();
|
||||||
ground->assign<ObjMesh>(ObjMesh("Resources/Ground.obj", ObjMesh::Settings()));
|
ground->assign<ObjMesh>(ObjMesh("Resources/Ground.obj", ObjMesh::Settings()));
|
||||||
ground->assign<Texture>("Resources/Ground003_2K_Color.jpg", Texture::Settings(true), false);
|
ground->assign<Texture>("Resources/Ground003_2K_Color.jpg", Texture::Settings(true), false);
|
||||||
ground->get<Texture>()->addNormalmap("Resources/Ground003_2K_Normal.jpg",
|
ground->get<Texture>()->addNormalmap("Resources/Ground003_2K_Normal.jpg", Texture::Settings(true));
|
||||||
Texture::Settings(true));
|
|
||||||
ground->assign<Material>(1.0, 0.0);
|
ground->assign<Material>(1.0, 0.0);
|
||||||
ground->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, 0.0f));
|
ground->get<Transform>()->set_origin(glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
|
|
||||||
@ -196,9 +196,6 @@ int main(int argc, char **argv) {
|
|||||||
Entity *sun = world->create();
|
Entity *sun = world->create();
|
||||||
sun->assign<DirectionalLight>(glm::normalize(glm::vec3(1.0, 1.0, 1.0)));
|
sun->assign<DirectionalLight>(glm::normalize(glm::vec3(1.0, 1.0, 1.0)));
|
||||||
|
|
||||||
// We're done loading geometry -> build the collision structure
|
|
||||||
collision_system->build();
|
|
||||||
|
|
||||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||||
Shader shadowShader("Shaders/shadow-vertex.vs", "Shaders/shadow-fragment.fs");
|
Shader shadowShader("Shaders/shadow-vertex.vs", "Shaders/shadow-fragment.fs");
|
||||||
Shader debugShader("Shaders/debug-vertex.vs", "Shaders/debug-fragment.fs");
|
Shader debugShader("Shaders/debug-vertex.vs", "Shaders/debug-fragment.fs");
|
||||||
@ -217,9 +214,7 @@ int main(int argc, char **argv) {
|
|||||||
renderSystem->render(world, defaultShader, shadowShader, debugShader);
|
renderSystem->render(world, defaultShader, shadowShader, debugShader);
|
||||||
|
|
||||||
ring->get<Transform>()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0));
|
ring->get<Transform>()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0));
|
||||||
sun->get<DirectionalLight>()->direction = glm::normalize(glm::vec3(
|
sun->get<DirectionalLight>()->direction = glm::normalize(glm::vec3(glm::rotate(glm::mat4(1), (float)elapsed_time * 0.5f, glm::vec3(0.0, 1.0, 0.0)) * glm::vec4(1.0, 1.0, 1.0, 0.0)));
|
||||||
glm::rotate(glm::mat4(1), (float)elapsed_time * 0.5f, glm::vec3(0.0, 1.0, 0.0)) *
|
|
||||||
glm::vec4(1.0, 1.0, 1.0, 0.0)));
|
|
||||||
|
|
||||||
/* Swap front and back buffers */
|
/* Swap front and back buffers */
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
||||||
clang_format_command="clang-format"
|
|
||||||
clang_tidy_command="run-clang-tidy"
|
|
||||||
fi
|
|
||||||
if [[ "$OSTYPE" == "linux"* ]]; then
|
|
||||||
clang_format_command="clang-format-11"
|
|
||||||
clang_tidy_command="run-clang-tidy-11"
|
|
||||||
fi
|
|
||||||
|
|
||||||
eval "$clang_format_command -i *.h *.cpp -style=file"
|
|
||||||
eval "$clang_tidy_command -fix"
|
|
Loading…
x
Reference in New Issue
Block a user