-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.h
407 lines (342 loc) · 11.8 KB
/
Renderer.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#ifndef RENDERER_H_INCLUDED
#define RENDERER_H_INCLUDED
#include "GfxAPI.h"
#include "Texture.h"
#include "Shader.h"
#include "IStaticInstance.h"
#include <vector>
#include <string>
#include <memory>
#include "math/common.h"
//#include "math/vector2.h"
//#include "math/vector3.h"
#include "Util.h"
#include "Shader.h"
#include "Graphics.h"
struct VideoResolution_t{
int w, h, bpp;
};
#define DIST_NEAR_PLANE 0.1f
#define DIST_FAR_PLANE 100.0f
//#define DIST_FAR_PLANE -1.0f
#define DEFAULT_FOV 60.0f //70
class Renderer : public IStaticInstance<Renderer>
{
public:
class VideoInfo
{
public:
VideoInfo():
w(1024),
h(768),
bpp(32),
fullscreen(false),
caption(std::string("")) {}
VideoInfo(int iw,int ih,int ibpp,bool bfullscreen,std::string scaption):
w(iw),
h(ih),
bpp(ibpp),
fullscreen(bfullscreen),
caption(scaption) {}
int w,h,bpp;
bool fullscreen;
std::string caption;
void set(VideoInfo *info)
{
w=info->w;
h=info->h;
bpp=info->bpp;
fullscreen=info->fullscreen;
caption=info->caption;
}
void set(int _w, int _h, int _b, bool _fs, std::string _caption)
{
w=_w; h=_h; bpp=_b; fullscreen=_fs; caption=_caption;
}
static VideoInfo newVideoInfo(int iw, int ih, int ibpp, bool bfullscreen, std::string scaption)
{
VideoInfo info;
info.w = iw;
info.h = ih;
info.bpp = ibpp;
info.fullscreen = bfullscreen;
info.caption = scaption;
return info;
}
};
enum eView
{
VIEW_ORTHO,
VIEW_PERSPECTIVE
};
private:
VideoInfo m_VideoInfo;
bool m_bError;
bool m_bWireframe;
bool m_bTextures;
bool m_bLighting;
bool m_bLightingState;
bool m_bShaders;
bool m_bShaderState;
float m_fFOV;
bool setDisplayMode();
std::unique_ptr<Program> m_spBaseProgram; // base pass (ambient)
std::unique_ptr<Program> m_spDefaultProgram; // default light pass
Program* m_pProgram; //weak
std::vector<Texture*> m_TextureSlots;
std::vector<Program::UniformID> m_TextureUniform;
Program::UniformID m_ViewMatrixUniform;
struct LightData
{
LightData():
vec(0),
atten(0),
color(0)
{}
//glm::vec4 vec;
//glm::vec3 atten;
//Color color;
Program::UniformID vec;
Program::UniformID atten;
Program::UniformID color;
};
std::vector<LightData> m_LightUniform;
glm::mat4 m_ViewMatrix;
unsigned int m_Flags;
void nullify() {
m_fFOV = DEFAULT_FOV;
m_bError = false;
m_bWireframe = false;
m_bTextures = true;
m_bShaders = false;
m_bLighting = false;
m_bLightingState = false;
m_bShaders = false;
m_bShaderState = false;
m_ViewMatrixUniform = 0;
m_Flags = 0;
m_pProgram = NULL;
}
static bool loadShaderPair(std::unique_ptr<Program>& program, const std::string& shader_name);
public:
Renderer();
Renderer(int w, int h, int b, bool fs, std::string caption);
//static Renderer* newVideo();
//static Renderer* newVideo(int w, int h, int b, bool fs, std::string caption);
virtual ~Renderer();
bool error() const { return m_bError; }
void draw();
void changeSettings(VideoInfo* info) { m_VideoInfo.set(info); }
bool isResolutionSupported(int w, int h, int b){
unsigned int r;
r = SDL_VideoModeOK(w, h, b, SDL_OPENGL);
if(r)
return true;
return false;
}
VideoInfo *getVideoInfo() { return &m_VideoInfo; }
int width() const { return m_VideoInfo.w; }
int height() const { return m_VideoInfo.h; }
glm::vec2 size() const {
return glm::vec2(
1.0f*m_VideoInfo.w,
1.0f*m_VideoInfo.h
);
}
void setFOV(float f) { m_fFOV = f; }
float getFOV() { return m_fFOV; }
static bool startIL();
bool startGL();
void endGL();
void clear();
void viewport(eView vm, float x = 0.0f, float y = 0.0f, float w = 1.0f, float h = 1.0f);
void perspective(float aspect);
void wireframe(bool t);
bool wireframe() const { return m_bWireframe; }
void toggleWireframe();
void textures(bool t);
bool textures() const { return m_bTextures; }
void toggleTextures();
bool screenshot() const;
enum {
UNBIND_LIGHTING = BIT(0),
BIND_LIGHTING = BIT(1),
DISABLE_LIGHTING = BIT(2),
ENABLE_LIGHTING = BIT(3)
};
bool pauseLighting() {
if(!lighting() || !lightingBound())
return false;
lighting(UNBIND_LIGHTING);
return true;
}
void resumeLighting(bool pause_result = true) {
if(!pause_result)
return;
lighting(BIND_LIGHTING);
}
bool lightingBound() const {return m_bLightingState;}
bool lighting() const { return m_bLighting; }
void lighting(unsigned int flags) {
if(flags & ENABLE_LIGHTING)
{
m_bLighting = true;
glDisable(GL_LIGHTING); //yes I typed DISABLE on purpose
m_bLightingState = false;
}
else if(flags & DISABLE_LIGHTING)
{
m_bLighting = false;
glDisable(GL_LIGHTING);
m_bLightingState = false;
}
if(m_bLighting)
{
if(flags & BIND_LIGHTING)
{
glEnable(GL_LIGHTING);
m_bLightingState = true;
}
else if(flags & UNBIND_LIGHTING)
{
glDisable(GL_LIGHTING);
m_bLightingState = false;
}
}
}
enum {
UNBIND_SHADERS = BIT(0),
BIND_SHADERS = BIT(1),
DISABLE_SHADERS = BIT(2),
ENABLE_SHADERS = BIT(3)
};
bool pauseShaders() {
if(!shaders() || !shadersBound())
return false;
shaders(UNBIND_SHADERS);
return true;
}
void resumeShaders(bool pause_result = true) {
if(!pause_result)
return;
shaders(BIND_SHADERS);
}
bool shaders() const { return m_bShaders; }
bool shadersBound() const { return m_bShaderState; }
void shaders(unsigned int flags) {
if(flags & ENABLE_SHADERS)
{
if(m_pProgram)
{
m_bShaders = true;
m_pProgram->use();
}
}
else if(flags & DISABLE_SHADERS)
{
Program::unuseAll();
m_bShaders = false;
}
if(m_bShaders && m_pProgram)
{
if(flags & BIND_SHADERS)
{
m_pProgram->use();
m_bShaderState = true;
}
else if(flags & UNBIND_SHADERS)
{
Program::unuseAll();
m_bShaderState = false;
}
}
}
void bindBaseShader() {
bindShader(m_spBaseProgram.get());
}
void bindDefaultShader() {
bindShader(m_spDefaultProgram.get());
}
void bindShader(Program* program) {
if (m_pProgram == program)
return;
m_pProgram = program;
m_pProgram->use();
}
void setViewUniform() {
if(m_pProgram)
{
glGetFloatv(GL_MODELVIEW_MATRIX, glm::value_ptr(m_ViewMatrix));
m_pProgram->uniform(m_ViewMatrixUniform, m_ViewMatrix);
}
}
//glm::mat4 getModelView() {
// glm::mat4 mat;
// glGetFloatv(GL_MODELVIEW_MATRIX, glm::value_ptr(mat));
//}
//void getModelView(glm::mat4& mat) {
// glGetFloatv(GL_MODELVIEW_MATRIX, glm::value_ptr(mat));
//}
const glm::mat4* getViewMatrix() const {
return &m_ViewMatrix;
}
void bindLight(glm::vec4& vec, glm::vec3& atten, Color& color, unsigned int layer = 0);
void unbindLights();
void bindTexture(Texture* t, unsigned int layer = 0);
//void unbindTextures(unsigned int nlayers = 1);
void unbindTextures();
enum {
TEX_BUMP = BIT(0)
};
unsigned int enabled(unsigned int flags) const {
return m_Flags & flags;
}
void drawLine(const glm::vec3& a, const glm::vec3& b) const
{
glBegin(GL_LINES);
glVertex2fv(glm::value_ptr(a));
glVertex2fv(glm::value_ptr(b));
glEnd();
}
void drawLineBox(const glm::vec3& min, const glm::vec3& max) const
{
glBegin(GL_LINES);
//glNormal3f( 0.0f, 0.0f,max.z);
glVertex3f(min.x, min.y, max.z);
glVertex3f(max.x, min.y, max.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(min.x, max.y, max.z);
// Back Face
//glNormal3f( 0.0f, 0.0f,min.z);
glVertex3f(min.x, min.y, min.z);
glVertex3f(min.x, max.y, min.z);
glVertex3f(max.x, max.y, min.z);
glVertex3f(max.x, min.y, min.z);
// Top Face
//glNormal3f( 0.0f,max.y, 0.0f);
glVertex3f(min.x, max.y, min.z);
glVertex3f(min.x, max.y, max.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(max.x, max.y, min.z);
// Bottom Face
//glNormal3f( 0.0f,min.y, 0.0f);
glVertex3f(min.x, min.y, min.z);
glVertex3f(max.x, min.y, min.z);
glVertex3f(max.x, min.y, max.z);
glVertex3f(min.x, min.y, max.z);
// Right face
//glNormal3f(max.y, 0.0f, 0.0f);
glVertex3f(max.x, min.y, min.z);
glVertex3f(max.x, max.y, min.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(max.x, min.y, max.z);
// Left Face
//glNormal3f(min.y, 0.0f, 0.0f);
glVertex3f(min.x, min.y, min.z);
glVertex3f(min.x, min.y, max.z);
glVertex3f(min.x, max.y, max.z);
glVertex3f(min.x, max.y, min.z);
glEnd();
}
};
#endif