Blender V2.61 - r43446

RAS_MaterialBucket.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #ifndef __RAS_MATERIALBUCKET
00033 #define __RAS_MATERIALBUCKET
00034 
00035 #include "RAS_TexVert.h"
00036 #include "CTR_Map.h"
00037 #include "STR_HashedString.h"
00038 #include "SG_QList.h"
00039 
00040 #include "MT_Transform.h"
00041 #include "RAS_IPolygonMaterial.h"
00042 #include "RAS_IRasterizer.h"
00043 #include "RAS_Deformer.h"
00044 
00045 #include <vector>
00046 #include <set>
00047 #include <list>
00048 using namespace std;
00049 
00050 /* Display List Slot */
00051 
00052 class KX_ListSlot
00053 {
00054 protected:
00055     int m_refcount;
00056 public:
00057     KX_ListSlot(){ m_refcount=1; }
00058     virtual ~KX_ListSlot() {}
00059     virtual int Release() { 
00060         if (--m_refcount > 0)
00061             return m_refcount;
00062         delete this;
00063         return 0;
00064     }
00065     virtual KX_ListSlot* AddRef() {
00066         m_refcount++;
00067         return this;
00068     }
00069     virtual void SetModified(bool mod)=0;
00070 };
00071 
00072 class RAS_DisplayArray;
00073 class RAS_MeshSlot;
00074 class RAS_MeshMaterial;
00075 class RAS_MaterialBucket;
00076 struct DerivedMesh;
00077 
00078 /* An array with data used for OpenGL drawing */
00079 
00080 class RAS_DisplayArray
00081 {
00082 public:
00083     vector<RAS_TexVert> m_vertex;
00084     vector<unsigned short> m_index;
00085     /* LINE currently isnt used */
00086     enum { LINE = 2, TRIANGLE = 3, QUAD = 4 } m_type;
00087     //RAS_MeshSlot *m_origSlot;
00088     
00089     /* Number of RAS_MeshSlot using this array */
00090     int m_users;
00091 
00092     enum { BUCKET_MAX_INDEX = 65535 };
00093     enum { BUCKET_MAX_VERTEX = 65535 };
00094 };
00095 
00096 /* Entry of a RAS_MeshObject into RAS_MaterialBucket */
00097 typedef std::vector<RAS_DisplayArray*>  RAS_DisplayArrayList;
00098 
00099 // The QList is used to link the mesh slots to the object
00100 // The DList is used to link the visible mesh slots to the material bucket
00101 class RAS_MeshSlot : public SG_QList
00102 {
00103     friend class RAS_ListRasterizer;
00104 private:
00105     //  indices into display arrays
00106     int                         m_startarray;
00107     int                         m_endarray;
00108     int                         m_startindex;
00109     int                         m_endindex;
00110     int                         m_startvertex;
00111     int                         m_endvertex;
00112     RAS_DisplayArrayList        m_displayArrays;
00113 
00114     // for construction only
00115     RAS_DisplayArray*           m_currentArray;
00116 
00117 public:
00118     // for rendering
00119     RAS_MaterialBucket*     m_bucket;
00120     RAS_MeshObject*         m_mesh;
00121     void*                   m_clientObj;
00122     RAS_Deformer*           m_pDeformer;
00123     DerivedMesh*            m_pDerivedMesh;
00124     double*                 m_OpenGLMatrix;
00125     // visibility
00126     bool                    m_bVisible;
00127     bool                    m_bCulled;
00128     // object color
00129     bool                    m_bObjectColor;
00130     MT_Vector4              m_RGBAcolor;
00131     // display lists
00132     KX_ListSlot*            m_DisplayList;
00133     bool                    m_bDisplayList;
00134     // joined mesh slots
00135     RAS_MeshSlot*           m_joinSlot;
00136     MT_Matrix4x4            m_joinInvTransform;
00137     list<RAS_MeshSlot*>     m_joinedSlots;
00138 
00139     RAS_MeshSlot();
00140     RAS_MeshSlot(const RAS_MeshSlot& slot);
00141     virtual ~RAS_MeshSlot();
00142     
00143     void init(RAS_MaterialBucket *bucket, int numverts);
00144 
00145     struct iterator {
00146         RAS_DisplayArray *array;
00147         RAS_TexVert *vertex;
00148         unsigned short *index;
00149         size_t startvertex;
00150         size_t endvertex;
00151         size_t totindex;
00152         size_t arraynum;
00153     };
00154 
00155     void begin(iterator& it);
00156     void next(iterator& it);
00157     bool end(iterator& it);
00158 
00159     /* used during construction */
00160     void SetDisplayArray(int numverts);
00161     RAS_DisplayArray *CurrentDisplayArray();
00162     void SetDeformer(RAS_Deformer* deformer);
00163 
00164     void AddPolygon(int numverts);
00165     int AddVertex(const RAS_TexVert& tv);
00166     void AddPolygonVertex(int offset);
00167 
00168     /* optimization */
00169     bool Split(bool force=false);
00170     bool Join(RAS_MeshSlot *target, MT_Scalar distance);
00171     bool Equals(RAS_MeshSlot *target);
00172 #ifdef USE_SPLIT
00173     bool IsCulled();
00174 #else
00175     bool IsCulled() { return m_bCulled; }
00176 #endif
00177     void SetCulled(bool culled) { m_bCulled = culled; }
00178     
00179     
00180 #ifdef WITH_CXX_GUARDEDALLOC
00181 public:
00182     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:RAS_MeshSlot"); }
00183     void operator delete( void *mem ) { MEM_freeN(mem); }
00184 #endif
00185 };
00186 
00187 /* Used by RAS_MeshObject, to point to it's slots in a bucket */
00188 
00189 class RAS_MeshMaterial
00190 {
00191 public:
00192     RAS_MeshSlot *m_baseslot;
00193     class RAS_MaterialBucket *m_bucket;
00194     CTR_Map<CTR_HashedPtr,RAS_MeshSlot*> m_slots;
00195 
00196 
00197 #ifdef WITH_CXX_GUARDEDALLOC
00198 public:
00199     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:RAS_MeshMaterial"); }
00200     void operator delete( void *mem ) { MEM_freeN(mem); }
00201 #endif
00202 };
00203 
00204 /* Contains a list of display arrays with the same material,
00205  * and a mesh slot for each mesh that uses display arrays in
00206  * this bucket */
00207 
00208 class RAS_MaterialBucket
00209 {
00210 public:
00211     RAS_MaterialBucket(RAS_IPolyMaterial* mat);
00212     virtual ~RAS_MaterialBucket();
00213     
00214     /* Bucket Sorting */
00215     struct less;
00216     typedef set<RAS_MaterialBucket*, less> Set;
00217 
00218     /* Material Properties */
00219     RAS_IPolyMaterial*      GetPolyMaterial() const;
00220     bool                    IsAlpha() const;
00221     bool                    IsZSort() const;
00222         
00223     /* Rendering */
00224     bool ActivateMaterial(const MT_Transform& cameratrans, RAS_IRasterizer* rasty,
00225         RAS_IRenderTools *rendertools);
00226     void RenderMeshSlot(const MT_Transform& cameratrans, RAS_IRasterizer* rasty,
00227         RAS_IRenderTools* rendertools, RAS_MeshSlot &ms);
00228     
00229     /* Mesh Slot Access */
00230     list<RAS_MeshSlot>::iterator msBegin();
00231     list<RAS_MeshSlot>::iterator msEnd();
00232 
00233     class RAS_MeshSlot* AddMesh(int numverts);
00234     class RAS_MeshSlot* CopyMesh(class RAS_MeshSlot *ms);
00235     void                RemoveMesh(class RAS_MeshSlot* ms);
00236     void                Optimize(MT_Scalar distance);
00237     void                ActivateMesh(RAS_MeshSlot* slot)
00238     {
00239         m_activeMeshSlotsHead.AddBack(slot);
00240     }
00241     SG_DList&           GetActiveMeshSlots()
00242     {
00243         return m_activeMeshSlotsHead;
00244     }
00245     RAS_MeshSlot*       GetNextActiveMeshSlot()
00246     {
00247         return (RAS_MeshSlot*)m_activeMeshSlotsHead.Remove();
00248     }
00249 
00250 private:
00251     list<RAS_MeshSlot>          m_meshSlots;            // all the mesh slots
00252     RAS_IPolyMaterial*          m_material;
00253     SG_DList                    m_activeMeshSlotsHead;  // only those which must be rendered
00254     
00255 
00256 #ifdef WITH_CXX_GUARDEDALLOC
00257 public:
00258     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:RAS_MaterialBucket"); }
00259     void operator delete( void *mem ) { MEM_freeN(mem); }
00260 #endif
00261 };
00262 
00263 #endif //__RAS_MATERIAL_BUCKET
00264