From 534f6f57e930020eb1ad726f4de4e90cf487e584 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Sat, 14 Sep 2019 03:31:40 -0400 Subject: [PATCH] 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(-) diff --git a/Assets/world_normal.png b/Assets/world_normal.png index d45512a006582b7169bbddd10e2e94b4d28d16db..e5f013c2f40a4ef5c2470681892b53991c27e737 100644 GIT binary patch delta 962 zcmV;z13mnV0<;N`BYy*&dQ@0+Qek%>aB^>EX>4U6ba`-PAZ2)IW&i+q+O3vrk}D?+ zh5xgPECERfq2-8IRd$f&=g@6?9``YGQ)y}}ObZbDj_{=Q&)+-!g@?#mDr&8Itr<`8 z;vDF-{PwJey~ik*>+|{0vKA0nc73+i>8$KT$w?Y-{PMX5+by$^LyP0wZE*l{?yrd2e!);ee6*y+l`f)0SKU5cnzV&6sQ+YJ91rU~fe*BtVcWiBhF(%tlm%8vq3& zXDQr4fK+)RNG>83Y#MZgy)nn=@@3_F9Hjz2Dpd?@Tz>+vN>S*?N`r<%MT@FdHErq| zsY)$XYpqsK&4q~-Q>$jy%q^O<(zMlPtu=4q?1GY1uWsJlJpy}1Nuhz+0gs5zU3%%- zYq#FI_ZX1RC__gbHrntJlc&rwb=GOKO`ox7K}uJmZTX4~t!>$5>#o~&+rHyP z?Lqa2_J0S|=s}GyQpe9;)G%sx7P^+8bI<5u24Y+Y#O)A(p!qP%jY{D|?l8;Fs0w8S zsfW#^9EmCrmPM?aUhLk;eVbcCuD{~O-yxR{y8nS3rzM}b{e;>)H{rO5-BoyUng!KY zr&@GU3ma$4cJ;vSewM`!eWSL&wzG|NB8pN?a+l{KYcLN!qZ#8Pd*b-hi353I5 z;c(E}xeSVuLWkw_@)tJ!+QG)^SJ%dX4%bFtYI;ZyD*tms|0ICMy^ zkXtca>Dv45=_uGIe4K`r)eO>J5Vb>HTkwN){>$*zhwfQRd)1*0epc0lzygF-)X%9kN kA9^s)DF2Ph^YkyCBRBq7nZMPMAt46`7$cPUDPWOF!yMt}WB>pF delta 22 ecmdnO*~2tJdGcvyd3H7}{lY)dypumN9|izbJ_rN= 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(); -- 2.39.5