aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-23 22:14:13 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-23 22:14:13 -0400
commitf6a0e340bc82cb5fb96f836686bd59aaffd5db97 (patch)
tree8d7e96bbd997be9d0a1e9bf088689366aeaae96e
parent0869328b12ff9b77000915f37443dcf2468d881e (diff)
Fixed text rendering
-rw-r--r--Scripts/init.lua2
-rw-r--r--Shaders/ui.frag8
-rw-r--r--Shaders/ui.vert6
-rw-r--r--src/render.cpp67
-rw-r--r--src/text.cpp92
-rw-r--r--src/text.hpp5
-rw-r--r--src/world.cpp10
7 files changed, 113 insertions, 77 deletions
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<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
+ 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 <http://www.gnu.org/licenses/>.
*/
+// 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<FT_Info, 96> data;
// Stores currently shown text at given index into VBO?
std::vector<Text> 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;