glfxscene.h

00001 /*-------------------------------------------------------------------------
00002 This source code is part of GLfx library (OpenGL effect library)
00003 For more information and updates see http://glfx.sourceforge.net/
00004 Copyright (C) 2005-6 Tomek Rozen <trozen at users sourceforge net>
00005 
00006 This library is free software; you can redistribute it and/or modify it
00007 under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License,
00009 or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful, but WITHOUT
00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00014 for more details.
00015 -------------------------------------------------------------------------*/
00016 
00017 #ifndef __GLFXSCENE_H__
00018 #define __GLFXSCENE_H__
00019 
00020 #include "glfxvec.h"
00021 
00022 #include <vector>
00023 #include <map>
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning(push)
00027 #pragma warning(disable: 4251) // 'member' : class 'class' needs to have dll-interface to be used by clients of class 'class'
00028 #endif
00029 
00030 namespace GLFX {
00032 namespace Scene {
00033 
00035 class Node
00036 {
00037 public:
00038 
00039     enum {
00041         TYPE_NODE=0,
00043         TYPE_STATIC_MESH,
00045         TYPE_CAMERA,
00047         TYPE_LIGHT,
00049         TYPE_USER_NODE=1024
00050     };
00051 
00052     Node() : _dirty(true), _disabled(false), _parent(0), _nodeTM(1.0f) {
00053     }
00054 
00056     Node(const Node& node) {
00057         _dirty = true;
00058         _disabled = node._disabled;
00059         _parent = 0;
00060         _nodeTM = node._nodeTM;
00061         _name = node._name;
00062         for(unsigned long i = 0; i < GetChildCount(); i++) {
00063             AddChild(GetChild(i)->Clone());
00064         }
00065     }
00066 
00067     virtual ~Node() {
00068         for(int i = 0; i < (int)_children.size(); i++) {
00069             Node* child = _children[i];
00070             delete child;
00071         }
00072         _children.clear();
00073     }
00074 
00076     virtual Node* Clone() {
00077         return new Node(*this);
00078     }
00079 
00081     virtual int GetType() {
00082         return TYPE_NODE;
00083     }
00084 
00087     virtual void UpdateTM(bool clearDirty) {
00088         if(_dirty) {
00089             if(_parent) {
00090                 _worldTM = _nodeTM * _parent->GetWorldTM();
00091             } else {
00092                 _worldTM = _nodeTM;
00093             }
00094         }
00095         for(NodeVecIt i = _children.begin(); i != _children.end(); i++) {
00096             Node* node = *i;
00097             node->UpdateTM(clearDirty);
00098         }
00099         if(clearDirty) {
00100             _dirty = false;
00101         }
00102     }
00103     
00105     unsigned long GetChildCount() const {
00106         return (int)_children.size();
00107     }
00108 
00112     Node* GetChild(unsigned long num) const {
00113         assert(num >= 0 && num < (int)_children.size());
00114         return _children[num];
00115     }
00116 
00119     void RemoveChild(unsigned long num) {
00120         assert(num >= 0 && num < (int)_children.size());
00121         delete _children[num];
00122         _children.erase(_children.begin() + num);
00123     }
00124 
00128     Node* DetachChild(unsigned long num) {
00129         assert(num >= 0 && num < (int)_children.size());
00130         Node* child = _children[num];
00131         _children.erase(_children.begin() + num);
00132         child->SetParent(0);
00133         return child;
00134     }
00135 
00139     unsigned long AddChild(Node* node) {
00140         _children.push_back(node);
00141         SetDirty();
00142         node->SetParent(this);
00143         return (unsigned long)_children.size() - 1;
00144     }
00145 
00147     const char* GetName() const {
00148         return _name.c_str();
00149     }
00150 
00152     void SetName(const char* name) {
00153         _name = name;
00154     }
00155 
00157     Matrix4& GetTM() {
00158         return _nodeTM;
00159     }
00160 
00162     const Matrix4& GetTM() const {
00163         return _nodeTM;
00164     }
00165 
00167     void SetTM(const Matrix4& tm) {
00168         _nodeTM = tm;
00169         SetDirty();
00170     }
00171 
00173     void ResetLocalTM() {
00174         _nodeTM = 1.0f;
00175         _dirty = true;
00176         for(unsigned long i = 0; i < GetChildCount(); i++) {
00177             GetChild(i)->ResetLocalTM();
00178         }
00179     }
00180 
00182     const Matrix4& GetWorldTM() const {
00183         return _worldTM;
00184     }
00185 
00187     bool IsDirty() const {
00188         return _dirty;
00189     }
00190 
00192     void SetDirty() {
00193         _dirty = true;
00194     }
00195 
00197     bool IsDisabled() const {
00198         return _disabled;
00199     }
00200 
00202     void SetDisabled(bool disabled) {
00203         _disabled = disabled;
00204     }
00205 
00207     Node* GetParent() {
00208         return _parent;
00209     }
00210 
00211 protected:
00212 
00214     void SetParent(Node* node) {
00215         _parent = node;
00216         SetDirty();
00217     }
00218 
00219 private:
00220 
00221     typedef std::vector<Node*> NodeVec;
00222     typedef NodeVec::iterator NodeVecIt;
00223 
00224     bool _disabled;
00225     bool _dirty;
00226 
00227     Matrix4 _nodeTM;            // relative to parent
00228     Matrix4 _worldTM;           // absolute world transform
00229 
00230     std::string _name;
00231     Node* _parent;
00232     NodeVec _children;
00233 
00234 };
00235 
00237 class Material : public IObject
00238 {
00239 public:
00240 
00242     Material(EffectManager* fxMgr) : _fxMgr(fxMgr) {
00243         ambient = Vec4(0, 0, 0, 1);
00244         diffuse = Vec4(1, 1, 1, 1);
00245         specular = Vec4(1, 1, 1, 1);
00246         level = 0.0f;
00247         transparency = 0.0f;
00248         _diffuseTex = 0;
00249         _refCount = 1;
00250     }
00251 
00252     virtual ~Material() {
00253         DeleteTextures();
00254     }
00255 
00256     unsigned long AddRef() {
00257         return ++_refCount;
00258     }
00259 
00260     unsigned long Release() {
00261         _refCount--;
00262         if(_refCount == 0) {
00263             delete this;
00264             return 0;
00265         }
00266         return _refCount;
00267     }
00268 
00270     const char* GetName() {
00271         return _name.c_str();
00272     }
00273 
00275     void SetName(const char* name) {
00276         _name = name;
00277     }
00278 
00280     GLuint GetDiffuseTexture() {
00281         return _diffuseTex;
00282     }
00283 
00285     const char* GetDiffuseMap() {
00286         if(_diffuseMap.length() == 0) {
00287             return 0;
00288         }
00289         return _diffuseMap.c_str();
00290     }
00291 
00293     void SetDiffuseMap(const char* mapName) {
00294         _diffuseMap = mapName;
00295     }
00296 
00298     const char* GetGlossMap() {
00299         if(_glossMap.length() == 0) {
00300             return 0;
00301         }
00302         return _glossMap.c_str();
00303     }
00304 
00306     void SetGlossMap(const char* mapName) {
00307         _glossMap = mapName;
00308     }
00309 
00311     const char* GetGlowMap() {
00312         if(_glowMap.length() == 0) {
00313             return 0;
00314         }
00315         return _glowMap.c_str();
00316     }
00317 
00319     void SetGlowMap(const char* mapName) {
00320         _glowMap = mapName;
00321     }
00322 
00324     const char* GetBumpMap() {
00325         if(_bumpMap.length() == 0) {
00326             return 0;
00327         }
00328         return _bumpMap.c_str();
00329     }
00330 
00332     void SetBumpMap(const char* mapName) {
00333         _bumpMap = mapName;
00334     }
00335 
00337     virtual void LoadTextures() {
00338         if(_diffuseTex) {
00339             _fxMgr->DeleteTexture(_diffuseTex);
00340             _diffuseTex = 0;
00341         }
00342         const char* mapName = GetDiffuseMap();
00343         if(mapName) {
00344             _diffuseTex = _fxMgr->LoadTexture(GL_TEXTURE_2D, mapName);
00345         }
00346     }
00347 
00349     virtual void DeleteTextures() {
00350         if(_diffuseTex) {
00351             _fxMgr->DeleteTexture(_diffuseTex);
00352             _diffuseTex = 0;
00353         }
00354     }
00355 
00356 public:
00357 
00359     Vec4 ambient;
00361     Vec4 diffuse;
00363     Vec4 specular;
00365     float level;
00367     float transparency;
00368 
00369 private:
00370 
00371     unsigned long _refCount;
00372     EffectManager* _fxMgr;
00373     std::string _name;
00374     std::string _diffuseMap;
00375     std::string _glossMap;
00376     std::string _glowMap;
00377     std::string _bumpMap;
00378     GLuint _diffuseTex;
00379 };
00380 
00382 class MeshNode : public Node
00383 {
00384 public:
00385 
00386     MeshNode() {
00387         _mesh = 0;
00388     }
00389 
00391     MeshNode(const MeshNode& node) {
00392         _mesh = node._mesh;
00393         if(_mesh) {
00394             _mesh->AddRef();
00395         }
00396         _materials.resize(node._materials.size());
00397         for(size_t i = 0; i < _materials.size(); i++) {
00398             _materials[i] = node._materials[i];
00399             if(_materials[i]) {
00400                 _materials[i]->AddRef();
00401             }
00402         }
00403     }
00404 
00405     ~MeshNode() {
00406         if(_mesh) {
00407             _mesh->Release();
00408             _mesh = 0;
00409         }
00410         for(size_t i = 0; i < _materials.size(); i++) {
00411             if(_materials[i]) {
00412                 _materials[i]->Release();
00413                 _materials[i] = 0;
00414             }
00415         }
00416     }
00417 
00418     virtual Node* Clone() {
00419         return new MeshNode(*this);
00420     }
00421 
00422     virtual int GetType() {
00423         return TYPE_STATIC_MESH;
00424     }
00425 
00427     void SetMesh(IMesh* m) {
00428         if(_mesh) {
00429             _mesh->Release();
00430         }
00431         _mesh = m;
00432         if(_mesh) {
00433             _mesh->AddRef();
00434         }
00435     }
00436 
00438     IMesh* GetMesh() const {
00439         return _mesh;
00440     }
00441 
00443     void SetMaterialCount(int num) {
00444         _materials.resize(num);
00445         std::fill(_materials.begin(), _materials.end(), (Material*)0);
00446     }
00447 
00449     int GetMaterialCount() const {
00450         return (int)_materials.size();
00451     }
00452 
00454     void SetMaterial(int num, Material* material) {
00455         if(_materials[num]) {
00456             _materials[num]->Release();
00457             _materials[num] = 0;
00458         }
00459         _materials[num] = material;
00460         if(_materials[num]) {
00461             _materials[num]->AddRef();
00462         }
00463     }
00464 
00466     Material* GetMaterial(int num) const {
00467         return _materials[num];
00468     }
00469 
00470 private:
00471 
00472     IMesh* _mesh;
00473     std::vector<Material*> _materials;
00474 
00475 };
00476 
00478 class CameraNode : public Node
00479 {
00480 public:
00481 
00482     CameraNode() {
00483         _fovx = 60.0f;
00484     }
00485 
00487     CameraNode(const CameraNode& node) {
00488         _fovx = node._fovx;
00489     }
00490 
00491     virtual Node* Clone() {
00492         return new CameraNode(*this);
00493     }
00494 
00495     virtual int GetType() {
00496         return TYPE_CAMERA;
00497     }
00498 
00500     Matrix4 GetCameraTM() const {
00501         return Inverse(GetWorldTM());
00502     }
00503 
00505     float GetFOV() const {
00506         return _fovx;
00507     }
00508 
00510     void SetFOV(float fovx) {
00511         _fovx = fovx;
00512     }
00513 
00514 private:
00515 
00516     float _fovx;
00517 
00518 };
00519 
00521 class LightNode : public Node
00522 {
00523 public:
00524 
00525     LightNode() {
00526         _attnStart = 0.0f;
00527         _attnEnd = 0.0f;
00528         _color = Vec4(1, 1, 1, 1);
00529     }
00530 
00532     LightNode(const LightNode& node) {
00533         _attnStart = node._attnStart;
00534         _attnEnd = node._attnEnd;
00535         _color = node._color;
00536     }
00537 
00538     virtual Node* Clone() {
00539         return new LightNode(*this);
00540     }
00541 
00542     virtual int GetType() {
00543         return TYPE_LIGHT;
00544     }
00545 
00548     void SetColor(const Vec4& color) {
00549         _color = color;
00550     }
00551 
00553     const Vec4& GetColor() const {
00554         return _color;
00555     }
00556 
00560     void SetAttenuation(float start, float end) {
00561         _attnStart = start;
00562         _attnEnd = end;
00563     }
00564 
00566     float GetAttenuationStart() const {
00567         return _attnStart;
00568     }
00569 
00571     float GetAttenuationEnd() const {
00572         return _attnEnd;
00573     }
00574 
00575 private:
00576 
00577     Vec4 _color;
00578     float _attnStart;
00579     float _attnEnd;
00580 
00581 };
00582 
00584 struct Frame
00585 {
00587     Vec3 pos;
00589     Quat rot;
00591     Vec3 scl;
00592 
00594     bool IsRotationOnly() const {
00595         return pos.Equal(Vec3(0.0f)) && scl.Equal(Vec3(1.0f));
00596     }
00597 
00602     void Blend(Frame& result, const Frame& other, float otherWeight) const {
00603         result.pos = Lerp(pos, other.pos, otherWeight);
00604         result.rot = Slerp(rot, other.rot, otherWeight, true);
00605         result.scl = Lerp(scl, other.scl, otherWeight);
00606     }
00607 
00612     void BlendRotation(Quat& result, const Frame& other, float otherWeight) const {
00613         result = Slerp(rot, other.rot, otherWeight, true);
00614     }
00615 
00618     void ComputeTM(Matrix4& tm) const {
00619         tm = MatrixScaling(scl) *
00620             MatrixRotationQuaternion(rot) *
00621             MatrixTranslation(pos);
00622     }
00623 
00624 };
00625 
00627 class Animation
00628 {
00629 public:
00630 
00631     Animation() {
00632     }
00633 
00634     ~Animation() {
00635     }
00636 
00641     void SetLength(unsigned long frameCount, float startTime, float frequency) {
00642         _startTime = startTime;
00643         _frequency = frequency;
00644         _frameCount = frameCount;
00645     }
00646 
00649     void Initialize(unsigned long nodeCount) {
00650         _nodeCount = nodeCount;
00651         _frames.resize(_nodeCount * _frameCount);
00652         for(size_t i = 0; i < _frames.size(); i++) {
00653             _frames[i].pos = Vec3(0.0f);
00654             _frames[i].rot = Quat(0.0f);
00655             _frames[i].scl = Vec3(1.0f);
00656         }
00657     }
00658 
00661     void SetName(const char* name) {
00662         _name = name;
00663     }
00664 
00666     const char* GetName() const {
00667         return _name.c_str();
00668     }
00669 
00671     float GetStartTime() const {
00672         return _startTime;
00673     }
00674 
00676     float GetEndTime() const {
00677         return _startTime + (float)_frameCount / _frequency;
00678     }
00679 
00682     float GetLength() const {
00683         return (float)_frameCount / _frequency;
00684     }
00685 
00688     unsigned long GetFrameCount() const {
00689         return _frameCount;
00690     }
00691 
00694     unsigned long GetNodeCount() const {
00695         return _nodeCount;
00696     }
00697 
00702     Frame& GetFrame(unsigned long frame, unsigned long node) {
00703         return _frames[frame * _nodeCount + node];
00704     }
00705 
00710     const Frame& GetFrame(unsigned long frame, unsigned long node) const {
00711         return _frames[frame * _nodeCount + node];
00712     }
00713 
00717     void ApplyAnimation(Node* node, float time) const {
00718         node->ResetLocalTM();
00719         MultAnimation(node, time);
00720     }
00721 
00727     void ConvertTimeToFrameInterp(float time, unsigned long& frame,
00728         float& interp) const
00729     {
00730         long frameNum = (int)((time - _startTime) * _frequency);
00731         if(frameNum < 0) {
00732             frameNum = 0;
00733             interp = 0.0f;
00734         } else if(frameNum >= (int)_frameCount - 1) {
00735             frameNum = (int)_frameCount - 1;
00736             interp = 0.0f;
00737         } else {
00738             interp = fmod((time - _startTime) * _frequency, 1.0f);
00739             if(interp < 0.0f) {
00740                 interp += 1.0f;
00741             }
00742         }
00743         assert(frameNum >= 0);
00744         frame = (unsigned long)frameNum;
00745     }
00746 
00750     void MultAnimation(Node* node, float time) const {
00751         unsigned long nodeNum = 0;
00752         unsigned long frameNum;
00753         float interp;
00754         ConvertTimeToFrameInterp(time, frameNum, interp);
00755         MultAnimationR(node, frameNum, interp, nodeNum);
00756         assert(nodeNum == _nodeCount);
00757     }
00758 
00765     void ApplyAnimationBlend(Node* node, float time,
00766         const Animation* otherAnim, float otherTime, float otherWeight)
00767     {
00768         node->ResetLocalTM();
00769         MultAnimationBlend(node, time, otherAnim, otherTime, otherWeight);
00770     }
00771 
00778     void MultAnimationBlend(Node* node, float time,
00779         const Animation* otherAnim, float otherTime, float otherWeight)
00780     {
00781         unsigned long nodeNum = 0;
00782         unsigned long frameNum, otherFrameNum;
00783         float interp, otherInterp;
00784         ConvertTimeToFrameInterp(time, frameNum, interp);
00785         otherAnim->ConvertTimeToFrameInterp(otherTime, otherFrameNum, otherInterp);
00786         MultAnimationBlendR(node, frameNum, interp, otherAnim, otherFrameNum,
00787             otherInterp, otherWeight, nodeNum);
00788         assert(nodeNum == _nodeCount);
00789     }
00790 
00791 private:
00792 
00793     void ComputeFrame(Frame& frame, unsigned long frameNum, float interp,
00794         unsigned long nodeNum) const
00795     {
00796         if(interp == 0.0f) {
00797             frame = GetFrame(frameNum, nodeNum);
00798         } else {
00799             const Frame& frame1 = GetFrame(frameNum, nodeNum);
00800             const Frame& frame2 = GetFrame(frameNum + 1, nodeNum);
00801             frame1.Blend(frame, frame2, interp);
00802         }
00803     }
00804 
00805     void MultAnimationR(Node* node, unsigned long frameNum, float interp,
00806         unsigned long& nodeNum) const
00807     {
00808         Frame frame;
00809         ComputeFrame(frame, frameNum, interp, nodeNum);
00810         Matrix4 tm;
00811         frame.ComputeTM(tm);
00812         node->SetTM(node->GetTM() * tm);
00813         nodeNum++;
00814         for(unsigned long i = 0; i < node->GetChildCount(); i++) {
00815             Node* child = node->GetChild(i);
00816             MultAnimationR(child, frameNum, interp, nodeNum);
00817         }
00818     }
00819 
00820     void MultAnimationBlendR(Node* node, unsigned long frameNum, float interp,
00821         const Animation* otherAnim, unsigned long otherFrameNum,
00822         float otherInterp, float otherWeight, unsigned long& nodeNum) const
00823     {
00824         Frame frame, otherFrame, blendFrame;
00825         ComputeFrame(frame, frameNum, interp, nodeNum);
00826         otherAnim->ComputeFrame(otherFrame, otherFrameNum, otherInterp, nodeNum);
00827         frame.Blend(blendFrame, otherFrame, otherWeight);
00828         Matrix4 tm;
00829         blendFrame.ComputeTM(tm);
00830         node->SetTM(node->GetTM() * tm);
00831         nodeNum++;
00832         for(unsigned long i = 0; i < node->GetChildCount(); i++) {
00833             Node* child = node->GetChild(i);
00834             MultAnimationR(child, frameNum, interp, nodeNum);
00835         }
00836     }
00837 
00838     std::string _name;
00839     float _startTime;
00840     float _frequency;
00841     unsigned long _frameCount;
00842     unsigned long _nodeCount;
00843     std::vector<Frame> _frames;
00844 
00845 };
00846 
00848 class AnimationSet
00849 {
00850 public:
00851 
00852     AnimationSet() {
00853     }
00854 
00855     virtual ~AnimationSet() {
00856         for(AnimVecIt i = _anims.begin(); i != _anims.end(); i++) {
00857             delete *i;
00858         }
00859     }
00860 
00863     void AddAnimation(Animation* anim) {
00864         _anims.push_back(anim);
00865         _animMap[anim->GetName()] = anim;
00866     }
00867 
00869     unsigned long GetAnimationCount() const {
00870         return (unsigned long)_anims.size();
00871     }
00872 
00876     const Animation* GetAnimation(unsigned long number) const {
00877         return _anims[number];
00878     }
00879 
00883     const Animation* FindAnimationByName(const char* name) const {
00884         AnimMapCIt i = _animMap.find(name);
00885         if(i == _animMap.end()) {
00886             return 0;
00887         }
00888         return i->second;
00889     }
00890 
00891 private:
00892 
00893     struct cstr_less
00894     {
00895         bool operator()(const char* a, const char* b) const {
00896             return strcmp(a, b) < 0;
00897         }
00898     };
00899 
00900     typedef std::vector<Animation*> AnimVec;
00901     typedef AnimVec::iterator AnimVecIt;
00902     typedef std::map<const char*, Animation*, cstr_less> AnimMap;
00903     typedef AnimMap::const_iterator AnimMapCIt;
00904 
00905     AnimVec _anims;
00906     AnimMap _animMap;
00907 
00908 };
00909 
00910 #pragma pack(4)
00911 
00913 struct DefaultVertex
00914 {
00916     Vec3 position;
00918     Vec3 normal;
00920     Vec2 texture;
00921 
00923     typedef VertexFormat<VC_VERTEX_3F, VC_NORMAL_3F, VC_TEXTURE0_2F> Format;
00924 };
00925 
00926 #pragma pack()
00927 
00929 class GLFX_EXPORT DefaultSceneBuilder : public ISceneBuilder
00930 {
00931 public:
00932 
00933     DefaultSceneBuilder();
00934     virtual ~DefaultSceneBuilder();
00935 
00936     // public interface
00937     bool Initialize(EffectManager* fxMgr);
00938     bool Finalize();
00939     int CreateNode(const char* nodeName, int type);
00940     int CreateMaterial(const char* materialName);
00941     int CreateAnimation(const char* animName);
00942     void SetNodeParentByName(int nodeId, const char* parentName);
00943     void SetNodeTM(int nodeId, const float* tm);
00944     void SetMeshParameters(int nodeId, GLenum primitiveType, unsigned long vertexCount, unsigned long indexCount, unsigned long primitiveGroupCount);
00945     bool SetMeshVertexData(int nodeId, unsigned long type, unsigned long dim, const float* data);
00946     bool SetMeshFaceIndices(int nodeId, const unsigned long* data);
00947     void SetMeshPrimitiveGroup(int nodeId, unsigned long groupId, unsigned long start, unsigned long count);
00948     void SetMeshMaterial(int nodeId, unsigned long groupId, int materialId);
00949     void SetLightColor(int nodeId, const float* color);
00950     void SetLightAttenuation(int nodeId, float attnStart, float attnEnd);
00951     void SetCameraFieldOfViewX(int nodeId, float fovx);
00952     void SetMaterialAmbientColor(int materialId, const float* color);
00953     void SetMaterialDiffuseColor(int materialId, const float* color);
00954     void SetMaterialSpecularColor(int materialId, const float* color);
00955     void SetMaterialSpecularLevel(int materialId, float level);
00956     void SetMaterialTransparency(int materialId, float transparency);
00957     void SetMaterialDiffuseMap(int materialId, const char* name);
00958     void SetMaterialGlossMap(int materialId, const char* name);
00959     void SetMaterialGlowMap(int materialId, const char* name);
00960     void SetMaterialBumpMap(int materialId, const char* name);
00961     void SetAnimationParameters(int animId, float startTime, float frequency, unsigned long frameCount);
00962     void SetAnimationNodeKeyframes(int animId, int nodeId, unsigned long type, float* data);
00963 
00964     // specific members
00965 
00968     EffectManager* GetEffectManager();
00969 
00972     Node* GetRootNode();
00973 
00976     AnimationSet* GetAnimationSet();
00977 
00978 protected:
00979 
00984     virtual Node* AllocNode(int type) {
00985         switch(type) {
00986         case Node::TYPE_NODE:
00987             return new Node();
00988         case Node::TYPE_STATIC_MESH:
00989             return new MeshNode();
00990         case Node::TYPE_CAMERA:
00991             return new CameraNode();
00992         case Node::TYPE_LIGHT:
00993             return new LightNode();
00994         }
00995         return 0;
00996     }
00997 
01000     virtual void DeleteNode(Node* node) {
01001         delete node;
01002     }
01003 
01011     virtual IMesh* CreateMesh(GLenum primitiveType, unsigned long vertexCount,
01012         unsigned long indexCount, unsigned long primitiveGroupCount);
01013 
01015     virtual bool IsMeshNode(Node* node);
01016 
01018     virtual bool IsLightNode(Node* node);
01019 
01021     virtual bool IsCameraNode(Node* node);
01022 
01026     virtual Material* AllocMaterial() {
01027         return new Material(GetEffectManager());
01028     }
01029 
01033     virtual Animation* AllocAnimation() {
01034         return new Animation();
01035     }
01036 
01039     virtual void DeleteAnimation(Animation* anim) {
01040         delete anim;
01041     }
01042 
01046     virtual AnimationSet* AllocAnimationSet() {
01047         return new AnimationSet();
01048     }
01049 
01052     virtual void DeleteAnimationSet(AnimationSet* animSet) {
01053         delete animSet;
01054     }
01055 
01056 private:
01057 
01058     struct NodeAnim
01059     {
01060         std::vector<Vec3> pos;
01061         std::vector<Quat> rot;
01062         std::vector<Vec3> scl;
01063     };
01064 
01065     typedef std::map<int, NodeAnim*> NodeAnimMap;
01066 
01067     struct NodeInfo
01068     {
01069         Node* node;
01070         std::string nodeName;
01071         std::string parentName;
01072         bool topLevel;
01073         std::vector<char> vertexData;
01074         std::vector<int> mats;
01075         NodeAnimMap anims;
01076     };
01077 
01078     typedef std::vector<NodeInfo*> NodeVec;
01079     typedef NodeVec::iterator NodeVecIt;
01080     typedef std::map<std::string, NodeInfo*> NodeMap;
01081     typedef NodeMap::iterator NodeMapIt;
01082     typedef std::vector<Material*> MatVec;
01083     typedef MatVec::iterator MatVecIt;
01084     typedef std::vector<Animation*> AnimVec;
01085     typedef AnimVec::iterator AnimVecIt;
01086     typedef std::map<Node*, int> NodeIdMap;
01087 
01088     void _CreateNodeIdMapR(Node* node, int& id, NodeIdMap& idMap);
01089 
01090     int _refCount;
01091     EffectManager* _fxMgr;
01092     NodeVec _nodes;
01093     NodeMap _nodeMap;
01094     MatVec _mats;
01095     Node* _root;
01096     AnimationSet* _animSet;
01097     AnimVec _anims;
01098 
01099 };
01100 
01101 }}
01102 
01103 #ifdef _MSC_VER
01104 #pragma warning(pop)
01105 #endif
01106 
01107 #endif // __GLFXSCENE_H__

SourceForge.net Logo