Most everything is built as shared libraries now except services.
[fail.git] / include / scenegraph / VertexBuffer.h
blob7a579db3d2815f41dde817bbb46993a00a6b8688
1 #ifndef AWFUL_SCENEGRAPH_VERTEXBUFFER_H_
2 #define AWFUL_SCENEGRAPH_VERTEXBUFFER_H_
4 #include "core/core.h"
5 #include "scenegraph/scenegraph_export.h"
6 #include "math/Vector2f.h"
7 #include "math/Vector3f.h"
8 #include "math/Vector4f.h"
9 #include "math/Vector4u8.h"
10 #include "IndexBuffer.h"
11 #include <vector>
13 namespace awful { namespace scenegraph
15 class AWSCENEGRAPH_EXPORT VertexBuffer : public Serializable
17 public:
18 enum e_AttrType
20 at_float,
21 at_double,
22 at_Vector2f,
23 at_Vector3f,
24 at_Vector4f,
25 at_Vector4u8
28 enum e_Attr
30 a_Position,
31 a_Normal,
32 a_Color,
33 a_SecondaryColor,
34 a_TexCoord0,
35 a_TexCoord1,
36 a_TexCoord2,
37 a_TexCoord3,
38 a_TexCoord4,
39 a_TexCoord5,
40 a_TexCoord6,
41 a_TexCoord7,
42 a_FogCoord
45 VertexBuffer() {}
46 VertexBuffer( const Serialization_tag& ) {}
48 // Vertex buffer binding and state management
49 void bind() const;
51 void renderSetup() const
53 if( ms_pBound == this )
54 return;
56 bind();
59 static void ClearBound()
61 ms_pBound = static_cast< const VertexBuffer* >( 0 );
64 // Vertex layout setup
65 void addAttribute( e_AttrType Type, e_Attr Attr );
67 // Buffer allocation
68 void resize( uint32_t Size )
70 m_Buffer.resize( Size );
73 // Buffer access.
74 void setFloatAttribute( uint32_t VertexIndex, uint8_t AttributeIndex, float Value )
76 // TODO: throw if the type doesn't match
77 m_Buffer.put( VertexIndex, m_ColumnIndices[AttributeIndex], Value );
79 float getFloatAttribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
81 return m_Buffer.get< float >( VertexIndex, m_ColumnIndices[AttributeIndex] );
84 void setDoubleAttribute( uint32_t VertexIndex, uint8_t AttributeIndex, double Value )
86 // TODO: throw if the type doesn't match
87 m_Buffer.put( VertexIndex, m_ColumnIndices[AttributeIndex], Value );
89 float getDoubleAttribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
91 return m_Buffer.get< double >( VertexIndex, m_ColumnIndices[AttributeIndex] );
94 void setVector2fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex, const math::Vector2f& Value )
96 // TODO: throw if the type doesn't match
97 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
98 m_Buffer.put( VertexIndex, attrIndex, Value.x() );
99 m_Buffer.put( VertexIndex, attrIndex + 1, Value.y() );
101 math::Vector2f getVector2fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
103 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
104 return math::Vector2f( m_Buffer.get< float >( VertexIndex, attrIndex ),
105 m_Buffer.get< float >( VertexIndex, attrIndex + 1 ) );
108 void setVector3fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex, const math::Vector3f& Value )
110 // TODO: throw if the type doesn't match
111 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
112 m_Buffer.put( VertexIndex, attrIndex, Value.x() );
113 m_Buffer.put( VertexIndex, attrIndex + 1, Value.y() );
114 m_Buffer.put( VertexIndex, attrIndex + 2, Value.z() );
116 math::Vector3f getVector3fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
118 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
119 return math::Vector3f( m_Buffer.get< float >( VertexIndex, attrIndex ),
120 m_Buffer.get< float >( VertexIndex, attrIndex + 1 ),
121 m_Buffer.get< float >( VertexIndex, attrIndex + 2 ) );
124 void setVector4fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex, const math::Vector4f& Value )
126 // TODO: throw if the type doesn't match
127 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
128 m_Buffer.put( VertexIndex, attrIndex, Value.x() );
129 m_Buffer.put( VertexIndex, attrIndex + 1, Value.y() );
130 m_Buffer.put( VertexIndex, attrIndex + 2, Value.z() );
131 m_Buffer.put( VertexIndex, attrIndex + 3, Value.w() );
133 math::Vector4f getVector4fAttribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
135 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
136 return math::Vector4f( m_Buffer.get< float >( VertexIndex, attrIndex ),
137 m_Buffer.get< float >( VertexIndex, attrIndex + 1 ),
138 m_Buffer.get< float >( VertexIndex, attrIndex + 2 ),
139 m_Buffer.get< float >( VertexIndex, attrIndex + 3 ) );
142 void setVector4u8Attribute( uint32_t VertexIndex, uint8_t AttributeIndex, const math::Vector4u8& Value )
144 // TODO: throw if the type doesn't match
145 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
146 m_Buffer.put( VertexIndex, attrIndex, Value.r() );
147 m_Buffer.put( VertexIndex, attrIndex + 1, Value.g() );
148 m_Buffer.put( VertexIndex, attrIndex + 2, Value.b() );
149 m_Buffer.put( VertexIndex, attrIndex + 3, Value.a() );
151 math::Vector4u8 getVector4u8Attribute( uint32_t VertexIndex, uint8_t AttributeIndex ) const
153 uint8_t attrIndex = m_ColumnIndices[AttributeIndex];
154 return math::Vector4u8( m_Buffer.get< uint8_t >( VertexIndex, attrIndex ),
155 m_Buffer.get< uint8_t >( VertexIndex, attrIndex + 1 ),
156 m_Buffer.get< uint8_t >( VertexIndex, attrIndex + 2 ),
157 m_Buffer.get< uint8_t >( VertexIndex, attrIndex + 3 ) );
159 private:
160 void enableStates() const;
161 void disableStates() const;
163 static ConstPointer< VertexBuffer > ms_pBound;
165 typedef std::pair< e_AttrType, e_Attr > Attribute;
166 typedef std::vector< Attribute > AttributeVec;
168 template< class C, typename T > friend struct awful::attribute_traits;
169 AttributeVec m_Attributes;
170 std::vector< uint8_t > m_ColumnIndices;
171 DynamicBuffer m_Buffer;
175 #endif