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

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

gpu: fix compilation errors and warnings in the vertex declaration code.

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