SceneGraph.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class SceneGraph 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Matthew Jacobs (jacobs.mh@gmail.com) 00005 @date Fall 2008 00006 00007 @brief 00008 Bare-bones scene graph used during scene parsing 00009 <!-------------------------------------------------------------------->**/ 00010 00011 #ifndef SCENE_GRAPH_H_ 00012 #define SCENE_GRAPH_H_ 00013 00014 #include <materials/Material.h> 00015 #include <shapes/Shape.h> 00016 #include <stack> 00017 00018 struct JSONParseData; 00019 struct SceneNode; 00020 00021 DECLARE_STL_TYPEDEF(std::vector<SceneNode*>, SceneNodeList); 00022 typedef std::stack <SceneNode*> SceneNodeStack; 00023 00024 enum SceneNodeType { 00025 SCENE_NODE_TRANSFORM = (1 << 0), 00026 SCENE_NODE_MATERIAL = (1 << 1), 00027 SCENE_NODE_SHAPE = (1 << 2), 00028 SCENE_NODE_INSTANCE = (1 << 3) 00029 }; 00030 00031 struct SceneNode { 00032 SceneNodeList children; 00033 SceneNodeType type; 00034 std::string name; 00035 00036 inline explicit SceneNode(const SceneNodeType &type_) 00037 : type(type_) 00038 { } 00039 00040 virtual bool flatten(Matrix4x4 &transform, 00041 Matrix4x4 &transformInv, 00042 JSONParseData &data) = 0; 00043 00044 virtual ~SceneNode() { 00045 FOREACH(SceneNodeListIter, children, iter) { 00046 safeDelete(*iter); 00047 } 00048 } 00049 }; 00050 00051 struct SceneNodeTransform : public SceneNode { 00052 Matrix4x4 transform; 00053 Matrix4x4 translation; 00054 Matrix4x4 rotation; 00055 Matrix4x4 scale; 00056 00057 SceneNodeTransform() 00058 : SceneNode(SCENE_NODE_TRANSFORM), 00059 transform(Matrix4x4::identity()), translation(Matrix4x4::identity()), 00060 rotation(Matrix4x4::identity()), scale(Matrix4x4::identity()) 00061 { } 00062 00063 virtual bool flatten(Matrix4x4 &trans, 00064 Matrix4x4 &transInv, 00065 JSONParseData &data); 00066 00067 inline Matrix4x4 getTransform() { 00068 return transform * translation * rotation * scale; 00069 } 00070 }; 00071 00072 struct SceneNodeMaterial : public SceneNode { 00073 Material *material; 00074 00075 SceneNodeMaterial(Material *material_ = NULL) 00076 : SceneNode(SCENE_NODE_MATERIAL), material(material_) 00077 { } 00078 00079 virtual bool flatten(Matrix4x4 &transform, 00080 Matrix4x4 &transformInv, 00081 JSONParseData &data); 00082 }; 00083 00084 struct SceneNodeShape : public SceneNode { 00085 Shape *shape; 00086 00087 SceneNodeShape(Shape *shape_ = NULL) 00088 : SceneNode(SCENE_NODE_SHAPE), shape(shape_) 00089 { } 00090 00091 virtual bool flatten(Matrix4x4 &transform, 00092 Matrix4x4 &transformInv, 00093 JSONParseData &data); 00094 }; 00095 00096 struct SceneNodeInstance : public SceneNode { 00097 std::string instancee; 00098 SceneNodeType instanceeType; 00099 00100 SceneNodeInstance(const std::string &instancee_, 00101 const SceneNodeType &instanceeType_) 00102 : SceneNode(SCENE_NODE_INSTANCE), instancee(instancee_), 00103 instanceeType(instanceeType_) 00104 { } 00105 00106 virtual bool flatten(Matrix4x4 &transform, 00107 Matrix4x4 &transformInv, 00108 JSONParseData &data); 00109 }; 00110 00111 struct SceneGraph { 00112 SceneNodeStack sceneGraph; 00113 00114 virtual ~SceneGraph() { 00115 reset(); 00116 } 00117 00118 inline void beginNode(SceneNode *node) { 00119 push(node); 00120 } 00121 00122 /// removes top node from stack and adds it to children of new top of stack 00123 inline void endNode() { 00124 SceneNode *node = pop(); 00125 00126 top()->children.push_back(node); 00127 } 00128 00129 /// flattens this scenegraph into the given output data 00130 virtual bool flatten(JSONParseData &outData); 00131 00132 /// adds another stack frame 00133 inline void push(SceneNode *node) { 00134 ASSERT(node); 00135 sceneGraph.push(node); 00136 } 00137 00138 /// @returns the top stack frame 00139 inline SceneNode *top() { 00140 ASSERT(!empty()); 00141 SceneNode *t = sceneGraph.top(); 00142 00143 ASSERT(t); 00144 return t; 00145 } 00146 00147 /// @returns and removes the top stack frame 00148 inline SceneNode *pop() { 00149 ASSERT(!empty()); 00150 SceneNode *t = top(); 00151 sceneGraph.pop(); 00152 00153 return t; 00154 } 00155 00156 /// @returns whether or not this scenegraph is currently empty 00157 inline bool empty() { 00158 return sceneGraph.empty(); 00159 } 00160 00161 /// @returns the number of frames in the stack 00162 inline unsigned size() { 00163 return sceneGraph.size(); 00164 } 00165 00166 /// clears the stack and frees its resources 00167 void reset() { 00168 while(!empty()) { 00169 SceneNode *t = top(); 00170 00171 safeDelete(t); 00172 pop(); 00173 } 00174 00175 ASSERT(sceneGraph.empty()); 00176 } 00177 00178 virtual bool flatten(SceneNode *node, Matrix4x4 transform, 00179 Matrix4x4 transformInv, 00180 JSONParseData &data); 00181 }; 00182 00183 #endif // SCENE_GRAPH_H_ 00184
Generated on 28 Feb 2009 for Milton by
1.5.6