FieldBuffer.hpp

00001 #ifndef __FieldBuffer_HPP
00002 #define __FieldBuffer_HPP
00003 
00004 #include <field/Field.hpp>
00005 #include <GLvish/ArrayTypes.hpp>
00006 #include <plankton/VActionNotifier.hpp>
00007 #include "fishGLDllApi.h"
00008 
00009 namespace GL
00010 {
00011         using namespace Wizt;
00012 
00013 template <class ArrayType>
00014 bool LoadArray(ArrayType&What, const MemCore::RefPtr<Fiber::MemBase>&SRC,
00015                BufferArray::usage usage_hint = BufferArray::STATIC_DRAW)
00016 {
00017 typedef typename ArrayType::value_type  value_type;
00018 
00019 Fiber::TypedIterator<value_type> TI = SRC;
00020         if (!TI)
00021                 return false;
00022 
00023 Fiber::CreativeIterator<value_type> it = *TI; 
00024 
00025         return What.load(it.ptr(), it.count()*sizeof(value_type), it.count(), usage_hint ); 
00026 }
00027 
00034 template <class ArrayType>
00035 struct  FieldBuffer
00036 {
00037         typedef typename ArrayType::value_type  value_type;
00038         typedef typename ArrayType::Base_t      Base_t;
00039 
00040 static  MemCore::RefPtr<Base_t> create(const MemCore::RefPtr<Fiber::MemBase>&MB, bool ForceMemCreation, 
00041                                        const Fiber::MemBase::Creator_t&theCreator, BufferArray::usage usage_hint = BufferArray::STATIC_DRAW)
00042         {
00043         using namespace MemCore;
00044         using namespace Fiber;
00045 
00046                 if (!MB)
00047                         throw BufferArray::Error("GL::FieldBuffer::create() No array data given", typeid(ArrayType), typeid(value_type) ); 
00048 //                      return MemCore::NullPtr();
00049 
00050         MemCore::RefPtr<TypedArray<value_type> > TA = MB;
00051                 if (!TA)
00052                         throw BufferArray::Error("Unexpected array data given", typeid(value_type), MB->getType() );
00053 //                      return MemCore::NullPtr();
00054 
00055         CreativeIterator<value_type>*TI = TA->creativeIterator();
00056                 if (!TI)
00057                 {
00058                         //printf("FieldBuffer(): Could not create CreativeIterator\n"); 
00059                         //TA.speak("FieldBuffer failure");
00060                         if (ForceMemCreation)
00061                         {
00062                                 TA = MB->makeMemArray(theCreator);
00063                                 if (!TA)
00064                                 {
00065                                         printf("FieldBuffer(): Could not create memory implementaion of procedural data!\n"); 
00066                                         return MemCore::NullPtr();
00067                                 }
00068                                 TI = TA->creativeIterator();
00069                                 if (!TI)
00070                                 {
00071                                         printf("FieldBuffer(): Ouch, memory copy does not have a CreativeIterator - internal bug?\n"); 
00072                                         return MemCore::NullPtr();
00073                                 }
00074                         }
00075                         else
00076                         {
00077                                 printf("FieldBuffer(): Could not create CreativeIterator\n"); 
00078                                 VActionNotifier::Warning("FieldBuffer(): Could not create CreativeIterator\n"); 
00079                                 return MemCore::NullPtr();
00080                         }
00081 
00082                         //printf("FieldBuffer():    all is right now due to in-memory creation\n"); 
00083                 }
00084 
00085         MemCore::RefPtr<ArrayType> B = new ArrayType();
00086                 if (!B->load(TI->ptr(), TI->count()*sizeof(value_type), TI->count(), usage_hint ) )
00087                 {
00088                         throw BufferArray::Error("Cannot load array data", typeid(value_type), MB->getType() ); 
00089                 }
00090                 return B;
00091         }
00092 
00093 template <class ArrayConstructorParam>
00094 static  MemCore::RefPtr<Base_t> createParam(const ArrayConstructorParam&AParam, const MemCore::RefPtr<Fiber::MemBase>&MB, 
00095                                             bool ForceMemCreation,  const Fiber::MemBase::Creator_t&theCreator,
00096                                             BufferArray::usage usage_hint = BufferArray::STATIC_DRAW)
00097         {
00098         using namespace MemCore;
00099         using namespace Fiber;
00100 
00101                 if (!MB)
00102                         throw BufferArray::Error("GL::FieldBuffer::create(param): No array data given", typeid(ArrayType), typeid(value_type) ); 
00103 //                      return MemCore::NullPtr();
00104 
00105         MemCore::RefPtr<TypedArray<value_type> > TA = MB;
00106                 if (!TA)
00107                         throw BufferArray::Error("Unexpected array data given", typeid(value_type), MB->getType() ); 
00108 //                      return MemCore::NullPtr();
00109 
00110         CreativeIterator<value_type>*TI = TA->creativeIterator();
00111                 if (!TI)
00112                 {
00113                         printf("FieldBuffer(): Could not create CreativeIterator\n"); 
00114                         if (ForceMemCreation)
00115                         {
00116                                 TA = MB->makeMemArray( theCreator );
00117                                 if (!TA)
00118                                 {
00119                                         printf("FieldBuffer(): Could not create memory implementation of procedural data!\n"); 
00120                                         return MemCore::NullPtr();
00121                                 }
00122                                 TI = TA->creativeIterator(); 
00123                                 if (!TI)
00124                                 {
00125                                         printf("FieldBuffer(): Ouch, memory copy does not have a CreativeIterator - internal bug?\n"); 
00126                                         return MemCore::NullPtr();
00127                                 }
00128                         }
00129                         else
00130                                 return MemCore::NullPtr();
00131                 } 
00132         MemCore::RefPtr<ArrayType> B = new ArrayType(AParam); 
00133                 if (!B->load(TI->ptr(), TI->count()*sizeof(value_type), TI->count(), usage_hint ) )
00134                 {
00135                         throw BufferArray::Error("Cannot load array data", typeid(value_type), MB->getType() );
00136                         return NullPtr();
00137                 } 
00138                 return B;
00139         }
00140 
00141 };
00142 
00144 template <>
00145 struct fishGL_API FieldBuffer<VertexArray>
00146 {
00148 static  MemCore::RefPtr<VertexArray> create(const MemCore::RefPtr<Fiber::MemBase>&MB, bool ForceMemCreation,
00149                                             const Fiber::MemBase::Creator_t&theCreator = NullPtr() );
00150 };
00151 
00153 template <>
00154 struct fishGL_API FieldBuffer<TexCoordArray>
00155 {
00157 static  MemCore::RefPtr<TexCoordArray> create(const MemCore::RefPtr<Fiber::MemBase>&MB, bool ForceMemCreation,
00158                                               const Fiber::MemBase::Creator_t&theCreator = NullPtr() );
00159 };
00160 
00162 template <>
00163 struct fishGL_API FieldBuffer<NormalArray>
00164 {
00166 static  MemCore::RefPtr<NormalArray> create(const MemCore::RefPtr<Fiber::MemBase>&MB, bool ForceMemCreation,
00167                                             const Fiber::MemBase::Creator_t&theCreator = NullPtr() );
00168 };
00169 
00170 
00171 } // namespace
00172 
00173 #endif // __FieldBuffer_HPP
00174