1 #ifndef AWFUL_SCENEGRAPH_VERTEXBUFFER_H_
2 #define AWFUL_SCENEGRAPH_VERTEXBUFFER_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"
13 namespace awful
{ namespace scenegraph
15 class AWSCENEGRAPH_EXPORT VertexBuffer
: public Serializable
46 VertexBuffer( const Serialization_tag
& ) {}
48 // Vertex buffer binding and state management
51 void renderSetup() const
53 if( ms_pBound
== this )
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
);
68 void resize( uint32_t Size
)
70 m_Buffer
.resize( Size
);
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 ) );
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
;