Minor cleanups and fix spotlights

This commit is contained in:
Peder Bergebakken Sundt 2019-03-20 00:21:51 +01:00
parent 4fbf2f296c
commit d7df5e5ab1
4 changed files with 75 additions and 60 deletions

View File

@ -44,7 +44,7 @@ struct Light { // point lights, coordinates in MV space
float spot_cuttof_cos; float spot_cuttof_cos;
}; };
#define N_LIGHTS 2 #define N_LIGHTS 3
uniform Light light[N_LIGHTS]; uniform Light light[N_LIGHTS];

View File

@ -24,12 +24,6 @@ using glm::vec4;
using glm::mat4; using glm::mat4;
typedef unsigned int uint; typedef unsigned int uint;
enum KeyFrameAction {
BOTTOM, TOP
};
#include <timestamps.h>
uint currentKeyFrame = 0; uint currentKeyFrame = 0;
uint previousKeyFrame = 0; uint previousKeyFrame = 0;
@ -42,12 +36,13 @@ SceneNode* sphereNode;
SceneNode* hudNode; SceneNode* hudNode;
SceneNode* textNode; SceneNode* textNode;
const uint N_LIGHTS = 2; const uint N_LIGHTS = 3;
SceneNode* lightNode[N_LIGHTS]; SceneNode* lightNode[N_LIGHTS];
// These are heap allocated, because they should not be initialised at the start of the program // These are heap allocated, because they should not be initialised at the start of the program
sf::Sound* sound; sf::Sound* sound;
sf::SoundBuffer* buffer; sf::SoundBuffer* buffer;
Gloom::Shader* default_shader; Gloom::Shader* default_shader;
Gloom::Shader* test_shader; Gloom::Shader* test_shader;
Gloom::Shader* plain_shader; Gloom::Shader* plain_shader;
@ -201,6 +196,10 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
lightNode[1]->light_color = vec3(0.0); lightNode[1]->light_color = vec3(0.0);
lightNode[1]->attenuation = vec3(1.0, 0.0, 0.000005); lightNode[1]->attenuation = vec3(1.0, 0.0, 0.000005);
lightNode[2]->position = {400, -200, 300};
lightNode[2]->nodeType = SPOT_LIGHT;
lightNode[2]->attenuation = vec3(1, 0, 0);
lightNode[2]->spot_target = lightNode[1];
textNode = createSceneNode(); textNode = createSceneNode();
textNode->setTexture(&t_charmap); textNode->setTexture(&t_charmap);
@ -216,37 +215,20 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
std::cout << "Ready. Click to start!" << std::endl; std::cout << "Ready. Click to start!" << std::endl;
} }
void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 const& V, mat4 const& P) { void stepScene(double timeDelta) {
mat4 transformationMatrix static double timeAcc = 0; // shrug
= glm::translate(mat4(1.0), node->position) timeAcc += timeDelta;
* glm::translate(mat4(1.0), node->referencePoint)
* glm::rotate(mat4(1.0), node->rotation.z, vec3(0,0,1))
* glm::rotate(mat4(1.0), node->rotation.y, vec3(0,1,0))
* glm::rotate(mat4(1.0), node->rotation.x, vec3(1,0,0))
* glm::scale(mat4(1.0), node->scale)
* glm::translate(mat4(1.0), -node->referencePoint);
mat4 M = transformationThusFar * transformationMatrix; plainNode->uvOffset.x += timeDelta*0.5;
plainNode->uvOffset.y -= timeDelta*0.5;
if (boxNode) boxNode->rotation.z += timeDelta;
lightNode[1]->rotation.z -= timeDelta;
//lightNode[1]->position.z = 80 + 40*glm::sin(5 * lightNode[1]->rotation.z);
//if(carNode) carNode->rotation.z += timeDelta;
node->MV = V*M;
node->MVP = P*node->MV;
node->MVnormal = glm::inverse(glm::transpose(node->MV));
for(SceneNode* child : node->children) {
updateNodeTransformations(child, M, V, P);
}
// move this into the renderNode method and have it be targeted_node from the spot
if (node->targeted_by != nullptr) {
assert(node->targeted_by->nodeType == SPOT_LIGHT);
node->targeted_by->rotation = vec3(node->MV*vec4(node->position, 1.0));
}
}
void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double timeDelta = getTimeDeltaSeconds();
/*
if(!hasStarted) { if(!hasStarted) {
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)) { if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)) {
@ -285,10 +267,40 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
} }
} }
*/
}
void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 const& V, mat4 const& P) {
mat4 transformationMatrix
= glm::translate(mat4(1.0), node->position)
* glm::translate(mat4(1.0), node->referencePoint)
* glm::rotate(mat4(1.0), node->rotation.z, vec3(0,0,1))
* glm::rotate(mat4(1.0), node->rotation.y, vec3(0,1,0))
* glm::rotate(mat4(1.0), node->rotation.x, vec3(1,0,0))
* glm::scale(mat4(1.0), node->scale)
* glm::translate(mat4(1.0), -node->referencePoint);
mat4 M = transformationThusFar * transformationMatrix;
node->MV = V*M;
node->MVP = P*node->MV;
node->MVnormal = glm::inverse(glm::transpose(node->MV));
for(SceneNode* child : node->children)
updateNodeTransformations(child, M, V, P);
}
void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double timeDelta = getTimeDeltaSeconds();
stepScene(timeDelta);
float aspect = float(windowWidth) / float(windowHeight);
mat4 projection = glm::perspective( mat4 projection = glm::perspective(
glm::radians(45.0f), // fovy glm::radians(45.0f), // fovy
float(windowWidth) / float(windowHeight), // aspect aspect, // aspect
0.1f, 5000.f // near, far 0.1f, 5000.f // near, far
); );
@ -298,20 +310,23 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
updateNodeTransformations(rootNode, mat4(1.0), cameraTransform, projection); updateNodeTransformations(rootNode, mat4(1.0), cameraTransform, projection);
// We orthographic now, bitches! // We orthographic now, bitches!
// set orthographic VP // set orthographic VP for hud
cameraTransform = mat4(1.0); cameraTransform = mat4(1.0);
projection = glm::ortho(-float(windowWidth) / float(windowHeight), float(windowWidth) / float(windowHeight), -1.0f, 1.0f); projection = glm::ortho(-aspect, aspect, -1.0f, 1.0f);
//update hud
updateNodeTransformations(hudNode, mat4(1.0), cameraTransform, projection); updateNodeTransformations(hudNode, mat4(1.0), cameraTransform, projection);
// update positions of nodes (like the car) // update spots
plainNode->uvOffset.x += timeDelta*0.5; for (SceneNode* node : lightNode) {
plainNode->uvOffset.y -= timeDelta*0.5; if (node->nodeType == SPOT_LIGHT && node->spot_target) {
if (boxNode) boxNode->rotation.z += timeDelta; node->spot_direction = glm::normalize(
lightNode[1]->rotation.z -= timeDelta; vec3(node->spot_target->MV * vec4(0,0,0,1))
lightNode[1]->position.z = 80 + 40*glm::sin(5 * lightNode[1]->rotation.z); - vec3(node->MV * vec4(0,0,0,1)));
//if(carNode) carNode->rotation.z += timeDelta; }
} }
}
void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader) { void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader) {
struct Light { // lights as stored in the shader struct Light { // lights as stored in the shader

View File

@ -1,7 +1,7 @@
#include "sceneGraph.hpp" #include "sceneGraph.hpp"
#include <iostream> #include <iostream>
SceneNode::SceneNode(SceneNodeType type = GEOMETRY) { SceneNode::SceneNode(SceneNodeType type) {
nodeType = type; nodeType = type;
} }
@ -17,10 +17,10 @@ void SceneNode::setMesh(const Mesh* mesh) {
} }
void SceneNode::setTexture( void SceneNode::setTexture(
const PNGImage* diffuse, const PNGImage* diffuse,
const PNGImage* normal=nullptr, const PNGImage* normal,
const PNGImage* displacement=nullptr, const PNGImage* displacement,
const PNGImage* reflection=nullptr, const PNGImage* reflection,
bool texture_reset=true) { bool texture_reset) {
static map<const PNGImage*, int> cache; static map<const PNGImage*, int> cache;
if (texture_reset){ if (texture_reset){
isTextured = false; isTextured = false;
@ -57,7 +57,7 @@ void SceneNode::setTexture(
isReflectionMapped = true; isReflectionMapped = true;
} }
} }
void SceneNode::setMaterial(const Material& mat, bool recursive=false) { void SceneNode::setMaterial(const Material& mat, bool recursive) {
reflexiveness = mat.reflexiveness; reflexiveness = mat.reflexiveness;
if (!mat.ignore_diffuse) diffuse_color = mat.diffuse_color; if (!mat.ignore_diffuse) diffuse_color = mat.diffuse_color;
if (!mat.ignore_emissive) emissive_color = mat.emissive_color; if (!mat.ignore_emissive) emissive_color = mat.emissive_color;

View File

@ -51,9 +51,9 @@ struct SceneNode {
uint lightID = -1; uint lightID = -1;
vec3 light_color = vec3(1.0); vec3 light_color = vec3(1.0);
vec3 attenuation = vec3(1.0, 0.0, 0.001); // 1 / (x + y*l + z*l*l) vec3 attenuation = vec3(1.0, 0.0, 0.001); // 1 / (x + y*l + z*l*l)
vec3 spot_direction = vec3(0.0); // in MV space, must be normalized
float spot_cuttof_cos = glm::cos(glm::radians(1.5)); float spot_cuttof_cos = glm::cos(glm::radians(1.5));
SceneNode* targeted_by = nullptr; // spot will follow this node vec3 spot_direction = vec3(0.0); // in MV space, must be normalized, automatically updated by spot_target
SceneNode* spot_target = nullptr; // spot will follow this node
// The node's position and rotation relative to its parent // The node's position and rotation relative to its parent
vec3 position = vec3(0, 0, 0); vec3 position = vec3(0, 0, 0);