aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-03-11 01:52:27 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-18 15:22:10 +0100
commit1156c0f58b01ff080b9d9ede15b449a845df6654 (patch)
tree426c14ffd9e6aac2d37af58211e78b1fae8d6fe6
parent12c36c61c3f8d19c44cb2e5ffdf3ed812a0390d2 (diff)
Starting on entity system...HEADmaster
-rw-r--r--include/entity/Entity.hpp46
-rw-r--r--include/entity/MeshInstance.hpp8
-rw-r--r--project.conf5
-rw-r--r--src/ModelLoader.cpp11
-rw-r--r--src/entity/Entity.cpp62
-rw-r--r--src/entity/MeshInstance.cpp7
-rw-r--r--src/main.cpp2
7 files changed, 136 insertions, 5 deletions
diff --git a/include/entity/Entity.hpp b/include/entity/Entity.hpp
new file mode 100644
index 0000000..853da3a
--- /dev/null
+++ b/include/entity/Entity.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "MeshInstance.hpp"
+#include "../types.hpp"
+
+#include <glm/gtc/type_ptr.hpp>
+
+namespace amalgine {
+ const int NUM_ENTITIES = 1024; // Should always be a power a multiple of 16, to utilize compiler auto-vectorization
+
+ using Entity = int;
+ const Entity INVALID_ENTITY = -1;
+
+ using EntityMaskType = u64;
+ enum EntityMask : EntityMaskType {
+ ENTITY_MASK_NONE = 0,
+ ENTITY_MASK_TRANSFORMATION = 1 << 0,
+ ENTITY_MASK_VELOCITY = 1 << 1,
+ ENTITY_MASK_MESH_COMPONENT = 1 << 2
+ };
+
+ class EntityWorld {
+ public:
+ EntityWorld();
+
+ // Returns @INVALID_ENTITY on failure
+ // O(n)
+ Entity create_entity(EntityMask type);
+ // O(1)
+ void remove_entity(Entity entity);
+
+ glm::mat4& get_transformation(Entity entity);
+ glm::vec3& get_velocity(Entity entity);
+ MeshInstance& get_mesh_instance(Entity entity);
+
+
+ void update();
+ private:
+ EntityMask mask[NUM_ENTITIES];
+
+ // Components
+ glm::mat4 transformation[NUM_ENTITIES];
+ glm::vec3 velocity[NUM_ENTITIES];
+ MeshInstance mesh_instance[NUM_ENTITIES];
+ };
+} \ No newline at end of file
diff --git a/include/entity/MeshInstance.hpp b/include/entity/MeshInstance.hpp
new file mode 100644
index 0000000..004c1c7
--- /dev/null
+++ b/include/entity/MeshInstance.hpp
@@ -0,0 +1,8 @@
+#pragma once
+
+namespace amalgine {
+ class MeshInstance {
+ public:
+ void update();
+ };
+} \ No newline at end of file
diff --git a/project.conf b/project.conf
index 150bfa7..6c95bda 100644
--- a/project.conf
+++ b/project.conf
@@ -4,6 +4,9 @@ type = "executable"
version = "0.1.0"
platforms = ["linux32", "linux64", "win64"]
+[config]
+expose_include_dirs = ["include"]
+
[lang.cpp]
version = "c++14"
@@ -14,4 +17,4 @@ glm = "0.9.8"
# TODO: Remove this and use a simple jpg and png loader
soil = "1.16.0"
# TODO: Add support for vcpkg in sibs and use that instead
-bullet = "2" \ No newline at end of file
+bullet = "2"
diff --git a/src/ModelLoader.cpp b/src/ModelLoader.cpp
index 60626f1..099349c 100644
--- a/src/ModelLoader.cpp
+++ b/src/ModelLoader.cpp
@@ -171,13 +171,18 @@ namespace amalgine {
for(unsigned int j = 0; j < num_textures; ++j) {
std::string texture_name = model_loader.extract_null_terminated_string();
+ if(texture_name.empty())
+ continue;
+
fprintf(stderr, "texture name: %s\n", texture_name.c_str());
std::string texture_path = dir_path + "/" + texture_name;
- assert(!*image); // TODO: Support multiple images
+ //assert(!*image); // TODO: Support multiple images
+ if(*image)
+ continue;
Result<Image*> image_result = Image::loadFromFile(texture_path.c_str());
if(!image_result) {
- fprintf(stderr, "%s\n", image_result.getErrorMsg().c_str());
- return false;
+ fprintf(stderr, "Warning: %s\n", image_result.getErrorMsg().c_str());
+ continue;
}
*image = image_result.unwrap();
}
diff --git a/src/entity/Entity.cpp b/src/entity/Entity.cpp
new file mode 100644
index 0000000..57f6110
--- /dev/null
+++ b/src/entity/Entity.cpp
@@ -0,0 +1,62 @@
+#include "../../include/entity/Entity.hpp"
+#include <assert.h>
+
+namespace amalgine {
+ EntityWorld::EntityWorld() {
+ for(int i = 0; i < NUM_ENTITIES; ++i) {
+ mask[i] = ENTITY_MASK_NONE;
+ transformation[i] = glm::mat4(1.0f);
+ velocity[i] = glm::vec3(0.0f, 0.0f, 0.0f);
+ mesh_instance[i] = MeshInstance();
+ }
+ }
+
+ Entity EntityWorld::create_entity(EntityMask type) {
+ for(int i = 0; i < NUM_ENTITIES; ++i) {
+ if(mask[i] == ENTITY_MASK_NONE) {
+ mask[i] = type;
+ return i;
+ }
+ }
+ return INVALID_ENTITY;
+ }
+
+ void EntityWorld::remove_entity(Entity entity) {
+ mask[entity] = ENTITY_MASK_NONE;
+ }
+
+ glm::mat4& EntityWorld::get_transformation(Entity entity) {
+ assert(mask[entity] & ENTITY_MASK_TRANSFORMATION);
+ return transformation[entity];
+ }
+
+ glm::vec3& EntityWorld::get_velocity(Entity entity) {
+ assert(mask[entity] & ENTITY_MASK_VELOCITY);
+ return velocity[entity];
+ }
+
+ MeshInstance& EntityWorld::get_mesh_instance(Entity entity) {
+ assert(mask[entity] & ENTITY_MASK_MESH_COMPONENT);
+ return mesh_instance[entity];
+ }
+
+ void EntityWorld::update() {
+ const float gravity = -9.8f;
+ for(int i = 0; i < NUM_ENTITIES; ++i) {
+ velocity[i].z -= gravity;
+ }
+
+ const EntityMaskType transform_velocity_mask = ENTITY_MASK_TRANSFORMATION | ENTITY_MASK_VELOCITY;
+ for(int i = 0; i < NUM_ENTITIES; ++i) {
+ if((mask[i] & transform_velocity_mask) == transform_velocity_mask) {
+ transformation[i][3].x += velocity[i].x;
+ transformation[i][3].y += velocity[i].y;
+ transformation[i][3].z += velocity[i].z;
+ }
+ }
+
+ for(int i = 0; i < NUM_ENTITIES; ++i) {
+ mesh_instance[i].update();
+ }
+ }
+} \ No newline at end of file
diff --git a/src/entity/MeshInstance.cpp b/src/entity/MeshInstance.cpp
new file mode 100644
index 0000000..ab7c931
--- /dev/null
+++ b/src/entity/MeshInstance.cpp
@@ -0,0 +1,7 @@
+#include "../../include/entity/MeshInstance.hpp"
+
+namespace amalgine {
+ void MeshInstance::update() {
+
+ }
+} \ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 7cefb17..ac88819 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -97,7 +97,7 @@ int main() {
std::unique_ptr<Shader> vertex_shader = load_shader_from_file("shaders/vertex.vert", Shader::Type::VERTEX);
std::unique_ptr<Shader> pixel_shader = load_shader_from_file("shaders/fragment.frag", Shader::Type::PIXEL);
std::unique_ptr<ShaderProgram> shader_program = build_shader_program_from_shaders({ vertex_shader.get(), pixel_shader.get() });
- Model buddha_model = load_model("/home/dec05eba/Downloads/ELI4OS.a3d", shader_program.get());
+ Model buddha_model = load_model("/home/dec05eba/Downloads/Zora Armor/link.a3d", shader_program.get());
buddha_model.frame.get_uniform_by_name("proj")->set(proj);
Result<Uniform> view_uniform = buddha_model.frame.get_uniform_by_name("view");
Result<Uniform> model_uniform = buddha_model.frame.get_uniform_by_name("model");