VertexField.hpp

00001 #ifndef __VERTEXFIELD_HPP
00002 #define __VERTEXFIELD_HPP
00003 
00004 
00005 #include <bone/BundleInput.hpp>
00006 #include <bone/FishField.hpp>
00007 #include <bone/FishSlice.hpp>
00008 
00009 #include <ocean/GLvish/VGLRenderObject.hpp>
00010 #include <ocean/GLvish/ArrayTypes.hpp>
00011 #include <ocean/GLvish/Shader.hpp>
00012 
00013 #include <ocean/eagle/PhysicalSpace.hpp>
00014 
00015 #include <ocean/shrimp/VEnum.hpp>
00016 
00017 #include <GL/fieldGL.hpp>
00018 #include <GL/FieldBuffer.hpp>
00019 
00020 #include <field/Fragment.hpp>
00021 
00022 #include "fisheyeDllApi.h"
00023 
00024 namespace Wizt
00025 {
00026 
00072 class    fisheye_API  VertexField : public MemCore::ReferenceBase<VertexField>
00073                                   , public virtual Fish<VObject>
00074                                   , public virtual Fish<Fiber::Field>
00075 {
00076 public: 
00080         struct  Error : GLError
00081         {
00082                 char    fieldname[256];
00083 
00084                 Error(const char*what, const string&f)
00085                 : GLError(0, what)
00086                 {
00087                         strncpy(fieldname, f.c_str(), sizeof(fieldname)-1); 
00088                         fieldname[sizeof(fieldname)-1] = '\0';
00089                 }
00090         }; 
00091 
00096         bool ConvertToElements; 
00097 
00102         VertexField(VObject*what,
00103                     const string&fieldname, bool ConvertToElements=true); 
00104 
00106         ~VertexField();
00107 
00112         virtual RefPtr<BufferArray> createVBOArray(const RefPtr<Fiber::MemBase>&MB,
00113                                                    const string&arrayname,
00114                                                    const RefPtr<GLProgram>&Shader,
00115                                                    const Fiber::MemBase::Creator_t&theCreator) const = 0;
00116 
00122         bool    append(VBO&myVBO, const RefPtr<Fiber::CreativeArrayBase>&CAb,
00123                        const string&arrayname, const RefPtr<GLProgram>&Shader
00124                        ) const; 
00125 
00133         virtual bool isOlderThan(const Ageable&A, const RefPtr<ValuePool>&Context) const; 
00134 
00139         virtual void updateAge(Ageable&A, const RefPtr<ValuePool>&Context) const;
00140 };
00141 
00148 template <class VBOArrayType>
00149 class   VBOField : public VertexField
00150 {
00151 public:
00152         typedef typename VBOArrayType::value_type value_type;
00153 
00154         VBOField(VObject*that, const string&fieldname)
00155         : Fish<VObject>(that)
00156         , Fish<Fiber::Field>(fieldname)
00157         , VertexField(that, fieldname)
00158         {
00159                 acceptType<value_type>();
00160         }
00161 
00162         override RefPtr<BufferArray> createVBOArray(const RefPtr<Fiber::MemBase>&MB,
00163                                                     const string&arrayname,
00164                                                     const RefPtr<GLProgram>&Shader,
00165                                                     const Fiber::MemBase::Creator_t&theCreator
00166                                                     ) const
00167         {
00168 //              return GL::FieldBuffer<VBOArrayType>::create( MB, ConvertToElements );
00169                 return GL::FieldBuffer<VBOArrayType>::create( MB, true, theCreator );
00170         }
00171 };
00172 
00173 
00178 template <class Type>
00179 class   VBOField<TypedTexCoordArray<Type> > : public VertexField
00180 {
00181 public:
00182         typedef TypedTexCoordArray<Type> VBOArrayType;
00183         typedef typename VBOArrayType::value_type value_type;
00184 
00185         int     TU;
00186 
00187         VBOField(VObject*that, const string&fieldname, int TextureUnit)
00188         : Fish<VObject>(that)
00189         , Fish<Fiber::Field>(fieldname)
00190         , VertexField(that, fieldname)
00191         , TU(TextureUnit)
00192         {
00193                 acceptType<value_type>();
00194         }
00195 
00196         override RefPtr<BufferArray> createVBOArray(const RefPtr<Fiber::MemBase>&MB,
00197                                                     const string&arrayname,
00198                                                     const RefPtr<GLProgram>&Shader,
00199                                                     const Fiber::MemBase::Creator_t&theCreator) const
00200         {
00201         int     Id = TU;
00202 #if 0
00203                 if (Shader)
00204                 {
00205                         if (!Shader->getUniformValuei( arrayname.c_str(), Id ) )
00206                         {
00207                                 throw BufferArray::Error("No texture ID defined in associated shader", typeid(Type), MB->getType() );
00208                         }
00209                 }
00210 #endif
00211 //              return GL::FieldBuffer<VBOArrayType>::create( MB, ConvertToElements ); 
00212                 return GL::FieldBuffer<VBOArrayType>::createParam(Id, MB, true, theCreator );
00213         }
00214 };
00215 
00226 template <class Type>
00227 class   VBOField<TypedVertexAttribArray<Type> > : public VertexField
00228 {
00229 public:
00230         typedef TypedVertexAttribArray<Type> VBOArrayType;
00231         typedef typename VBOArrayType::value_type value_type;
00232 
00233         VBOField(VObject*that, const string&fieldname)
00234         : Fish<VObject>(that)
00235         , Fish<Fiber::Field>(fieldname)
00236         , VertexField(that, fieldname)
00237         {
00238                 acceptType<value_type>();
00239         }
00240 
00241         override RefPtr<BufferArray> createVBOArray(const RefPtr<Fiber::MemBase>&MB,
00242                                                     const string&arrayname,
00243                                                     const RefPtr<GLProgram>&Shader,
00244                                                     const Fiber::MemBase::Creator_t&theCreator) const
00245         {
00246                 if (!Shader)
00247                         return NullPtr(); 
00248 
00249         int     Id = Shader->getAttribID( arrayname.c_str() );
00250                 if (Id<0)
00251                         throw BufferArray::Error("Invalid attribute ID used in associated shader", typeid(Type), MB->getType() ); 
00252 
00253                 return GL::FieldBuffer<VBOArrayType>::createParam(Id, MB, true, theCreator ); 
00254         }
00255 };
00256 
00257 } // namespace Wizt
00258 
00259 #endif  // __VERTEXFIELD_HPP