diff --git a/res/shaders/texture.frag b/res/shaders/texture.frag index a2995c5..a81c55e 100644 --- a/res/shaders/texture.frag +++ b/res/shaders/texture.frag @@ -2,9 +2,11 @@ in layout(location = 1) vec2 textureCoordinates; +layout(binding = 0) uniform sampler2D textureSampler; + out vec4 fragColor; void main() { - fragColor = vec4(1.0, 0.0, 0.0, 1.0); + fragColor = texture(textureSampler, textureCoordinates); } diff --git a/res/shaders/texture.vert b/res/shaders/texture.vert index ad121ef..9295f9a 100644 --- a/res/shaders/texture.vert +++ b/res/shaders/texture.vert @@ -1,6 +1,6 @@ #version 430 core -in layout(location = 0) vec2 position; +in layout(location = 0) vec3 position; in layout(location = 2) vec2 textureCoordinates_in; out layout(location = 1) vec2 textureCoordinates; @@ -9,6 +9,6 @@ uniform mat4 orthoMatrix; void main() { - gl_Position = vec4(position, 0.0, 1.0); + gl_Position = orthoMatrix * vec4(position.xy, 0.0, 1.0); textureCoordinates = textureCoordinates_in; } diff --git a/src/gamelogic.cpp b/src/gamelogic.cpp index e7e909a..54dc46b 100644 --- a/src/gamelogic.cpp +++ b/src/gamelogic.cpp @@ -1,3 +1,4 @@ +#include "utilities/window.hpp" #include #include #include @@ -173,7 +174,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) { auto img = loadPNGFile("../res/textures/charmap.png"); textureNode->textureID = createTextureFromImage(&img); - auto textureMesh = generateTextGeometryBuffer("hello world", 39.0 / 29, 0.4); + auto textureMesh = generateTextGeometryBuffer("foo", 39.0 / 29, windowWidth); textureNode->vertexArrayObjectID = generateBuffer(textureMesh); textureNode->VAOIndexCount = textureMesh.indices.size(); textureNode->nodeType = FLAT_GEOMETRY; @@ -381,16 +382,17 @@ void renderNode(SceneNode* node) { } } -void renderTexture(SceneNode *node, glm::mat4 orthoMatrix) { +void renderTexture(SceneNode *node) { + auto orthoMatrix = glm::ortho(0.0f, static_cast(windowWidth), 0.0f, static_cast(windowHeight)); GLint orthoLoc = textureShader->getUniformFromName("orthoMatrix"); glUniformMatrix4fv(orthoLoc, 1, GL_FALSE, glm::value_ptr(orthoMatrix)); if (node->nodeType == FLAT_GEOMETRY) { - glBindTexture(GL_TEXTURE_2D, node->textureID); + glBindTextureUnit(0, node->textureID); glBindVertexArray(node->vertexArrayObjectID); glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr); } for (auto* child : node->children) { - renderTexture(child, orthoMatrix); + renderTexture(child); } } @@ -399,11 +401,9 @@ void renderFrame(GLFWwindow* window) { glfwGetWindowSize(window, &w, &h); glViewport(0, 0, w, h); - glm::mat4 orthoMatrix = glm::ortho(0.0f, static_cast(w), 0.0f, static_cast(h), -1.0f, 1.0f); - shader->activate(); renderNode(rootNode); textureShader->activate(); - renderTexture(rootNode, orthoMatrix); + renderTexture(rootNode); } diff --git a/src/utilities/glfont.cpp b/src/utilities/glfont.cpp index 98c4246..f0092c2 100644 --- a/src/utilities/glfont.cpp +++ b/src/utilities/glfont.cpp @@ -39,8 +39,8 @@ Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth auto u = (int)text.at(i) / 128.0f; mesh.textureCoordinates[4*i + 0] = {u, 0}; mesh.textureCoordinates[4*i + 1] = {u + 1/128.0f, 0}; - mesh.textureCoordinates[4*i + 2] = {u + 1/128.0f, characterHeight/128.0f}; - mesh.textureCoordinates[4*i + 3] = {u, characterHeight/128.0f}; + mesh.textureCoordinates[4*i + 2] = {u + 1/128.0f, 1}; + mesh.textureCoordinates[4*i + 3] = {u, 1}; } return mesh;