Merged SceneNode and GraphicalNode, now all nodes have a transform, and the project has less classes
This commit is contained in:
parent
78073bac45
commit
ae2a8997e0
@ -1,17 +1,13 @@
|
|||||||
#include "containernode.h"
|
#include "containernode.h"
|
||||||
#include <iostream>
|
#include "scenetree.h"
|
||||||
|
|
||||||
ContainerNode::ContainerNode()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ContainerNode::~ContainerNode()
|
ContainerNode::~ContainerNode()
|
||||||
{
|
{
|
||||||
|
setSceneTree(nullptr);
|
||||||
for(SceneNode* child : m_children)
|
for(SceneNode* child : m_children)
|
||||||
delete child;
|
delete child;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Container Node
|
|
||||||
void ContainerNode::update()
|
void ContainerNode::update()
|
||||||
{
|
{
|
||||||
for(SceneNode* node : m_nodesToRemove)
|
for(SceneNode* node : m_nodesToRemove)
|
||||||
@ -19,25 +15,43 @@ void ContainerNode::update()
|
|||||||
for(auto it = m_children.begin(); it != m_children.end();)
|
for(auto it = m_children.begin(); it != m_children.end();)
|
||||||
{
|
{
|
||||||
if(*it == node)
|
if(*it == node)
|
||||||
|
{
|
||||||
it = m_children.erase(it);
|
it = m_children.erase(it);
|
||||||
|
node->setSceneTree(nullptr);
|
||||||
|
node->setParent(nullptr);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
node->setSceneTree(nullptr);
|
|
||||||
node->setParent(nullptr);
|
|
||||||
}
|
}
|
||||||
m_nodesToRemove.clear();
|
m_nodesToRemove.clear();
|
||||||
|
|
||||||
for(SceneNode* node : m_nodesToAdd)
|
for(SceneNode* node : m_nodesToAdd)
|
||||||
{
|
{
|
||||||
m_children.push_back(node);
|
if(m_scene != nullptr)
|
||||||
node->setSceneTree(m_scene);
|
node->setSceneTree(m_scene);
|
||||||
|
m_children.push_back(node);
|
||||||
node->setParent(this);
|
node->setParent(this);
|
||||||
|
node->setParentTransform(m_transform);
|
||||||
|
node->setParentVisible(isVisible());
|
||||||
}
|
}
|
||||||
m_nodesToAdd.clear();
|
m_nodesToAdd.clear();
|
||||||
|
|
||||||
for(SceneNode* sn : m_children)
|
if(m_transformChanged)
|
||||||
sn->update();
|
m_combinedTransform = m_transform * m_parentTransform;
|
||||||
|
for(SceneNode* child : m_children)
|
||||||
|
{
|
||||||
|
if(m_transformChanged)
|
||||||
|
child->setParentTransform(m_combinedTransform);
|
||||||
|
child->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContainerNode::setSceneTree(SceneTree* tree)
|
||||||
|
{
|
||||||
|
SceneNode::setSceneTree(tree);
|
||||||
|
for(SceneNode* child : m_children)
|
||||||
|
child->setSceneTree(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContainerNode::addChild(SceneNode *node)
|
void ContainerNode::addChild(SceneNode *node)
|
||||||
@ -52,9 +66,8 @@ void ContainerNode::removeChild(SceneNode* node)
|
|||||||
m_nodesToRemove.push_back(node);
|
m_nodesToRemove.push_back(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContainerNode::setSceneTree(SceneTree *tree)
|
void ContainerNode::updateVisibility(bool visible)
|
||||||
{
|
{
|
||||||
SceneNode::setSceneTree(tree);
|
for(SceneNode* child : m_children)
|
||||||
for(auto child : m_children)
|
child->setParentVisible(visible);
|
||||||
child->setSceneTree(tree);
|
|
||||||
}
|
}
|
||||||
|
@ -2,39 +2,30 @@
|
|||||||
#define CONTAINERNODE_H
|
#define CONTAINERNODE_H
|
||||||
|
|
||||||
#include "scenenode.h"
|
#include "scenenode.h"
|
||||||
|
#include "glm/vec2.hpp"
|
||||||
|
#include "glm/mat4x4.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The ContainerNode class represents a node which main use is to contain a collection of nodes
|
|
||||||
*/
|
|
||||||
class ContainerNode : public SceneNode
|
class ContainerNode : public SceneNode
|
||||||
{
|
{
|
||||||
std::vector<SceneNode*> m_nodesToRemove;
|
std::vector<SceneNode*> m_nodesToRemove;
|
||||||
std::vector<SceneNode*> m_nodesToAdd;
|
std::vector<SceneNode*> m_nodesToAdd;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<SceneNode*> m_children;
|
||||||
|
glm::mat4 m_combinedTransform;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ContainerNode();
|
|
||||||
virtual ~ContainerNode();
|
virtual ~ContainerNode();
|
||||||
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
/**
|
|
||||||
* @brief addChild adds node in the ContainerNode's children list
|
virtual void setSceneTree(SceneTree* tree);
|
||||||
*/
|
|
||||||
|
virtual void updateVisibility(bool visible);
|
||||||
|
|
||||||
void addChild(SceneNode* node);
|
void addChild(SceneNode* node);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief removeChild removes the first iteration of node of the children list
|
|
||||||
*/
|
|
||||||
void removeChild(SceneNode* node);
|
void removeChild(SceneNode* node);
|
||||||
/**
|
|
||||||
* @brief setSceneTree is called when this node is added to a SceneTree
|
|
||||||
*
|
|
||||||
* @param tree
|
|
||||||
*/
|
|
||||||
void setSceneTree(SceneTree *tree);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<SceneNode*> m_children;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONTAINERNODE_H
|
#endif // CONTAINERNODE_H
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
#include <btBulletCollisionCommon.h>
|
#include <btBulletCollisionCommon.h>
|
||||||
#include <btBulletDynamicsCommon.h>
|
#include <btBulletDynamicsCommon.h>
|
||||||
#include "scenetree.h"
|
#include "scenetree.h"
|
||||||
#include "graphicalnode.h"
|
#include "scenenode.h"
|
||||||
|
|
||||||
void GibGeneratorNode::createGib(GraphicalNode* graphicalPart,
|
void GibGeneratorNode::createGib(SceneNode* graphicalPart,
|
||||||
btCollisionShape *physicsShape,
|
btCollisionShape *physicsShape,
|
||||||
float mass,
|
float mass,
|
||||||
const glm::vec3 &pos,
|
const glm::vec3 &pos,
|
||||||
|
@ -7,17 +7,17 @@
|
|||||||
|
|
||||||
class btCollisionShape;
|
class btCollisionShape;
|
||||||
class btRigidBody;
|
class btRigidBody;
|
||||||
class GraphicalNode;
|
class SceneNode;
|
||||||
|
|
||||||
class GibGeneratorNode : public ContainerNode
|
class GibGeneratorNode : public ContainerNode
|
||||||
{
|
{
|
||||||
struct Gib
|
struct Gib
|
||||||
{
|
{
|
||||||
btRigidBody* body;
|
btRigidBody* body;
|
||||||
GraphicalNode* graphics;
|
SceneNode* graphics;
|
||||||
unsigned int expiration;
|
unsigned int expiration;
|
||||||
|
|
||||||
Gib(btRigidBody* b, GraphicalNode* g, unsigned int e) :
|
Gib(btRigidBody* b, SceneNode* g, unsigned int e) :
|
||||||
body(b),
|
body(b),
|
||||||
graphics(g),
|
graphics(g),
|
||||||
expiration(e)
|
expiration(e)
|
||||||
@ -32,7 +32,7 @@ class GibGeneratorNode : public ContainerNode
|
|||||||
std::vector<Gib*> m_gibs;
|
std::vector<Gib*> m_gibs;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void createGib(GraphicalNode* graphicalPart,
|
void createGib(SceneNode* graphicalPart,
|
||||||
btCollisionShape *physicsShape,
|
btCollisionShape *physicsShape,
|
||||||
float masses,
|
float masses,
|
||||||
const glm::vec3 &pos = glm::vec3(0),
|
const glm::vec3 &pos = glm::vec3(0),
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
#include "graphicalcontainernode.h"
|
|
||||||
#include "scenetree.h"
|
|
||||||
|
|
||||||
GraphicalContainerNode::~GraphicalContainerNode()
|
|
||||||
{
|
|
||||||
setSceneTree(nullptr);
|
|
||||||
for(GraphicalNode* child : m_children)
|
|
||||||
delete child;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalContainerNode::update()
|
|
||||||
{
|
|
||||||
for(GraphicalNode* node : m_nodesToRemove)
|
|
||||||
{
|
|
||||||
for(auto it = m_children.begin(); it != m_children.end();)
|
|
||||||
{
|
|
||||||
if(*it == node)
|
|
||||||
{
|
|
||||||
it = m_children.erase(it);
|
|
||||||
node->setSceneTree(nullptr);
|
|
||||||
node->setParent(nullptr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_nodesToRemove.clear();
|
|
||||||
|
|
||||||
for(GraphicalNode* node : m_nodesToAdd)
|
|
||||||
{
|
|
||||||
if(m_scene != nullptr)
|
|
||||||
node->setSceneTree(m_scene);
|
|
||||||
m_children.push_back(node);
|
|
||||||
node->setParent(this);
|
|
||||||
node->setParentTransform(m_transform);
|
|
||||||
node->setParentVisible(isVisible());
|
|
||||||
}
|
|
||||||
m_nodesToAdd.clear();
|
|
||||||
|
|
||||||
if(m_transformChanged)
|
|
||||||
m_combinedTransform = m_transform * m_parentTransform;
|
|
||||||
for(GraphicalNode* child : m_children)
|
|
||||||
{
|
|
||||||
if(m_transformChanged)
|
|
||||||
child->setParentTransform(m_combinedTransform);
|
|
||||||
child->update();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalContainerNode::setSceneTree(SceneTree* tree)
|
|
||||||
{
|
|
||||||
SceneNode::setSceneTree(tree);
|
|
||||||
for(GraphicalNode* child : m_children)
|
|
||||||
child->setSceneTree(tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalContainerNode::addChild(GraphicalNode *node)
|
|
||||||
{
|
|
||||||
if(node != nullptr)
|
|
||||||
m_nodesToAdd.push_back(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalContainerNode::removeChild(GraphicalNode *node)
|
|
||||||
{
|
|
||||||
if(node != nullptr)
|
|
||||||
m_nodesToRemove.push_back(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalContainerNode::updateVisibility(bool visible)
|
|
||||||
{
|
|
||||||
for(GraphicalNode* child : m_children)
|
|
||||||
child->setParentVisible(visible);
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
#ifndef GRAPHICALCONTAINERNODE_H
|
|
||||||
#define GRAPHICALCONTAINERNODE_H
|
|
||||||
|
|
||||||
#include "graphicalnode.h"
|
|
||||||
#include "glm/vec2.hpp"
|
|
||||||
#include "glm/mat4x4.hpp"
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class GraphicalContainerNode : public GraphicalNode
|
|
||||||
{
|
|
||||||
std::vector<GraphicalNode*> m_nodesToRemove;
|
|
||||||
std::vector<GraphicalNode*> m_nodesToAdd;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<GraphicalNode*> m_children;
|
|
||||||
glm::mat4 m_combinedTransform;
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual ~GraphicalContainerNode();
|
|
||||||
|
|
||||||
virtual void update();
|
|
||||||
|
|
||||||
virtual void setSceneTree(SceneTree* tree);
|
|
||||||
|
|
||||||
virtual void updateVisibility(bool visible);
|
|
||||||
|
|
||||||
void addChild(GraphicalNode* node);
|
|
||||||
void removeChild(GraphicalNode* node);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // GRAPHICALCONTAINERNODE_H
|
|
@ -1,138 +0,0 @@
|
|||||||
#include "graphicalnode.h"
|
|
||||||
#include "scenetree.h"
|
|
||||||
#include "glm/ext.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
void GraphicalNode::setSceneTree(SceneTree *tree)
|
|
||||||
{
|
|
||||||
if(isVisible())
|
|
||||||
{
|
|
||||||
if(m_scene != nullptr)
|
|
||||||
m_scene->removeFromIndex(this);
|
|
||||||
if(tree != nullptr)
|
|
||||||
tree->addToIndex(this);
|
|
||||||
}
|
|
||||||
m_scene = tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::resetTransform()
|
|
||||||
{
|
|
||||||
setTransform(glm::mat4());
|
|
||||||
}
|
|
||||||
|
|
||||||
// tools
|
|
||||||
void GraphicalNode::moveTo(const glm::vec3 &position)
|
|
||||||
{
|
|
||||||
m_transform = glm::translate(glm::mat4(), position);
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::translate(const glm::vec3 &vector)
|
|
||||||
{
|
|
||||||
m_transform = glm::translate(m_transform, vector);
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::lookAt(const glm::vec3 &target, const glm::vec3 &upVector)
|
|
||||||
{
|
|
||||||
glm::vec3 pos = glm::vec3(m_transform[3]);
|
|
||||||
m_transform = glm::lookAt(pos, target, upVector);
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::rotate(float angle, const glm::vec3 &vector)
|
|
||||||
{
|
|
||||||
m_transform = glm::rotate(m_transform, angle, vector);
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::scale(const glm::vec3 &scaleFactor)
|
|
||||||
{
|
|
||||||
m_transform = glm::scale(m_transform, scaleFactor);
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::moveTo2D(const glm::vec2 &position)
|
|
||||||
{
|
|
||||||
setTransform(glm::translate(m_transform,glm::vec3(position.x,position.y,0) - glm::vec3(m_transform[3])));
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::resize2D(const glm::vec2 oldDimension, glm::vec2 newDimension)
|
|
||||||
{
|
|
||||||
scale2D(glm::vec2(newDimension / oldDimension));
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::scale2D(const glm::vec2 scaleFactor)
|
|
||||||
{
|
|
||||||
setTransform(glm::scale(m_transform, glm::vec3(scaleFactor.x,scaleFactor.y,1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::rotate2D(const glm::vec2 ¢er, float angle)
|
|
||||||
{
|
|
||||||
setTransform(glm::rotate(m_transform,angle,glm::vec3(0,0,1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// setters
|
|
||||||
void GraphicalNode::setTransform(const glm::mat4 &transform)
|
|
||||||
{
|
|
||||||
m_transform = transform;
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// could be optimised by just storing a pointer, but unexpected behavior may happen ?
|
|
||||||
void GraphicalNode::setParentTransform(const glm::mat4 &transform)
|
|
||||||
{
|
|
||||||
m_parentTransform = transform;
|
|
||||||
m_transformChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::toggleVisibility()
|
|
||||||
{
|
|
||||||
setVisible(!m_visible);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::setVisible(bool visible)
|
|
||||||
{
|
|
||||||
if(m_parentVisible)
|
|
||||||
{
|
|
||||||
if(m_visible && !visible)
|
|
||||||
updateVisibility(false);
|
|
||||||
if(visible && !m_visible)
|
|
||||||
updateVisibility(true);
|
|
||||||
}
|
|
||||||
m_visible = visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::setParentVisible(bool visible)
|
|
||||||
{
|
|
||||||
if(m_visible)
|
|
||||||
{
|
|
||||||
if(m_parentVisible && !visible)
|
|
||||||
updateVisibility(false);
|
|
||||||
if(visible && !m_parentVisible)
|
|
||||||
updateVisibility(true);
|
|
||||||
}
|
|
||||||
m_parentVisible = visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::updateVisibility(bool visible)
|
|
||||||
{
|
|
||||||
if(!m_scene)
|
|
||||||
return;
|
|
||||||
if(visible)
|
|
||||||
m_scene->addToIndex(this);
|
|
||||||
else
|
|
||||||
m_scene->removeFromIndex(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::SparrowMotionState::getWorldTransform(btTransform& worldTrans ) const
|
|
||||||
{
|
|
||||||
worldTrans.setFromOpenGLMatrix(glm::value_ptr(m_node->getTransform()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphicalNode::SparrowMotionState::setWorldTransform(const btTransform& worldTrans)
|
|
||||||
{
|
|
||||||
glm::mat4 t;
|
|
||||||
worldTrans.getOpenGLMatrix(glm::value_ptr(t));
|
|
||||||
m_node->setTransform(t);
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
#ifndef GRAPHICALNODE_H
|
|
||||||
#define GRAPHICALNODE_H
|
|
||||||
|
|
||||||
#include "scenenode.h"
|
|
||||||
#include <glm/mat4x4.hpp>
|
|
||||||
#include <glm/vec3.hpp>
|
|
||||||
#include "LinearMath/btMotionState.h"
|
|
||||||
|
|
||||||
class SceneTree;
|
|
||||||
|
|
||||||
class GraphicalNode : public SceneNode
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
class SparrowMotionState : public btMotionState
|
|
||||||
{
|
|
||||||
GraphicalNode* m_node;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SparrowMotionState(GraphicalNode* node) : m_node(node) {}
|
|
||||||
|
|
||||||
virtual void getWorldTransform(btTransform& worldTrans ) const;
|
|
||||||
|
|
||||||
//Bullet only calls the update of worldtransform for active objects
|
|
||||||
virtual void setWorldTransform(const btTransform& worldTrans);
|
|
||||||
};
|
|
||||||
|
|
||||||
protected:
|
|
||||||
// m_parentTransform is the base transform for this element
|
|
||||||
glm::mat4 m_parentTransform;
|
|
||||||
// m_transform is the relative transformation matrix of this node
|
|
||||||
glm::mat4 m_transform;
|
|
||||||
|
|
||||||
bool m_parentVisible;
|
|
||||||
bool m_visible;
|
|
||||||
|
|
||||||
bool m_transformChanged;
|
|
||||||
|
|
||||||
SparrowMotionState m_motionState;
|
|
||||||
|
|
||||||
virtual void updateVisibility(bool visible);
|
|
||||||
|
|
||||||
public:
|
|
||||||
GraphicalNode(bool visible = true) : m_parentVisible(true), m_visible(visible), m_transformChanged(true), m_motionState(this) {}
|
|
||||||
virtual ~GraphicalNode() { setVisible(false); }
|
|
||||||
|
|
||||||
virtual void setSceneTree(SceneTree* tree);
|
|
||||||
|
|
||||||
// transform methods
|
|
||||||
void setTransform(const glm::mat4 &transform);
|
|
||||||
const glm::mat4& getTransform() { return m_transform; }
|
|
||||||
void setParentTransform(const glm::mat4 &transform);
|
|
||||||
const glm::mat4& getParentTransform() { return m_parentTransform; }
|
|
||||||
void resetTransform();
|
|
||||||
|
|
||||||
// visibility methods
|
|
||||||
bool isVisible(){return m_parentVisible && m_visible;}
|
|
||||||
void toggleVisibility();
|
|
||||||
void setVisible(bool visible);
|
|
||||||
void setParentVisible(bool visible);
|
|
||||||
|
|
||||||
// transformation tools :
|
|
||||||
void moveTo(const glm::vec3 &position);
|
|
||||||
void translate(const glm::vec3 &vector);
|
|
||||||
void lookAt(const glm::vec3 &target, const glm::vec3 &upVector = glm::vec3(0, 1, 0));
|
|
||||||
void rotate(float angle, const glm::vec3 &vector);
|
|
||||||
void scale(const glm::vec3 &scaleFactor);
|
|
||||||
|
|
||||||
//2D tools:
|
|
||||||
void moveTo2D(const glm::vec2 &position);
|
|
||||||
void rotate2D(const glm::vec2 ¢er, float angle);
|
|
||||||
void scale2D(const glm::vec2 scaleFactor);
|
|
||||||
void resize2D(const glm::vec2 oldDimension, glm::vec2 newDimension);
|
|
||||||
|
|
||||||
// this is used to synchronize a bullet rigidbody's transform with a GraphicalNode transform
|
|
||||||
SparrowMotionState* getMotionState() { return &m_motionState; }
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // GRAPHICALNODE_H
|
|
@ -14,5 +14,5 @@ glm::vec2 GUINode::getPosition(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GUINode::update(){
|
void GUINode::update(){
|
||||||
GraphicalContainerNode::update();
|
ContainerNode::update();
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#ifndef GUINODE_H
|
#ifndef GUINODE_H
|
||||||
#define GUINODE_H
|
#define GUINODE_H
|
||||||
|
|
||||||
#include "scene/graphicalcontainernode.h"
|
#include "scene/containernode.h"
|
||||||
|
|
||||||
class GUINode : public GraphicalContainerNode
|
class GUINode : public ContainerNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GUINode();
|
GUINode();
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
void LightNode::setSceneTree(SceneTree *tree)
|
void LightNode::setSceneTree(SceneTree *tree)
|
||||||
{
|
{
|
||||||
GraphicalNode::setSceneTree(tree);
|
SceneNode::setSceneTree(tree);
|
||||||
if(m_scene != nullptr)
|
if(m_scene != nullptr)
|
||||||
m_scene->registerLightType(m_light->getFlags());
|
m_scene->registerLightType(m_light->getFlags());
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#ifndef LIGHTNODE_H
|
#ifndef LIGHTNODE_H
|
||||||
#define LIGHTNODE_H
|
#define LIGHTNODE_H
|
||||||
|
|
||||||
#include "graphicalnode.h"
|
#include "scenenode.h"
|
||||||
#include "glm/mat4x4.hpp"
|
#include "glm/mat4x4.hpp"
|
||||||
#include "SparrowRenderer/scene.h"
|
#include "SparrowRenderer/scene.h"
|
||||||
|
|
||||||
class LightNode : public GraphicalNode
|
class LightNode : public SceneNode
|
||||||
{
|
{
|
||||||
Light *m_light;
|
Light *m_light;
|
||||||
public:
|
public:
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
void MeshNode::setSceneTree(SceneTree *tree)
|
void MeshNode::setSceneTree(SceneTree *tree)
|
||||||
{
|
{
|
||||||
GraphicalNode::setSceneTree(tree);
|
SceneNode::setSceneTree(tree);
|
||||||
if(m_scene != nullptr)
|
if(m_scene != nullptr)
|
||||||
m_scene->registerMeshType(m_geometry.mesh->getFlags());
|
m_scene->registerMeshType(m_geometry.mesh->getFlags());
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef MESHNODE_H
|
#ifndef MESHNODE_H
|
||||||
#define MESHNODE_H
|
#define MESHNODE_H
|
||||||
|
|
||||||
#include "graphicalnode.h"
|
#include "scenenode.h"
|
||||||
#include "glm/mat4x4.hpp"
|
#include "glm/mat4x4.hpp"
|
||||||
#include "SparrowRenderer/scene.h"
|
#include "SparrowRenderer/scene.h"
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ class btRigidBody;
|
|||||||
* @brief The MeshNode class holds a mesh
|
* @brief The MeshNode class holds a mesh
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class MeshNode : public GraphicalNode
|
class MeshNode : public SceneNode
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
GeometryNode m_geometry;
|
GeometryNode m_geometry;
|
||||||
@ -24,7 +24,7 @@ protected:
|
|||||||
public:
|
public:
|
||||||
// WARNING ! this class doesn't handle the destruction of the rendered mesh and the eventually generated rigidbody
|
// WARNING ! this class doesn't handle the destruction of the rendered mesh and the eventually generated rigidbody
|
||||||
// this behaviour allows the use of the same meshes in multiple nodes
|
// this behaviour allows the use of the same meshes in multiple nodes
|
||||||
MeshNode(Mesh* mesh, bool visible = true) : GraphicalNode(visible), m_geometry(mesh, glm::mat4()), m_rigidBody(nullptr) {}
|
MeshNode(Mesh* mesh, bool visible = true) : SceneNode(visible), m_geometry(mesh, glm::mat4()), m_rigidBody(nullptr) {}
|
||||||
|
|
||||||
virtual void setSceneTree(SceneTree* tree);
|
virtual void setSceneTree(SceneTree* tree);
|
||||||
|
|
||||||
|
@ -27,12 +27,12 @@ void MusicNode::stop()
|
|||||||
m_music.stop();
|
m_music.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MusicNode::setPlayingOffset(int offset)
|
void MusicNode::seek(int offset)
|
||||||
{
|
{
|
||||||
m_music.setPlayingOffset(sf::seconds(offset));
|
m_music.setPlayingOffset(sf::seconds(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MusicNode::setLoop(bool l)
|
void MusicNode::loop(bool l)
|
||||||
{
|
{
|
||||||
m_music.setLoop(l);
|
m_music.setLoop(l);
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ public:
|
|||||||
void play();
|
void play();
|
||||||
void pause();
|
void pause();
|
||||||
void stop();
|
void stop();
|
||||||
void setPlayingOffset(int offset);
|
void seek(int offset);
|
||||||
void setLoop(bool l);
|
void loop(bool l);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MUSICNODE_H
|
#endif // MUSICNODE_H
|
||||||
|
@ -1,15 +1,5 @@
|
|||||||
#include "objectnode.h"
|
#include "objectnode.h"
|
||||||
|
#include "tools/transform.h"
|
||||||
glm::mat4 Transform::getMatrix() const
|
|
||||||
{
|
|
||||||
return glm::toMat4(m_rotation) * glm::translate(glm::mat4(), m_position);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Transform::setFromMatrix(const glm::mat4& mat)
|
|
||||||
{
|
|
||||||
m_position = glm::vec3(mat[3]);
|
|
||||||
m_rotation = glm::conjugate(glm::toQuat(mat));
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectNode::ObjectNode()
|
ObjectNode::ObjectNode()
|
||||||
{
|
{
|
||||||
@ -26,7 +16,6 @@ void ObjectNode::update()
|
|||||||
// TODO : lua update
|
// TODO : lua update
|
||||||
|
|
||||||
// children update
|
// children update
|
||||||
m_graphics.update();
|
|
||||||
ContainerNode::update();
|
ContainerNode::update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,18 +23,3 @@ void ObjectNode::gui()
|
|||||||
{
|
{
|
||||||
// imgui editor
|
// imgui editor
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectNode::luaBind()
|
|
||||||
{
|
|
||||||
// TODO do lua bindings
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectNode::getTransform(Transform& t)
|
|
||||||
{
|
|
||||||
t.setFromMatrix(m_graphics.getTransform());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectNode::setTransform(const Transform& t)
|
|
||||||
{
|
|
||||||
m_graphics.setTransform(t.getMatrix());
|
|
||||||
}
|
|
||||||
|
@ -2,47 +2,27 @@
|
|||||||
#define OBJECTNODE_H
|
#define OBJECTNODE_H
|
||||||
|
|
||||||
#include "containernode.h"
|
#include "containernode.h"
|
||||||
#include "graphicalcontainernode.h"
|
|
||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
#include <glm/ext.hpp>
|
#include <glm/ext.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class GraphicalContainerNode;
|
struct Transform;
|
||||||
|
|
||||||
struct Transform
|
class ContainerNode;
|
||||||
{
|
|
||||||
glm::vec3 m_position;
|
|
||||||
glm::quat m_rotation;
|
|
||||||
|
|
||||||
void getPosition(float* pos) { memcpy(pos, glm::value_ptr(m_position), sizeof(float)*3); }
|
|
||||||
void setPosition(const float* pos) { memcpy(glm::value_ptr(m_position), pos, sizeof(float)*3); }
|
|
||||||
void getRotation(float* rot) { memcpy(rot, glm::value_ptr(m_rotation), sizeof(float)*4); }
|
|
||||||
void setRotation(const float* rot) { memcpy(glm::value_ptr(m_rotation), rot, sizeof(float)*4); }
|
|
||||||
|
|
||||||
glm::mat4 getMatrix() const;
|
|
||||||
void setFromMatrix(const glm::mat4& mat);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ObjectNode : public ContainerNode
|
class ObjectNode : public ContainerNode
|
||||||
{
|
{
|
||||||
GraphicalContainerNode m_graphics;
|
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
||||||
std::string initScript;
|
|
||||||
std::string updateScript;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ObjectNode();
|
ObjectNode();
|
||||||
|
|
||||||
void init();
|
virtual void init();
|
||||||
|
|
||||||
void update();
|
virtual void update();
|
||||||
|
|
||||||
void gui();
|
void gui();
|
||||||
|
|
||||||
static void luaBind();
|
|
||||||
|
|
||||||
// LUA accessible methods
|
// LUA accessible methods
|
||||||
|
|
||||||
// instanciation
|
// instanciation
|
||||||
@ -50,8 +30,7 @@ public:
|
|||||||
ObjectNode* clone();
|
ObjectNode* clone();
|
||||||
|
|
||||||
// positionning
|
// positionning
|
||||||
void getTransform(Transform& t);
|
|
||||||
void setTransform(const Transform& t);
|
|
||||||
|
|
||||||
// physics
|
// physics
|
||||||
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
#include "scenenode.h"
|
|
||||||
#include "scenetree.h"
|
|
||||||
|
|
||||||
Engine &SceneNode::getEngine()
|
|
||||||
{
|
|
||||||
return m_scene->getEngine();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SceneNode::setParent(SceneNode* parent)
|
|
||||||
{
|
|
||||||
if(m_toDestroy && parent == nullptr)
|
|
||||||
delete this;
|
|
||||||
else
|
|
||||||
m_parent = parent;
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
#ifndef SCENENODE_H
|
|
||||||
#define SCENENODE_H
|
|
||||||
|
|
||||||
class SceneTree;
|
|
||||||
class Light;
|
|
||||||
class GeometryNode;
|
|
||||||
class Engine;
|
|
||||||
|
|
||||||
class SceneNode
|
|
||||||
{
|
|
||||||
bool m_toDestroy;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Engine& getEngine();
|
|
||||||
SceneNode* m_parent;
|
|
||||||
SceneTree* m_scene;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SceneNode() : m_toDestroy(false), m_parent(nullptr), m_scene(nullptr) {}
|
|
||||||
|
|
||||||
// bool m_enabled;
|
|
||||||
virtual void update() = 0;
|
|
||||||
virtual Light* getLight() {return nullptr;}
|
|
||||||
virtual GeometryNode* getGeometryNode() {return nullptr;}
|
|
||||||
virtual void setSceneTree(SceneTree* tree) {m_scene = tree;}
|
|
||||||
virtual void setParent(SceneNode* parent);
|
|
||||||
virtual SceneNode* getParent() { return m_parent; }
|
|
||||||
// virtual void toggleNode(){m_enabled = !m_enabled;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief killWhenOrphan this method can be called if you wish to delete this object as soon as it has no parent.
|
|
||||||
*/
|
|
||||||
virtual void destroyWhenOrphan() { m_toDestroy = true; }
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // SCENENODE_H
|
|
@ -7,7 +7,6 @@
|
|||||||
// Scene
|
// Scene
|
||||||
#include <SparrowRenderer/mesh.h>
|
#include <SparrowRenderer/mesh.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "scene/scenenode.h"
|
|
||||||
#include "scene/cameranode.h"
|
#include "scene/cameranode.h"
|
||||||
|
|
||||||
#include <btBulletDynamicsCommon.h>
|
#include <btBulletDynamicsCommon.h>
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#ifndef SCRIPTNODE_H
|
#ifndef SCRIPTNODE_H
|
||||||
#define SCRIPTNODE_H
|
#define SCRIPTNODE_H
|
||||||
|
|
||||||
#include "scene/graphicalnode.h"
|
#include "scene/scenenode.h"
|
||||||
#include "sol.hpp"
|
#include "sol.hpp"
|
||||||
|
|
||||||
class ScriptNode : public GraphicalNode
|
class ScriptNode : public SceneNode
|
||||||
{
|
{
|
||||||
sol::state m_script;
|
sol::state m_script;
|
||||||
std::vector<std::string> functions_name;
|
std::vector<std::string> functions_name;
|
||||||
|
@ -22,7 +22,7 @@ public:
|
|||||||
setVisible(true);
|
setVisible(true);
|
||||||
moveTo2D(glm::vec2(0));
|
moveTo2D(glm::vec2(0));
|
||||||
}
|
}
|
||||||
GraphicalNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];}
|
SceneNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];}
|
||||||
void push(TextNode*);
|
void push(TextNode*);
|
||||||
unsigned int size(){return m_children.size();}
|
unsigned int size(){return m_children.size();}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "glm/vec2.hpp"
|
#include "glm/vec2.hpp"
|
||||||
|
|
||||||
#include "scene/scenetree.h"
|
#include "scene/scenetree.h"
|
||||||
#include "scene/graphicalcontainernode.h"
|
#include "scene/containernode.h"
|
||||||
|
|
||||||
#include "scene/gui/guinode.h"
|
#include "scene/gui/guinode.h"
|
||||||
#include "scene/gui/callback.h"
|
#include "scene/gui/callback.h"
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
#include <scene/textnode.h>
|
#include <scene/textnode.h>
|
||||||
#include <scene/playercharacternode.h>
|
#include <scene/playercharacternode.h>
|
||||||
#include <scene/lightnode.h>
|
#include <scene/lightnode.h>
|
||||||
#include <scene/graphicalcontainernode.h>
|
#include <scene/containernode.h>
|
||||||
#include <tools/graph.h>
|
#include <tools/graph.h>
|
||||||
#include <tools/pathfinder.h>
|
#include <tools/pathfinder.h>
|
||||||
#include <tools/loader.h>
|
#include <tools/loader.h>
|
||||||
@ -84,7 +84,7 @@ public:
|
|||||||
|
|
||||||
void generateTerrain(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
void generateTerrain(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
||||||
{
|
{
|
||||||
GraphicalContainerNode* terrainContainer = new GraphicalContainerNode();
|
ContainerNode* terrainContainer = new ContainerNode();
|
||||||
scene->getRootObject()->addChild(terrainContainer);
|
scene->getRootObject()->addChild(terrainContainer);
|
||||||
TestGen gen;
|
TestGen gen;
|
||||||
PBRMaterial *mat = new PBRMaterial();
|
PBRMaterial *mat = new PBRMaterial();
|
||||||
@ -115,7 +115,7 @@ void generateTerrain(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
|||||||
|
|
||||||
void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
||||||
{
|
{
|
||||||
GraphicalContainerNode* sponzaContainer = new GraphicalContainerNode();
|
ContainerNode* sponzaContainer = new ContainerNode();
|
||||||
scene->getRootObject()->addChild(sponzaContainer);
|
scene->getRootObject()->addChild(sponzaContainer);
|
||||||
Loader::setTexDirectory("data/sponza/");
|
Loader::setTexDirectory("data/sponza/");
|
||||||
std::vector<Mesh*> meshes = Loader::loadMesh("sponza.obj");
|
std::vector<Mesh*> meshes = Loader::loadMesh("sponza.obj");
|
||||||
@ -236,7 +236,7 @@ public:
|
|||||||
else if(m_config->scene == "sandbox")
|
else if(m_config->scene == "sandbox")
|
||||||
{
|
{
|
||||||
sun->initShadowMap(4096);
|
sun->initShadowMap(4096);
|
||||||
GraphicalContainerNode* sandboxContainer = new GraphicalContainerNode();
|
ContainerNode* sandboxContainer = new ContainerNode();
|
||||||
scene->getRootObject()->addChild(sandboxContainer);
|
scene->getRootObject()->addChild(sandboxContainer);
|
||||||
std::vector<Mesh*> meshes = Loader::loadMesh("sandbox.obj");
|
std::vector<Mesh*> meshes = Loader::loadMesh("sandbox.obj");
|
||||||
for(Mesh* m : meshes)
|
for(Mesh* m : meshes)
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include "tools/loader.h"
|
#include "tools/loader.h"
|
||||||
#include "tools/loadingthread.h"
|
#include "tools/loadingthread.h"
|
||||||
#include "SparrowRenderer/texture.h"
|
#include "SparrowRenderer/texture.h"
|
||||||
#include "scene/graphicalcontainernode.h"
|
#include "scene/containernode.h"
|
||||||
#include "resourcemanager.h"
|
#include "resourcemanager.h"
|
||||||
#include <imgui/imgui.h>
|
#include <imgui/imgui.h>
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ void Potator::throwShield()
|
|||||||
}
|
}
|
||||||
float throwForce = 5.f;
|
float throwForce = 5.f;
|
||||||
|
|
||||||
GraphicalContainerNode *node = new GraphicalContainerNode();
|
ContainerNode *node = new ContainerNode();
|
||||||
for(Mesh * m : m_shieldMeshes)
|
for(Mesh * m : m_shieldMeshes)
|
||||||
node->addChild(new MeshNode(m));
|
node->addChild(new MeshNode(m));
|
||||||
createGib(node, m_shieldShape, m_shieldMass, pos, dir*throwForce, 30000);
|
createGib(node, m_shieldShape, m_shieldMass, pos, dir*throwForce, 30000);
|
||||||
@ -107,7 +107,7 @@ void Potator::throwBottle()
|
|||||||
}
|
}
|
||||||
float throwForce = 5.f;
|
float throwForce = 5.f;
|
||||||
|
|
||||||
GraphicalContainerNode *node = new GraphicalContainerNode();
|
ContainerNode *node = new ContainerNode();
|
||||||
for(Mesh * m : m_bottleMeshes)
|
for(Mesh * m : m_bottleMeshes)
|
||||||
node->addChild(new MeshNode(m));
|
node->addChild(new MeshNode(m));
|
||||||
createGib(node, m_bottleShape, m_bottleMass, pos, dir*throwForce, 30000);
|
createGib(node, m_bottleShape, m_bottleMass, pos, dir*throwForce, 30000);
|
||||||
@ -124,7 +124,7 @@ void Potator::throwSword()
|
|||||||
}
|
}
|
||||||
float throwForce = 5.f;
|
float throwForce = 5.f;
|
||||||
|
|
||||||
GraphicalContainerNode *node = new GraphicalContainerNode();
|
ContainerNode *node = new ContainerNode();
|
||||||
for(Mesh * m : m_swordMeshes)
|
for(Mesh * m : m_swordMeshes)
|
||||||
node->addChild(new MeshNode(m));
|
node->addChild(new MeshNode(m));
|
||||||
createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 30000);
|
createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 30000);
|
||||||
|
12
src/tools/transform.cpp
Normal file
12
src/tools/transform.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "transform.h"
|
||||||
|
|
||||||
|
glm::mat4 Transform::getMatrix() const
|
||||||
|
{
|
||||||
|
return glm::toMat4(m_rotation) * glm::translate(glm::mat4(), m_position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Transform::setFromMatrix(const glm::mat4& mat)
|
||||||
|
{
|
||||||
|
m_position = glm::vec3(mat[3]);
|
||||||
|
m_rotation = glm::conjugate(glm::toQuat(mat));
|
||||||
|
}
|
24
src/tools/transform.h
Normal file
24
src/tools/transform.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef TRANSFORM_H
|
||||||
|
#define TRANSFORM_H
|
||||||
|
|
||||||
|
#include <glm/mat4x4.hpp>
|
||||||
|
#include <glm/vec3.hpp>
|
||||||
|
#include <glm/ext.hpp>
|
||||||
|
|
||||||
|
struct Transform
|
||||||
|
{
|
||||||
|
glm::vec3 m_position;
|
||||||
|
glm::quat m_rotation;
|
||||||
|
|
||||||
|
static createTransform();
|
||||||
|
|
||||||
|
void getPosition(float* pos) { memcpy(pos, glm::value_ptr(m_position), sizeof(float)*3); }
|
||||||
|
void setPosition(const float* pos) { memcpy(glm::value_ptr(m_position), pos, sizeof(float)*3); }
|
||||||
|
void getRotation(float* rot) { memcpy(rot, glm::value_ptr(m_rotation), sizeof(float)*4); }
|
||||||
|
void setRotation(const float* rot) { memcpy(glm::value_ptr(m_rotation), rot, sizeof(float)*4); }
|
||||||
|
|
||||||
|
glm::mat4 getMatrix() const;
|
||||||
|
void setFromMatrix(const glm::mat4& mat);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TRANSFORM_H
|
@ -4,6 +4,7 @@
|
|||||||
#include "scene/meshnode.h"
|
#include "scene/meshnode.h"
|
||||||
#include "iostream"
|
#include "iostream"
|
||||||
#include "sparrowshell/scriptnode.h"
|
#include "sparrowshell/scriptnode.h"
|
||||||
|
#include "transform.h"
|
||||||
|
|
||||||
std::vector<std::string> utils::split(const std::string &line, char sep){
|
std::vector<std::string> utils::split(const std::string &line, char sep){
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
@ -18,7 +19,11 @@ std::vector<std::string> utils::split(const std::string &line, char sep){
|
|||||||
|
|
||||||
void utils::initScriptingUtilsFunctions(ScriptNode* scriptNode)
|
void utils::initScriptingUtilsFunctions(ScriptNode* scriptNode)
|
||||||
{
|
{
|
||||||
|
scriptNode->getScriptEngine().new_usertype<Transform>("Transform",
|
||||||
|
"getPosition",&Transform::getPosition,
|
||||||
|
"setPosition",&Transform::setPosition,
|
||||||
|
"getRotation",&Transform::getRotation,
|
||||||
|
"setRotation",&Transform::setRotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void utils::initStandardScene()
|
void utils::initStandardScene()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user