2019-02-19 16:16:13 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-15 16:34:17 +01:00
|
|
|
#include <chrono>
|
2019-02-19 16:16:13 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
#include <fstream>
|
2019-03-15 16:34:17 +01:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <glm/mat4x4.hpp>
|
2019-03-15 18:06:49 +01:00
|
|
|
#include <map>
|
2019-03-15 16:34:17 +01:00
|
|
|
#include <stack>
|
|
|
|
#include <stdbool.h>
|
2019-03-15 18:06:49 +01:00
|
|
|
#include <utilities/glutils.h>
|
2019-03-15 16:34:17 +01:00
|
|
|
#include <utilities/shader.hpp>
|
|
|
|
#include <vector>
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
using glm::vec3;
|
|
|
|
using glm::mat4;
|
|
|
|
using std::map;
|
|
|
|
using std::vector;
|
|
|
|
typedef unsigned int uint;
|
|
|
|
|
2019-02-19 16:16:13 +01:00
|
|
|
enum SceneNodeType {
|
2019-03-16 18:01:10 +01:00
|
|
|
GEOMETRY, POINT_LIGHT, SPOT_LIGHT, TEXTURED_GEOMETRY, NORMAL_TEXTURED_GEOMETRY
|
2019-02-19 16:16:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SceneNode {
|
2019-03-15 18:06:49 +01:00
|
|
|
SceneNode(SceneNodeType type = GEOMETRY) {
|
|
|
|
nodeType = type;
|
2019-03-14 12:43:41 +01:00
|
|
|
}
|
2019-03-15 18:06:49 +01:00
|
|
|
|
|
|
|
void setMesh(Mesh* mesh) {
|
|
|
|
static map<Mesh*, int> cache;
|
|
|
|
|
|
|
|
if (cache.find(mesh) == cache.end())
|
|
|
|
cache[mesh] = generateBuffer(*mesh, nodeType==NORMAL_TEXTURED_GEOMETRY);
|
|
|
|
|
|
|
|
vertexArrayObjectID = cache[mesh];
|
|
|
|
VAOIndexCount = mesh->indices.size();
|
2019-02-19 16:16:13 +01:00
|
|
|
}
|
2019-03-15 18:06:49 +01:00
|
|
|
void setTexture(PNGImage* diffuse, PNGImage* normal = nullptr) {
|
|
|
|
static map<PNGImage*, int> cache;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
if (cache.find(diffuse) == cache.end()) cache[diffuse] = generateTexture(*diffuse);
|
|
|
|
diffuseTextureID = cache[diffuse];
|
|
|
|
|
|
|
|
if (!normal) return;
|
|
|
|
if (cache.find(normal) == cache.end()) cache[normal] = generateTexture(*normal);
|
|
|
|
normalTextureID = cache[normal];
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<SceneNode*> children;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
|
|
|
// The node's position and rotation relative to its parent
|
2019-03-16 16:33:20 +01:00
|
|
|
vec3 position = vec3(0, 0, 0);
|
|
|
|
vec3 rotation = vec3(0, 0, 0);
|
|
|
|
vec3 scale = vec3(1, 1, 1);
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-16 16:42:30 +01:00
|
|
|
// set this if the shape uses a custom shader other than the inherited one
|
2019-03-15 16:43:51 +01:00
|
|
|
Gloom::Shader* shader = nullptr;
|
|
|
|
|
2019-02-19 16:16:13 +01:00
|
|
|
// A transformation matrix representing the transformation of the node's location relative to its parent. This matrix is updated every frame.
|
2019-03-15 18:06:49 +01:00
|
|
|
mat4 MVP; // MVP
|
|
|
|
mat4 MV; // MV
|
|
|
|
mat4 MVnormal; // transpose(inverse(MV))
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// The location of the node's reference point (center of rotation)
|
2019-03-16 16:33:20 +01:00
|
|
|
vec3 referencePoint = vec3(0, 0, 0);
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// VAO IDs refering to a loaded Mesh and its length
|
2019-03-16 16:33:20 +01:00
|
|
|
int vertexArrayObjectID = -1;
|
|
|
|
uint VAOIndexCount = 0;
|
2019-03-14 12:43:41 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// textures
|
|
|
|
uint diffuseTextureID;
|
|
|
|
uint normalTextureID;
|
2019-03-14 12:43:41 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// shader flags
|
2019-03-16 16:33:20 +01:00
|
|
|
bool isIlluminated = true;
|
|
|
|
bool isInverted = false;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
|
|
|
// Node type is used to determine how to handle the contents of a node
|
|
|
|
SceneNodeType nodeType;
|
|
|
|
|
|
|
|
// for lights:
|
2019-03-15 18:06:49 +01:00
|
|
|
uint lightID;
|
2019-03-16 16:33:20 +01:00
|
|
|
SceneNode* targeted_by = nullptr; // spot
|
2019-02-19 16:16:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Struct for keeping track of 2D coordinates
|
|
|
|
|
|
|
|
SceneNode* createSceneNode();
|
2019-03-14 12:43:41 +01:00
|
|
|
SceneNode* createSceneNode(SceneNodeType type);
|
2019-02-19 16:16:13 +01:00
|
|
|
void addChild(SceneNode* parent, SceneNode* child);
|
|
|
|
void printNode(SceneNode* node);
|