TDT4230_final_project/src/sceneGraph.hpp

127 lines
3.1 KiB
C++
Raw Normal View History

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>
#include <map>
2019-03-15 16:34:17 +01:00
#include <stack>
#include <stdbool.h>
#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
using glm::vec2;
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 20:12:35 +01:00
GEOMETRY,
POINT_LIGHT,
SPOT_LIGHT,
2019-02-19 16:16:13 +01:00
};
struct SceneNode {
SceneNode(SceneNodeType type = GEOMETRY) {
nodeType = type;
2019-03-14 12:43:41 +01:00
}
void setMesh(Mesh* mesh) {
static map<Mesh*, int> cache;
if (cache.find(mesh) == cache.end())
2019-03-16 20:12:35 +01:00
cache[mesh] = generateBuffer(*mesh, isNormalMapped || isDisplacementMapped);
vertexArrayObjectID = cache[mesh];
VAOIndexCount = mesh->indices.size();
2019-02-19 16:16:13 +01:00
}
2019-03-16 20:12:35 +01:00
void setTexture(PNGImage* diffuse, PNGImage* normal=nullptr, PNGImage* displacement=nullptr) {
static map<PNGImage*, int> cache;
2019-03-16 20:12:35 +01:00
assert(vertexArrayObjectID==-1);
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-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-16 20:12:35 +01:00
// this node
SceneNodeType nodeType;
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);
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
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
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
// VAO IDs refering to a loaded Mesh and its length
int vertexArrayObjectID = -1;
uint VAOIndexCount = 0;
2019-03-16 20:12:35 +01:00
// textures
2019-03-16 20:12:35 +01:00
float shinyness = 10.0; // specular power
vec2 uvOffset = vec2(0.0, 0.0); // specular power
uint diffuseTextureID;
uint normalTextureID;
2019-03-16 20:12:35 +01:00
uint displacementTextureID;
float displacementCoefficient = 0.1; // in units
// shader flags
2019-03-16 20:12:35 +01:00
bool isTextured = false;
bool isNormalMapped = false;
bool isDisplacementMapped = false;
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);