1 namespace gle
2 {
3     enum
4     {
5         ATTRIB_VERTEX       = 0,
6         ATTRIB_COLOR        = 1,
7         ATTRIB_TEXCOORD0    = 2,
8         ATTRIB_TEXCOORD1    = 3,
9         ATTRIB_NORMAL       = 4,
10         ATTRIB_TANGENT      = 5,
11         ATTRIB_BONEWEIGHT   = 6,
12         ATTRIB_BONEINDEX    = 7,
13         MAXATTRIBS          = 8
14     };
15 
16     extern const char * const attribnames[MAXATTRIBS];
17     extern ucharbuf attribbuf;
18 
19     extern void begin(GLenum mode);
20     extern void begin(GLenum mode, int numverts);
21     extern void multidraw();
22     extern void defattribs(const char *fmt);
23     extern void defattrib(int type, int size, int format);
24 
25     #define GLE_DEFATTRIB(name, type, defaultsize, defaultformat) \
26         static inline void def##name(int size = defaultsize, int format = defaultformat) { defattrib(type, size, format); }
27 
28     GLE_DEFATTRIB(vertex, ATTRIB_VERTEX, 3, GL_FLOAT)
29     GLE_DEFATTRIB(color, ATTRIB_COLOR, 3, GL_FLOAT)
30     GLE_DEFATTRIB(texcoord0, ATTRIB_TEXCOORD0, 2, GL_FLOAT)
31     GLE_DEFATTRIB(texcoord1, ATTRIB_TEXCOORD1, 2, GL_FLOAT)
32     GLE_DEFATTRIB(normal, ATTRIB_NORMAL, 3, GL_FLOAT)
33     GLE_DEFATTRIB(tangent, ATTRIB_TANGENT, 4, GL_FLOAT)
34     GLE_DEFATTRIB(boneweight, ATTRIB_BONEWEIGHT, 4, GL_UNSIGNED_BYTE)
35     GLE_DEFATTRIB(boneindex, ATTRIB_BONEINDEX, 4, GL_UNSIGNED_BYTE)
36 
37     #define GLE_INITATTRIB(name, index, suffix, type) \
38         static inline void name##suffix(type x) { glVertexAttrib1##suffix##_(index, x); } \
39         static inline void name##suffix(type x, type y) { glVertexAttrib2##suffix##_(index, x, y); } \
40         static inline void name##suffix(type x, type y, type z) { glVertexAttrib3##suffix##_(index, x, y, z); } \
41         static inline void name##suffix(type x, type y, type z, type w) { glVertexAttrib4##suffix##_(index, x, y, z, w); }
42     #define GLE_INITATTRIBF(name, index) \
43         GLE_INITATTRIB(name, index, f, float) \
44         static inline void name(const vec &v) { glVertexAttrib3fv_(index, v.v); } \
45         static inline void name(const vec &v, float w) { glVertexAttrib4f_(index, v.x, v.y, v.z, w); } \
46         static inline void name(const vec2 &v) { glVertexAttrib2fv_(index, v.v); } \
47         static inline void name(const vec4 &v) { glVertexAttrib4fv_(index, v.v); }
48     #define GLE_INITATTRIBN(name, index, suffix, type, defaultw) \
49         static inline void name##suffix(type x, type y, type z, type w = defaultw) { glVertexAttrib4N##suffix##_(index, x, y, z, w); }
50 
GLE_INITATTRIBF(vertex,ATTRIB_VERTEX)51     GLE_INITATTRIBF(vertex, ATTRIB_VERTEX)
52     GLE_INITATTRIBF(color, ATTRIB_COLOR)
53     GLE_INITATTRIBN(color, ATTRIB_COLOR, ub, uchar, 255)
54     static inline void color(const bvec &v, uchar alpha = 255) { glVertexAttrib4Nub_(ATTRIB_COLOR, v.x, v.y, v.z, alpha); }
color(const bvec4 & v)55     static inline void color(const bvec4 &v) { glVertexAttrib4Nubv_(ATTRIB_COLOR, v.v); }
GLE_INITATTRIBF(texcoord0,ATTRIB_TEXCOORD0)56     GLE_INITATTRIBF(texcoord0, ATTRIB_TEXCOORD0)
57     GLE_INITATTRIBF(texcoord1, ATTRIB_TEXCOORD1)
58     static inline void normal(float x, float y, float z) { glVertexAttrib4f_(ATTRIB_NORMAL, x, y, z, 0.0f); }
normal(const vec & v)59     static inline void normal(const vec &v) { glVertexAttrib4f_(ATTRIB_NORMAL, v.x, v.y, v.z, 0.0f); }
60     static inline void tangent(float x, float y, float z, float w = 1.0f) { glVertexAttrib4f_(ATTRIB_TANGENT, x, y, z, w); }
61     static inline void tangent(const vec &v, float w = 1.0f) { glVertexAttrib4f_(ATTRIB_TANGENT, v.x, v.y, v.z, w); }
tangent(const vec4 & v)62     static inline void tangent(const vec4 &v) { glVertexAttrib4fv_(ATTRIB_TANGENT, v.v); }
63 
64     #define GLE_ATTRIBPOINTER(name, index, defaultnormalized, defaultsize, defaulttype) \
65         static inline void enable##name() { glEnableVertexAttribArray_(index); } \
66         static inline void disable##name() { glDisableVertexAttribArray_(index); } \
67         static inline void name##pointer(int stride, const void *data, GLenum type = defaulttype, int size = defaultsize, GLenum normalized = defaultnormalized) { \
68             glVertexAttribPointer_(index, size, type, normalized, stride, data); \
69         }
70 
enableattrib(int index)71     static inline void enableattrib(int index) { glEnableVertexAttribArray_(index); }
disableattrib(int index)72     static inline void disableattrib(int index) { glDisableVertexAttribArray_(index); }
73     GLE_ATTRIBPOINTER(vertex, ATTRIB_VERTEX, GL_FALSE, 3, GL_FLOAT)
74     GLE_ATTRIBPOINTER(color, ATTRIB_COLOR, GL_TRUE, 4, GL_UNSIGNED_BYTE)
75     GLE_ATTRIBPOINTER(texcoord0, ATTRIB_TEXCOORD0, GL_FALSE, 2, GL_FLOAT)
76     GLE_ATTRIBPOINTER(texcoord1, ATTRIB_TEXCOORD1, GL_FALSE, 2, GL_FLOAT)
77     GLE_ATTRIBPOINTER(normal, ATTRIB_NORMAL, GL_TRUE, 3, GL_FLOAT)
78     GLE_ATTRIBPOINTER(tangent, ATTRIB_TANGENT, GL_TRUE, 4, GL_FLOAT)
79     GLE_ATTRIBPOINTER(boneweight, ATTRIB_BONEWEIGHT, GL_TRUE, 4, GL_UNSIGNED_BYTE)
80     GLE_ATTRIBPOINTER(boneindex, ATTRIB_BONEINDEX, GL_FALSE, 4, GL_UNSIGNED_BYTE)
81 
82     template<class T>
attrib(T x)83     static inline void attrib(T x)
84     {
85         if(attribbuf.check(sizeof(T)))
86         {
87             T *buf = (T *)attribbuf.pad(sizeof(T));
88             buf[0] = x;
89         }
90     }
91 
92     template<class T>
attrib(T x,T y)93     static inline void attrib(T x, T y)
94     {
95         if(attribbuf.check(2*sizeof(T)))
96         {
97             T *buf = (T *)attribbuf.pad(2*sizeof(T));
98             buf[0] = x;
99             buf[1] = y;
100         }
101     }
102 
103     template<class T>
attrib(T x,T y,T z)104     static inline void attrib(T x, T y, T z)
105     {
106         if(attribbuf.check(3*sizeof(T)))
107         {
108             T *buf = (T *)attribbuf.pad(3*sizeof(T));
109             buf[0] = x;
110             buf[1] = y;
111             buf[2] = z;
112         }
113     }
114 
115     template<class T>
attrib(T x,T y,T z,T w)116     static inline void attrib(T x, T y, T z, T w)
117     {
118         if(attribbuf.check(4*sizeof(T)))
119         {
120             T *buf = (T *)attribbuf.pad(4*sizeof(T));
121             buf[0] = x;
122             buf[1] = y;
123             buf[2] = z;
124             buf[3] = w;
125         }
126     }
127 
128     template<size_t N, class T>
attribv(const T * v)129     static inline void attribv(const T *v)
130     {
131         attribbuf.put((const uchar *)v, N*sizeof(T));
132     }
133 
134     #define GLE_ATTRIB(suffix, type) \
135         static inline void attrib##suffix(type x) { attrib<type>(x); } \
136         static inline void attrib##suffix(type x, type y) { attrib<type>(x, y); } \
137         static inline void attrib##suffix(type x, type y, type z) { attrib<type>(x, y, z); } \
138         static inline void attrib##suffix(type x, type y, type z, type w) { attrib<type>(x, y, z, w); }
139 
GLE_ATTRIB(f,float)140     GLE_ATTRIB(f, float)
141     GLE_ATTRIB(d, double)
142     GLE_ATTRIB(b, char)
143     GLE_ATTRIB(ub, uchar)
144     GLE_ATTRIB(s, short)
145     GLE_ATTRIB(us, ushort)
146     GLE_ATTRIB(i, int)
147     GLE_ATTRIB(ui, uint)
148 
149     static inline void attrib(const vec &v) { attribf(v.x, v.y, v.z); }
attrib(const vec2 & v)150     static inline void attrib(const vec2 &v) { attribf(v.x, v.y); }
attrib(const vec4 & v)151     static inline void attrib(const vec4 &v) { attribf(v.x, v.y, v.z, v.w); }
attrib(const ivec & v)152     static inline void attrib(const ivec &v) { attribi(v.x, v.y, v.z); }
attrib(const ivec2 & v)153     static inline void attrib(const ivec2 &v) { attribi(v.x, v.y); }
attrib(const ivec4 & v)154     static inline void attrib(const ivec4 &v) { attribi(v.x, v.y, v.z, v.w); }
attrib(const bvec & b)155     static inline void attrib(const bvec &b) { attribub(b.x, b.y, b.z); }
attrib(const bvec & b,uchar w)156     static inline void attrib(const bvec &b, uchar w) { attribub(b.x, b.y, b.z, w); }
attrib(const bvec4 & b)157     static inline void attrib(const bvec4 &b) { attribub(b.x, b.y, b.z, b.w); }
158 
159     extern int end();
160     extern void disable();
161 
162     extern void enablequads();
163     extern void disablequads();
164     extern void drawquads(int offset, int count);
165 
166     extern void setup();
167     extern void cleanup();
168 }
169 
170