Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glemu.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <SDL.h>
5 #include <SDL_opengl.h> // for GLenum, GL_FLOAT
6 #include <stddef.h> // for size_t
7 
8 #include "SDL_opengl.h" // for GL_ARRAY_BUFFER
9 #include "inexor/engine/glexts.hpp" // for glVertexAttrib4f_
10 #include "inexor/network/legacy/buffer_types.hpp" // for ucharbuf
11 #include "inexor/shared/cube_types.hpp" // for uchar, uint, ushort
12 #include "inexor/shared/geom.hpp" // for vec, vec::(anonymous)
13 
14 namespace gle
15 {
16  enum
17  {
27  };
28 
29  extern const char * const attribnames[MAXATTRIBS];
30  extern ucharbuf attribbuf;
31 
32  extern int enabled;
33  extern void forcedisable();
34  static inline void disable() { if(enabled) forcedisable(); }
35 
36  extern void begin(GLenum mode);
37  extern void begin(GLenum mode, int numverts);
38  extern void multidraw();
39  extern void defattribs(const char *fmt);
40  extern void defattrib(int type, int size, int format);
41 
42  #define GLE_DEFATTRIB(name, type, defaultsize, defaultformat) \
43  static inline void def##name(int size = defaultsize, int format = defaultformat) { defattrib(type, size, format); }
44 
45  GLE_DEFATTRIB(vertex, ATTRIB_VERTEX, 3, GL_FLOAT)
46  GLE_DEFATTRIB(color, ATTRIB_COLOR, 3, GL_FLOAT)
47  GLE_DEFATTRIB(texcoord0, ATTRIB_TEXCOORD0, 2, GL_FLOAT)
48  GLE_DEFATTRIB(texcoord1, ATTRIB_TEXCOORD1, 2, GL_FLOAT)
49  GLE_DEFATTRIB(normal, ATTRIB_NORMAL, 3, GL_FLOAT)
50  GLE_DEFATTRIB(tangent, ATTRIB_TANGENT, 4, GL_FLOAT)
51  GLE_DEFATTRIB(boneweight, ATTRIB_BONEWEIGHT, 4, GL_UNSIGNED_BYTE)
52  GLE_DEFATTRIB(boneindex, ATTRIB_BONEINDEX, 4, GL_UNSIGNED_BYTE)
53 
54  #define GLE_INITATTRIB(name, index, suffix, type) \
55  static inline void name##suffix(type x) { glVertexAttrib1##suffix##_(index, x); } \
56  static inline void name##suffix(type x, type y) { glVertexAttrib2##suffix##_(index, x, y); } \
57  static inline void name##suffix(type x, type y, type z) { glVertexAttrib3##suffix##_(index, x, y, z); } \
58  static inline void name##suffix(type x, type y, type z, type w) { glVertexAttrib4##suffix##_(index, x, y, z, w); }
59  #define GLE_INITATTRIBF(name, index) \
60  GLE_INITATTRIB(name, index, f, float) \
61  static inline void name(const vec &v) { glVertexAttrib3fv_(index, v.v); } \
62  static inline void name(const vec &v, float w) { glVertexAttrib4f_(index, v.x, v.y, v.z, w); } \
63  static inline void name(const vec2 &v) { glVertexAttrib2fv_(index, v.v); } \
64  static inline void name(const vec4 &v) { glVertexAttrib4fv_(index, v.v); }
65  #define GLE_INITATTRIBN(name, index, suffix, type, defaultw) \
66  static inline void name##suffix(type x, type y, type z, type w = defaultw) { glVertexAttrib4N##suffix##_(index, x, y, z, w); }
67 
69  GLE_INITATTRIBF(color, ATTRIB_COLOR)
70  GLE_INITATTRIBN(color, ATTRIB_COLOR, ub, uchar, 255)
71  static inline void color(const bvec &v, uchar alpha = 255) { glVertexAttrib4Nub_(ATTRIB_COLOR, v.x, v.y, v.z, alpha); }
72  static inline void color(const bvec4 &v) { glVertexAttrib4Nubv_(ATTRIB_COLOR, v.v); }
73  GLE_INITATTRIBF(texcoord0, ATTRIB_TEXCOORD0)
74  GLE_INITATTRIBF(texcoord1, ATTRIB_TEXCOORD1)
75  static inline void normal(float x, float y, float z) { glVertexAttrib4f_(ATTRIB_NORMAL, x, y, z, 0.0f); }
76  static inline void normal(const vec &v) { glVertexAttrib4f_(ATTRIB_NORMAL, v.x, v.y, v.z, 0.0f); }
77  static inline void tangent(float x, float y, float z, float w = 1.0f) { glVertexAttrib4f_(ATTRIB_TANGENT, x, y, z, w); }
78  static inline void tangent(const vec &v, float w = 1.0f) { glVertexAttrib4f_(ATTRIB_TANGENT, v.x, v.y, v.z, w); }
79  static inline void tangent(const vec4 &v) { glVertexAttrib4fv_(ATTRIB_TANGENT, v.v); }
80 
81  #define GLE_ATTRIBPOINTER(name, index, defaultnormalized, defaultsize, defaulttype, prepare) \
82  static inline void enable##name() { prepare; glEnableVertexAttribArray_(index); } \
83  static inline void disable##name() { glDisableVertexAttribArray_(index); } \
84  static inline void name##pointer(int stride, const void *data, GLenum type = defaulttype, int size = defaultsize, GLenum normalized = defaultnormalized) { \
85  prepare; \
86  glVertexAttribPointer_(index, size, type, normalized, stride, data); \
87  }
88 
89  static inline void enableattrib(int index) { disable(); glEnableVertexAttribArray_(index); }
90  static inline void disableattrib(int index) { glDisableVertexAttribArray_(index); }
91  GLE_ATTRIBPOINTER(vertex, ATTRIB_VERTEX, GL_FALSE, 3, GL_FLOAT, disable())
92  GLE_ATTRIBPOINTER(color, ATTRIB_COLOR, GL_TRUE, 4, GL_UNSIGNED_BYTE, )
93  GLE_ATTRIBPOINTER(texcoord0, ATTRIB_TEXCOORD0, GL_FALSE, 2, GL_FLOAT, )
94  GLE_ATTRIBPOINTER(texcoord1, ATTRIB_TEXCOORD1, GL_FALSE, 2, GL_FLOAT, )
95  GLE_ATTRIBPOINTER(normal, ATTRIB_NORMAL, GL_TRUE, 3, GL_FLOAT, )
96  GLE_ATTRIBPOINTER(tangent, ATTRIB_TANGENT, GL_TRUE, 4, GL_FLOAT, )
97  GLE_ATTRIBPOINTER(boneweight, ATTRIB_BONEWEIGHT, GL_TRUE, 4, GL_UNSIGNED_BYTE, )
98  GLE_ATTRIBPOINTER(boneindex, ATTRIB_BONEINDEX, GL_FALSE, 4, GL_UNSIGNED_BYTE, )
99 
100  static inline void bindebo(GLuint ebo) { disable(); glBindBuffer_(GL_ELEMENT_ARRAY_BUFFER, ebo); }
101  static inline void clearebo() { glBindBuffer_(GL_ELEMENT_ARRAY_BUFFER, 0); }
102  static inline void bindvbo(GLuint vbo) { disable(); glBindBuffer_(GL_ARRAY_BUFFER, vbo); }
103  static inline void clearvbo() { glBindBuffer_(GL_ARRAY_BUFFER, 0); }
104 
105  template<class T>
106  static inline void attrib(T x)
107  {
108  if(attribbuf.check(sizeof(T)))
109  {
110  T *buf = (T *)attribbuf.pad(sizeof(T));
111  buf[0] = x;
112  }
113  }
114 
115  template<class T>
116  static inline void attrib(T x, T y)
117  {
118  if(attribbuf.check(2*sizeof(T)))
119  {
120  T *buf = (T *)attribbuf.pad(2*sizeof(T));
121  buf[0] = x;
122  buf[1] = y;
123  }
124  }
125 
126  template<class T>
127  static inline void attrib(T x, T y, T z)
128  {
129  if(attribbuf.check(3*sizeof(T)))
130  {
131  T *buf = (T *)attribbuf.pad(3*sizeof(T));
132  buf[0] = x;
133  buf[1] = y;
134  buf[2] = z;
135  }
136  }
137 
138  template<class T>
139  static inline void attrib(T x, T y, T z, T w)
140  {
141  if(attribbuf.check(4*sizeof(T)))
142  {
143  T *buf = (T *)attribbuf.pad(4*sizeof(T));
144  buf[0] = x;
145  buf[1] = y;
146  buf[2] = z;
147  buf[3] = w;
148  }
149  }
150 
151  template<size_t N, class T>
152  static inline void attribv(const T *v)
153  {
154  attribbuf.put((const uchar *)v, N*sizeof(T));
155  }
156 
157  #define GLE_ATTRIB(suffix, type) \
158  static inline void attrib##suffix(type x) { attrib<type>(x); } \
159  static inline void attrib##suffix(type x, type y) { attrib<type>(x, y); } \
160  static inline void attrib##suffix(type x, type y, type z) { attrib<type>(x, y, z); } \
161  static inline void attrib##suffix(type x, type y, type z, type w) { attrib<type>(x, y, z, w); }
162 
163  GLE_ATTRIB(f, float)
164  GLE_ATTRIB(d, double)
165  GLE_ATTRIB(b, char)
166  GLE_ATTRIB(ub, uchar)
167  GLE_ATTRIB(s, short)
168  GLE_ATTRIB(us, ushort)
169  GLE_ATTRIB(i, int)
170  GLE_ATTRIB(ui, uint)
171 
172  static inline void attrib(const vec &v) { attribf(v.x, v.y, v.z); }
173  static inline void attrib(const vec &v, float w) { attribf(v.x, v.y, v.z, w); }
174  static inline void attrib(const vec2 &v) { attribf(v.x, v.y); }
175  static inline void attrib(const vec4 &v) { attribf(v.x, v.y, v.z, v.w); }
176  static inline void attrib(const ivec &v) { attribi(v.x, v.y, v.z); }
177  static inline void attrib(const ivec &v, int w) { attribi(v.x, v.y, v.z, w); }
178  static inline void attrib(const ivec2 &v) { attribi(v.x, v.y); }
179  static inline void attrib(const ivec4 &v) { attribi(v.x, v.y, v.z, v.w); }
180  static inline void attrib(const bvec &b) { attribub(b.x, b.y, b.z); }
181  static inline void attrib(const bvec &b, uchar w) { attribub(b.x, b.y, b.z, w); }
182  static inline void attrib(const bvec4 &b) { attribub(b.x, b.y, b.z, b.w); }
183 
184  extern int end();
185 
186  extern void enablequads();
187  extern void disablequads();
188  extern void drawquads(int offset, int count);
189 
190  extern void setup();
191  extern void cleanup();
192 }
193 
void cleanup()
Definition: glemu.cpp:344
Definition: glemu.hpp:20
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray_
Definition: rendergl.cpp:174
void put(const T &val)
put one element at the end of the buffer.
Definition: buffer_types.hpp:73
Definition: normal.cpp:31
typedef GLenum(APIENTRYP PFNGLCHECKFRAMEBUFFERSTATUSPROC)(GLenum target)
template implementation of buffers (networking e.g.).
Definition: buffer_types.hpp:14
static void clearvbo()
Definition: glemu.hpp:103
#define GLE_INITATTRIBN(name, index, suffix, type, defaultw)
Definition: glemu.hpp:65
unsigned int uint
Definition: cube_types.hpp:9
PFNGLVERTEXATTRIB4FVPROC glVertexAttrib4fv_
Definition: rendergl.cpp:190
void forcedisable()
Definition: glemu.cpp:324
StringFormatter::FmtMark fmt
Marker that starts formatting a string.
Definition: StringFormatter.cpp:5
PFNGLVERTEXATTRIB4NUBVPROC glVertexAttrib4Nubv_
Definition: rendergl.cpp:201
static void bindvbo(GLuint vbo)
Definition: glemu.hpp:102
2-dimensional float vectors
Definition: geom.hpp:38
void begin(GLenum mode)
Definition: glemu.cpp:220
ucharbuf attribbuf
Definition: glemu.cpp:35
#define GLE_DEFATTRIB(name, type, defaultsize, defaultformat)
Definition: glemu.hpp:42
static GLuint vbo
Definition: glemu.cpp:51
void setup()
Definition: glemu.cpp:333
int numverts
Definition: explosion.cpp:35
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray_
Definition: rendergl.cpp:175
void multidraw()
Definition: glemu.cpp:245
static void clearebo()
Definition: glemu.hpp:101
ICOMMAND * f(float *a, float *b), floatret(*a **b)
4-dimensional float vector all methods stay basicly the same but with an extra dimension ...
Definition: geom.hpp:343
void format(tagval *args, int numargs)
Definition: command.cpp:2584
bool check(int n)
Definition: buffer_types.hpp:118
Definition: glemu.hpp:21
typedef GLuint(APIENTRYP PFNGLGETUNIFORMBLOCKINDEXPROC)(GLuint program
unsigned short ushort
Definition: cube_types.hpp:8
PFNGLBINDBUFFERPROC glBindBuffer_
Definition: rendergl.cpp:123
Definition: glemu.hpp:22
Definition: glemu.hpp:18
int enabled
Definition: glemu.cpp:38
GLuint index
Definition: glexts.hpp:412
Definition: glemu.hpp:19
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
Definition: glemu.hpp:24
Definition: glemu.hpp:25
3-dimensional UNSIGNED CHAR vector.
Definition: geom.hpp:1398
2 dimensional integer vector.
Definition: geom.hpp:1304
void disablequads()
Definition: glemu.cpp:84
#define GLE_ATTRIB(suffix, type)
Definition: glemu.hpp:157
void defattrib(int type, int size, int format)
Definition: glemu.cpp:109
int d
Definition: octaedit.cpp:1749
static void attribv(const T *v)
Definition: glemu.hpp:152
PFNGLVERTEXATTRIB4FPROC glVertexAttrib4f_
Definition: rendergl.cpp:189
static void disable()
Definition: glemu.hpp:34
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
int end()
Definition: glemu.cpp:256
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
GLintptr offset
Definition: glexts.hpp:354
4 dimensional integer vector.
Definition: geom.hpp:1365
PFNGLVERTEXATTRIB4NUBPROC glVertexAttrib4Nub_
Definition: rendergl.cpp:200
void defattribs(const char *fmt)
Definition: glemu.cpp:139
static void attrib(T x)
Definition: glemu.hpp:106
T * pad(int numvals)
Definition: buffer_types.hpp:65
Definition: world.hpp:82
static void tangent(float x, float y, float z, float w=1.0f)
Definition: glemu.hpp:77
GLsizei count
Definition: glexts.hpp:57
#define GLE_INITATTRIBF(name, index)
Definition: glemu.hpp:59
static void enableattrib(int index)
Definition: glemu.hpp:89
mathmatics for vectors, matrices, quaterions and more
static void bindebo(GLuint ebo)
Definition: glemu.hpp:100
static void normal(float x, float y, float z)
Definition: glemu.hpp:75
static void color(const bvec &v, uchar alpha=255)
Definition: glemu.hpp:71
void enablequads()
Definition: glemu.cpp:56
3-dimensional INTEGER vectors
Definition: geom.hpp:1226
Definition: geom.hpp:1455
Definition: glemu.hpp:26
Definition: glemu.hpp:23
static void disableattrib(int index)
Definition: glemu.hpp:90
const char *const attribnames[MAXATTRIBS]
Definition: glemu.hpp:29
#define GLE_ATTRIBPOINTER(name, index, defaultnormalized, defaultsize, defaulttype, prepare)
Definition: glemu.hpp:81
void drawquads(int offset, int count)
Definition: glemu.cpp:93