This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrender.cpp
427 lines (356 loc) · 14.4 KB
/
render.cpp
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "render.h"
#include "shader.h"
#include "opengl.h"
#include "win.h"
#include "text.h"
#include <cfloat>
#include "FullbrightShader.h"
#include "FullbrightVertColorShader.h"
#include "FullbrightTexturedShader.h"
#include "FullbrightTexturedTextShader.h"
#include "FullbrightTexturedVertColorShader.h"
#include "FullbrightTexturedVertColorTextShader.h"
#include "PhongTexturedShader.h"
#ifdef DEBUG
// If there is a glError this outputs it along with a message to stderr.
// otherwise there is no output.
void GLErrorCheck(const char* message)
{
GLenum val = glGetError();
switch (val)
{
case GL_INVALID_ENUM:
fprintf(stderr, "GL_INVALID_ENUM : %s\n", message);
break;
case GL_INVALID_VALUE:
fprintf(stderr, "GL_INVALID_VALUE : %s\n", message);
break;
case GL_INVALID_OPERATION:
fprintf(stderr, "GL_INVALID_OPERATION : %s\n", message);
break;
#ifndef GL_ES_VERSION_2_0
case GL_STACK_OVERFLOW:
fprintf(stderr, "GL_STACK_OVERFLOW : %s\n", message);
break;
case GL_STACK_UNDERFLOW:
fprintf(stderr, "GL_STACK_UNDERFLOW : %s\n", message);
break;
#endif
case GL_OUT_OF_MEMORY:
fprintf(stderr, "GL_OUT_OF_MEMORY : %s\n", message);
break;
case GL_NO_ERROR:
break;
}
}
#endif
FullbrightShader* s_fullbrightShader = 0;
FullbrightVertColorShader* s_fullbrightVertColorShader = 0;
FullbrightTexturedShader* s_fullbrightTexturedShader = 0;
FullbrightTexturedTextShader* s_fullbrightTexturedTextShader = 0;
FullbrightTexturedVertColorShader* s_fullbrightTexturedVertColorShader = 0;
FullbrightTexturedVertColorTextShader* s_fullbrightTexturedVertColorTextShader = 0;
PhongTexturedShader* s_phongTexturedShader = 0;
Shader* s_prevShader = 0;
GLuint s_checker = 0;
static GLint CreateCheckerTexture()
{
GLuint retVal = 0;
glGenTextures(1, &retVal);
glBindTexture(GL_TEXTURE_2D, retVal);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
const int W = 512;
const uint8_t O = 100;
const uint8_t X = 255;
uint8_t* checker = new uint8_t[W * W];
int i, j;
for (i = 0; i < W; i++) {
for (j = 0; j < W; j++) {
if (i < W/2) {
if (j < W/2)
checker[i * W + j] = O;
else
checker[i * W + j] = X;
} else {
if (j < W/2)
checker[i * W + j] = X;
else
checker[i * W + j] = O;
}
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, W, W, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, checker);
delete [] checker;
return retVal;
}
// prints all available OpenGL extensions
static void _DumpExtensions()
{
const GLubyte* extensions = glGetString(GL_EXTENSIONS);
printf("extensions =\n");
std::string str((const char *)extensions);
size_t s = 0;
size_t t = str.find_first_of(' ', s);
while (t != std::string::npos)
{
printf(" %s\n", str.substr(s, t - s).c_str());
s = t + 1;
t = str.find_first_of(' ', s);
}
}
void RenderInit(bool verbose)
{
// print out gl version info
if (verbose)
{
const GLubyte* version = glGetString(GL_VERSION);
printf("OpenGL\n");
printf(" version = %s\n", version);
const GLubyte* vendor = glGetString(GL_VENDOR);
printf(" vendor = %s\n", vendor);
const GLubyte* renderer = glGetString(GL_RENDERER);
printf(" renderer = %s\n", renderer);
const GLubyte* shadingLanguageVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf(" shader language version = %s\n", shadingLanguageVersion);
int maxTextureUnits;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
printf(" max texture units = %d\n", maxTextureUnits);
#ifndef GL_ES_VERSION_2_0
int maxVertexUniforms, maxFragmentUniforms;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &maxVertexUniforms);
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &maxFragmentUniforms);
printf(" max vertex uniforms = %d\n", maxVertexUniforms);
printf(" max fragment uniforms = %d\n", maxFragmentUniforms);
#endif
}
if (verbose)
_DumpExtensions();
s_fullbrightShader = new FullbrightShader();
if (!s_fullbrightShader->compileAndLink())
exit(EXIT_FAILURE);
s_fullbrightVertColorShader = new FullbrightVertColorShader();
if (!s_fullbrightVertColorShader->compileAndLink())
exit(EXIT_FAILURE);
s_fullbrightTexturedShader = new FullbrightTexturedShader();
if (!s_fullbrightTexturedShader->compileAndLink())
exit(EXIT_FAILURE);
s_fullbrightTexturedTextShader = new FullbrightTexturedTextShader();
if (!s_fullbrightTexturedTextShader->compileAndLink())
exit(EXIT_FAILURE);
s_fullbrightTexturedVertColorShader = new FullbrightTexturedVertColorShader();
if (!s_fullbrightTexturedVertColorShader->compileAndLink())
exit(EXIT_FAILURE);
s_fullbrightTexturedVertColorTextShader = new FullbrightTexturedVertColorTextShader();
if (!s_fullbrightTexturedVertColorTextShader->compileAndLink())
exit(EXIT_FAILURE);
s_phongTexturedShader = new PhongTexturedShader();
if (!s_phongTexturedShader->compileAndLink())
exit(EXIT_FAILURE);
s_checker = CreateCheckerTexture();
}
void RenderShutdown()
{
delete s_fullbrightShader;
delete s_fullbrightVertColorShader;
delete s_fullbrightTexturedShader;
delete s_fullbrightTexturedTextShader;
delete s_fullbrightTexturedVertColorShader;
delete s_fullbrightTexturedVertColorTextShader;
delete s_phongTexturedShader;
}
static Vector4f UintColorToVector4(uint32_t color)
{
return Vector4f((color & 0xff) / 255.0f,
((color >> 8) & 0xff) / 255.0f,
((color >> 16) & 0xff) / 255.0f,
((color >> 24) & 0xff) / 255.0f);
}
void RenderBegin()
{
s_prevShader = 0;
}
void RenderEnd()
{
}
void RenderTextBegin(const Matrixf& projMatrix, const Matrixf& viewMatrix, const Matrixf& modelMatrix)
{
Matrixf fullMatrix = projMatrix * viewMatrix * modelMatrix;
s_fullbrightShader->setMat(fullMatrix);
s_fullbrightVertColorShader->setMat(fullMatrix);
s_fullbrightTexturedShader->setMat(fullMatrix);
s_fullbrightTexturedTextShader->setMat(fullMatrix);
s_fullbrightTexturedVertColorShader->setMat(fullMatrix);
s_fullbrightTexturedVertColorTextShader->setMat(fullMatrix);
}
void RenderText(const std::vector<gb::Quad>& quadVec)
{
static uint16_t indices[] = {0, 2, 1, 2, 3, 1};
static std::vector<float> s_attribVec;
static std::vector<uint16_t> s_indexVec;
s_attribVec.clear();
s_indexVec.clear();
// disable depth writes
glDepthMask(GL_FALSE);
Vector2f minCorner(FLT_MAX, FLT_MAX);
Vector2f maxCorner(-FLT_MAX, -FLT_MAX);
// draw bg quads
int i = 0;
for (auto &quad : quadVec)
{
const WIN_TextUserData* data = (const WIN_TextUserData*)quad.userData;
Vector4f bg_color = UintColorToVector4(data->bg_color);
// TODO: but this in cfg
bg_color.w = 0.85f;
uint32_t y_offset = data->line_height / 3; // hack
Vector2f origin = Vector2f(quad.pen.x, quad.pen.y + y_offset);
Vector2f size = Vector2f(data->max_advance, -(float)data->line_height);
// keep track of min and max corner.
if (i == 0)
minCorner = origin;
else if (i == quadVec.size() - 1)
maxCorner = origin + size;
float attrib[28] = {
origin.x, origin.y, 0,
bg_color.x, bg_color.y, bg_color.z, bg_color.w,
origin.x + size.x, origin.y, 0,
bg_color.x, bg_color.y, bg_color.z, bg_color.w,
origin.x, origin.y + size.y, 0,
bg_color.x, bg_color.y, bg_color.z, bg_color.w,
origin.x + size.x, origin.y + size.y, 0,
bg_color.x, bg_color.y, bg_color.z, bg_color.w
};
for (int j = 0; j < 28; j++)
s_attribVec.push_back(attrib[j]);
for (int j = 0; j < 6; j++)
s_indexVec.push_back(4 * i + indices[j]);
i++;
}
s_fullbrightVertColorShader->apply(s_prevShader, reinterpret_cast<float*>(&s_attribVec[0]));
s_prevShader = s_fullbrightVertColorShader;
glDrawElements(GL_TRIANGLES, s_indexVec.size(), GL_UNSIGNED_SHORT, reinterpret_cast<uint16_t*>(&s_indexVec[0]));
s_attribVec.clear();
s_indexVec.clear();
// hack: assuming the texture for the first quad is relevent for all of them!
s_fullbrightTexturedVertColorTextShader->setTex(quadVec[0].glTexObj);
s_fullbrightTexturedVertColorTextShader->setLodBias(-1.0f);
// draw fg glyphs
const float kDepthOffset = 0.0f;
i = 0;
for (auto &quad : quadVec) {
const WIN_TextUserData* data = (const WIN_TextUserData*)quad.userData;
if (quad.size.x > 0 && quad.size.y > 0)
{
const float inset = 2.0f;
const float uvInset = inset / 1024.0f; // HACK 1024 is hardcoded size of font texture cache.
Vector2f origin = Vector2f(quad.origin.x + inset, quad.origin.y + inset);
Vector2f size = Vector2f(quad.size.x - 2*inset, quad.size.y - 2*inset);
Vector2f uv_origin = Vector2f(quad.uvOrigin.x + uvInset, quad.uvOrigin.y + uvInset);
Vector2f uv_size = Vector2f(quad.uvSize.x - 2*uvInset, quad.uvSize.y - 2*uvInset);
Vector4f fg_color = UintColorToVector4(data->fg_color);
float attrib[36] = {
origin.x, origin.y, kDepthOffset, uv_origin.x, uv_origin.y,
fg_color.x, fg_color.y, fg_color.z, fg_color.w,
origin.x + size.x, origin.y, kDepthOffset, uv_origin.x + uv_size.x, uv_origin.y,
fg_color.x, fg_color.y, fg_color.z, fg_color.w,
origin.x, origin.y + size.y, kDepthOffset, uv_origin.x, uv_origin.y + uv_size.y,
fg_color.x, fg_color.y, fg_color.z, fg_color.w,
origin.x + size.x, origin.y + size.y, kDepthOffset, uv_origin.x + uv_size.x, uv_origin.y + uv_size.y,
fg_color.x, fg_color.y, fg_color.z, fg_color.w
};
for (int j = 0; j < 36; j++)
s_attribVec.push_back(attrib[j]);
for (int j = 0; j < 6; j++)
s_indexVec.push_back(4 * i + indices[j]);
i++;
}
}
s_fullbrightTexturedVertColorTextShader->apply(s_prevShader, reinterpret_cast<float*>(&s_attribVec[0]));
s_prevShader = s_fullbrightTexturedVertColorTextShader;
glDrawElements(GL_TRIANGLES, s_indexVec.size(), GL_UNSIGNED_SHORT, reinterpret_cast<uint16_t*>(&s_indexVec[0]));
// enable depth writes
glDepthMask(GL_TRUE);
// disable color buffer writes
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
if (quadVec.size())
{
float pos[12] = {
minCorner.x, minCorner.y, 0,
maxCorner.x, minCorner.y, 0,
minCorner.x, maxCorner.y, 0,
maxCorner.x, maxCorner.y, 0
};
s_fullbrightShader->apply(s_prevShader, pos);
s_prevShader = s_fullbrightShader;
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
// re-enable color buffer writes.
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
GL_ERROR_CHECK("TextRenderFunc");
}
void RenderTextEnd()
{
}
void RenderFloor(const Matrixf& projMatrix, const Matrixf& viewMatrix, float height)
{
Matrixf worldMatrix = Matrixf::Trans(Vector3f(0, height, 0));
Matrixf normalMatrix = worldMatrix;
normalMatrix.SetTrans(Vector3f(0, 0, 0));
const float kFeetToMeters = 0.3048;
s_phongTexturedShader->setFullMat(projMatrix * viewMatrix * worldMatrix);
s_phongTexturedShader->setWorldMat(worldMatrix);
s_phongTexturedShader->setWorldNormalMat(normalMatrix);
s_phongTexturedShader->setColor(Vector4f(1, 1, 1, 1));
s_phongTexturedShader->setTex(s_checker);
static bool init = false;
if (!init) {
std::vector<Vector3f> lightWorldPos;
lightWorldPos.push_back(Vector3f(0, 4 * kFeetToMeters, 0));
lightWorldPos.push_back(Vector3f(10 * kFeetToMeters, -1, -1.0f));
lightWorldPos.push_back(Vector3f(0, 4 * kFeetToMeters, 0));
s_phongTexturedShader->setLightWorldPos(lightWorldPos);
std::vector<Vector3f> lightColor;
lightColor.push_back(Vector3f(1, 1, 1));
lightColor.push_back(Vector3f(1, 0, 0));
lightColor.push_back(Vector3f(0, 0, 1));
s_phongTexturedShader->setLightColor(lightColor);
std::vector<float> lightStrength;
lightStrength.push_back(10);
lightStrength.push_back(5);
lightStrength.push_back(5);
s_phongTexturedShader->setLightStrength(lightStrength);
s_phongTexturedShader->setNumLights(3);
init = true;
}
float kOffset = 1000.0f * kFeetToMeters;
float kTexOffset = 100.0f;
float attrib[32] = {
0 - kOffset, 0, 0 - kOffset, 0, 0, 0, 1, 0,
0 + kOffset, 0, 0 - kOffset, 0 + kTexOffset, 0, 0, 1, 0,
0 - kOffset, 0, 0 + kOffset, 0, 0 + kTexOffset, 0, 1, 0,
0 + kOffset, 0, 0 + kOffset, 0 + kTexOffset, 0 + kTexOffset, 0, 1, 0
};
s_phongTexturedShader->apply(s_prevShader, attrib);
static uint16_t indices[] = {0, 2, 1, 2, 3, 1};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
void RenderScreenAlignedQuad(GLuint texture, Vector2f viewportSize, Vector2f quadPos, Vector2f quadSize)
{
Matrixf projMatrix = Matrixf::Ortho(0, viewportSize.x, 0, viewportSize.y, -1, 1);
s_fullbrightTexturedShader->setMat(projMatrix);
s_fullbrightTexturedShader->setColor(Vector4f(1, 1, 1, 1));
s_fullbrightTexturedShader->setTex(texture);
float attrib[20] = {
quadPos.x, quadPos.y, 0, 0, 0,
quadPos.x + quadSize.x, quadPos.y, 0, 1, 0,
quadPos.x, quadPos.y + quadSize.y, 0, 0, 1,
quadPos.x + quadSize.x, quadPos.y + quadSize.y, 0, 1, 1
};
s_fullbrightTexturedShader->apply(s_prevShader, attrib);
s_prevShader = s_fullbrightTexturedShader;
static uint16_t indices[] = {0, 2, 1, 2, 3, 1};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}