Back to index

nux  3.0.0
GLVertexResourceManager.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef GLVERTEXRESOURCEMANAGER_H
00024 #define GLVERTEXRESOURCEMANAGER_H
00025 
00026 #include "GLResourceManager.h"
00027 #include "IOpenGLVertexBuffer.h"
00028 #include "IOpenGLIndexBuffer.h"
00029 #include "IOpenGLVertexDeclaration.h"
00030 #include "MeshData.h"
00031 
00032 namespace nux
00033 {
00034   class VertexBuffer: public ResourceData
00035   {
00036     NUX_DECLARE_OBJECT_TYPE(VertexBuffer, ResourceData);
00037 
00038   public:
00039     VertexBuffer();
00040     virtual ~VertexBuffer();
00041     VertexBuffer(int Size, int Stride);
00043     VertexBuffer(const VertexBuffer&);
00045     VertexBuffer& operator = (const VertexBuffer&);
00046 
00048 
00052     void Allocate(int num_index, int size);
00053     int GetSize() const;
00054     int GetStride() const;
00055     int GetNumElement() const;
00056     const void* GetPtrRawData() const;
00057     void* GetPtrRawData();
00058 
00059   public:
00060     std::vector<unsigned char> _Data;
00061     int _Stride;
00062   };
00063 
00064   class IndexBuffer: public ResourceData
00065   {
00066     NUX_DECLARE_OBJECT_TYPE(IndexBuffer, ResourceData);
00067 
00068     // IndexBuffer is very similar to MeshBuffer except that vertex indices
00069     // are not inter-mixed with other vertex data. So the Stride here should be 2 bytes or 4 bytes.
00070   public:
00071     IndexBuffer();
00072     virtual ~IndexBuffer();
00073     IndexBuffer(int Size, int Stride);
00075     IndexBuffer(const IndexBuffer&);
00077     IndexBuffer& operator = (const IndexBuffer&);
00078 
00079     void Allocate(int Size, int Stride);
00080     int GetSize() const;
00081     int GetStride() const;
00082     int GetNumIndex() const;
00083     const void* GetPtrRawData() const;
00084     void* GetPtrRawData();
00085 
00086   public:
00087     std::vector<int> _Data;
00088     int _Stride;
00089     int _num_index;
00090   };
00091 
00092   class VertexDeclaration: public ResourceData
00093   {
00094     NUX_DECLARE_OBJECT_TYPE(NVertexDeclaration, ResourceData);
00095 
00096   public:
00097     VertexDeclaration();
00098     ~VertexDeclaration();
00099     void AddVertexComponent(VERTEXELEMENT);
00100     std::vector<VERTEXELEMENT> _declaration;
00101   };
00102 
00103   class CachedVertexBuffer: public CachedResourceData
00104   {
00105     NUX_DECLARE_OBJECT_TYPE(CachedVertexBuffer, CachedResourceData);
00106   public:
00107     CachedVertexBuffer(NResourceSet* ResourceManager, VertexBuffer* resource);
00108     ~CachedVertexBuffer();
00109 
00110     ObjectPtr<IOpenGLVertexBuffer> _vertex_buffer;
00111 
00112     bool UpdateResource(ResourceData* Resource);
00113     int GetElementSize() const;
00114     int GetBufferStride() const;
00115 
00116   private:
00117     void LoadVertexData(VertexBuffer* vertex_buffer);
00118     int _Size;
00119     int _Stride;
00120   };
00121 
00122   class CachedIndexBuffer: public CachedResourceData
00123   {
00124     NUX_DECLARE_OBJECT_TYPE(CachedIndexBuffer, CachedResourceData);
00125   public:
00126     CachedIndexBuffer(NResourceSet* ResourceManager, IndexBuffer* resource);
00127     ~CachedIndexBuffer();
00128 
00129     ObjectPtr<IOpenGLIndexBuffer>  _index_buffer;
00130 
00131     bool UpdateResource(ResourceData* Resource);
00132     int GetElementSize() const;
00133     int GetBufferStride() const;
00134     int GetNumIndex() const;
00135 
00136   private:
00137     void LoadIndexData(IndexBuffer* index_buffer);
00138     int _Size;
00139     int _Stride;
00140     int _num_index;
00141   };
00142 
00143   class CachedVertexDeclaration: public CachedResourceData
00144   {
00145     NUX_DECLARE_OBJECT_TYPE(CachedVertexDeclaration, CachedResourceData);
00146   public:
00147     CachedVertexDeclaration(NResourceSet* ResourceManager, VertexDeclaration* Resource);
00148     ~CachedVertexDeclaration();
00149 
00150     bool UpdateResource(ResourceData* Resource);
00151     ObjectPtr<IOpenGLVertexDeclaration> _declaration;
00152   };
00153 
00154   class CachedMeshBuffer;
00155 
00156   class MeshBuffer: public ResourceData
00157   {
00158     NUX_DECLARE_OBJECT_TYPE(MeshBuffer, ResourceData);
00159   public:
00160     MeshBuffer(NUX_FILE_LINE_PROTO);
00161     virtual ~MeshBuffer();
00162 
00163     bool Update(const MeshData* mesh_data);
00164 
00165     ObjectPtr<CachedMeshBuffer> GetCachedMeshBuffer();
00166 
00167   public:
00168     VertexBuffer*       _vertex_buffer;
00169     IndexBuffer*        _index_buffer;
00170     VertexDeclaration*  _vertex_declaration;
00171     NuxMeshPrimitiveType _mesh_primitive_type;
00172   };
00173 
00174   class CachedMeshBuffer: public CachedResourceData
00175   {
00176     NUX_DECLARE_OBJECT_TYPE(CachedMeshBuffer, CachedResourceData);
00177   public:
00178     ObjectPtr<CachedVertexBuffer> _cached_vertex_buffer;
00179     ObjectPtr<CachedVertexDeclaration> _cached_vertex_declaration;
00180     ObjectPtr<CachedIndexBuffer> _cached_index_buffer;
00181     NuxMeshPrimitiveType _mesh_primitive_type;
00182 
00183     CachedMeshBuffer(NResourceSet* ResourceManager, MeshBuffer* resource);
00184     ~CachedMeshBuffer();
00185 
00186     bool UpdateResource(ResourceData* Resource);
00187   };
00188 }
00189 
00190 #endif // GLVERTEXRESOURCEMANAGER_H