# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/Array.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef Array_INCLUDE_ONCE
    00033 #define Array_INCLUDE_ONCE
    00034
    00035 // mic fixme: we should not include vlGraphics here
    00036
    00037 #include <vlGraphics/GLBufferObject.hpp>
    00038 #include <vlCore/half.hpp>
    00039 #include <vector>
    00040
    00041 namespace vl
    00042 {
    00043 //-----------------------------------------------------------------------------
    00044 // ArrayAbstract
    00045 //-----------------------------------------------------------------------------
    00060   class ArrayAbstract: public Object
    00061   {
    00062   public:
    00063     virtual const char* className() { return "vl::ArrayAbstract"; }
    00064
    00066     ArrayAbstract()
    00067     {
    00068       VL_DEBUG_SET_OBJECT_NAME()
    00069       mBufferGPU = new GLBufferObject;
    00070       mVBODirty = true;
    00071       mVBOUsage = vl::BU_STATIC_DRAW;
    00072     }
    00073
    00075     ArrayAbstract(const ArrayAbstract& other): Object(other)
    00076     {
    00077       operator=(other);
    00078     }
    00080     void operator=(const ArrayAbstract& other)
    00081     {
    00082       gpuBuffer()->resize( other.gpuBuffer()->bytesUsed() );
    00083       memcpy( ptr(), other.ptr(), bytesUsed() );
    00084       setVBODirty(true);
    00085     }
    00086
    00087     virtual ref<ArrayAbstract> clone() const = 0;
    00088
    00089     const GLBufferObject* gpuBuffer() const { return mBufferGPU.get(); }
    00090     GLBufferObject* gpuBuffer() { return mBufferGPU.get(); }
    00091
    00092     void clear() { if (gpuBuffer()) gpuBuffer()->clear(); }
    00093
    00095     const unsigned char* ptr() const { return gpuBuffer() ? gpuBuffer()->ptr() : NULL; }
    00096
    00098     unsigned char* ptr() { return gpuBuffer() ? gpuBuffer()->ptr() : NULL; }
    00099
    00101     virtual size_t bytesUsed() const { return gpuBuffer() ? gpuBuffer()->bytesUsed() : 0; }
    00102
    00104     virtual size_t glSize() const = 0;
    00105
    00107     virtual int glType() const = 0;
    00108
    00110     virtual size_t size() const = 0;
    00111
    00113     virtual Sphere computeBoundingSphere() const = 0;
    00114
    00116     virtual AABB computeBoundingBox() const = 0;
    00117
    00119     virtual void transform(const mat4& m) = 0;
    00120
    00122     virtual void normalize() = 0;
    00123
    00125     virtual vec4 vectorAsVec4(size_t vector_index) const = 0;
    00126
    00128     virtual vec3 vectorAsVec3(size_t vector_index) const = 0;
    00129
    00131     virtual int compare(int a, int b) const = 0;
    00132
    00134     bool isVBODirty() const { return mVBODirty; }
    00135
    00137     void setVBODirty(bool dirty) { mVBODirty = dirty; }
    00138
    00140     EGLBufferUsage usage() const { return mVBOUsage; }
    00141
    00143     void setUsage(EGLBufferUsage usage) { mVBOUsage = usage; }
    00144
    00145     void updateVBO(bool discard_local_storage=false)
    00146     {
    00147       if (isVBODirty())
    00148       {
    00149         gpuBuffer()->setBufferData(usage(),discard_local_storage);
    00150         setVBODirty(false);
    00151       }
    00152     }
    00153
    00154   protected:
    00155     ref<GLBufferObject> mBufferGPU;
    00156     EGLBufferUsage mVBOUsage;
    00157     bool mVBODirty;
    00158   };
    00159 //-----------------------------------------------------------------------------
    00160 // Array
    00161 //-----------------------------------------------------------------------------
    00180   template <typename T_VectorType, typename T_Scalar, int T_GL_Size, GLenum T_GL_Type>
    00181   class Array: public ArrayAbstract
    00182   {
    00183   public:
    00184     virtual const char* className() { return "vl::Array"; }
    00185     typedef T_Scalar scalar_type;
    00186
    00187     virtual size_t glSize() const { return T_GL_Size; }
    00188     virtual int glType() const { return T_GL_Type; }
    00189
    00190     virtual ref<ArrayAbstract> clone() const
    00191     {
    00192       ref<Array> arr = new Array;
    00193       if (size())
    00194       {
    00195         arr->resize(size());
    00196         memcpy(arr->ptr(), ptr(), bytesUsed());
    00197       }
    00198       return arr;
    00199     }
    00200
    00201     void clear() { resize(0); gpuBuffer()->deleteGLBufferObject(); }
    00202     void resize(size_t dim) { gpuBuffer()->resize(dim*bytesPerVector()); setVBODirty(true); }
    00203     size_t size() const { return bytesUsed() / bytesPerVector(); }
    00204     size_t sizeGPU() const { return gpuBuffer() ? gpuBuffer()->byteCountGPU() / bytesPerVector() : 0; }
    00205     size_t scalarCount() const { return size() * glSize(); }
    00206     size_t scalarCountGPU() const { return sizeGPU() * glSize(); }
    00207
    00208     const T_VectorType* begin() const { return reinterpret_cast<const T_VectorType*>(ptr()); }
    00209     T_VectorType* begin() { return reinterpret_cast<T_VectorType*>(ptr()); }
    00210
    00211     const T_VectorType* end() const { return (reinterpret_cast<const T_VectorType*>(ptr()))+size(); }
    00212     T_VectorType* end() { return (reinterpret_cast<T_VectorType*>(ptr()))+size(); }
    00213
    00214     T_VectorType& at(size_t i) { VL_CHECK(i<size()); return *(reinterpret_cast<T_VectorType*>(ptr())+i); }
    00215     const T_VectorType& at(size_t i) const { VL_CHECK(i<size()); return *(reinterpret_cast<const T_VectorType*>(ptr())+i); }
    00216
    00217     T_VectorType& operator[](size_t i) { return at(i); }
    00218     const T_VectorType& operator[](size_t i) const { return at(i); }
    00219
    00220     virtual size_t bytesPerVector() const { return sizeof(T_VectorType); }
    00221
    00222     Sphere computeBoundingSphere() const
    00223     {
    00224       AABB aabb;
    00225       const int count = T_GL_Size == 4 ? 3 : T_GL_Size;
    00226       for(size_t i=0; i<size(); ++i)
    00227       {
    00228         vec3 v;
    00229         const T_Scalar* pv = reinterpret_cast<const T_Scalar*>(&at(i));
    00230         for( int j=0; j<count; ++j )
    00231           v.ptr()[j] = (Real)pv[j];
    00232         aabb += v;
    00233       }
    00234       Real radius = 0;
    00235       vec3 center = aabb.center();
    00236       for(size_t i=0; i<size(); ++i)
    00237       {
    00238         vec3 v;
    00239         const T_Scalar* pv = reinterpret_cast<const T_Scalar*>(&at(i));
    00240         for( int j=0; j<count; ++j )
    00241           v.ptr()[j] = (Real)pv[j];
    00242         Real r = (v-center).lengthSquared();
    00243         if (r > radius)
    00244           radius = r;
    00245       }
    00246       return Sphere( center, sqrt(radius) );
    00247     }
    00248
    00249     AABB computeBoundingBox() const
    00250     {
    00251       AABB aabb;
    00252       const int count = T_GL_Size == 4 ? 3 : T_GL_Size;
    00253       for(size_t i=0; i<size(); ++i)
    00254       {
    00255         vec3 v;
    00256         const T_Scalar* pv = reinterpret_cast<const T_Scalar*>(&at(i));
    00257         for( int j=0; j<count; ++j )
    00258           v.ptr()[j] = (Real)pv[j];
    00259         aabb += v;
    00260       }
    00261       return aabb;
    00262     }
    00263
    00264     void transform(const mat4& m)
    00265     {
    00266       for(size_t i=0; i<size(); ++i)
    00267       {
    00268         vec4 v(0,0,0,1);
    00269         T_Scalar* pv = reinterpret_cast<T_Scalar*>(&at(i));
    00270         // read
    00271         for( size_t j=0; j<T_GL_Size; ++j )
    00272           v.ptr()[j] = (Real)pv[j];
    00273         // transform
    00274         v = m * v;
    00275         // write
    00276         for( size_t j=0; j<T_GL_Size; ++j )
    00277           pv[j] = (T_Scalar)v.ptr()[j];
    00278       }
    00279     }
    00280
    00281     void normalize()
    00282     {
    00283       for(size_t i=0; i<size(); ++i)
    00284       {
    00285         vec4 v(0,0,0,0);
    00286         T_Scalar* pv = reinterpret_cast<T_Scalar*>(&at(i));
    00287         // read
    00288         for( size_t j=0; j<T_GL_Size; ++j )
    00289           v.ptr()[j] = (Real)pv[j];
    00290         // normalize
    00291         v.normalize();
    00292         // write
    00293         for( unsigned j=0; j<T_GL_Size; ++j )
    00294           pv[j] = (T_Scalar)v.ptr()[j];
    00295       }
    00296     }
    00297
    00298     vec4 vectorAsVec4(size_t vector_index) const
    00299     {
    00300       vec4 v(0,0,0,1);
    00301       const T_Scalar* pv = reinterpret_cast<const T_Scalar*>(&at(vector_index));
    00302       for( size_t j=0; j<T_GL_Size; ++j )
    00303         v.ptr()[j] = (Real)pv[j];
    00304       return v;
    00305     }
    00306
    00307     vec3 vectorAsVec3(size_t vector_index) const
    00308     {
    00309       vec3 v;
    00310       const T_Scalar* pv = reinterpret_cast<const T_Scalar*>(&at(vector_index));
    00311       const int count = T_GL_Size == 4 ? 3 : T_GL_Size;
    00312       for( int j=0; j<count; ++j )
    00313         v.ptr()[j] = (Real)pv[j];
    00314       return v;
    00315     }
    00316
    00317     int compare(int a, int b) const
    00318     {
    00319       const T_Scalar* pa = reinterpret_cast<const T_Scalar*>(&at(a));
    00320       const T_Scalar* pb = reinterpret_cast<const T_Scalar*>(&at(b));
    00321       for( size_t i=0; i<T_GL_Size; ++i )
    00322         if ( pa[i] != pb[i] )
    00323           return pa[i] < pb[i] ? -1 : +1;
    00324       return 0;
    00325     }
    00326
    00327     void operator=(const std::vector<T_VectorType>& vector)
    00328     {
    00329       resize(vector.size());
    00330       if (vector.empty())
    00331         return;
    00332       else
    00333         memcpy(ptr(),&vector[0],sizeof(vector[0])*vector.size());
    00334     }
    00335   };
    00336 //-----------------------------------------------------------------------------
    00337 // Array typedefs
    00338 //-----------------------------------------------------------------------------
    00340   typedef Array<GLfloat, GLfloat, 1, GL_FLOAT> ArrayFloat1;
    00342   typedef Array<fvec2,   GLfloat, 2, GL_FLOAT> ArrayFloat2;
    00344   typedef Array<fvec3,   GLfloat, 3, GL_FLOAT> ArrayFloat3;
    00346   typedef Array<fvec4,   GLfloat, 4, GL_FLOAT> ArrayFloat4;
    00347
    00349   typedef Array<GLdouble, GLdouble, 1, GL_DOUBLE> ArrayDouble1;
    00351   typedef Array<dvec2,    GLdouble, 2, GL_DOUBLE> ArrayDouble2;
    00353   typedef Array<dvec3,    GLdouble, 3, GL_DOUBLE> ArrayDouble3;
    00355   typedef Array<dvec4,    GLdouble, 4, GL_DOUBLE> ArrayDouble4;
    00356
    00358   typedef Array<GLint, GLint, 1, GL_INT> ArrayInt1;
    00360   typedef Array<ivec2, GLint, 2, GL_INT> ArrayInt2;
    00362   typedef Array<ivec3, GLint, 3, GL_INT> ArrayInt3;
    00364   typedef Array<ivec4, GLint, 4, GL_INT> ArrayInt4;
    00365
    00367   typedef Array<GLuint,GLuint, 1, GL_UNSIGNED_INT> ArrayUInt1;
    00369   typedef Array<uvec2, GLuint, 2, GL_UNSIGNED_INT> ArrayUInt2;
    00371   typedef Array<uvec3, GLuint, 3, GL_UNSIGNED_INT> ArrayUInt3;
    00373   typedef Array<uvec4, GLuint, 4, GL_UNSIGNED_INT> ArrayUInt4;
    00374
    00376   typedef Array<GLbyte, GLbyte, 1, GL_BYTE> ArrayByte1;
    00378   typedef Array<bvec2,  GLbyte, 2, GL_BYTE> ArrayByte2;
    00380   typedef Array<bvec3,  GLbyte, 3, GL_BYTE> ArrayByte3;
    00382   typedef Array<bvec4,  GLbyte, 4, GL_BYTE> ArrayByte4;
    00383
    00385   typedef Array<GLubyte, GLubyte, 1, GL_UNSIGNED_BYTE> ArrayUByte1;
    00387   typedef Array<ubvec2,  GLubyte, 2, GL_UNSIGNED_BYTE> ArrayUByte2;
    00389   typedef Array<ubvec3,  GLubyte, 3, GL_UNSIGNED_BYTE> ArrayUByte3;
    00391   typedef Array<ubvec4,  GLubyte, 4, GL_UNSIGNED_BYTE> ArrayUByte4;
    00392
    00394   typedef Array<GLshort, GLshort, 1, GL_SHORT> ArrayShort1;
    00396   typedef Array<svec2,   GLshort, 2, GL_SHORT> ArrayShort2;
    00398   typedef Array<svec3,   GLshort, 3, GL_SHORT> ArrayShort3;
    00400   typedef Array<svec4,   GLshort, 4, GL_SHORT> ArrayShort4;
    00401
    00403   typedef Array<GLushort, GLushort, 1, GL_UNSIGNED_SHORT> ArrayUShort1;
    00405   typedef Array<usvec2,   GLushort, 2, GL_UNSIGNED_SHORT> ArrayUShort2;
    00407   typedef Array<usvec3,   GLushort, 3, GL_UNSIGNED_SHORT> ArrayUShort3;
    00409   typedef Array<usvec4,   GLushort, 4, GL_UNSIGNED_SHORT> ArrayUShort4;
    00410
    00412   typedef Array<half,  half, 1, GL_HALF_FLOAT> ArrayHFloat1;
    00414   typedef Array<hvec2, half, 2, GL_HALF_FLOAT> ArrayHFloat2;
    00416   typedef Array<hvec3, half, 3, GL_HALF_FLOAT> ArrayHFloat3;
    00418   typedef Array<hvec4, half, 4, GL_HALF_FLOAT> ArrayHFloat4;
    00419
    00421   typedef Array<GLuint,GLuint, 1, GL_FIXED> ArrayFixed1;
    00423   typedef Array<uvec2, GLuint, 2, GL_FIXED> ArrayFixed2;
    00425   typedef Array<uvec3, GLuint, 3, GL_FIXED> ArrayFixed3;
    00427   typedef Array<uvec4, GLuint, 4, GL_FIXED> ArrayFixed4;
    00428
    00430   typedef Array<GLint, GLint, 1, GL_INT_2_10_10_10_REV> ArrayInt_2_10_10_10_REV1;
    00432   typedef Array<ivec2, GLint, 2, GL_INT_2_10_10_10_REV> ArrayInt_2_10_10_10_REV2;
    00434   typedef Array<ivec3, GLint, 3, GL_INT_2_10_10_10_REV> ArrayInt_2_10_10_10_REV3;
    00436   typedef Array<ivec4, GLint, 4, GL_INT_2_10_10_10_REV> ArrayInt_2_10_10_10_REV4;
    00437
    00439   typedef Array<GLuint,GLuint, 1, GL_UNSIGNED_INT_2_10_10_10_REV> ArrayUInt_2_10_10_10_REV1;
    00441   typedef Array<uvec2, GLuint, 2, GL_UNSIGNED_INT_2_10_10_10_REV> ArrayUInt_2_10_10_10_REV2;
    00443   typedef Array<uvec3, GLuint, 3, GL_UNSIGNED_INT_2_10_10_10_REV> ArrayUInt_2_10_10_10_REV3;
    00445   typedef Array<uvec4, GLuint, 4, GL_UNSIGNED_INT_2_10_10_10_REV> ArrayUInt_2_10_10_10_REV4;
    00446 }
    00447
    00448 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:20.
Permission is granted to use this page to write and publish articles regarding Visualization Library.