Some reorganisation of the abf directories.
[fail.git] / scenegraph / vtxarray.h
blob09437a8cd275cda9a738d9bc1df15bb3b48a6fcf
1 #ifndef AWFUL_SCENEGRAPH_VTXARRAY_H_
2 #define AWFUL_SCENEGRAPH_VTXARRAY_H_
4 #include "common.h"
5 #include "vector2.h"
6 #include "vector3.h"
7 #include "rgbcolint.h"
8 #include <vector>
10 /***** TODO
12 This template based vertex buffer is simple, efficient, very flexible and easy to extend. Me like.
13 It will be a little less clean when it comes to serialization and language bindings however.
14 For language bindings with swig, specific instances will have to be defined.
15 Same thing with serialization.
18 old notes from jerky, probably partially obsolete:
20 0. Just do a quick and dirty, plain vertex array implementation, implementing
21 operator[] to access vertices, as well as points 1 and 3 below.
23 Later (= someday = when I feel like doing it) implement VBO support (points 2, 4 and 5 below)
25 1. Define vertex attributes (order + type) - All float as it seems the only properly supported
26 format for speed
28 2. Make a mmap function (then access vertices with operator[])
30 3. A "select" or "activate" or "configure" function that makes it the current vertex array (binds the VBO
31 and setup the vertex arrays)
33 4. uses only vbos for now. It will be possible to add some transparent fallback to regular vertex arrays
34 later if needed (VBO is going to be part of the core starting with opengl 1.5, so
35 it will probably always be available anyway)
37 5. more to come as I progress and read the VBO docs
41 namespace sluggish
43 class VtxArray : public RefCounted
45 public:
46 virtual void disableStates() const = 0;
47 virtual void enableStates() const = 0;
48 virtual void bind() const = 0;
51 namespace VtxAttr
53 // Note: the classes used to represent these types must not be padded.
55 class Position
57 public:
58 typedef Vec3 type;
60 static void DisableState()
62 glDisableClientState( GL_VERTEX_ARRAY );
65 static void EnableState()
67 glEnableClientState( GL_VERTEX_ARRAY );
70 static void Bind( const type* pArray_, int Stride_ )
72 glVertexPointer( 3, GL_FLOAT, Stride_, pArray_ );
76 class Normal
78 public:
79 typedef Vec3 type;
81 static void DisableState()
83 glDisableClientState( GL_NORMAL_ARRAY );
86 static void EnableState()
88 glEnableClientState( GL_NORMAL_ARRAY );
91 static void Bind( const type* pArray_, int Stride_ )
93 glNormalPointer( GL_FLOAT, Stride_, pArray_ );
97 class Color
99 public:
100 typedef RGBColInt type;
102 static void DisableState()
104 glDisableClientState( GL_COLOR_ARRAY );
107 static void EnableState()
109 glEnableClientState( GL_COLOR_ARRAY );
112 static void Bind( const type* pArray_, int Stride_ )
114 glColorPointer( 4, GL_UNSIGNED_BYTE, Stride_, pArray_ );
118 class TexCoords
120 public:
121 typedef Vec2 type;
123 static void DisableState()
125 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
128 static void EnableState()
130 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
133 static void Bind( const type* pArray_, int Stride_ )
135 glTexCoordPointer( 2, GL_FLOAT, Stride_, pArray_ );
140 template< class A1, class A2 = void, class A3 = void, class A4 = void > class Vertex
142 public:
143 typename A1::type a1;
144 typename A2::type a2;
145 typename A3::type a3;
146 typename A4::type a4;
149 template< class A1, class A2, class A3 > class Vertex< A1, A2, A3 >
151 public:
152 typename A1::type a1;
153 typename A2::type a2;
154 typename A3::type a3;
157 template< class A1, class A2 > class Vertex< A1, A2 >
159 public:
160 typename A1::type a1;
161 typename A2::type a2;
164 template< class A1 > class Vertex< A1 >
166 public:
167 typename A1::type a1;
170 template< class A1, class A2 = void, class A3 = void, class A4 = void >
171 class VertexArray : public VtxArray, public std::vector< Vertex< A1, A2, A3, A4 > >
173 public:
174 typedef Vertex< A1, A2, A3, A4 > vertex_type;
176 VertexArray( typename std::vector< Vertex< A1, A2, A3, A4 > >::size_type Size_ ) :
177 std::vector< Vertex< A1, A2, A3, A4 > >( Size_ )
181 virtual void disableStates() const
183 A1::DisableState();
184 A2::DisableState();
185 A3::DisableState();
186 A4::DisableState();
189 virtual void enableStates() const
191 A1::EnableState();
192 A2::EnableState();
193 A3::EnableState();
194 A4::EnableState();
197 virtual void bind() const
199 const typename A1::type* pA1Array = &( *this )[0].a1;
200 A1::Bind( pA1Array, sizeof( vertex_type ) );
201 const typename A2::type* pA2Array = &( *this )[0].a2;
202 A2::Bind( pA2Array, sizeof( vertex_type ) );
203 const typename A3::type* pA3Array = &( *this )[0].a3;
204 A3::Bind( pA3Array, sizeof( vertex_type ) );
205 const typename A4::type* pA4Array = &( *this )[0].a4;
206 A4::Bind( pA4Array, sizeof( vertex_type ) );
210 template< class A1, class A2, class A3 >
211 class VertexArray< A1, A2, A3 > : public VtxArray, public std::vector< Vertex< A1, A2, A3 > >
213 public:
214 typedef Vertex< A1, A2, A3 > vertex_type;
216 VertexArray( typename std::vector< Vertex< A1, A2, A3 > >::size_type Size_ ) :
217 std::vector< Vertex< A1, A2, A3 > >( Size_ )
221 virtual void disableStates() const
223 A1::DisableState();
224 A2::DisableState();
225 A3::DisableState();
228 virtual void enableStates() const
230 A1::EnableState();
231 A2::EnableState();
232 A3::EnableState();
235 virtual void bind() const
237 const typename A1::type* pA1Array = &( *this )[0].a1;
238 A1::Bind( pA1Array, sizeof( vertex_type ) );
239 const typename A2::type* pA2Array = &( *this )[0].a2;
240 A2::Bind( pA2Array, sizeof( vertex_type ) );
241 const typename A3::type* pA3Array = &( *this )[0].a3;
242 A3::Bind( pA3Array, sizeof( vertex_type ) );
246 template< class A1, class A2 >
247 class VertexArray< A1, A2 > : public VtxArray, public std::vector< Vertex< A1, A2 > >
249 public:
250 typedef Vertex< A1, A2 > vertex_type;
252 VertexArray( typename std::vector< Vertex< A1, A2 > >::size_type Size_ ) :
253 std::vector< Vertex< A1, A2 > >( Size_ )
257 virtual void disableStates() const
259 A1::DisableState();
260 A2::DisableState();
263 virtual void enableStates() const
265 A1::EnableState();
266 A2::EnableState();
269 virtual void bind() const
271 const typename A1::type* pA1Array = &( *this )[0].a1;
272 A1::Bind( pA1Array, sizeof( vertex_type ) );
273 const typename A2::type* pA2Array = &( *this )[0].a2;
274 A2::Bind( pA2Array, sizeof( vertex_type ) );
278 template< class A1 >
279 class VertexArray< A1 > : public VtxArray, public std::vector< Vertex< A1 > >
281 public:
282 typedef Vertex< A1 > vertex_type;
284 VertexArray( typename std::vector< Vertex< A1 > >::size_type Size_ ) :
285 std::vector< Vertex< A1 > >( Size_ )
289 virtual void disableStates() const
291 A1::DisableState();
294 virtual void enableStates() const
296 A1::EnableState();
299 virtual void bind() const
301 const typename A1::type* pA1Array = &( *this )[0].a1;
302 A1::Bind( pA1Array, sizeof( vertex_type ) );
307 #endif