From 2b095e30f7786526da9339ccada3aae7f2ab5df4 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Tue, 10 Sep 2019 23:55:54 -0400 Subject: Started world system --- src/render.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/render.hpp') diff --git a/src/render.hpp b/src/render.hpp index 0e4275e..8dcb434 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -22,11 +22,11 @@ #ifndef SYSTEM_RENDER_HPP_ #define SYSTEM_RENDER_HPP_ -#include "shader.hpp" - #include +#include #include +#include #define GLM_FORCE_RADIANS #include @@ -34,6 +34,8 @@ #include #include +#include "shader.hpp" + class RenderSystem : public entityx::System { private: -- cgit v1.2.3 From 534f6f57e930020eb1ad726f4de4e90cf487e584 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Sat, 14 Sep 2019 03:31:40 -0400 Subject: World can now draw --- Assets/world_normal.png | Bin 268 -> 1204 bytes Scripts/world.lua | 3 --- src/engine.cpp | 2 +- src/render.cpp | 26 ++++++++++++++++++++++++-- src/render.hpp | 7 +++++-- src/script.hpp | 2 ++ src/world.hpp | 6 +++++- 7 files changed, 37 insertions(+), 9 deletions(-) (limited to 'src/render.hpp') diff --git a/Assets/world_normal.png b/Assets/world_normal.png index d45512a..e5f013c 100644 Binary files a/Assets/world_normal.png and b/Assets/world_normal.png differ diff --git a/Scripts/world.lua b/Scripts/world.lua index 246ece0..6a526dd 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -72,13 +72,10 @@ world = { YDepth = math.random(2,5) for Y = 0,ysize-1 do if Y == YGen then - --self.data[Z][X][Y] = 0 self:setData(X, Y, Z, "grass"); elseif Y < YGen and Y > (YGen - YDepth) then - --self.data[Z][X][Y] = 1 self:setData(X, Y, Z, "dirt"); elseif Y < YGen then - --self.data[Z][X][Y] = 2 self:setData(X, Y, Z, "stone"); end --print(X..","..Y..","..Z); diff --git a/src/engine.cpp b/src/engine.cpp index 6e6a0e5..191493e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -44,8 +44,8 @@ int Engine::init(void) systems.add(); systems.add(); systems.add(entities); - systems.add(); systems.add(); + systems.add(*(systems.system().get())); systems.add(entities, *(systems.system().get())); systems.add(); systems.configure(); diff --git a/src/render.cpp b/src/render.cpp index ceef025..5efccce 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -69,7 +69,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, ); glm::mat4 model = glm::mat4(1.0f); - model = glm::scale(model, glm::vec3(2.0f)); + model = glm::scale(model, glm::vec3(8.0f)); glUseProgram(s); @@ -178,7 +178,28 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, 5*sizeof(float), (void*)(3*sizeof(float))); glDrawArrays(GL_TRIANGLES, 0, 6); }); - + + std::basic_string& wm = worldSystem.current()->getMesh(); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getTexture()); + glUniform1i(q, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getNormal()); + glUniform1i(n, 1); + + glBindBuffer(GL_ARRAY_BUFFER, world_vbo); + glBufferData(GL_ARRAY_BUFFER, + wm.size() * sizeof(WorldMeshData), + &wm.front(), + GL_STREAM_DRAW); + + glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, + 6*sizeof(float), 0); + glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(3*sizeof(float))); + glDrawArrays(GL_TRIANGLES, 0, wm.size()); /************* * CLEANUP * @@ -262,6 +283,7 @@ int RenderSystem::init(void) //glClearColor(0.6, 0.8, 1.0, 0.0); camPos = glm::vec3(0.0f, 0.0f, 0.5f); + glGenBuffers(1, &world_vbo); return 0; } diff --git a/src/render.hpp b/src/render.hpp index 8dcb434..82ca844 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -35,6 +35,7 @@ #include #include "shader.hpp" +#include "world.hpp" class RenderSystem : public entityx::System { @@ -48,10 +49,12 @@ private: Shader worldShader; glm::vec3 camPos; + GLuint world_vbo; + WorldSystem &worldSystem; public: - RenderSystem(void) : - window(nullptr, SDL_DestroyWindow) {} + RenderSystem(WorldSystem & _ws) : + window(nullptr, SDL_DestroyWindow), worldSystem(_ws) {} ~RenderSystem(void) { diff --git a/src/script.hpp b/src/script.hpp index 5c4c780..87027d5 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -50,6 +50,8 @@ private: entityx::EntityManager& manager; + // TODO possibly emit events to spawn worlds instead of passing the + // world system around like a toy WorldSystem &worldSystem; public: diff --git a/src/world.hpp b/src/world.hpp index 81e5b9d..5933306 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -33,7 +33,7 @@ struct WorldMeshData float posX, posY, posZ; float texX, texY; float transparency; -}; +}__attribute__((packed)); struct WorldMaterial { @@ -100,6 +100,9 @@ public: /* RENDERING */ void generateMesh(); + std::basic_string& getMesh() {return mesh;} + GLuint getTexture() {return registry.at(0).texture.tex;} + GLuint getNormal() {return registry.at(0).normal.tex;}; /* SEED */ unsigned int getSeed(); @@ -125,6 +128,7 @@ public: } World* addWorld(sol::object); + World* current() {return currentWorld;}; void cleanup() { worlds.clear(); -- cgit v1.2.3 From 0236eb7f6391c9d925dcaaddb8cb01ecfb7d5e55 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Mon, 16 Sep 2019 18:16:43 -0400 Subject: Render system is now decoupled from the world system --- src/engine.cpp | 10 ++++++++-- src/events/world.hpp | 8 ++++++-- src/render.cpp | 49 ++++++++++++++++++++++++++++++------------------- src/render.hpp | 20 +++++++++++++++----- src/world.cpp | 20 +++++++++++++++++++- src/world.hpp | 5 +++++ 6 files changed, 83 insertions(+), 29 deletions(-) (limited to 'src/render.hpp') diff --git a/src/engine.cpp b/src/engine.cpp index e38020d..a2d0e9b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -45,7 +45,7 @@ int Engine::init(void) systems.add(); systems.add(entities); systems.add(); - systems.add(*(systems.system().get())); + systems.add(); systems.add(entities, *(systems.system().get())); systems.add(); systems.configure(); @@ -57,13 +57,19 @@ int Engine::init(void) "it." << std::endl; } + // Initially update the world to send all systems world data + systems.update(0); return 0; } void Engine::logicLoop(void) { entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */ - double elapsed = 0; + double elapsed = 1000; /**< Time elapsed since last logic loop. This + should be initialized to something larger + than our logic loop period (50ms), so + the logic loop is run during our first + loop. */ while (shouldRun()) { auto start = mc::now(); diff --git a/src/events/world.hpp b/src/events/world.hpp index 41b9569..e5969c0 100644 --- a/src/events/world.hpp +++ b/src/events/world.hpp @@ -31,9 +31,13 @@ struct WorldChangeEvent struct WorldMeshUpdateEvent { GLuint worldVBO; + unsigned int numVertex; + GLuint worldTexture; + GLuint worldNormal; - WorldMeshUpdateEvent(GLuint v) : - worldVBO(v) {} + WorldMeshUpdateEvent(GLuint v, unsigned int p, + GLuint t, GLuint n) : + worldVBO(v), numVertex(p), worldTexture(t), worldNormal(n) {} }; #endif//EVENTS_WORLD_HPP diff --git a/src/render.cpp b/src/render.cpp index 0c60b00..b991b2f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -27,6 +27,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { + events.subscribe(*this); init(); } @@ -179,27 +180,28 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glDrawArrays(GL_TRIANGLES, 0, 6); }); - std::basic_string& wm = worldSystem.current()->getMesh(); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getTexture()); - glUniform1i(q, 0); + // If we were given a world VBO render it + if (worldVBO) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, worldTexture); + glUniform1i(q, 0); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getNormal()); - glUniform1i(n, 1); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, worldNormal); + glUniform1i(n, 1); - glBindBuffer(GL_ARRAY_BUFFER, world_vbo); - glBufferData(GL_ARRAY_BUFFER, - wm.size() * sizeof(WorldMeshData), - &wm.front(), - GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, worldVBO); + //glBufferData(GL_ARRAY_BUFFER, + // wm.size() * sizeof(WorldMeshData), + // &wm.front(), + // GL_STREAM_DRAW); - glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, - 6*sizeof(float), 0); - glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, - 6*sizeof(float), (void*)(3*sizeof(float))); - glDrawArrays(GL_TRIANGLES, 0, wm.size()); + glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, + 6*sizeof(float), 0); + glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(3*sizeof(float))); + glDrawArrays(GL_TRIANGLES, 0, worldVertex); + } /************* * CLEANUP * @@ -283,8 +285,17 @@ int RenderSystem::init(void) //glClearColor(0.6, 0.8, 1.0, 0.0); camPos = glm::vec3(0.0f, 0.0f, 5.0f); - glGenBuffers(1, &world_vbo); return 0; } +/************ +* EVENTS * +************/ +void RenderSystem::receive(const WorldMeshUpdateEvent &wmu) +{ + worldVBO = wmu.worldVBO; + worldVertex = wmu.numVertex; + worldTexture = wmu.worldTexture; + worldNormal = wmu.worldNormal; +} diff --git a/src/render.hpp b/src/render.hpp index 82ca844..26e525b 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -36,8 +36,10 @@ #include "shader.hpp" #include "world.hpp" +#include "events/world.hpp" -class RenderSystem : public entityx::System +class RenderSystem : public entityx::System, + public entityx::Receiver { private: constexpr static const char *title = "gamedev2"; @@ -49,12 +51,14 @@ private: Shader worldShader; glm::vec3 camPos; - GLuint world_vbo; - WorldSystem &worldSystem; + GLuint worldVBO = 0; + unsigned int worldVertex = 0; + GLuint worldTexture = 0; + GLuint worldNormal = 0; public: - RenderSystem(WorldSystem & _ws) : - window(nullptr, SDL_DestroyWindow), worldSystem(_ws) {} + RenderSystem() : + window(nullptr, SDL_DestroyWindow) {} ~RenderSystem(void) { @@ -80,6 +84,12 @@ public: * @return Zero on success, non-zero on error */ int init(void); + + /************ + * EVENTS * + ************/ + void receive(const WorldMeshUpdateEvent &wmu); + }; #endif // SYSTEM_RENDER_HPP_ diff --git a/src/world.cpp b/src/world.cpp index 9e45d60..7710ecc 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -55,6 +55,8 @@ World::World(sol::object param) if (generate != sol::nil) generate(this); + // Create our world VBO + glGenBuffers(1, &worldVBO); // Generate our mesh generateMesh(); } @@ -74,7 +76,6 @@ void World::registerMaterial(std::string name, sol::object data) std::cerr << "Material: " << name << " was already registered" << std::endl; } - } else { // TODO better logging std::cerr << "Material registration must have a table" << std::endl; @@ -157,6 +158,14 @@ void World::generateMesh() } } } + + glBindBuffer(GL_ARRAY_BUFFER, worldVBO); + glBufferData(GL_ARRAY_BUFFER, + mesh.size() * sizeof(mesh), + &mesh.front(), + GL_STREAM_DRAW); + + meshUpdated = true; } /* SEED */ @@ -199,4 +208,13 @@ void WorldSystem::update([[maybe_unused]]entityx::EntityManager& entities, events.emit(currentWorld); std::cout << "Emitted" << std::endl; } + + if (currentWorld->meshUpdated) { + events.emit( + currentWorld->worldVBO, + currentWorld->mesh.size(), + currentWorld->getTexture(), + currentWorld->getNormal() + ); + } } diff --git a/src/world.hpp b/src/world.hpp index 5933306..696f0aa 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -59,6 +59,7 @@ struct WorldMaterial class World { + friend class WorldSystem; private: unsigned int seed; unsigned int layers; @@ -71,7 +72,11 @@ private: std::unordered_map string_registry; std::vector registry; +protected: + // RENDER std::basic_string mesh; + GLuint worldVBO; + bool meshUpdated = false; public: /* VARS */ sol::function generate; -- cgit v1.2.3