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;
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);
}

View File

@@ -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;
}

View File

@@ -1,3 +1,4 @@
#include "utilities/window.hpp"
#include <chrono>
#include <GLFW/glfw3.h>
#include <glad/glad.h>
@@ -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<float>(windowWidth), 0.0f, static_cast<float>(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<float>(w), 0.0f, static_cast<float>(h), -1.0f, 1.0f);
shader->activate();
renderNode(rootNode);
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;
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;