This commit is contained in:
2026-02-04 13:56:37 +01:00
parent 42c5a81473
commit 8b42f97594
4 changed files with 14 additions and 12 deletions

View File

@@ -2,9 +2,11 @@
in layout(location = 1) vec2 textureCoordinates; in layout(location = 1) vec2 textureCoordinates;
layout(binding = 0) uniform sampler2D textureSampler;
out vec4 fragColor; out vec4 fragColor;
void main() void main()
{ {
fragColor = vec4(1.0, 0.0, 0.0, 1.0); fragColor = texture(textureSampler, textureCoordinates);
} }

View File

@@ -1,6 +1,6 @@
#version 430 core #version 430 core
in layout(location = 0) vec2 position; in layout(location = 0) vec3 position;
in layout(location = 2) vec2 textureCoordinates_in; in layout(location = 2) vec2 textureCoordinates_in;
out layout(location = 1) vec2 textureCoordinates; out layout(location = 1) vec2 textureCoordinates;
@@ -9,6 +9,6 @@ uniform mat4 orthoMatrix;
void main() void main()
{ {
gl_Position = vec4(position, 0.0, 1.0); gl_Position = orthoMatrix * vec4(position.xy, 0.0, 1.0);
textureCoordinates = textureCoordinates_in; textureCoordinates = textureCoordinates_in;
} }

View File

@@ -1,3 +1,4 @@
#include "utilities/window.hpp"
#include <chrono> #include <chrono>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h> #include <glad/glad.h>
@@ -173,7 +174,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
auto img = loadPNGFile("../res/textures/charmap.png"); auto img = loadPNGFile("../res/textures/charmap.png");
textureNode->textureID = createTextureFromImage(&img); 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->vertexArrayObjectID = generateBuffer(textureMesh);
textureNode->VAOIndexCount = textureMesh.indices.size(); textureNode->VAOIndexCount = textureMesh.indices.size();
textureNode->nodeType = FLAT_GEOMETRY; 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<float>(windowWidth), 0.0f, static_cast<float>(windowHeight));
GLint orthoLoc = textureShader->getUniformFromName("orthoMatrix"); GLint orthoLoc = textureShader->getUniformFromName("orthoMatrix");
glUniformMatrix4fv(orthoLoc, 1, GL_FALSE, glm::value_ptr(orthoMatrix)); glUniformMatrix4fv(orthoLoc, 1, GL_FALSE, glm::value_ptr(orthoMatrix));
if (node->nodeType == FLAT_GEOMETRY) { if (node->nodeType == FLAT_GEOMETRY) {
glBindTexture(GL_TEXTURE_2D, node->textureID); glBindTextureUnit(0, node->textureID);
glBindVertexArray(node->vertexArrayObjectID); glBindVertexArray(node->vertexArrayObjectID);
glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr);
} }
for (auto* child : node->children) { for (auto* child : node->children) {
renderTexture(child, orthoMatrix); renderTexture(child);
} }
} }
@@ -399,11 +401,9 @@ void renderFrame(GLFWwindow* window) {
glfwGetWindowSize(window, &w, &h); glfwGetWindowSize(window, &w, &h);
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
glm::mat4 orthoMatrix = glm::ortho(0.0f, static_cast<float>(w), 0.0f, static_cast<float>(h), -1.0f, 1.0f);
shader->activate(); shader->activate();
renderNode(rootNode); renderNode(rootNode);
textureShader->activate(); textureShader->activate();
renderTexture(rootNode, orthoMatrix); renderTexture(rootNode);
} }

View File

@@ -39,8 +39,8 @@ Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth
auto u = (int)text.at(i) / 128.0f; auto u = (int)text.at(i) / 128.0f;
mesh.textureCoordinates[4*i + 0] = {u, 0}; mesh.textureCoordinates[4*i + 0] = {u, 0};
mesh.textureCoordinates[4*i + 1] = {u + 1/128.0f, 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 + 2] = {u + 1/128.0f, 1};
mesh.textureCoordinates[4*i + 3] = {u, characterHeight/128.0f}; mesh.textureCoordinates[4*i + 3] = {u, 1};
} }
return mesh; return mesh;