Move hudNode out of root node, remove HUD node type, make main loop fetch window size and propagate it though update and render calls

This commit is contained in:
Peder Bergebakken Sundt 2019-03-16 18:01:10 +01:00
parent ce277dfce5
commit c699330ab5
6 changed files with 43 additions and 43 deletions

View File

@ -53,6 +53,10 @@ Gloom::Shader* test_shader;
Gloom::Shader* plain_shader; Gloom::Shader* plain_shader;
Gloom::Shader* post_shader; Gloom::Shader* post_shader;
vec3 cameraPosition = vec3(0, 0, 400);
vec3 cameraLookAt = vec3(500, 500, 0);
vec3 cameraUpward = vec3(0, 0, 1);
const vec3 boxDimensions(180, 90, 50); const vec3 boxDimensions(180, 90, 50);
const vec3 padDimensions(30, 3, 40); const vec3 padDimensions(30, 3, 40);
@ -122,16 +126,17 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
Mesh sphere = generateSphere(1.0, 40, 40); Mesh sphere = generateSphere(1.0, 40, 40);
rootNode = createSceneNode(); rootNode = createSceneNode();
hudNode = createSceneNode();
boxNode = createSceneNode(NORMAL_TEXTURED_GEOMETRY); boxNode = createSceneNode(NORMAL_TEXTURED_GEOMETRY);
padNode = createSceneNode(); padNode = createSceneNode();
ballNode = createSceneNode(); ballNode = createSceneNode();
hudNode = createSceneNode(HUD);
textNode = createSceneNode(TEXTURED_GEOMETRY); textNode = createSceneNode(TEXTURED_GEOMETRY);
rootNode->children.push_back(boxNode); rootNode->children.push_back(boxNode);
rootNode->children.push_back(padNode); rootNode->children.push_back(padNode);
rootNode->children.push_back(ballNode); rootNode->children.push_back(ballNode);
rootNode->children.push_back(hudNode);
hudNode->children.push_back(textNode); hudNode->children.push_back(textNode);
//rootNode->children.push_back(textNode); //rootNode->children.push_back(textNode);
@ -142,7 +147,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
padNode->setMesh(&pad); padNode->setMesh(&pad);
ballNode->setMesh(&sphere); ballNode->setMesh(&sphere);
// task 1a, add point lights // add lights
for (int i = 0; i<3; i++) { for (int i = 0; i<3; i++) {
lightNode[i] = createSceneNode(POINT_LIGHT); lightNode[i] = createSceneNode(POINT_LIGHT);
lightNode[i]->lightID = i; lightNode[i]->lightID = i;
@ -160,8 +165,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
// hud // hud
Mesh hello_world = generateTextGeometryBuffer("Skjer'a bagera?", 1.3, 2); Mesh hello_world = generateTextGeometryBuffer("Skjer'a bagera?", 1.3, 2);
textNode->position = vec3(-1.0, 0.0, 0.0); textNode->position = vec3(-1.0, -1.0, 0.0);
textNode->rotation = vec3(0.0, 0.0, 0.0);
textNode->setMesh(&hello_world); textNode->setMesh(&hello_world);
textNode->setTexture(&t_charmap); textNode->setTexture(&t_charmap);
textNode->isIlluminated = false; textNode->isIlluminated = false;
@ -173,16 +177,9 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
} }
void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 V, mat4 P) { void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 V, mat4 P) {
mat4 transformationMatrix(1.0); mat4 transformationMatrix(1.0);
switch(node->nodeType) { switch(node->nodeType) {
case HUD:
// We orthographic now, bitches!
// set orthographic VP
V = mat4(1.0);
P = glm::ortho(-float(windowWidth) / float(windowHeight), float(windowWidth) / float(windowHeight), -1.0f, 1.0f);//, -10.0f, 120.0f);
break;
case NORMAL_TEXTURED_GEOMETRY: case NORMAL_TEXTURED_GEOMETRY:
case TEXTURED_GEOMETRY: case TEXTURED_GEOMETRY:
case GEOMETRY: case GEOMETRY:
@ -218,7 +215,7 @@ void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4
} }
} }
void updateFrame(GLFWwindow* window) { void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double timeDelta = getTimeDeltaSeconds(); double timeDelta = getTimeDeltaSeconds();
@ -345,17 +342,24 @@ void updateFrame(GLFWwindow* window) {
} }
} }
mat4 projection = glm::perspective(glm::radians(90.0f), float(windowWidth) / float(windowHeight), 0.1f, mat4 projection = glm::perspective(
120.f); glm::radians(45.0f), // fovy
float(windowWidth) / float(windowHeight), // aspect
0.1f, 50000.f // near, far
);
// hardcoded camera position... // hardcoded camera position...
mat4 cameraTransform mat4 cameraTransform
= glm::translate(mat4(1), vec3(0, 0, 0)) = glm::lookAt(cameraPosition, cameraLookAt, cameraUpward);
* glm::rotate(mat4(1.0), 0.2f, vec3(1, 0, 0))
* glm::rotate(mat4(1.0), float(M_PI), vec3(0, 1, 0));
updateNodeTransformations(rootNode, mat4(1.0), cameraTransform, projection); updateNodeTransformations(rootNode, mat4(1.0), cameraTransform, projection);
// We orthographic now, bitches!
// set orthographic VP
cameraTransform = mat4(1.0);
projection = glm::ortho(-float(windowWidth) / float(windowHeight), float(windowWidth) / float(windowHeight), -1.0f, 1.0f);
updateNodeTransformations(hudNode, mat4(1.0), cameraTransform, projection);
boxNode->position = {-boxDimensions.x / 2, -boxDimensions.y / 2 - 15, boxDimensions.z - 10}; boxNode->position = {-boxDimensions.x / 2, -boxDimensions.y / 2 - 15, boxDimensions.z - 10};
padNode->position = {-boxDimensions.x / 2 + (1 - padPositionX) * (boxDimensions.x - padDimensions.x), padNode->position = {-boxDimensions.x / 2 + (1 - padPositionX) * (boxDimensions.x - padDimensions.x),
-boxDimensions.y / 2 - 15, -boxDimensions.y / 2 - 15,
@ -429,7 +433,6 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
lights[id].push_to_shader(s, id); lights[id].push_to_shader(s, id);
break; break;
} }
case HUD:
default: default:
break; break;
} }
@ -439,10 +442,9 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
} }
} }
void renderFrame(GLFWwindow* window) { void renderFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight); glViewport(0, 0, windowWidth, windowHeight);
renderNode(rootNode); renderNode(rootNode);
renderNode(hudNode);
} }

View File

@ -4,5 +4,5 @@
#include <utilities/window.hpp> #include <utilities/window.hpp>
void initGame(GLFWwindow* window, CommandLineOptions options); void initGame(GLFWwindow* window, CommandLineOptions options);
void updateFrame(GLFWwindow* window); void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight);
void renderFrame(GLFWwindow* window); void renderFrame(GLFWwindow* window, int windowWidth, int windowHeight);

View File

@ -36,13 +36,13 @@ GLFWwindow* initialise()
glfwSetErrorCallback(glfwErrorCallback); glfwSetErrorCallback(glfwErrorCallback);
// Set additional window options // Set additional window options
glfwWindowHint(GLFW_RESIZABLE, windowResizable); glfwWindowHint(GLFW_RESIZABLE, c_windowResizable);
glfwWindowHint(GLFW_SAMPLES, windowSamples); // MSAA glfwWindowHint(GLFW_SAMPLES, c_windowSamples); // MSAA
// Create window using GLFW // Create window using GLFW
GLFWwindow* window = glfwCreateWindow(windowWidth, GLFWwindow* window = glfwCreateWindow(c_windowWidth,
windowHeight, c_windowHeight,
windowTitle.c_str(), c_windowTitle.c_str(),
nullptr, nullptr,
nullptr); nullptr);

View File

@ -39,13 +39,11 @@ void runProgram(GLFWwindow* window, CommandLineOptions options)
// Clear colour and depth buffers // Clear colour and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int w, h;
glfwGetWindowSize(window, &w, &h);
updateFrame(window); updateFrame(window, w, h);
renderFrame(window); renderFrame(window, w, h);
// Handle other events // Handle other events

View File

@ -22,7 +22,7 @@ using std::vector;
typedef unsigned int uint; typedef unsigned int uint;
enum SceneNodeType { enum SceneNodeType {
GEOMETRY, POINT_LIGHT, SPOT_LIGHT, HUD, TEXTURED_GEOMETRY, NORMAL_TEXTURED_GEOMETRY GEOMETRY, POINT_LIGHT, SPOT_LIGHT, TEXTURED_GEOMETRY, NORMAL_TEXTURED_GEOMETRY
}; };
struct SceneNode { struct SceneNode {

View File

@ -7,11 +7,11 @@
#include <string> #include <string>
// Constants // Constants
const int windowWidth = 1366; const int c_windowWidth = 1366;
const int windowHeight = 768; const int c_windowHeight = 768;
const std::string windowTitle = "Glowbox"; const std::string c_windowTitle = "Glowbox";
const GLint windowResizable = GL_TRUE; const GLint c_windowResizable = GL_TRUE;
const int windowSamples = 4; const int c_windowSamples = 4;
struct CommandLineOptions { struct CommandLineOptions {
bool enableMusic; bool enableMusic;