aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-18 14:40:23 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-18 14:40:23 -0400
commit0869328b12ff9b77000915f37443dcf2468d881e (patch)
tree1e9e4d5ca43661a4aca8f5052f1ba67d23b9d955
parent0e7f7791fd592f0240a30168a9a570c57b0f5880 (diff)
parent8b834d0440f85a452694fb5cbb2cd9f4dae07aa2 (diff)
Added UI rendering support to render loop, although fonts don't render yet
-rw-r--r--Scripts/init.lua8
-rw-r--r--Shaders/ui.frag16
-rw-r--r--Shaders/ui.vert19
-rw-r--r--src/render.cpp95
-rw-r--r--src/render.hpp2
-rw-r--r--src/text.cpp51
-rw-r--r--src/world.cpp7
7 files changed, 166 insertions, 32 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua
index d2abb00..9e6848e 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -4,19 +4,19 @@ player = {
Player = 0,
EventListeners = {
MoveLeftPressed = function(self)
- self.Velocity.x = self.Velocity.x - 3
+ self.Velocity.x = self.Velocity.x - 3.0
self.Render.flipx = true;
end,
MoveLeftReleased = function(self)
game.puts("default", self.Position.x, self.Position.y, "Hey.")
- self.Velocity.x = self.Velocity.x + 3
+ self.Velocity.x = self.Velocity.x + 3.0
end,
MoveRightPressed = function(self)
- self.Velocity.x = self.Velocity.x + 3
+ self.Velocity.x = self.Velocity.x + 3.0
self.Render.flipx = false;
end,
MoveRightReleased = function(self)
- self.Velocity.x = self.Velocity.x - 3
+ self.Velocity.x = self.Velocity.x - 3.0
end,
JumpKeyPressed = function(self)
if self.Physics.standing == true then
diff --git a/Shaders/ui.frag b/Shaders/ui.frag
new file mode 100644
index 0000000..118108c
--- /dev/null
+++ b/Shaders/ui.frag
@@ -0,0 +1,16 @@
+#version 130
+
+uniform sampler2D sampler;
+
+in vec2 texCoord;
+in vec4 color;
+
+out vec4 FragColor;
+
+void main(){
+ vec4 pixelColor = texture2D(sampler, vec2(texCoord.x, texCoord.y));
+ //TODO allow antialiasing
+ //if (pixelColor.w != 1.0f)
+ // discard;
+ FragColor = pixelColor * color;
+}
diff --git a/Shaders/ui.vert b/Shaders/ui.vert
new file mode 100644
index 0000000..ee4f92c
--- /dev/null
+++ b/Shaders/ui.vert
@@ -0,0 +1,19 @@
+#version 130
+
+in vec3 coord2d;
+in vec2 tex_coord;
+
+uniform mat4 projection;
+uniform mat4 view;
+uniform mat4 model;
+
+out vec2 texCoord;
+out vec4 color;
+
+void main(){
+ texCoord = tex_coord;
+ color = vec4(1.0, 1.0, 1.0, 1.0);
+ //color = tex_color;
+ //gl_Position = ortho * vec4(coord2d.xyz, 1.0);
+ gl_Position = projection * view * model * vec4(coord2d, 1.0f);
+}
diff --git a/src/render.cpp b/src/render.cpp
index 3eea57b..1b5ba62 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -39,17 +39,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 *
@@ -140,7 +140,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
*************/
entities.each<Render, Position>(
- [this, a, q, t, n, f](entityx::Entity, Render &r, Position &p) {
+ [this](entityx::Entity, Render &r, Position &p) {
if (!r.visible)
return;
@@ -223,32 +223,74 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
glDrawArrays(GL_TRIANGLES, 0, worldVertex);
}
+ glDisableVertexAttribArray(a);
+ glDisableVertexAttribArray(t);
+
+ /******************
+ * UI RENDERING *
+ ******************/
+
+ 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");
+ static GLuint uiS_a = uiShader.getAttribute("coord2d");
+ static GLuint uiS_t = uiShader.getAttribute("tex_coord");
+ static GLuint uiS_q = uiShader.getUniform("sampler");
+
+ 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, 1.0f, 0.0f)); // Up
+
+ scale = 1.0f;
+ scaleWidth = static_cast<float>(width) / scale;
+ scaleHeight = static_cast<float>(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);
+
+ glUniformMatrix4fv(uiS_v, 1, GL_FALSE, glm::value_ptr(view));
+ glUniformMatrix4fv(uiS_p, 1, GL_FALSE, glm::value_ptr(projection));
+ glUniformMatrix4fv(uiS_m, 1, GL_FALSE, glm::value_ptr(model));
+
+ glEnableVertexAttribArray(uiS_a);
+ glEnableVertexAttribArray(uiS_t);
+
// Update all UI VBOs
for (auto& r : uiRenders) {
auto& render = r.second;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, render.tex);
- glUniform1i(q, 0);
+ glUniform1i(uiS_q, 0);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, render.normal);
- glUniform1i(n, 1);
+ //glActiveTexture(GL_TEXTURE1);
+ //glBindTexture(GL_TEXTURE_2D, render.normal);
+ //glUniform1i(n, 1);
glBindBuffer(GL_ARRAY_BUFFER, r.first);
- glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
+ glVertexAttribPointer(uiS_a, 3, GL_FLOAT, GL_FALSE,
6*sizeof(float), 0);
- glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE,
+ glVertexAttribPointer(uiS_t, 2, GL_FLOAT, GL_FALSE,
6*sizeof(float), (void*)(3*sizeof(float)));
glDrawArrays(GL_TRIANGLES, 0, render.vertex);
}
+ glDisableVertexAttribArray(uiS_a);
+ glDisableVertexAttribArray(uiS_t);
+
/*************
* CLEANUP *
*************/
- glDisableVertexAttribArray(a);
- glDisableVertexAttribArray(t);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_CULL_FACE);
@@ -317,8 +359,19 @@ int RenderSystem::init(void)
worldShader.addUniform("AmbientLight");
worldShader.addUniform("Flipped");
+ uiShader.createProgram("Shaders/ui.vert", "Shaders/ui.frag");
+
+ uiShader.addUniform("projection");
+ uiShader.addUniform("view");
+ uiShader.addUniform("model");
+
+ uiShader.addAttribute("coord2d");
+ uiShader.addAttribute("tex_coord");
+
+ uiShader.addUniform("sampler");
+
glEnableVertexAttribArray(worldShader.getAttribute("vertex"));
- glUseProgram(worldShader.getProgram());
+ glEnableVertexAttribArray(uiShader.getAttribute("coord2d"));
// TODO
//glPolygonOffset(1.0, 1.0);
diff --git a/src/render.hpp b/src/render.hpp
index eabf4be..88668cc 100644
--- a/src/render.hpp
+++ b/src/render.hpp
@@ -64,6 +64,7 @@ private:
SDL_GLContext context;
Shader worldShader;
+ Shader uiShader;
glm::vec3 camPos;
// Map of VBOs and their render data
@@ -74,7 +75,6 @@ private:
GLuint worldTexture = 0;
GLuint worldNormal = 0;
entityx::Entity player; // Save the player so we can track the camera
-
public:
RenderSystem() :
window(nullptr, SDL_DestroyWindow) {}
diff --git a/src/text.cpp b/src/text.cpp
index 1381eb2..fdb3245 100644
--- a/src/text.cpp
+++ b/src/text.cpp
@@ -131,15 +131,62 @@ void TextSystem::updateVBOs(void)
auto& d = data.second;
d.buffer.clear();
for (auto& text : d.text) {
+ float cOff = 0.0f;
for (char c : text.text) {
if (c < 32)
continue;
d.buffer += {
- text.x, text.y, text.z,
- d.data[c].offset.first, d.data[c].offset.second,
+ text.x+cOff,
+ text.y,
+ text.z,
+ d.data[c].offset.first,
+ d.data[c].offset.second+d.data[c].dim.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,
+ 1.0f
+ };
+ d.buffer += {
+ text.x+cOff,
+ text.y+d.data[c].dim.second,
+ text.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,
+ 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,
+ 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,
+ d.data[c].offset.second,
+ 1.0f
+ };
+
+ cOff += d.data[c].dim.first + d.data[c].advance.first;
}
}
diff --git a/src/world.cpp b/src/world.cpp
index 93cf511..fb7870d 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -230,15 +230,14 @@ void WorldSystem::update([[maybe_unused]]entityx::EntityManager& entities,
if (currentWorld == nullptr) {
currentWorld = &(worlds.front());
events.emit<WorldChangeEvent>(currentWorld);
- std::cout << "Emitted" << std::endl;
}
if (currentWorld->meshUpdated) {
- events.emit<NewRenderEvent>(
+ events.emit<WorldMeshUpdateEvent>(
currentWorld->worldVBO,
+ currentWorld->mesh.size(),
currentWorld->getTexture(),
- currentWorld->getNormal(),
- currentWorld->mesh.size()
+ currentWorld->getNormal()
);
}
}