PixelLightAPI  .
VertexBuffer.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: VertexBuffer.h                                 *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLRENDERER_VERTEXBUFFER_H__
00024 #define __PLRENDERER_VERTEXBUFFER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/Container/Array.h>
00032 #include <PLGraphics/Color/Color4.h>
00033 #include "PLRenderer/Renderer/Buffer.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Forward declarations                                  ]
00038 //[-------------------------------------------------------]
00039 namespace PLMath {
00040     class Vector3;
00041 }
00042 namespace PLRenderer {
00043     class IndexBuffer;
00044 }
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Namespace                                             ]
00049 //[-------------------------------------------------------]
00050 namespace PLRenderer {
00051 
00052 
00053 //[-------------------------------------------------------]
00054 //[ Classes                                               ]
00055 //[-------------------------------------------------------]
00056 /**
00057 *  @brief
00058 *    Renderer vertex buffer (VBO) resource
00059 *
00060 *  @note
00061 *    - If for instance fog coordinates (Capabilities::bVertexBufferFogCoord) are not supported by the
00062 *      GPU, this data is ignored for rendering but can still be used inside your vertex buffer
00063 */
00064 class VertexBuffer : public Buffer {
00065 
00066 
00067     //[-------------------------------------------------------]
00068     //[ Public definitions                                    ]
00069     //[-------------------------------------------------------]
00070     public:
00071         /**
00072         *  @brief
00073         *    Vertex attribute semantic
00074         *
00075         *  @note
00076         *    - Defines input vertex data to the pipeline, on legacy hardware, this should be defined in this order (performance)
00077         */
00078         enum ESemantic {
00079             Position     =  0,  /**< Position data (1 channel) */
00080             BlendWeight  =  1,  /**< Blend weight data (1 channel) */
00081             Normal       =  2,  /**< Normal data (1 channel, only Float3 allowed!) */
00082             Color        =  3,  /**< Color data (2 channels, only RGBA allowed, second channel requires Capabilities::bVertexBufferSecondaryColor) */
00083             FogCoord     =  4,  /**< Fog coordinate data (1 channel, only Float1 allowed, requires FixedFunctions::Capabilities::bVertexBufferFogCoord) */
00084             PointSize    =  5,  /**< Point sprite size data (1 channel, only Float1 allowed! Known as "PSize", but this name may conflict with OS definitions) */
00085             BlendIndices =  6,  /**< Blend indices data (1 channel) */
00086             TexCoord     =  7,  /**< Texture coordinate data (n channels) */
00087             Tangent      =  8,  /**< Tangent data (1 channel, only Float3 allowed!) */
00088             Binormal     =  9   /**< Binormal (also referred to as bitangent) data (1 channel, only Float3 allowed!) */
00089         };
00090         static const PLCore::uint8 NumOfSemantics      = 10;    /**< Number of vertex attribute semantics */
00091         static const PLCore::uint8 MaxPipelineChannels = 16;    /**< Maximum possible number of channels */
00092 
00093         /**
00094         *  @brief
00095         *    Vertex attribute types
00096         */
00097         enum EType {
00098             RGBA   = 0, /**< Color (API dependent storage, do always use GetColor() and SetColor()!) */
00099             Float1 = 1, /**< Float 1 */
00100             Float2 = 2, /**< Float 2 */
00101             Float3 = 3, /**< Float 3 */
00102             Float4 = 4, /**< Float 4 */
00103             Short2 = 5, /**< Short 2 */
00104             Short4 = 6  /**< Short 4 */
00105         };
00106 
00107 
00108     //[-------------------------------------------------------]
00109     //[ Public structures                                     ]
00110     //[-------------------------------------------------------]
00111     public:
00112         /**
00113         *  @brief
00114         *    Defines input vertex attribute to the pipeline
00115         */
00116         struct Attribute {
00117             ESemantic      nSemantic;       /**< Any member of the vertex attribute semantic enumeration type */
00118             PLCore::uint32 nChannel;        /**< Pipeline channel (see ESemantic) */
00119             EType          nType;           /**< Any member of the EType enumeration type */
00120             PLCore::uint32 nOffset;         /**< Offset of the vertex attribute */
00121             // API dependent
00122             PLCore::uint32 nSizeAPI;        /**< Size of the vertex attribute */
00123             PLCore::uint32 nTypeAPI;        /**< API dependent vertex type */
00124             PLCore::uint32 nComponentsAPI;  /**< Number of vertex type components */
00125         };
00126 
00127 
00128     //[-------------------------------------------------------]
00129     //[ Public functions                                      ]
00130     //[-------------------------------------------------------]
00131     public:
00132         /**
00133         *  @brief
00134         *    Destructor
00135         */
00136         PLRENDERER_API virtual ~VertexBuffer();
00137 
00138         /**
00139         *  @brief
00140         *    Returns the number of vertex attributes
00141         *
00142         *  @return
00143         *    Number of vertex attributes
00144         */
00145         inline PLCore::uint32 GetNumOfVertexAttributes() const;
00146 
00147         /**
00148         *  @brief
00149         *    Clears the vertex attributes
00150         *
00151         *  @return
00152         *    'true' if all went fine, else 'false'
00153         *
00154         *  @note
00155         *    - This is only possible if the vertex buffer is not allocated (see Allocate())
00156         *    - The virtual buffer function Clear() only clears the buffer itself,
00157         *      ClearVertexAttributes() clears the vertex attributes definition
00158         */
00159         PLRENDERER_API bool ClearVertexAttributes();
00160 
00161         /**
00162         *  @brief
00163         *    Adds vertex attribute
00164         *
00165         *  @param[in] nSemantic
00166         *    Any member of the vertex attribute semantic enumeration type
00167         *  @param[in] nChannel
00168         *    Pipeline channel (see ESemantic, maximum see MaxPipelineChannels)
00169         *  @param[in] nType
00170         *    Any member of the EType enumeration type
00171         *
00172         *  @return
00173         *    'true' if all went fine, else 'false' (maybe this vertex attribute is already there)
00174         *
00175         *  @note
00176         *    - You shouldn't change the vertex attribute after the vertex buffer was
00177         *      allocated using the Allocate()-function! (performance!)
00178         *    - It the buffer is still locked, it's forced to be unlocked immediately if all went fine
00179         */
00180         PLRENDERER_API bool AddVertexAttribute(ESemantic nSemantic, PLCore::uint32 nChannel, EType nType);
00181 
00182         /**
00183         *  @brief
00184         *    Returns a vertex attribute
00185         *
00186         *  @param[in] nIndex
00187         *    Index of the vertex attribute which should be returned
00188         *
00189         *  @return
00190         *    The requested vertex attribute, a null pointer on error
00191         */
00192         inline const Attribute *GetVertexAttribute(PLCore::uint32 nIndex = 0) const;
00193 
00194         /**
00195         *  @brief
00196         *    Returns the first found vertex attribute with the requested semantic
00197         *
00198         *  @param[in] nSemantic
00199         *    Vertex attribute semantic
00200         *  @param[in] nChannel
00201         *    Pipeline channel (see ESemantic, maximum see MaxPipelineChannels)
00202         *
00203         *  @return
00204         *    The first found vertex attribute with the requested semantic, a null pointer on error
00205         */
00206         PLRENDERER_API const Attribute *GetVertexAttribute(ESemantic nSemantic, PLCore::uint32 nChannel = 0) const;
00207 
00208         /**
00209         *  @brief
00210         *    Returns the vertex size (in bytes)
00211         *
00212         *  @return
00213         *    Vertex size (in bytes)
00214         */
00215         inline PLCore::uint32 GetVertexSize() const;
00216 
00217         /**
00218         *  @brief
00219         *    Copy operator
00220         *
00221         *  @param[in] cSource
00222         *    Source to copy from
00223         *
00224         *  @return
00225         *    Reference to this object
00226         */
00227         PLRENDERER_API VertexBuffer &operator =(const VertexBuffer &cSource);
00228 
00229         /**
00230         *  @brief
00231         *    Returns the vertex buffer bounding box
00232         *
00233         *  @param[out] vMinPos
00234         *    Will receive the minimum bounding box position
00235         *  @param[out] vMaxPos
00236         *    Will receive the maximum bounding box position
00237         *  @param[in]  pIndexBuffer
00238         *    Optional index buffer to take only a set of vertices into account, set to a null pointer if not used
00239         */
00240         PLRENDERER_API void CalculateBoundingBox(PLMath::Vector3 &vMinPos, PLMath::Vector3 &vMaxPos, PLRenderer::IndexBuffer *pIndexBuffer = nullptr);
00241 
00242         /**
00243         *  @brief
00244         *    Returns the vertex buffer bounding sphere
00245         *
00246         *  @param[out] vPos
00247         *    Will receive the bounding sphere position
00248         *  @param[out] fRadius
00249         *    Will receive the bounding sphere radius
00250         *  @param[in]  pIndexBuffer
00251         *    Optional index buffer to take only a set of vertices into account, set to a null pointer if not used
00252         */
00253         PLRENDERER_API void CalculateBoundingSphere(PLMath::Vector3 &vPos, float &fRadius, PLRenderer::IndexBuffer *pIndexBuffer = nullptr);
00254 
00255 
00256     //[-------------------------------------------------------]
00257     //[ Public virtual VertexBuffer functions                 ]
00258     //[-------------------------------------------------------]
00259     public:
00260         /**
00261         *  @brief
00262         *    Returns the data of a vertex buffer attribute
00263         *
00264         *  @param[in] nIndex
00265         *    Vertex index
00266         *  @param[in] nSemantic
00267         *    Any member of the vertex attribute semantic enumeration type
00268         *  @param[in] nChannel
00269         *    Pipeline channel (see ESemantic)
00270         *
00271         *  @return
00272         *    The data of the locked vertex buffer, a null pointer on error
00273         *
00274         *  @note
00275         *    - See GetData() in Buffer
00276         *    - This function will give you the correct offset of the requested
00277         *      vertex attribute
00278         *    - When manipulating color data you should use GetColor() and SetColor()!
00279         */
00280         virtual void *GetData(PLCore::uint32 nIndex, PLCore::uint32 nSemantic, PLCore::uint32 nChannel = 0) = 0;
00281 
00282         /**
00283         *  @brief
00284         *    Returns the RGBA color of a vertex
00285         *
00286         *  @param[in] nIndex
00287         *    Vertex index
00288         *  @param[in] nChannel
00289         *    Pipeline channel (see ESemantic)
00290         *
00291         *  @return
00292         *    The RGBA color of a vertex
00293         *
00294         *  @note
00295         *    - Because the different APIs will store the colors in different formats
00296         *      you should always use this offered color functions which are API
00297         *      independent!
00298         */
00299         virtual PLGraphics::Color4 GetColor(PLCore::uint32 nIndex, PLCore::uint32 nChannel = 0) = 0;
00300 
00301         /**
00302         *  @brief
00303         *    Set the RGBA color of a vertex
00304         *
00305         *  @param[in] nIndex
00306         *    Vertex index
00307         *  @param[in] cColor
00308         *    Color to set
00309         *  @param[in] nChannel
00310         *    Pipeline channel (see ESemantic)
00311         *
00312         *  @return
00313         *    'true' if all went fine, else 'false'
00314         *
00315         *  @see
00316         *    - GetColor()
00317         */
00318         virtual bool SetColor(PLCore::uint32 nIndex, const PLGraphics::Color4 &cColor, PLCore::uint32 nChannel = 0) = 0;
00319 
00320 
00321     //[-------------------------------------------------------]
00322     //[ Protected functions                                   ]
00323     //[-------------------------------------------------------]
00324     protected:
00325         /**
00326         *  @brief
00327         *    Constructor
00328         *
00329         *  @param[in] cRenderer
00330         *    Owner renderer
00331         */
00332         PLRENDERER_API VertexBuffer(Renderer &cRenderer);
00333 
00334 
00335     //[-------------------------------------------------------]
00336     //[ Protected data                                        ]
00337     //[-------------------------------------------------------]
00338     protected:
00339         PLCore::Array<Attribute*> m_lstVertexAttributes;    /**< The vertex attributes */
00340         PLCore::uint32            m_nVertexSize;            /**< Size in bytes of the vertex buffer (in bytes) */
00341 
00342 
00343     //[-------------------------------------------------------]
00344     //[ Private functions                                     ]
00345     //[-------------------------------------------------------]
00346     private:
00347         /**
00348         *  @brief
00349         *    Copy constructor
00350         *
00351         *  @param[in] cSource
00352         *    Source to copy from
00353         */
00354         VertexBuffer(const VertexBuffer &cSource);
00355 
00356 
00357     //[-------------------------------------------------------]
00358     //[ Private virtual VertexBuffer functions                ]
00359     //[-------------------------------------------------------]
00360     private:
00361         /**
00362         *  @brief
00363         *    Is called after a vertex attribute was added to setup some API dependent stuff
00364         *
00365         *  @param[in] cAttribute
00366         *    The added vertex attribute
00367         *
00368         *  @note
00369         *    - This function will setup the API dependent vertex attribute data
00370         *    - It will also update the current vertex size
00371         */
00372         virtual void VertexAttributeAdded(Attribute &cAttribute) = 0;
00373 
00374 
00375     //[-------------------------------------------------------]
00376     //[ Public virtual Buffer functions                       ]
00377     //[-------------------------------------------------------]
00378     public:
00379         PLRENDERER_API virtual void *GetData() override;
00380 
00381 
00382 };
00383 
00384 
00385 //[-------------------------------------------------------]
00386 //[ Namespace                                             ]
00387 //[-------------------------------------------------------]
00388 } // PLRenderer
00389 
00390 
00391 //[-------------------------------------------------------]
00392 //[ Implementation                                        ]
00393 //[-------------------------------------------------------]
00394 #include "PLRenderer/Renderer/VertexBuffer.inl"
00395 
00396 
00397 #endif // __PLRENDERER_VERTEXBUFFER_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:05
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported