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