rectongle

This commit is contained in:
2026-02-02 18:54:29 +01:00
parent 82f9bdd604
commit 42c5a81473
4 changed files with 89 additions and 66 deletions
+1 -3
View File
@@ -1,12 +1,10 @@
#version 430 core
in layout(location = 0) vec3 normal;
in layout(location = 1) vec2 textureCoordinates;
in layout(location = 2) vec3 worldPosition;
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0);
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
+14
View File
@@ -0,0 +1,14 @@
#version 430 core
in layout(location = 0) vec2 position;
in layout(location = 2) vec2 textureCoordinates_in;
out layout(location = 1) vec2 textureCoordinates;
uniform mat4 orthoMatrix;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
textureCoordinates = textureCoordinates_in;
}
+67 -57
View File
@@ -119,18 +119,6 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
shader = new Gloom::Shader();
shader->makeBasicShader("../res/shaders/simple.vert", "../res/shaders/simple.frag");
std::cout << "loading textures..." << std::endl;
textureShader = new Gloom::Shader();
textureShader->makeBasicShader("../res/shaders/simple.vert", "../res/shaders/texture.frag");
auto img = loadPNGFile("../res/textures/charmap.png");
auto tid = createTextureFromImage(&img);
auto mesh = generateTextGeometryBuffer("hello world", 39.0 / 29.0, 120.0);
generateBuffer(mesh);
std::cout << "loaded textures" << std::endl;
Mesh padMesh = cube(padDimensions, glm::vec2(30, 40), true);
Mesh boxMesh = cube(boxDimensions, glm::vec2(90), true, true); // inverted for interior view
Mesh ballMesh = generateSphere(1.0, 40, 40);
@@ -144,10 +132,6 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
padNode = createSceneNode();
ballNode = createSceneNode();
textureNode = createSceneNode();
addChild(rootNode, textureNode);
textureNode->position = {0.0, 0.0, 0.0};
rootNode->children.push_back(boxNode);
rootNode->children.push_back(padNode);
rootNode->children.push_back(ballNode);
@@ -173,13 +157,31 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
auto* ballLight = lightNodes[2];
boxNode->children.push_back(ceilingLight);
ceilingLight->position = { 0, 0, 5 };
ceilingLight->position = { 0, 10, 0 };
boxNode->children.push_back(padLight);
padLight->position = { -10, 0, 5 };
padNode->children.push_back(padLight);
padLight->position = { 0, 10, 0 };
boxNode->children.push_back(ballLight);
ballLight->position = { 10, 0, 5 };
ballNode->children.push_back(ballLight);
ballLight->position = { 0, 0, 0 };
std::cout << "loading textures..." << std::endl;
textureShader = new Gloom::Shader();
textureShader->makeBasicShader("../res/shaders/texture.vert", "../res/shaders/texture.frag");
textureNode = createSceneNode();
auto img = loadPNGFile("../res/textures/charmap.png");
textureNode->textureID = createTextureFromImage(&img);
auto textureMesh = generateTextGeometryBuffer("hello world", 39.0 / 29, 0.4);
textureNode->vertexArrayObjectID = generateBuffer(textureMesh);
textureNode->VAOIndexCount = textureMesh.indices.size();
textureNode->nodeType = FLAT_GEOMETRY;
rootNode->children.push_back(textureNode);
textureNode->position = {0, 0, 0};
std::cout << "loaded textures" << std::endl;
getTimeDeltaSeconds(); // prime the timer
@@ -332,56 +334,64 @@ void updateFrame(GLFWwindow* window) {
}
void updateNodeTransformations(SceneNode* node, glm::mat4 vpSoFar, glm::mat4 modelSoFar) {
glm::mat4 local = glm::translate(node->position)
* glm::translate(node->referencePoint)
* glm::rotate(node->rotation.y, glm::vec3(0,1,0))
* glm::rotate(node->rotation.x, glm::vec3(1,0,0))
* glm::rotate(node->rotation.z, glm::vec3(0,0,1))
* glm::scale(node->scale)
* glm::translate(-node->referencePoint);
if (node->nodeType != FLAT_GEOMETRY) {
glm::mat4 local = glm::translate(node->position)
* glm::translate(node->referencePoint)
* glm::rotate(node->rotation.y, glm::vec3(0,1,0))
* glm::rotate(node->rotation.x, glm::vec3(1,0,0))
* glm::rotate(node->rotation.z, glm::vec3(0,0,1))
* glm::scale(node->scale)
* glm::translate(-node->referencePoint);
node->currentTransformationMatrix = vpSoFar * local; // MVP
node->modelMatrix = modelSoFar * local; // world transform
node->normalMatrix = glm::transpose(glm::inverse(glm::mat3(node->modelMatrix)));
node->currentTransformationMatrix = vpSoFar * local; // MVP
node->modelMatrix = modelSoFar * local; // world transform
node->normalMatrix = glm::transpose(glm::inverse(glm::mat3(node->modelMatrix)));
if (node->nodeType == POINT_LIGHT) {
node->worldPosition = glm::vec3(node->modelMatrix * glm::vec4(0, 0, 0, 1));
if (node->nodeType == POINT_LIGHT) {
node->worldPosition = glm::vec3(node->modelMatrix * glm::vec4(0, 0, 0, 1));
}
}
for (auto* child : node->children) {
updateNodeTransformations(child, node->currentTransformationMatrix, node->modelMatrix);
}
}
void renderNode(SceneNode* node) {
glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(node->currentTransformationMatrix));
glUniformMatrix4fv(4, 1, GL_FALSE, glm::value_ptr(node->modelMatrix));
glUniformMatrix3fv(5, 1, GL_FALSE, glm::value_ptr(node->normalMatrix));
if (node->nodeType != FLAT_GEOMETRY) {
glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(node->currentTransformationMatrix));
glUniformMatrix4fv(4, 1, GL_FALSE, glm::value_ptr(node->modelMatrix));
glUniformMatrix3fv(5, 1, GL_FALSE, glm::value_ptr(node->normalMatrix));
glUniform3fv(shader->getUniformFromName("cameraPosition"), 1, glm::value_ptr(cameraPosition));
glUniform3fv(shader->getUniformFromName("ballPosition"), 1, glm::value_ptr(ballPosition));
glUniform3fv(shader->getUniformFromName("cameraPosition"), 1, glm::value_ptr(cameraPosition));
glUniform3fv(shader->getUniformFromName("ballPosition"), 1, glm::value_ptr(ballPosition));
for (size_t i = 0; i < lights.size(); i++) {
std::string base = "lights[" + std::to_string(i) + "]";
glUniform3fv(shader->getUniformFromName((base + ".position").c_str()), 1, glm::value_ptr(lights[i].position));
glUniform3fv(shader->getUniformFromName((base + ".color").c_str()), 1, glm::value_ptr(lights[i].color));
for (size_t i = 0; i < lights.size(); i++) {
std::string base = "lights[" + std::to_string(i) + "]";
glUniform3fv(shader->getUniformFromName((base + ".position").c_str()), 1, glm::value_ptr(lights[i].position));
glUniform3fv(shader->getUniformFromName((base + ".color").c_str()), 1, glm::value_ptr(lights[i].color));
}
if (node->nodeType == GEOMETRY && node->vertexArrayObjectID != -1) {
glBindVertexArray(node->vertexArrayObjectID);
glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr);
}
}
if (node->nodeType == GEOMETRY && node->vertexArrayObjectID != -1) {
glBindVertexArray(node->vertexArrayObjectID);
glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr);
}
for (auto* child : node->children) {
renderNode(child);
}
}
void renderTexture(SceneNode *node) {
glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(node->currentTransformationMatrix));
glUniformMatrix4fv(4, 1, GL_FALSE, glm::value_ptr(node->modelMatrix));
glUniformMatrix3fv(5, 1, GL_FALSE, glm::value_ptr(node->normalMatrix));
void renderTexture(SceneNode *node, glm::mat4 orthoMatrix) {
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);
glBindVertexArray(node->vertexArrayObjectID);
glDrawElements(GL_TRIANGLES, node->VAOIndexCount, GL_UNSIGNED_INT, nullptr);
}
for (auto* child : node->children) {
renderTexture(child, orthoMatrix);
}
}
void renderFrame(GLFWwindow* window) {
@@ -389,11 +399,11 @@ 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);
shader->deactivate();
textureShader->activate();
renderTexture(textureNode);
textureShader->deactivate();
renderTexture(rootNode, orthoMatrix);
}
+7 -6
View File
@@ -1,4 +1,5 @@
#include <iostream>
#include <string>
#include "glfont.h"
Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth, float totalTextWidth) {
@@ -7,7 +8,7 @@ Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth
unsigned int vertexCount = 4 * text.length();
unsigned int indexCount = 6 * text.length();
unsigned int texCoordCount = characterWidth * characterHeight * text.length();
unsigned int texCoordCount = 4 * text.length();
Mesh mesh;
@@ -35,11 +36,11 @@ Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth
mesh.indices.at(6 * i + 4) = 4 * i + 2;
mesh.indices.at(6 * i + 5) = 4 * i + 3;
for (int v = 0; v < characterWidth; v++) {
for (int u = 0; u < characterHeight; u++) {
mesh.textureCoordinates.at(i * (v * characterWidth + u)) = {u / characterWidth, v / characterHeight};
}
}
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};
}
return mesh;