From 927348d9d8eb16b469c23ef32486bea5f94c5469 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 17 Sep 2019 15:06:39 -0400 Subject: made VBO rendering accessible --- src/render.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'src/render.cpp') diff --git a/src/render.cpp b/src/render.cpp index b991b2f..2a51b70 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -27,7 +27,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { - events.subscribe(*this); + events.subscribe(*this); init(); } @@ -180,27 +180,25 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glDrawArrays(GL_TRIANGLES, 0, 6); }); - // If we were given a world VBO render it - if (worldVBO) { + // Update all given VBOs + for (auto& r : renders) { + auto& render = r.second; + glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, worldTexture); + glBindTexture(GL_TEXTURE_2D, render.tex); glUniform1i(q, 0); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, worldNormal); + glBindTexture(GL_TEXTURE_2D, render.normal); glUniform1i(n, 1); - glBindBuffer(GL_ARRAY_BUFFER, worldVBO); - //glBufferData(GL_ARRAY_BUFFER, - // wm.size() * sizeof(WorldMeshData), - // &wm.front(), - // GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, r.first); 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); + glDrawArrays(GL_TRIANGLES, 0, render.vertex); } /************* @@ -292,10 +290,10 @@ int RenderSystem::init(void) /************ * EVENTS * ************/ -void RenderSystem::receive(const WorldMeshUpdateEvent &wmu) + +void RenderSystem::receive(const NewRenderEvent &nre) { - worldVBO = wmu.worldVBO; - worldVertex = wmu.numVertex; - worldTexture = wmu.worldTexture; - worldNormal = wmu.worldNormal; + renders.insert_or_assign(nre.vbo, + RenderData(nre.tex, nre.normal, nre.vertex)); } + -- cgit v1.2.3 From 8b834d0440f85a452694fb5cbb2cd9f4dae07aa2 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Wed, 18 Sep 2019 13:49:46 -0400 Subject: Started adding UI rendering to render loop --- src/render.cpp | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/render.cpp') diff --git a/src/render.cpp b/src/render.cpp index 2b49b2c..0c92475 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -37,17 +37,17 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::TimeDelta dt) { // TODO move these to only happen once to speed up rendering - GLuint s = worldShader.getProgram(); - GLuint v = worldShader.getUniform("view"); - GLuint p = worldShader.getUniform("projection"); - GLuint m = worldShader.getUniform("model"); - GLuint a = worldShader.getAttribute("vertex"); - GLuint t = worldShader.getAttribute("texc"); - - GLuint q = worldShader.getUniform("textu"); - GLuint n = worldShader.getUniform("normu"); - GLuint b = worldShader.getUniform("AmbientLight"); - GLuint f = worldShader.getUniform("Flipped"); + static GLuint s = worldShader.getProgram(); + static GLuint v = worldShader.getUniform("view"); + static GLuint p = worldShader.getUniform("projection"); + static GLuint m = worldShader.getUniform("model"); + static GLuint a = worldShader.getAttribute("vertex"); + static GLuint t = worldShader.getAttribute("texc"); + + static GLuint q = worldShader.getUniform("textu"); + static GLuint n = worldShader.getUniform("normu"); + static GLuint b = worldShader.getUniform("AmbientLight"); + static GLuint f = worldShader.getUniform("Flipped"); /*********** * SETUP * @@ -138,7 +138,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, *************/ entities.each( - [this, a, q, t, n, f](entityx::Entity, Render &r, Position &p) { + [this](entityx::Entity, Render &r, Position &p) { if (!r.visible) return; @@ -221,6 +221,27 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glDrawArrays(GL_TRIANGLES, 0, worldVertex); } + /****************** + * UI RENDERING * + ******************/ + + view = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), // Pos + glm::vec3(0.0f, 0.0f, 0.0f), // Facing + glm::vec3(0.0f, 1.0f, 0.0f)); // Up + + scale = 1.0f; + scaleWidth = static_cast(width) / scale; + scaleHeight = static_cast(height) / scale; + + projection = glm::ortho(-(scaleWidth/2), // Left + (scaleWidth/2), // Right + -(scaleHeight/2), // Bottom + (scaleHeight/2), // Top + 10.0f, // zFar + -10.0f); // zNear + + model = glm::mat4(1.0f); + /************* * CLEANUP * *************/ -- cgit v1.2.3 From f6a0e340bc82cb5fb96f836686bd59aaffd5db97 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Mon, 23 Sep 2019 22:14:13 -0400 Subject: Fixed text rendering --- Scripts/init.lua | 2 +- Shaders/ui.frag | 8 ++++- Shaders/ui.vert | 6 ++-- src/render.cpp | 67 ++++++++++++++++++++--------------------- src/text.cpp | 92 +++++++++++++++++++++++++++++++++++--------------------- src/text.hpp | 5 +++ src/world.cpp | 10 +++--- 7 files changed, 113 insertions(+), 77 deletions(-) (limited to 'src/render.cpp') diff --git a/Scripts/init.lua b/Scripts/init.lua index 9e6848e..8559f7c 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -8,7 +8,7 @@ player = { self.Render.flipx = true; end, MoveLeftReleased = function(self) - game.puts("default", self.Position.x, self.Position.y, "Hey.") + game.puts("default", self.Position.x, self.Position.y+100, "Hey. Hag?") self.Velocity.x = self.Velocity.x + 3.0 end, MoveRightPressed = function(self) diff --git a/Shaders/ui.frag b/Shaders/ui.frag index 118108c..63e7eb0 100644 --- a/Shaders/ui.frag +++ b/Shaders/ui.frag @@ -1,5 +1,11 @@ #version 130 +#ifdef GL_FRAGMENT_PRECISION_HIGH +precision highp float; +#else +precision mediump float; +#endif + uniform sampler2D sampler; in vec2 texCoord; @@ -12,5 +18,5 @@ void main(){ //TODO allow antialiasing //if (pixelColor.w != 1.0f) // discard; - FragColor = pixelColor * color; + FragColor = pixelColor.r * color; } diff --git a/Shaders/ui.vert b/Shaders/ui.vert index ee4f92c..d2e3902 100644 --- a/Shaders/ui.vert +++ b/Shaders/ui.vert @@ -12,8 +12,8 @@ out vec4 color; void main(){ texCoord = tex_coord; - color = vec4(1.0, 1.0, 1.0, 1.0); + color = vec4(0.0f, 0.0f, 0.0f, 1.0f); //color = tex_color; - //gl_Position = ortho * vec4(coord2d.xyz, 1.0); - gl_Position = projection * view * model * vec4(coord2d, 1.0f); + //gl_Position = ortho * vec4(coord2d.xyz, 1.0f); + gl_Position = projection * view * model * vec4(coord2d.xyz, 1.0f); } diff --git a/src/render.cpp b/src/render.cpp index 1b5ba62..c06b7b3 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -51,12 +51,20 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, static GLuint b = worldShader.getUniform("AmbientLight"); static GLuint f = worldShader.getUniform("Flipped"); + static glm::vec3 rot = glm::vec3(0.0f, 0.0f, -1.0f); + /*********** * SETUP * ***********/ + + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_POLYGON_OFFSET_FILL); + glm::mat4 view = glm::lookAt(camPos, // Pos - glm::vec3(camPos.x, camPos.y, 0.0f), // Facing + camPos + rot, // Facing glm::vec3(0.0f, 1.0f, 0.0f)); // Up //glm::mat4 projection = glm::perspective(45.0f, @@ -72,24 +80,19 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, (scaleWidth/2), // Right -(scaleHeight/2), // Bottom (scaleHeight/2), // Top - 10.0f, // zFar - -10.0f // zNear + 100.0f, // zFar + -100.0f // zNear ); glm::mat4 model = glm::mat4(1.0f); glUseProgram(s); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); glUniformMatrix4fv(v, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(p, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(m, 1, GL_FALSE, glm::value_ptr(model)); - glEnable(GL_CULL_FACE); - glEnable(GL_POLYGON_OFFSET_FILL); - glEnableVertexAttribArray(a); glEnableVertexAttribArray(t); @@ -211,11 +214,6 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glUniform1i(n, 1); 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, @@ -226,11 +224,13 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glDisableVertexAttribArray(a); glDisableVertexAttribArray(t); + glUseProgram(0); + /****************** * UI RENDERING * ******************/ - static GLuint uiS = uiShader.getProgram(); + static GLuint uiS = uiShader.getProgram(); static GLuint uiS_v = uiShader.getUniform("view"); static GLuint uiS_p = uiShader.getUniform("projection"); static GLuint uiS_m = uiShader.getUniform("model"); @@ -241,19 +241,19 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glUseProgram(uiS); view = glm::lookAt(glm::vec3(0.0f, 0.0f, 10.0f), // Pos - glm::vec3(0.0f, 0.0f, 0.0f), // Facing + glm::vec3(0.0f, 0.0f, 1.0f), // Facing glm::vec3(0.0f, 1.0f, 0.0f)); // Up scale = 1.0f; scaleWidth = static_cast(width) / scale; scaleHeight = static_cast(height) / scale; - projection = glm::ortho(-(scaleWidth/2), // Left - (scaleWidth/2), // Right - -(scaleHeight/2), // Bottom - (scaleHeight/2), // Top - 10.0f, // zFar - -10.0f); // zNear + projection = glm::ortho(-scaleWidth/2.0f, // Left + scaleWidth/2, // Right + -scaleHeight/2, // Bottom + scaleHeight/2, // Top + 100.0f, // zFar + -100.0f); // zNear model = glm::mat4(1.0f); @@ -268,16 +268,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, for (auto& r : uiRenders) { auto& render = r.second; - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE9); glBindTexture(GL_TEXTURE_2D, render.tex); - glUniform1i(uiS_q, 0); - - //glActiveTexture(GL_TEXTURE1); - //glBindTexture(GL_TEXTURE_2D, render.normal); - //glUniform1i(n, 1); + glUniform1i(uiS_q, 9); glBindBuffer(GL_ARRAY_BUFFER, r.first); - glVertexAttribPointer(uiS_a, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), 0); glVertexAttribPointer(uiS_t, 2, GL_FLOAT, GL_FALSE, @@ -292,19 +287,16 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, * CLEANUP * *************/ + glUseProgram(0); + glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_CULL_FACE); - glUseProgram(0); - SDL_GL_SwapWindow(window.get()); } int RenderSystem::init(void) { - // Select an OpenGL 4.3 profile. - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) { std::cerr << "SDL video failed to initialize: " @@ -324,6 +316,13 @@ int RenderSystem::init(void) return -1; } + // Select an OpenGL 4.3 profile. + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, + SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + context = SDL_GL_CreateContext(window.get()); GLenum err; @@ -376,7 +375,7 @@ int RenderSystem::init(void) // TODO //glPolygonOffset(1.0, 1.0); - //glClearColor(0.6, 0.8, 1.0, 0.0); + glClearColor(0.6, 0.8, 1.0, 0.0); camPos = glm::vec3(0.0f, 0.0f, 5.0f); diff --git a/src/text.cpp b/src/text.cpp index fdb3245..e0eb158 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -69,14 +69,18 @@ void TextSystem::loadFont(const std::string& name, // auto& font = fontData[name]; - glGenTextures(1, &font.tex); glGenBuffers(1, &font.vbo); + + glGenTextures(1, &font.tex); glBindTexture(GL_TEXTURE_2D, font.tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, 0); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); @@ -89,9 +93,15 @@ void TextSystem::loadFont(const std::string& name, // Load each character and add it to the texture // + font.width = width; + font.height = height; + float offsetX = 0, offsetY = 0; for (int c = 32; c < 128; c++) { - FT_Load_Char(face, c, FT_LOAD_RENDER); + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { + std::cerr << "Unrecognized character: " << c << std::endl; + continue; + } auto* g = face->glyph; glTexSubImage2D(GL_TEXTURE_2D, 0, offsetX, offsetY, @@ -99,13 +109,13 @@ void TextSystem::loadFont(const std::string& name, GL_RED, GL_UNSIGNED_BYTE, g->bitmap.buffer); - auto& d = font.data[c - 32]; + auto& d = font.data[c-32]; d.dim = { g->bitmap.width, g->bitmap.rows }; d.bitmap = { g->bitmap_left, g->bitmap_top }; d.advance = { g->advance.x >> 6, g->advance.y >> 6 }; d.offset = { offsetX / width, offsetY / height }; - offsetX += g->bitmap.width; + offsetX += g->bitmap.width;// + 1.0f; // Keep offsetY at zero? } @@ -131,69 +141,83 @@ void TextSystem::updateVBOs(void) auto& d = data.second; d.buffer.clear(); for (auto& text : d.text) { - float cOff = 0.0f; + float tx = text.x; + float ty = text.y; for (char c : text.text) { if (c < 32) continue; + c -= 32; + + float x = tx + d.data[c].bitmap.first; + float y = ty - (d.data[c].dim.second - d.data[c].bitmap.second); + float z = text.z; + + float w = d.data[c].dim.first; + float h = d.data[c].dim.second; + + tx += d.data[c].advance.first; + ty += d.data[c].advance.second; + + if (w == 0.0f || h == 0.0f) + continue; + d.buffer += { - text.x+cOff, - text.y, - text.z, + x, + y, + z, d.data[c].offset.first, - d.data[c].offset.second+d.data[c].dim.second, + d.data[c].offset.second+d.data[c].dim.second/d.height, 1.0f }; d.buffer += { - text.x+cOff+d.data[c].dim.first, - text.y, - text.z, - d.data[c].offset.first+d.data[c].dim.first, - d.data[c].offset.second+d.data[c].dim.second, + x+w, + y, + z, + d.data[c].offset.first+d.data[c].dim.first/d.width, + d.data[c].offset.second+d.data[c].dim.second/d.height, 1.0f }; d.buffer += { - text.x+cOff, - text.y+d.data[c].dim.second, - text.z, + x, + y+h, + z, d.data[c].offset.first, d.data[c].offset.second, 1.0f }; d.buffer += { - text.x+cOff+d.data[c].dim.first, - text.y, - text.z, - d.data[c].offset.first+d.data[c].dim.first, - d.data[c].offset.second+d.data[c].dim.second, + x+w, + y, + z, + d.data[c].offset.first+d.data[c].dim.first/d.width, + d.data[c].offset.second+d.data[c].dim.second/d.height, 1.0f }; d.buffer += { - text.x+cOff+d.data[c].dim.first, - text.y+d.data[c].dim.second, - text.z, - d.data[c].offset.first+d.data[c].dim.first, + x+w, + y+h, + z, + d.data[c].offset.first+d.data[c].dim.first/d.width, d.data[c].offset.second, 1.0f }; d.buffer += { - text.x+cOff, - text.y+d.data[c].dim.second, - text.z, - d.data[c].offset.first+d.data[c].dim.first, + x, + y+h, + z, + d.data[c].offset.first, d.data[c].offset.second, 1.0f }; - - cOff += d.data[c].dim.first + d.data[c].advance.first; } } glBindBuffer(GL_ARRAY_BUFFER, d.vbo); glBufferData(GL_ARRAY_BUFFER, - d.text.size() * sizeof(TextMeshData), d.text.data(), - GL_STREAM_DRAW); + d.buffer.size() * sizeof(TextMeshData), d.buffer.data(), + GL_DYNAMIC_DRAW); } } diff --git a/src/text.hpp b/src/text.hpp index c3479ca..d68613d 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -18,6 +18,8 @@ * along with this program. If not, see . */ +// hello + #ifndef SYSTEM_TEXT_HPP_ #define SYSTEM_TEXT_HPP_ @@ -61,6 +63,9 @@ struct Font { GLuint tex; GLuint vbo; + float width; + float height; + std::array data; // Stores currently shown text at given index into VBO? std::vector text; diff --git a/src/world.cpp b/src/world.cpp index fb7870d..feff728 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -162,9 +162,9 @@ void World::generateMesh() glBindBuffer(GL_ARRAY_BUFFER, worldVBO); glBufferData(GL_ARRAY_BUFFER, - mesh.size() * sizeof(mesh), - &mesh.front(), - GL_STREAM_DRAW); + mesh.size() * sizeof(WorldMeshData), + mesh.data(), + GL_STATIC_DRAW); meshUpdated = true; } @@ -200,7 +200,9 @@ double World::getHeight(double x, double y, double z) } } } catch (...) { // If we get any errors, just let the character - return y; + //return y; + (void)y; + return 0.0; } return Y; -- cgit v1.2.3 From be9d14233e9fc7b7c5e4fc3711125132e3cee151 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Tue, 24 Sep 2019 02:29:41 -0400 Subject: Increased texture size for world, made world a little more detailed, and added bouncing ball --- Assets/ball.png | Bin 0 -> 5146 bytes Assets/ball_normal.png | Bin 0 -> 5885 bytes Assets/world.png | Bin 236 -> 11118 bytes Assets/world_normal.png | Bin 1877 -> 10591 bytes Scripts/init.lua | 29 ++++++++++++++++---- Scripts/world.lua | 42 ++++++++++++++++++---------- Shaders/world.frag | 2 ++ Shaders/world.vert | 3 ++ src/render.cpp | 71 ++++++++++++++++++++++++++++-------------------- src/world.cpp | 27 +++++++++++++----- 10 files changed, 118 insertions(+), 56 deletions(-) create mode 100644 Assets/ball.png create mode 100644 Assets/ball_normal.png (limited to 'src/render.cpp') diff --git a/Assets/ball.png b/Assets/ball.png new file mode 100644 index 0000000..b8dca47 Binary files /dev/null and b/Assets/ball.png differ diff --git a/Assets/ball_normal.png b/Assets/ball_normal.png new file mode 100644 index 0000000..d238e30 Binary files /dev/null and b/Assets/ball_normal.png differ diff --git a/Assets/world.png b/Assets/world.png index c972c87..f6c78d9 100644 Binary files a/Assets/world.png and b/Assets/world.png differ diff --git a/Assets/world_normal.png b/Assets/world_normal.png index 1818895..c691e1e 100644 Binary files a/Assets/world_normal.png and b/Assets/world_normal.png differ diff --git a/Scripts/init.lua b/Scripts/init.lua index 8559f7c..cdd6ccb 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -55,20 +55,37 @@ player = { --end --self.visibleTick = self.visibleTick + 1 end, - PhysicsIdle = function(self) - if self.Velocity.x < 0 then - self.Render.flipx = true - elseif self.Velocity.x > 0 then - self.Render.flipx = false + visibleTick = 0 +} + +ball = { + Position = { + x = 20, + y = 100 + }, + Velocity = { + x = 0.0, + y = 0.0, + }, + Physics = 0, + Render = { + texture = "Assets/ball.png", + normal = "Assets/ball_normal.png", + visible = true, + }, + Idle = function(self) + if self.Physics.standing == true then + self.Velocity.y = self.Velocity.y + 15 + self.Velocity.x = math.random(-1, 1); end end, - visibleTick = 0 } -- Create the world dofile("Scripts/world.lua") playerSpawn = game.spawn(player); +game.spawn(ball); ------------------- -- SERIALIZING -- diff --git a/Scripts/world.lua b/Scripts/world.lua index 044559a..3b56d9a 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -13,51 +13,63 @@ world = { texture = { file = "Assets/world.png", offset = { x = 0, y = 0 }, - size = { x = 8, y = 8 } + size = { x = 64, y = 64 } }, normal = { file = "Assets/world_normal.png", offset = { x = 0, y = 0 }, - size = { x = 8, y = 8 } + size = { x = 64, y = 64 } } }); self:registerMaterial("dirt", { texture = { file = "Assets/world.png", - offset = { x = 8, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 64, y = 0 }, + size = { x = 64, y = 64 } }, normal = { file = "Assets/world_normal.png", - offset = { x = 8, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 64, y = 0 }, + size = { x = 64, y = 64 } } }); self:registerMaterial("stone", { texture = { file = "Assets/world.png", - offset = { x = 16, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 128, y = 0 }, + size = { x = 64, y = 64 } }, normal = { file = "Assets/world_normal.png", - offset = { x = 16, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 128, y = 0 }, + size = { x = 64, y = 64 } } }); self:registerMaterial("flower", { texture = { file = "Assets/world.png", - offset = { x = 24, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 192, y = 0 }, + size = { x = 64, y = 64 } }, normal = { file = "Assets/world_normal.png", - offset = { x = 24, y = 0 }, - size = { x = 8, y = 8 } + offset = { x = 192, y = 0 }, + size = { x = 64, y = 64 } }, passable = true }); + self:registerMaterial("trunk", { + texture = { + file = "Assets/world.png", + offset = { x = 256, y = 0 }, + size = { x = 64, y = 64 } + }, + normal = { + file = "Assets/world_normal.png", + offset = { x = 256, y = 0 }, + size = { x = 64, y = 64 } + } + }); end, Generate = function(self) @@ -83,6 +95,8 @@ world = { elseif Y == YGen + 1 then if math.random(0, 100) == 53 then self:setData(X, Y, Z, "flower"); + elseif math.random(0, 100) == 45 then + self:setData(X, Y, Z, "trunk"); end end --print(X..","..Y..","..Z); diff --git a/Shaders/world.frag b/Shaders/world.frag index 18945a8..79d87aa 100644 --- a/Shaders/world.frag +++ b/Shaders/world.frag @@ -9,6 +9,7 @@ precision mediump float; uniform sampler2D textu; uniform sampler2D normu; +in float fragTrans; in vec2 texCoord; in vec4 fragCoord; out vec4 FragColor; @@ -25,6 +26,7 @@ void main() vec3 Falloff = vec3(0.4, 0.1, 0.002); vec4 DiffuseColor = texture2D(textu, texCoord); + DiffuseColor *= fragTrans; if (DiffuseColor.a < 0.1f) discard; diff --git a/Shaders/world.vert b/Shaders/world.vert index aa183a2..28fd307 100644 --- a/Shaders/world.vert +++ b/Shaders/world.vert @@ -3,16 +3,19 @@ //layout(location = 0)in vec3 vertex; in vec3 vertex; in vec2 texc; +in float trans; uniform mat4 projection; uniform mat4 view; uniform mat4 model; +out float fragTrans; out vec2 texCoord; out vec4 fragCoord; void main() { + fragTrans = trans; texCoord = texc; fragCoord = vec4(vertex, 1.0f); gl_Position = projection * view * model * fragCoord; diff --git a/src/render.cpp b/src/render.cpp index c06b7b3..9e63a71 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -45,6 +45,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, static GLuint m = worldShader.getUniform("model"); static GLuint a = worldShader.getAttribute("vertex"); static GLuint t = worldShader.getAttribute("texc"); + static GLuint r = worldShader.getAttribute("trans"); static GLuint q = worldShader.getUniform("textu"); static GLuint n = worldShader.getUniform("normu"); @@ -52,6 +53,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, static GLuint f = worldShader.getUniform("Flipped"); static glm::vec3 rot = glm::vec3(0.0f, 0.0f, -1.0f); + camPos.z = 15.0f; /*********** * SETUP * @@ -76,15 +78,21 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, float scaleWidth = static_cast(width) / scale; float scaleHeight = static_cast(height) / scale; - glm::mat4 projection = glm::ortho(-(scaleWidth/2), // Left - (scaleWidth/2), // Right - -(scaleHeight/2), // Bottom - (scaleHeight/2), // Top - 100.0f, // zFar - -100.0f // zNear - ); + //glm::mat4 projection = glm::ortho(-(scaleWidth/2), // Left + // (scaleWidth/2), // Right + // -(scaleHeight/2), // Bottom + // (scaleHeight/2), // Top + // 100.0f, // zFar + // -100.0f // zNear + // ); + + glm::mat4 projection = glm::perspective(45.0f, + ((float)width/(float)height), + 0.01f, + 2048.0f); glm::mat4 model = glm::mat4(1.0f); + model = glm::scale(model, glm::vec3(1.0f, 1.0f, -1.0f)); glUseProgram(s); @@ -95,6 +103,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glEnableVertexAttribArray(a); glEnableVertexAttribArray(t); + glEnableVertexAttribArray(r); // Ambient light, for now this is static GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f}; @@ -143,9 +152,9 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, *************/ entities.each( - [this](entityx::Entity, Render &r, Position &p) { + [this](entityx::Entity, Render &rend, Position &p) { - if (!r.visible) + if (!rend.visible) return; // If our component was created via script, call the entity's @@ -155,28 +164,28 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, //} float w = 0.5f; - float h = (float)r.texture.height/r.texture.width; + float h = (float)rend.texture.height/rend.texture.width; GLuint tri_vbo; GLfloat tri_data[] = { - (float)p.x-w, (float)p.y , 00.0f, 0.0f, 1.0f, - (float)p.x+w, (float)p.y , 00.0f, 1.0f, 1.0f, - (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f, - - (float)p.x+w, (float)p.y , 00.0f, 1.0f, 1.0f, - (float)p.x+w, (float)p.y+h, 00.0f, 1.0f, 0.0f, - (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f, + (float)p.x-w, (float)p.y , 0.0f, 0.0f, 1.0f, 1.0f, + (float)p.x+w, (float)p.y , 0.0f, 1.0f, 1.0f, 1.0f, + (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f, + + (float)p.x+w, (float)p.y , 0.0f, 1.0f, 1.0f, 1.0f, + (float)p.x+w, (float)p.y+h, 0.0f, 1.0f, 0.0f, 1.0f, + (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f, }; bool flipped = false; // TODO flip nicely (aka model transformations) - if (r.flipX) { - std::swap(tri_data[3], tri_data[8]); - tri_data[13] = tri_data[3]; + if (rend.flipX) { + std::swap(tri_data[3], tri_data[9]); + tri_data[15] = tri_data[3]; - std::swap(tri_data[23], tri_data[28]); - tri_data[18] = tri_data[23]; + std::swap(tri_data[27], tri_data[33]); + tri_data[21] = tri_data[27]; flipped = true; } @@ -184,11 +193,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glUniform1i(f, flipped ? 1 : 0); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, r.texture.tex); + glBindTexture(GL_TEXTURE_2D, rend.texture.tex); glUniform1i(q, 0); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, r.normal.tex); + glBindTexture(GL_TEXTURE_2D, rend.normal.tex); glUniform1i(n, 1); glGenBuffers(1, &tri_vbo); @@ -196,9 +205,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW); glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, - 5*sizeof(float), 0); - glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, - 5*sizeof(float), (void*)(3*sizeof(float))); + 6*sizeof(float), 0); + glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(3*sizeof(float))); + glVertexAttribPointer(r, 1, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(5*sizeof(float))); glDrawArrays(GL_TRIANGLES, 0, 6); }); glUniform1i(f, 0); @@ -218,6 +229,8 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, 6*sizeof(float), 0); glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float))); + glVertexAttribPointer(r, 1, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(5*sizeof(float))); glDrawArrays(GL_TRIANGLES, 0, worldVertex); } @@ -348,6 +361,7 @@ int RenderSystem::init(void) worldShader.addAttribute("vertex"); worldShader.addAttribute("texc"); + worldShader.addAttribute("trans"); worldShader.addUniform("textu"); worldShader.addUniform("normu"); @@ -372,10 +386,9 @@ int RenderSystem::init(void) glEnableVertexAttribArray(worldShader.getAttribute("vertex")); glEnableVertexAttribArray(uiShader.getAttribute("coord2d")); - // TODO //glPolygonOffset(1.0, 1.0); - glClearColor(0.6, 0.8, 1.0, 0.0); + //glClearColor(0.6, 0.8, 1.0, 0.0); camPos = glm::vec3(0.0f, 0.0f, 5.0f); diff --git a/src/world.cpp b/src/world.cpp index feff728..cd89a22 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -137,7 +137,7 @@ void World::generateMesh() // Preallocate size of vertexes mesh = std::basic_string(); - for (float Z = 0; Z < data.size(); Z++) { + for (float Z = data.size() - 1; Z >= 0; Z--) { for (float X = 0; X < data.at(Z).size(); X++) { for (float Y = 0; Y < data.at(Z).at(X).size(); Y++) { int d = data.at(Z).at(X).at(Y); @@ -149,13 +149,26 @@ void World::generateMesh() glm::vec2& to = t.offset; glm::vec2& ts = t.size; - mesh += {X , Y , Z, to.x , to.y+ts.y, 1.0}; - mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, 1.0}; - mesh += {X , Y+1, Z, to.x , to.y , 1.0}; + float tr = 1.0f; + + // TODO play with this a bit so it only goes trans + // if player is behind the front layer + try { + if (Z < data.size() - 1 && Z >= 0) { + if (data.at(Z+1).at(X).at(Y) == -1) + tr = 1.0f; + } + } catch (...) { + tr = 1.0f; + } + + mesh += {X , Y , Z, to.x , to.y+ts.y, tr}; + mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, tr}; + mesh += {X , Y+1, Z, to.x , to.y , tr}; - mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, 1.0}; - mesh += {X+1, Y+1, Z, to.x+ts.x, to.y , 1.0}; - mesh += {X , Y+1, Z, to.x , to.y , 1.0}; + mesh += {X+1, Y , Z, to.x+ts.x, to.y+ts.y, tr}; + mesh += {X+1, Y+1, Z, to.x+ts.x, to.y , tr}; + mesh += {X , Y+1, Z, to.x , to.y , tr}; } } } -- cgit v1.2.3