2019-03-17 15:30:21 +01:00
|
|
|
#include "sceneGraph.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-03-20 00:21:51 +01:00
|
|
|
SceneNode::SceneNode(SceneNodeType type) {
|
2019-03-19 20:25:23 +01:00
|
|
|
nodeType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneNode::setMesh(const Mesh* mesh) {
|
|
|
|
static map<const Mesh*, int> cache;
|
|
|
|
|
|
|
|
if (cache.find(mesh) == cache.end())
|
|
|
|
cache[mesh] = generateBuffer(*mesh, isNormalMapped || isDisplacementMapped);
|
|
|
|
|
|
|
|
vertexArrayObjectID = cache[mesh];
|
|
|
|
VAOIndexCount = mesh->indices.size();
|
|
|
|
isVertexColored = ! mesh->colors.empty();
|
2019-03-22 18:01:42 +01:00
|
|
|
mesh_has_transparancy = mesh->has_transparancy;
|
2019-03-19 20:25:23 +01:00
|
|
|
}
|
|
|
|
void SceneNode::setTexture(
|
2019-03-20 00:21:51 +01:00
|
|
|
const PNGImage* diffuse,
|
|
|
|
const PNGImage* normal,
|
|
|
|
const PNGImage* displacement,
|
|
|
|
const PNGImage* reflection,
|
|
|
|
bool texture_reset) {
|
2019-03-19 20:25:23 +01:00
|
|
|
static map<const PNGImage*, int> cache;
|
|
|
|
if (texture_reset){
|
|
|
|
isTextured = false;
|
|
|
|
isNormalMapped = false;
|
|
|
|
isDisplacementMapped = false;
|
|
|
|
isReflectionMapped = false;
|
2019-03-22 18:01:42 +01:00
|
|
|
tex_has_transparancy = false;
|
2019-03-19 20:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (diffuse) {
|
|
|
|
if (cache.find(diffuse) == cache.end())
|
|
|
|
cache[diffuse] = generateTexture(*diffuse);
|
|
|
|
diffuseTextureID = cache[diffuse];
|
|
|
|
isTextured = true;
|
2019-03-22 18:01:42 +01:00
|
|
|
tex_has_transparancy = diffuse->has_transparancy;
|
2019-03-19 20:25:23 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reflection) {
|
|
|
|
if (cache.find(reflection) == cache.end())
|
|
|
|
cache[reflection] = generateTexture(*reflection);
|
|
|
|
reflectionTextureID = cache[reflection];
|
|
|
|
isReflectionMapped = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 00:21:51 +01:00
|
|
|
void SceneNode::setMaterial(const Material& mat, bool recursive) {
|
2019-03-19 20:25:23 +01:00
|
|
|
reflexiveness = mat.reflexiveness;
|
|
|
|
if (!mat.ignore_diffuse) diffuse_color = mat.diffuse_color;
|
|
|
|
if (!mat.ignore_emissive) emissive_color = mat.emissive_color;
|
|
|
|
if (!mat.ignore_specular) specular_color = mat.specular_color;
|
|
|
|
if (!mat.ignore_specular) shininess = mat.shininess;
|
2019-03-28 09:35:22 +01:00
|
|
|
if (!mat.ignore_backlight)backlight_color= mat.backlight_color;
|
|
|
|
if (!mat.ignore_backlight)backlight_strength= mat.backlight_strength;
|
2019-03-19 20:25:23 +01:00
|
|
|
setTexture(
|
|
|
|
mat.diffuse_texture,
|
|
|
|
mat.normal_texture,
|
|
|
|
mat.displacement_texture,
|
|
|
|
mat.reflection_texture,
|
|
|
|
mat.texture_reset
|
|
|
|
);
|
|
|
|
if (recursive) for (SceneNode* child : children)
|
|
|
|
child->setMaterial(mat, true);
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:05:50 +01:00
|
|
|
bool SceneNode::has_no_transforms() const {
|
|
|
|
return position.x == 0 && position.y == 0 && position.z == 0
|
|
|
|
&& rotation.x == 0 && rotation.y == 0 && rotation.z == 0
|
|
|
|
&& scale.x == 1 && scale.y == 1 && scale.z == 1;
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:04:50 +01:00
|
|
|
bool SceneNode::has_transparancy() const {
|
2019-03-22 18:01:42 +01:00
|
|
|
return mesh_has_transparancy
|
|
|
|
|| tex_has_transparancy
|
|
|
|
|| opacity < 0.98;
|
2019-03-22 00:04:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SceneNode* SceneNode::clone() const {
|
|
|
|
SceneNode* out = new SceneNode();
|
|
|
|
*out = *this;
|
|
|
|
out->children.clear();
|
|
|
|
for (SceneNode* child : children) {
|
|
|
|
out->children.push_back(child->clone());
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:25:23 +01:00
|
|
|
|
2019-03-17 15:30:21 +01:00
|
|
|
SceneNode* createSceneNode() {
|
|
|
|
return new SceneNode();
|
|
|
|
}
|
|
|
|
SceneNode* createSceneNode(SceneNodeType type) {
|
|
|
|
return new SceneNode(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a child node to its parent's list of children
|
|
|
|
void addChild(SceneNode* parent, SceneNode* child) {
|
|
|
|
parent->children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretty prints the current values of a SceneNode instance to stdout
|
|
|
|
void printNode(SceneNode* node) {
|
|
|
|
printf(
|
|
|
|
"SceneNode {\n"
|
|
|
|
" Child count: %i\n"
|
|
|
|
" Rotation: (%f, %f, %f)\n"
|
|
|
|
" Location: (%f, %f, %f)\n"
|
|
|
|
" Reference point: (%f, %f, %f)\n"
|
|
|
|
" VAO ID: %i\n"
|
|
|
|
"}\n",
|
|
|
|
int(node->children.size()),
|
|
|
|
node->rotation.x, node->rotation.y, node->rotation.z,
|
|
|
|
node->position.x, node->position.y, node->position.z,
|
|
|
|
node->referencePoint.x, node->referencePoint.y, node->referencePoint.z,
|
|
|
|
node->vertexArrayObjectID);
|
|
|
|
}
|