1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The VertexBuffer and VertexDeclaration classes |
---|
13 | // ---------------------------------------------- |
---|
14 | // |
---|
15 | |
---|
16 | #if !defined __LOL_VERTEXBUFFER_H__ |
---|
17 | #define __LOL_VERTEXBUFFER_H__ |
---|
18 | |
---|
19 | #include <cstring> |
---|
20 | |
---|
21 | namespace lol |
---|
22 | { |
---|
23 | |
---|
24 | class VertexBuffer |
---|
25 | { |
---|
26 | friend class VertexDeclaration; |
---|
27 | |
---|
28 | public: |
---|
29 | VertexBuffer(size_t size); |
---|
30 | ~VertexBuffer(); |
---|
31 | |
---|
32 | void *Lock(size_t offset, size_t size); |
---|
33 | void Unlock(); |
---|
34 | |
---|
35 | private: |
---|
36 | class VertexBufferData *m_data; |
---|
37 | }; |
---|
38 | |
---|
39 | struct VertexUsage |
---|
40 | { |
---|
41 | enum Value |
---|
42 | { |
---|
43 | Position = 0, |
---|
44 | BlendWeight, |
---|
45 | BlendIndices, |
---|
46 | Normal, |
---|
47 | PointSize, |
---|
48 | TexCoord, |
---|
49 | Tangent, |
---|
50 | Binormal, |
---|
51 | TessFactor, |
---|
52 | PositionT, |
---|
53 | Color, |
---|
54 | Fog, |
---|
55 | Depth, |
---|
56 | Sample, |
---|
57 | } |
---|
58 | m_value; |
---|
59 | |
---|
60 | inline VertexUsage(Value v) { m_value = v; } |
---|
61 | inline operator Value() { return m_value; } |
---|
62 | }; |
---|
63 | |
---|
64 | struct MeshPrimitive |
---|
65 | { |
---|
66 | enum Value |
---|
67 | { |
---|
68 | Triangles, |
---|
69 | Points, |
---|
70 | } |
---|
71 | m_value; |
---|
72 | |
---|
73 | inline MeshPrimitive(Value v) { m_value = v; } |
---|
74 | inline operator Value() { return m_value; } |
---|
75 | }; |
---|
76 | |
---|
77 | class VertexStreamBase |
---|
78 | { |
---|
79 | friend class VertexDeclaration; |
---|
80 | |
---|
81 | protected: |
---|
82 | enum |
---|
83 | { |
---|
84 | Typevoid = 0, |
---|
85 | Typehalf, Typef16vec2, Typef16vec3, Typef16vec4, |
---|
86 | Typefloat, Typevec2, Typevec3, Typevec4, |
---|
87 | Typedouble, Typedvec2, Typedvec3, Typedvec4, |
---|
88 | Typeint8_t, Typei8vec2, Typei8vec3, Typei8vec4, |
---|
89 | Typeuint8_t, Typeu8vec2, Typeu8vec3, Typeu8vec4, |
---|
90 | Typeint16_t, Typei16vec2, Typei16vec3, Typei16vec4, |
---|
91 | Typeuint16_t, Typeu16vec2, Typeu16vec3, Typeu16vec4, |
---|
92 | Typeint32_t, Typeivec2, Typeivec3, Typeivec4, |
---|
93 | Typeuint32_t, Typeuvec2, Typeuvec3, Typeuvec4, |
---|
94 | }; |
---|
95 | |
---|
96 | #define LOL_TYPE(T) \ |
---|
97 | static uint8_t GetType(T *x) { (void)x; return Type##T; } |
---|
98 | |
---|
99 | LOL_TYPE(void) |
---|
100 | LOL_TYPE(half) LOL_TYPE(f16vec2) LOL_TYPE(f16vec3) LOL_TYPE(f16vec4) |
---|
101 | LOL_TYPE(float) LOL_TYPE(vec2) LOL_TYPE(vec3) LOL_TYPE(vec4) |
---|
102 | LOL_TYPE(double) LOL_TYPE(dvec2) LOL_TYPE(dvec3) LOL_TYPE(dvec4) |
---|
103 | LOL_TYPE(int8_t) LOL_TYPE(i8vec2) LOL_TYPE(i8vec3) LOL_TYPE(i8vec4) |
---|
104 | LOL_TYPE(uint8_t) LOL_TYPE(u8vec2) LOL_TYPE(u8vec3) LOL_TYPE(u8vec4) |
---|
105 | LOL_TYPE(int16_t) LOL_TYPE(i16vec2) LOL_TYPE(i16vec3) LOL_TYPE(i16vec4) |
---|
106 | LOL_TYPE(uint16_t) LOL_TYPE(u16vec2) LOL_TYPE(u16vec3) LOL_TYPE(u16vec4) |
---|
107 | LOL_TYPE(int32_t) LOL_TYPE(ivec2) LOL_TYPE(ivec3) LOL_TYPE(ivec4) |
---|
108 | LOL_TYPE(uint32_t) LOL_TYPE(uvec2) LOL_TYPE(uvec3) LOL_TYPE(uvec4) |
---|
109 | #undef LOL_TYPE |
---|
110 | |
---|
111 | template<typename T> inline void AddStream(int n, VertexUsage usage) |
---|
112 | { |
---|
113 | m_streams[n].stream_type = GetType((T *)NULL); |
---|
114 | m_streams[n].usage = usage; |
---|
115 | m_streams[n].size = sizeof(T); |
---|
116 | } |
---|
117 | |
---|
118 | VertexStreamBase() {} |
---|
119 | |
---|
120 | private: |
---|
121 | struct { uint8_t stream_type, usage, size; } m_streams[12 + 1]; |
---|
122 | |
---|
123 | static VertexStreamBase const Empty; |
---|
124 | }; |
---|
125 | |
---|
126 | template<> |
---|
127 | inline void VertexStreamBase::AddStream<void>(int n, VertexUsage usage) |
---|
128 | { |
---|
129 | (void)usage; |
---|
130 | m_streams[n].size = 0; |
---|
131 | } |
---|
132 | |
---|
133 | template<typename T1 = void, typename T2 = void, typename T3 = void, |
---|
134 | typename T4 = void, typename T5 = void, typename T6 = void, |
---|
135 | typename T7 = void, typename T8 = void, typename T9 = void, |
---|
136 | typename T10 = void, typename T11 = void, typename T12 = void> |
---|
137 | class VertexStream : public VertexStreamBase |
---|
138 | { |
---|
139 | public: |
---|
140 | inline VertexStream(VertexUsage u1, |
---|
141 | VertexUsage u2 = VertexUsage::Position, |
---|
142 | VertexUsage u3 = VertexUsage::Position, |
---|
143 | VertexUsage u4 = VertexUsage::Position, |
---|
144 | VertexUsage u5 = VertexUsage::Position, |
---|
145 | VertexUsage u6 = VertexUsage::Position, |
---|
146 | VertexUsage u7 = VertexUsage::Position, |
---|
147 | VertexUsage u8 = VertexUsage::Position, |
---|
148 | VertexUsage u9 = VertexUsage::Position, |
---|
149 | VertexUsage u10 = VertexUsage::Position, |
---|
150 | VertexUsage u11 = VertexUsage::Position, |
---|
151 | VertexUsage u12 = VertexUsage::Position) |
---|
152 | { |
---|
153 | AddStream<T1>(0, u1); AddStream<T2>(1, u2); |
---|
154 | AddStream<T3>(2, u3); AddStream<T4>(3, u4); |
---|
155 | AddStream<T5>(4, u5); AddStream<T6>(5, u6); |
---|
156 | AddStream<T7>(6, u7); AddStream<T8>(7, u8); |
---|
157 | AddStream<T9>(8, u9); AddStream<T10>(9, u10); |
---|
158 | AddStream<T11>(10, u11); AddStream<T12>(11, u12); |
---|
159 | } |
---|
160 | }; |
---|
161 | |
---|
162 | class VertexDeclaration |
---|
163 | { |
---|
164 | public: |
---|
165 | VertexDeclaration(VertexStreamBase const &s1, |
---|
166 | VertexStreamBase const &s2 = VertexStreamBase::Empty, |
---|
167 | VertexStreamBase const &s3 = VertexStreamBase::Empty, |
---|
168 | VertexStreamBase const &s4 = VertexStreamBase::Empty, |
---|
169 | VertexStreamBase const &s5 = VertexStreamBase::Empty, |
---|
170 | VertexStreamBase const &s6 = VertexStreamBase::Empty, |
---|
171 | VertexStreamBase const &s7 = VertexStreamBase::Empty, |
---|
172 | VertexStreamBase const &s8 = VertexStreamBase::Empty, |
---|
173 | VertexStreamBase const &s9 = VertexStreamBase::Empty, |
---|
174 | VertexStreamBase const &s10 = VertexStreamBase::Empty, |
---|
175 | VertexStreamBase const &s11 = VertexStreamBase::Empty, |
---|
176 | VertexStreamBase const &s12 = VertexStreamBase::Empty); |
---|
177 | ~VertexDeclaration(); |
---|
178 | |
---|
179 | void Bind(); |
---|
180 | void DrawElements(MeshPrimitive type, int skip, int count); |
---|
181 | void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip, |
---|
182 | int vcount, int skip, int count); |
---|
183 | void Unbind(); |
---|
184 | void SetStream(VertexBuffer *vb, ShaderAttrib attr1, |
---|
185 | ShaderAttrib attr2 = ShaderAttrib(), |
---|
186 | ShaderAttrib attr3 = ShaderAttrib(), |
---|
187 | ShaderAttrib attr4 = ShaderAttrib(), |
---|
188 | ShaderAttrib attr5 = ShaderAttrib(), |
---|
189 | ShaderAttrib attr6 = ShaderAttrib(), |
---|
190 | ShaderAttrib attr7 = ShaderAttrib(), |
---|
191 | ShaderAttrib attr8 = ShaderAttrib(), |
---|
192 | ShaderAttrib attr9 = ShaderAttrib(), |
---|
193 | ShaderAttrib attr10 = ShaderAttrib(), |
---|
194 | ShaderAttrib attr11 = ShaderAttrib(), |
---|
195 | ShaderAttrib attr12 = ShaderAttrib()); |
---|
196 | |
---|
197 | private: |
---|
198 | void Initialize(); |
---|
199 | void AddStream(VertexStreamBase const &); |
---|
200 | |
---|
201 | struct { uint8_t stream_type, index, usage, size; } m_streams[12 + 1]; |
---|
202 | int m_count; |
---|
203 | |
---|
204 | void *m_data; |
---|
205 | }; |
---|
206 | |
---|
207 | } /* namespace lol */ |
---|
208 | |
---|
209 | #endif // __LOL_VERTEXBUFFER_H__ |
---|
210 | |
---|