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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
/*
* render.c
*
* Render the scene
*
* (c) 2008 Thomas White <taw27@cam.ac.uk>
*
* thrust3d - a silly game
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glew.h>
#include <glu.h>
#include <SDL.h>
#include <math.h>
#include <stdio.h>
#include "model.h"
#include "game.h"
#include "render.h"
#include "texture.h"
#include "utils.h"
#include "render-text.h"
#define PANEL_ALPHA 0.3
/* Utility function to load and compile a shader, checking the info log */
static GLuint render_load_shader(const char *filename, GLenum type) {
GLuint shader;
char text[4096];
size_t len;
FILE *fh;
int l;
GLint status;
fh = fopen(filename, "r");
if ( fh == NULL ) {
fprintf(stderr, "Couldn't load shader '%s'\n", filename);
return 0;
}
len = fread(text, 1, 4095, fh);
fclose(fh);
text[len] = '\0';
const GLchar *source = text;
shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if ( status == GL_FALSE ) {
glGetShaderInfoLog(shader, 4095, &l, text);
if ( l > 0 ) {
printf("%s\n", text); fflush(stdout);
} else {
printf("Shader compilation failed.\n");
}
}
return shader;
}
static int render_validate_shader(GLuint shader) {
GLint status;
int l;
char text[4096];
glValidateProgram(shader);
glGetProgramiv(shader, GL_VALIDATE_STATUS, &status);
if ( status == GL_FALSE ) {
glGetProgramInfoLog(shader, 4095, &l, text);
if ( l > 0 ) {
printf("%s\n", text); fflush(stdout);
} else {
printf("Shader did not validate successfully.\n");
}
return 0;
}
return 1;
}
static void render_load_shaders(RenderContext *r) {
/* Lighting */
r->lighting_vert = render_load_shader(DATADIR"/shaders/lighting.vert", GL_VERTEX_SHADER);
r->lighting_frag = render_load_shader(DATADIR"/shaders/lighting.frag", GL_FRAGMENT_SHADER);
r->lighting_program = glCreateProgram();
glAttachShader(r->lighting_program, r->lighting_vert);
glAttachShader(r->lighting_program, r->lighting_frag);
glLinkProgram(r->lighting_program);
render_validate_shader(r->lighting_program);
/* Swirlyness */
r->swirly_vert = render_load_shader(DATADIR"/shaders/swirlytron.vert", GL_VERTEX_SHADER);
r->swirly_frag = render_load_shader(DATADIR"/shaders/swirlytron.frag", GL_FRAGMENT_SHADER);
r->swirly_program = glCreateProgram();
glAttachShader(r->swirly_program, r->swirly_vert);
glAttachShader(r->swirly_program, r->swirly_frag);
glLinkProgram(r->swirly_program);
render_validate_shader(r->swirly_program);
}
static void render_delete_shaders(RenderContext *r) {
glDetachShader(r->lighting_program, r->lighting_frag);
glDetachShader(r->lighting_program, r->lighting_vert);
glDeleteShader(r->lighting_vert);
glDeleteShader(r->lighting_frag);
glDeleteProgram(r->lighting_program);
}
#define HEMI_ROUND_BITS 16
#define HEMI_UP_BITS 8
#define HEMI_NUM_VERTICES (4 * HEMI_ROUND_BITS * HEMI_UP_BITS)
#define ADD_VERTEX \
r->hemisphere_v[3*i + 0] = xv; \
r->hemisphere_v[3*i + 1] = yv; \
r->hemisphere_v[3*i + 2] = zv; \
r->hemisphere_n[3*i + 0] = xv; \
r->hemisphere_n[3*i + 1] = yv; \
r->hemisphere_n[3*i + 2] = zv; \
r->hemisphere_t[2*i + 0] = 0.5 + 0.5*sin(xv); \
r->hemisphere_t[2*i + 1] = 0.5 + 0.5*sin(yv); \
i++;
/* OpenGL initial setup */
RenderContext *render_setup(int width, int height, int disable_vbos, int disable_fbos, int disable_shaders) {
RenderContext *r;
r = malloc(sizeof(RenderContext));
if ( r == NULL ) return NULL;
glewInit();
if ( disable_vbos == 0 ) {
if ( GLEW_VERSION_1_5 || GLEW_ARB_vertex_buffer_object ) {
r->vbos = 1;
} else {
r->vbos = 0;
fprintf(stderr, "Vertex buffer objects are not supported by your graphics card (or maybe just by its driver).\n");
fprintf(stderr, " -> Geometry performance may be less than optimal.\n");
}
} else {
printf("The use of vertex buffer objects has been disabled at your request.\n");
r->vbos = 0;
}
if ( r->vbos == 0 ) {
fprintf(stderr, " -> Geometry performance may be less than optimal.\n");
}
if ( disable_fbos == 0 ) {
if ( GLEW_EXT_framebuffer_object ) {
r->fbos = 1;
} else {
r->fbos = 0;
fprintf(stderr, "Framebuffer objects are not supported by your graphics card (or maybe just by its driver).\n");
}
} else {
printf("The use of framebuffer objects has been disabled at your request.\n");
r->fbos = 0;
}
if ( r->fbos == 0 ) {
fprintf(stderr, " -> Reflections and GPU dynamic textures disabled.\n");
}
if ( disable_shaders == 0 ) {
if ( ( GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100 )
|| GLEW_VERSION_2_0 ) {
r->shaders = 1;
} else {
r->shaders = 0;
fprintf(stderr, "Shaders are not supported by your graphics card (or maybe just by its driver).\n");
}
} else {
printf("The use of shaders has been disabled at your request.\n");
r->shaders = 0;
}
if ( r->shaders == 0 ) {
fprintf(stderr, " -> Per-fragment lighting and GPU dynamic textures disabled.\n");
}
r->width = width;
r->height = height;
r->aspect = (GLfloat)width/(GLfloat)height;
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if ( r->fbos ) {
/* FBO for rendering swirlyness */
glGenFramebuffersEXT(1, &r->swirly_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo);
/* Add a (texture) colour buffer to the FBO */
glGenTextures(1, &r->swirly_texture);
glBindTexture(GL_TEXTURE_2D, r->swirly_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r->swirly_texture, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
/* Create coordinates for a hemisphere to reuse later */
r->hemisphere_v = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat));
r->hemisphere_n = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat));
r->hemisphere_t = malloc(2*HEMI_NUM_VERTICES*sizeof(GLfloat));
double step_up = M_PI_2/(double)HEMI_UP_BITS;
double step_round = 2*M_PI/(double)HEMI_ROUND_BITS;
int is, js;
int i = 0;
for ( is=0; is<HEMI_ROUND_BITS; is++ ) {
for ( js=0; js<HEMI_UP_BITS; js++ ) {
double theta, phi;
GLfloat xv, yv, zv;
theta = (2*M_PI/(double)HEMI_ROUND_BITS) * (double)is;
phi = (M_PI_2/(double)HEMI_UP_BITS) * (double)js;
xv = cos(theta)*cos(phi);
yv = sin(theta)*cos(phi);
zv = sin(phi);
ADD_VERTEX
xv = cos(theta+step_round)*cos(phi);
yv = sin(theta+step_round)*cos(phi);
zv = sin(phi);
ADD_VERTEX
xv = cos(theta+step_round)*cos(phi+step_up);
yv = sin(theta+step_round)*cos(phi+step_up);
zv = sin(phi+step_up);
ADD_VERTEX
xv = cos(theta)*cos(phi+step_up);
yv = sin(theta)*cos(phi+step_up);
zv = sin(phi+step_up);
ADD_VERTEX
}
}
if ( r->shaders ) render_load_shaders(r);
r->num_textures = 0;
/* Load misc texture bits */
texture_load(r, "radioactive");
texture_load(r, "fuel");
render_text_setup(r);
return r;
}
void render_shutdown(RenderContext *r) {
if ( r->shaders ) render_delete_shaders(r);
texture_free_all(r);
}
static GLenum render_gltype(PrimitiveType type) {
switch ( type ) {
case PRIMITIVE_QUADS : return GL_QUADS;
case PRIMITIVE_TRIANGLES : return GL_TRIANGLES;
default : break;
}
return GL_FALSE;
}
static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderContext *r, int *nvert, GLfloat alpha) {
int j;
Model *m;
GLfloat x, y, z;
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
int wibble;
if ( r->shaders ) glUniform1f(glGetUniformLocation(r->lighting_program, "alpha"), alpha);
if ( nvert == NULL ) nvert = &wibble;
m = instance->model;
if ( m == NULL ) return 0; /* No model to draw */
x = instance->x;
y = instance->y;
z = instance->z;
for ( j=0; j<m->num_primitives; j++ ) {
Primitive *p;
p = m->primitives[j];
if ( ((p->attribs & ATTRIB_PULSE) && !(p->attribs & ATTRIB_SWIRLY)) || ((p->attribs & ATTRIB_SWIRLY) && !(r->fbos && r->shaders)) ) {
float s;
s = fabsf(0.4*cosf(t * 0.001));
GLfloat c[] = {s*p->col_r, s*p->col_g, s*p->col_b, 1.0};
glMaterialfv(GL_FRONT, GL_EMISSION, c);
glColor4f(0.3, 0.3, 0.3, alpha);
} else if ( (p->attribs & ATTRIB_COLOUR) && !(p->attribs & ATTRIB_SWIRLY) ) {
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glColor4f(p->col_r, p->col_g, p->col_b, alpha);
} else {
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glColor4f(1.0, 1.0, 1.0, alpha);
}
/* Set specular colour and shininess if required */
if ( p->attribs & ATTRIB_SHINY ) {
GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, p->shininess); /* FIXME: Shader ignores this value */
} else {
glMaterialfv(GL_FRONT, GL_SPECULAR, black);
}
if ( p->type != PRIMITIVE_HEMISPHERE ) {
/* Location and orientation */
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */
/* Texture */
if ( p->attribs & ATTRIB_SWIRLY ) {
if ( r->fbos && r->shaders ) {
glBindTexture(GL_TEXTURE_2D, r->swirly_texture);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(glGetUniformLocation(r->lighting_program, "texture_emits"), 1);
} else {
if ( r->shaders ) {
glUniform1i(glGetUniformLocation(r->lighting_program, "texture_enabled"), 0);
} /* else don't enable texturing */
}
} else if ( p->texture != NULL ) {
Texture *texture;
texture = texture_lookup(r, p->texture);
if ( texture != NULL ) {
glBindTexture(GL_TEXTURE_2D, texture->texname);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if ( r->shaders ) glUniform1i(glGetUniformLocation(r->lighting_program, "texture_enabled"), 1);
} else {
if ( r->shaders ) glUniform1i(glGetUniformLocation(r->lighting_program, "texture_enabled"), 0);
}
} else {
if ( r->shaders ) glUniform1i(glGetUniformLocation(r->lighting_program, "texture_enabled"), 0);
}
if ( r->vbos ) {
glBindBuffer(GL_ARRAY_BUFFER, p->vertices_buffer);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, p->normals_buffer);
glNormalPointer(GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, p->texcoords_buffer);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} else {
glVertexPointer(3, GL_FLOAT, 0, p->vertices);
glNormalPointer(GL_FLOAT, 0, p->normals);
glTexCoordPointer(2, GL_FLOAT, 0, p->texcoords);
}
glDrawArrays(render_gltype(p->type), 0, p->num_vertices);
*nvert += p->num_vertices;
glDisable(GL_TEXTURE_2D);
glPopMatrix();
if ( p->attribs & ATTRIB_SWIRLY ) {
if ( r->shaders ) glUniform1i(glGetUniformLocation(r->lighting_program, "texture_emits"), 0);
}
} else {
glPushMatrix();
glTranslatef(x+p->vertices[0], y+p->vertices[1], z+p->vertices[2]);
glScalef(p->radius, p->radius, p->radius);
glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */
glEnable(GL_RESCALE_NORMAL);
glVertexPointer(3, GL_FLOAT, 0, r->hemisphere_v);
glNormalPointer(GL_FLOAT, 0, r->hemisphere_n);
glTexCoordPointer(2, GL_FLOAT, 0, r->hemisphere_t);
glDrawArrays(GL_QUADS, 0, HEMI_NUM_VERTICES);
*nvert += HEMI_NUM_VERTICES;
if ( r->shaders ) glUniform1i(glGetUniformLocation(r->lighting_program, "texture_emits"), 0);
glDisable(GL_RESCALE_NORMAL);
glPopMatrix();
}
}
return 0;
}
static void render_draw_line(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2) {
GLfloat col[] = {0.0, 0.3, 0.0, 1.0};
glMaterialfv(GL_FRONT, GL_EMISSION, col);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
}
static void render_draw_stuff(Game *game, Uint32 t, GLfloat alpha) {
int i;
int nvert = 0;
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for ( i=0; i<game->num_rooms; i++ ) {
Room *room;
int j;
room = game->rooms[i];
if ( room == NULL ) return;
//render_setup_lighting(game, room);
for ( j=0; j<room->num_objects; j++ ) {
GLfloat x, y, z;
if ( room->objects[j] == NULL ) continue;
x = room->rx - game->cur_room_x;
y = room->ry - game->cur_room_y;
z = room->rz - game->cur_room_z;
glPushMatrix();
glTranslatef(10.0*x, 10.0*y, 10.0*z);
render_model_instance_draw(room->objects[j], t, game->render, &nvert, alpha);
glPopMatrix();
}
}
//printf("%i ***\n", nvert);
glPopClientAttrib();
}
static void render_setup_lighting(Game *game) {
GLfloat pos[4];
GLfloat dir[3];
GLfloat ambient[4] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[4];
GLfloat specular[4];
glEnable(GL_LIGHTING);
/* Set a very dull background */
ambient[0] = 0.07; ambient[1] = 0.07; ambient[2] = 0.07; ambient[3] = 1.0;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
/* All lights have zero ambient (shader will ignore them) */
ambient[0] = 0.0; ambient[1] = 0.0; ambient[2] = 0.0;
/* Lander craft's headlight */
pos[0] = game->lander->x;
pos[1] = game->lander->y;
pos[2] = game->lander->z+0.3;
pos[3] = 1.0;
glLightfv(GL_LIGHT0, GL_POSITION, pos);
dir[0] = sinf(game->lander->yaw);
dir[1] = cosf(game->lander->yaw);
dir[2] = 0.0;
dir[3] = 1.0;
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20.0);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.02);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005);
/* Headlight colours */
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); /* Initialised to zero above */
diffuse[0] = 1.0; diffuse[1] = 1.0; diffuse[2] = 1.0; diffuse[3] = 1.0;
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
specular[0] = 1.0; specular[1] = 1.0; specular[2] = 1.0; specular[3] = 1.0;
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_LIGHT0);
/* A very dim fill-in light. Adds a tiny amount of shape to things, and lifts the floor a little */
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); /* Initialised to zero above */
pos[0] = -1.0; pos[1] = 0.8; pos[2] = 4.0; pos[3] = 0.0;
glLightfv(GL_LIGHT1, GL_POSITION, pos);
diffuse[0] = 0.05; diffuse[1] = 0.05; diffuse[2] = 0.05; diffuse[3] = 1.0;
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
specular[0] = 0.0; specular[1] = 0.0; specular[2] = 0.0; specular[3] = 1.0;
glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
glEnable(GL_LIGHT1);
/* A fill-in light, only used on its own, to light the lander craft itself */
glLightfv(GL_LIGHT2, GL_AMBIENT, ambient); /* Initialised to zero above */
pos[0] = -1.0; pos[1] = 0.8; pos[2] = 4.0; pos[3] = 0.0;
glLightfv(GL_LIGHT2, GL_POSITION, pos);
diffuse[0] = 0.3; diffuse[1] = 0.3; diffuse[2] = 0.3; diffuse[3] = 1.0;
glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse);
specular[0] = 1.0; specular[1] = 1.0; specular[2] = 1.0; specular[3] = 1.0;
glLightfv(GL_LIGHT2, GL_SPECULAR, specular);
}
void render_set_wireframe(int wireframe) {
if ( wireframe != 0 ) {
glPolygonMode(GL_FRONT, GL_LINE);
} else {
glPolygonMode(GL_FRONT, GL_FILL);
}
}
static void render_draw_2d(RenderContext *r, Game *game) {
Texture *texture;
GLfloat cr, cg, cb;
/* Set up transforms for 2D rendering */
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(0.95, 0.95, 1.0);
/* Radiation symbol */
texture = texture_lookup(r, "radioactive");
if ( texture != NULL ) {
glBindTexture(GL_TEXTURE_2D, texture->texname);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
glColor4f(1.0, 1.0, 1.0, PANEL_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(-1.0, -1.0); /* Bottom left */
glTexCoord2f(1.0, 0.0);
glVertex2f(-0.9, -1.0); /* Bottom right */
glTexCoord2f(1.0, 1.0);
glVertex2f(-0.9, -1.0+0.1*r->aspect); /* Top right */
glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0, -1.0+0.1*r->aspect); /* Top left */
glEnd();
glDisable(GL_TEXTURE_2D);
/* Radiation meter */
cg = 1.0 - game->radiation;
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 0.0, PANEL_ALPHA);
glVertex2f(-1.0, -0.8); /* Bottom left */
glVertex2f(-0.9, -0.8); /* Bottom right */
glColor4f(0.8, cg, 0.0, PANEL_ALPHA);
glVertex2f(-0.9, -0.8+(1.8*game->radiation)); /* Top right */
glVertex2f(-1.0, -0.8+(1.8*game->radiation)); /* Top left */
glEnd();
/* Fuel symbol */
texture = texture_lookup(r, "fuel");
if ( texture != NULL ) {
glBindTexture(GL_TEXTURE_2D, texture->texname);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
glColor4f(1.0, 1.0, 1.0, PANEL_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(0.9, -1.0); /* Bottom left */
glTexCoord2f(1.0, 0.0);
glVertex2f(1.0, -1.0); /* Bottom right */
glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, -1.0+0.1*r->aspect); /* Top right */
glTexCoord2f(0.0, 1.0);
glVertex2f(0.9, -1.0+0.1*r->aspect); /* Top left */
glEnd();
glDisable(GL_TEXTURE_2D);
/* Fuel meter */
cr = 1.0 - game->fuel;
cg = game->fuel;
cb = 0.2 + 0.1*game->fuel;
glBegin(GL_QUADS);
glColor4f(1.0, 0.0, 0.2, PANEL_ALPHA);
glVertex2f(0.9, -0.8); /* Bottom left */
glVertex2f(1.0, -0.8); /* Bottom right */
glColor4f(cr, cg, cb, PANEL_ALPHA);
glVertex2f(1.0, -0.8+(1.8*game->fuel)); /* Top right */
glVertex2f(0.9, -0.8+(1.8*game->fuel)); /* Top left */
glEnd();
Room *room;
room = game_find_room(game, game->cur_room_x, game->cur_room_y, game->cur_room_z);
if ( room != NULL ) {
if ( room->comment != NULL ) render_text_write(-1.0, 1.0, room->comment, r);
}
}
void render_draw(Game *game, Uint32 t) {
RenderContext *r;
GLfloat amb[] = { 0.0, 0.0, 0.0, 1.0 };
r = game->render;
if ( r->fbos ) {
/* Render some swirlyness */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 64, 64);
if ( r->shaders ) glUseProgram(r->swirly_program);
if ( r->shaders ) glUniform1f(glGetUniformLocation(game->render->swirly_program, "time"), t);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->swirly_program, "landed"), game->lander->recharging);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->swirly_program, "rechargeripple"), game->lander->recharging);
if ( r->shaders ) glUniform2f(glGetUniformLocation(game->render->swirly_program, "lander"), game->platform_rel_x, game->platform_rel_y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glVertex2f(-1.0, -1.0);
glVertex2f(+1.0, -1.0);
glVertex2f(+1.0, +1.0);
glVertex2f(-1.0, +1.0);
glEnd();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
/* Main view */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, r->width, r->height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, (GLfloat)r->width/(GLfloat)r->height, 0.1, 100.0); /* Depth buffer 10cm to 100m */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(game->lander->x-(game->view_dist*sinf(game->lander->yaw)*cosf(game->view_angle)),
game->lander->y-(game->view_dist*cosf(game->lander->yaw)*cosf(game->view_angle)),
game->lander->z+(game->view_dist*sinf(game->view_angle)),
game->lander->x, game->lander->y, game->lander->z,
sqrtf(2.0)*sinf(game->lander->yaw)*sinf(game->view_angle),
sqrtf(2.0)*cosf(game->lander->yaw)*sinf(game->view_angle), sqrtf(2.0)*cosf(game->view_angle));
if ( r->shaders ) glUseProgram(r->lighting_program);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "texture"), 0); /* Using GL_TEXTURE0 */
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "texture_only"), 0);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "texture_enabled"), 1);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "fill_light_enabled"), 0);
render_setup_lighting(game);
amb[0] = 0.02; amb[1] = 0.02; amb[2] = 0.02;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
render_draw_stuff(game, t, 1.0);
/* Draw the lander */
glEnable(GL_LIGHT2);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT1);
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "fill_light_enabled"), 1);
render_model_instance_draw(game->lander, t, r, NULL, 1.0);
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "fill_light_enabled"), 0);
glPopClientAttrib();
if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->lighting_program, "texture_enabled"), 0);
render_draw_line(game->lander->x, game->lander->y, game->lander->z, game->lander->x, game->lander->y, game->lander->z-200.0);
glDisable(GL_LIGHT2);
/* Back faces (done last to make blending work properly) */
if ( r->shaders ) glUseProgram(0); /* Speed things up a bit by not using per-fragment lighting for back faces */
glFrontFace(GL_CW);
glPolygonOffset(1.0, 1.0);
glEnable(GL_POLYGON_OFFSET_FILL);
render_setup_lighting(game);
amb[0] = 0.1; amb[1] = 0.1; amb[2] = 0.1;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
render_draw_stuff(game, t, 0.5);
glDisable(GL_POLYGON_OFFSET_FILL);
glFrontFace(GL_CCW);
render_draw_2d(r, game);
SDL_GL_SwapBuffers();
}
|