2019-02-19 16:16:13 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
#include <assert.h>
|
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-17 15:17:44 +01:00
|
|
|
using glm::vec2;
|
2019-03-15 18:06:49 +01:00
|
|
|
using glm::vec3;
|
2019-03-18 11:48:11 +01:00
|
|
|
using glm::vec4;
|
2019-03-15 18:06:49 +01:00
|
|
|
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 20:12:35 +01:00
|
|
|
GEOMETRY,
|
|
|
|
POINT_LIGHT,
|
|
|
|
SPOT_LIGHT,
|
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
|
|
|
|
2019-03-18 11:48:11 +01:00
|
|
|
void setMesh(const Mesh* mesh) {
|
|
|
|
static map<const Mesh*, int> cache;
|
2019-03-15 18:06:49 +01:00
|
|
|
|
|
|
|
if (cache.find(mesh) == cache.end())
|
2019-03-16 20:12:35 +01:00
|
|
|
cache[mesh] = generateBuffer(*mesh, isNormalMapped || isDisplacementMapped);
|
2019-03-15 18:06:49 +01:00
|
|
|
|
|
|
|
vertexArrayObjectID = cache[mesh];
|
|
|
|
VAOIndexCount = mesh->indices.size();
|
2019-03-18 11:48:11 +01:00
|
|
|
isColorMapped = ! mesh->colors.empty();
|
2019-02-19 16:16:13 +01:00
|
|
|
}
|
2019-03-18 11:48:11 +01:00
|
|
|
void setTexture(const PNGImage* diffuse, const PNGImage* normal=nullptr, const PNGImage* displacement=nullptr) {
|
|
|
|
static map<const PNGImage*, int> cache;
|
2019-03-16 20:12:35 +01:00
|
|
|
assert(vertexArrayObjectID==-1);
|
2019-03-17 15:17:44 +01:00
|
|
|
isTextured = false;
|
|
|
|
isNormalMapped = false;
|
|
|
|
isDisplacementMapped = false;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
if (diffuse) {
|
|
|
|
if (cache.find(diffuse) == cache.end())
|
|
|
|
cache[diffuse] = generateTexture(*diffuse);
|
|
|
|
diffuseTextureID = cache[diffuse];
|
|
|
|
isTextured = true;
|
|
|
|
}
|
2019-03-15 18:06:49 +01:00
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
if (normal) {
|
|
|
|
if (cache.find(normal) == cache.end())
|
|
|
|
cache[normal] = generateTexture(*normal);
|
|
|
|
normalTextureID = cache[normal];
|
|
|
|
isNormalMapped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (displacement) {
|
|
|
|
if (cache.find(displacement) == cache.end())
|
|
|
|
cache[displacement] = generateTexture(*displacement);
|
|
|
|
displacementTextureID = cache[displacement];
|
|
|
|
isDisplacementMapped = true;
|
|
|
|
}
|
2019-03-15 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
// this node
|
|
|
|
SceneNodeType nodeType;
|
2019-03-15 18:06:49 +01:00
|
|
|
vector<SceneNode*> children;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
// light specific:
|
|
|
|
uint lightID = -1;
|
|
|
|
vec3 color_emissive = vec3(0.0);
|
2019-03-17 15:17:44 +01:00
|
|
|
vec3 color_diffuse = vec3(0.0);
|
|
|
|
vec3 color_specular = vec3(0.0);
|
2019-03-16 20:12:35 +01:00
|
|
|
vec3 attenuation = vec3(1.0, 0.0, 0.001); // 1 / (x + y*l + z*l*l)
|
|
|
|
float spot_cuttof_angle = glm::radians(1.5); // radians
|
|
|
|
SceneNode* targeted_by = nullptr; // spot will follow this node
|
|
|
|
|
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);
|
2019-03-16 20:12:35 +01:00
|
|
|
vec3 rotation = vec3(0, 0, 0); // also used as spot-target
|
2019-03-16 16:33:20 +01:00
|
|
|
vec3 scale = vec3(1, 1, 1);
|
2019-03-16 20:12:35 +01:00
|
|
|
vec3 referencePoint = vec3(0, 0, 0); // center of rotation, in model space
|
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-16 20:12:35 +01:00
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// textures
|
2019-03-18 11:48:11 +01:00
|
|
|
float shininess = 10.0; // specular power
|
|
|
|
vec4 basecolor = vec4(1.0);
|
2019-03-17 15:17:44 +01:00
|
|
|
vec2 uvOffset = vec2(0.0, 0.0); // specular power
|
2019-03-15 18:06:49 +01:00
|
|
|
uint diffuseTextureID;
|
|
|
|
uint normalTextureID;
|
2019-03-16 20:12:35 +01:00
|
|
|
uint displacementTextureID;
|
|
|
|
float displacementCoefficient = 0.1; // in units
|
|
|
|
|
2019-03-15 18:06:49 +01:00
|
|
|
// shader flags
|
2019-03-16 20:12:35 +01:00
|
|
|
bool isTextured = false;
|
2019-03-18 11:48:11 +01:00
|
|
|
bool isColorMapped = false;
|
2019-03-16 20:12:35 +01:00
|
|
|
bool isNormalMapped = false;
|
|
|
|
bool isDisplacementMapped = false;
|
2019-03-16 16:33:20 +01:00
|
|
|
bool isIlluminated = true;
|
|
|
|
bool isInverted = false;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-16 20:12:35 +01:00
|
|
|
// rendering
|
|
|
|
Gloom::Shader* shader = nullptr;
|
|
|
|
mat4 MVP; // MVP
|
|
|
|
mat4 MV; // MV
|
|
|
|
mat4 MVnormal; // transpose(inverse(MV))
|
|
|
|
|
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);
|