source: trunk/src/gpu/vertexbuffer.h @ 1224

Last change on this file since 1224 was 1224, checked in by sam, 9 years ago

gpu: start working on a template-based vertex buffer class.

File size: 3.0 KB
Line 
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 GpuVbo class
13// ----------------
14//
15
16#if !defined __LOL_VERTEXBUFFER_H__
17#define __LOL_VERTEXBUFFER_H__
18
19namespace lol
20{
21
22class VertexBuffer
23{
24public:
25    VertexBuffer(VertexBuffer const& that)
26    {
27        memcpy(this, &that, sizeof(*this));
28
29        /* Now call the platform-specific initialisation code */
30        Initialize();
31    }
32
33    ~VertexBuffer() {}
34
35protected:
36    VertexBuffer() {}
37
38    enum StreamType
39    {
40        VBO_TYPE_VOID,
41        VBO_TYPE_FLOAT,
42        VBO_TYPE_VEC2,
43        VBO_TYPE_VEC3,
44        VBO_TYPE_VEC4,
45        VBO_TYPE_I16VEC4,
46        VBO_TYPE_U8VEC4,
47    };
48
49    template<typename T> static StreamType GetType();
50
51    struct { StreamType stream_type; int index; } m_streams[12 + 1];
52
53private:
54    void Initialize();
55};
56
57template<> VertexBuffer::StreamType VertexBuffer::GetType<void>() { return VBO_TYPE_VOID; }
58template<> VertexBuffer::StreamType VertexBuffer::GetType<float>() { return VBO_TYPE_FLOAT; }
59template<> VertexBuffer::StreamType VertexBuffer::GetType<vec2>() { return VBO_TYPE_VEC2; }
60template<> VertexBuffer::StreamType VertexBuffer::GetType<vec3>() { return VBO_TYPE_VEC3; }
61template<> VertexBuffer::StreamType VertexBuffer::GetType<vec4>() { return VBO_TYPE_VEC4; }
62template<> VertexBuffer::StreamType VertexBuffer::GetType<i16vec4>() { return VBO_TYPE_I16VEC4; }
63template<> VertexBuffer::StreamType VertexBuffer::GetType<u8vec4>() { return VBO_TYPE_U8VEC4; }
64
65template<typename T1 = void,  typename T2 = void,  typename T3 = void,
66         typename T4 = void,  typename T5 = void,  typename T6 = void,
67         typename T7 = void,  typename T8 = void,  typename T9 = void,
68         typename T10 = void, typename T11 = void, typename T12 = void>
69class VertexDeclaration : public VertexBuffer
70{
71public:
72    /* Arguments are the stream index; default to zero */
73    VertexDeclaration(int s1 = 0,  int s2 = 0,  int s3 = 0,
74                      int s4 = 0,  int s5 = 0,  int s6 = 0,
75                      int s7 = 0,  int s8 = 0,  int s9 = 0,
76                      int s10 = 0, int s11 = 0, int s12 = 0)
77    {
78        for (int i = 0; i < 12 + 1; i++)
79            m_streams[i].stream_type = VBO_TYPE_VOID;
80
81        AddStream<T1>(0, s1); AddStream<T2>(1, s2); AddStream<T3>(2, s3);
82        AddStream<T4>(3, s4); AddStream<T5>(4, s5); AddStream<T6>(5, s6);
83        AddStream<T7>(6, s7); AddStream<T8>(7, s8); AddStream<T9>(8, s9);
84        AddStream<T10>(9, s10); AddStream<T11>(10, s11); AddStream<T12>(11, s12);
85    }
86
87private:
88    template<typename T> void AddStream(int index, int stream)
89    {
90        m_streams[index].stream_type = GetType<T>();
91        m_streams[index].index = index;
92    }
93};
94
95} /* namespace lol */
96
97#endif // __LOL_VERTEXBUFFER_H__
98
Note: See TracBrowser for help on using the repository browser.