forked from janneku/blackbeltsorvihero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl.h
250 lines (203 loc) · 4.09 KB
/
gl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#if !defined(_GL_H)
#define _GL_H
#if defined(CONFIG_GLES)
#include <SDL.h>
#include <GLES/gl.h>
#include "gles_compat.h"
#define GLEW_VERSION_1_5 1
#define GLEW_VERSION_2_0 0
#define GL_EXT_framebuffer_object 1
#define glOrtho glOrthof
#define glFrustum glFrustumf
#define glUseProgram(x) ((void) (x))
#define glColor4fv(x) ((void) (x))
#define glewInit()
#else
#include <GL/glew.h>
#endif
#if defined(CONFIG_SDL_GLES)
#if defined(CONFIG_N950)
#else
#include <SDL_gles.h>
#define SDL_GL_SwapBuffers SDL_GLES_SwapBuffers
#endif /* CONFIG_N950 */
#undef SDL_OPENGL
#define SDL_OPENGL 0
#endif
#include <SDL_ttf.h>
#include <string>
#include <vector>
#include <assert.h>
#include "utils.h"
const GLuint INVALID_TEXTURE = -1;
class color {
public:
float r, g, b, a;
color() {}
color(float i_r, float i_g, float i_b, float i_a = 1) :
r(i_r), g(i_g), b(i_b), a(i_a)
{
}
};
struct Bitmap {
int width;
int height;
int components;
std::string pixels;
};
class Text {
DISABLE_COPY_AND_ASSIGN(Text);
public:
Text();
~Text();
bool ready() const { return m_texture != INVALID_TEXTURE; }
void init(TTF_Font *font, const std::string &text);
void draw() const;
private:
GLuint m_texture;
int m_width;
int m_height;
};
class Image {
DISABLE_COPY_AND_ASSIGN(Image);
public:
Image();
~Image();
int width() const { return m_width; }
int height() const { return m_height; }
bool ready() const { return m_texture != INVALID_TEXTURE; }
void load(const char *fname);
void draw() const;
void scale(float s);
private:
GLuint m_texture;
int m_components;
int m_width;
int m_height;
};
struct Vertex {
vec3 pos;
vec3 norm;
vec2 tc;
};
struct VertexTexCoord {
vec3 pos;
vec2 tc;
};
struct VertexColor {
vec3 pos;
color c;
};
struct Face {
Vertex vert[3];
};
struct Light {
color diffuse;
vec3 pos;
int num_faces;
};
struct Group {
color diffuse;
GLuint buffer;
GLuint texture;
int num_faces;
std::string name;
float intensity;
bool specular;
std::vector<Face> faces;
vec3 pos;
std::list<Light> lights;
};
class Model {
DISABLE_COPY_AND_ASSIGN(Model);
public:
Model() {}
~Model();
void load(const char *fname);
void set_lights(bool bloom = false) const;
void draw() const;
void set_alpha(float a);
Group *find_material(const std::string &name);
private:
std::map<std::string, Group> m_groups;
void load_materials(const char *fname);
};
class GLState {
public:
static const int MAX_STATES = 8;
GLState() :
m_num_states(0)
{}
~GLState()
{
for (int i = 0; i < m_num_states; ++i) {
glDisable(m_states[i]);
}
}
void enable(int state)
{
assert(m_num_states < MAX_STATES);
glEnable(state);
m_states[m_num_states++] = state;
}
private:
GLenum m_states[MAX_STATES];
int m_num_states;
};
class GLClientState {
public:
static const int MAX_STATES = 4;
GLClientState() :
m_num_states(0)
{}
~GLClientState()
{
for (int i = 0; i < m_num_states; ++i) {
glDisableClientState(m_states[i]);
}
}
void enable(int state)
{
assert(m_num_states < MAX_STATES);
glEnableClientState(state);
m_states[m_num_states++] = state;
}
private:
GLenum m_states[MAX_STATES];
int m_num_states;
};
class GLMatrixScope {
public:
GLMatrixScope()
{
glPushMatrix();
}
~GLMatrixScope()
{
glPopMatrix();
}
};
extern const float white[];
extern const float black[];
extern const color white_color;
extern const color black_color;
extern bool shaders_enabled;
void set_light(const vec3 &pos, const color &diffuse, bool bloom = false);
void kill_lights();
void draw_faces(const Face *f, int len);
void draw_array(GLenum type, const Vertex *v, int len);
void draw_array(GLenum type, const VertexColor *v, int len);
void draw_array(GLenum type, const VertexTexCoord *v, int len);
GLuint draw_text(TTF_Font *font, const std::string &text);
GLuint create_fbo(GLuint *img, int width, int height, bool bilinear);
GLuint load_texture(const char *fname);
GLuint load_program(const char *vs_source, const char *fs_source);
void begin_bloom();
void end_bloom();
void draw_bloom();
void begin_blur();
void end_blur();
void draw_blur(float strength);
void flush_gl();
void init_gl();
#endif