aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: 07bdbd19346b2e4b8b53d8e06e99e0fecfbebe66 (plain)
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
/*
 * 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

#define GL_GLEXT_PROTOTYPES 1
#include <gl.h>
#include <glext.h>
#include <SDL.h>
#include <glu.h>
#include <math.h>
#include <stdio.h>

#include "model.h"
#include "game.h"
#include "render.h"
#include "texture.h"
#include "utils.h"

/* Utility function to load and compile a shader, checking the info log */
static GLhandleARB render_load_shader(const char *filename, GLenum type) {

	GLhandleARB 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 GLcharARB *source = text;
	shader = glCreateShaderObjectARB(type);
	glShaderSourceARB(shader, 1, &source, NULL);
	glCompileShaderARB(shader);
	glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); 
	if ( status == GL_FALSE ) {
		glGetInfoLogARB(shader, 4095, &l, text);
		if ( l > 0 ) {
			printf("%s\n", text); fflush(stdout);
		}
	}
	
	return shader;
	
}

static void render_load_shaders(RenderContext *r) {
	
	r->lighting_vert = render_load_shader(DATADIR"/shaders/lighting.vert", GL_VERTEX_SHADER_ARB);
	r->lighting_frag = render_load_shader(DATADIR"/shaders/lighting.frag", GL_FRAGMENT_SHADER_ARB);
	r->lighting_program = glCreateProgramObjectARB();
	glAttachObjectARB(r->lighting_program, r->lighting_vert);
	glAttachObjectARB(r->lighting_program, r->lighting_frag);
	glLinkProgramARB(r->lighting_program);
	
}

static void render_delete_shaders(RenderContext *r) {

	glDetachObjectARB(r->lighting_program, r->lighting_frag);
	glDetachObjectARB(r->lighting_program, r->lighting_vert);
	glDeleteObjectARB(r->lighting_vert);
	glDeleteObjectARB(r->lighting_frag);
	glDeleteObjectARB(r->lighting_program);

}

#define HEMI_ROUND_BITS 64
#define HEMI_UP_BITS 32
#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;	\
	i++;

/* OpenGL initial setup */
RenderContext *render_setup(int width, int height) {
	
	RenderContext *r;
	
	r = malloc(sizeof(RenderContext));
	if ( r == NULL ) return NULL;
	
	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);
	
	/* Create a small FBO for rendering reflections with */
	glGenFramebuffersEXT(1, &r->fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->fbo);
 	/* Add a (texture) colour buffer to the FBO */
	glGenTextures(1, &r->fbotex);
	glBindTexture(GL_TEXTURE_2D, r->fbotex);
	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, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r->fbotex, 0);
	/* Add a depth buffer to the FBO */
	glGenRenderbuffersEXT(1, &r->fbodepth);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, r->fbodepth);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, 256, 256);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, r->fbodepth);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, r->fbodepth);
	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));
	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
		}
	}
	
	render_load_shaders(r);
	
	r->num_textures = 0;
	
	/* Load misc texture bits */
	texture_load(r, "radioactive");
	texture_load(r, "fuel");
	
	return r;
	
}

void render_shutdown(RenderContext *r) {
	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 j;
	Model *m;
	GLfloat x, y, z;
	GLfloat black[] = {0.0, 0.0, 0.0};
			
	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 ) {
			float s;
			s = fabsf(cosf(t * 0.001));
			GLfloat c[] = {s*p->col_r, s*p->col_g, s*p->col_b};
			glMaterialfv(GL_FRONT, GL_EMISSION, c);
			glColor3f(0.3, 0.3, 0.3);
		} else if ( p->attribs & ATTRIB_COLOUR ) {
			glMaterialfv(GL_FRONT, GL_EMISSION, black);
			glColor3f(p->col_r, p->col_g, p->col_b);
		} else {
			glMaterialfv(GL_FRONT, GL_EMISSION, black);
			glColor3f(1.0, 1.0, 1.0);
		}
		if ( p->attribs & ATTRIB_SHINY ) {
			GLfloat white[] = {1.0, 1.0, 1.0};
			glMaterialfv(GL_FRONT, GL_SPECULAR, white);
			glMaterialf(GL_FRONT, GL_SHININESS, p->shininess);
		} 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->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);
				}
			}
		
			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);
			
		} else {
		
			glPushMatrix();
			glTranslatef(x+p->vertices[0], y+p->vertices[1], z+p->vertices[2]);
			glScalef(p->radius, p->radius, p->radius);
			glEnable(GL_RESCALE_NORMAL);
			glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0);	/* Minus sign defines +yaw as "right" */
			glVertexPointer(3, GL_FLOAT, 0, r->hemisphere_v);
			glNormalPointer(GL_FLOAT, 0, r->hemisphere_n);
			glEnable(GL_TEXTURE_GEN_S);
			glEnable(GL_TEXTURE_GEN_T);
			glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
			glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
			glBindTexture(GL_TEXTURE_2D, r->fbotex);
			glEnable(GL_TEXTURE_2D);
			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
			glDrawArrays(GL_QUADS, 0, HEMI_NUM_VERTICES);
			glDisable(GL_TEXTURE_GEN_S);
			glDisable(GL_TEXTURE_GEN_T);
			glDisable(GL_RESCALE_NORMAL);
			
		}
		
		glPopMatrix();
		
		glDisable(GL_TEXTURE_2D);
		
	}

	return 0;

}

static void render_draw_line(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2) {

	GLfloat red[] = {1.0, 0.0, 0.0, 1.0};

	glMaterialfv(GL_FRONT, GL_EMISSION, red);
	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) {
	
	int i;
	
	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);
			glPopMatrix();
		}
		
	}
	
	glPopClientAttrib();
	
}

static void render_setup_lighting(Game *game) {

	GLfloat pos[] = {-1.0, -0.8, 1.3, 0.0};
	GLfloat ambient[4];
	GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
	
	glEnable(GL_LIGHTING);
	
	ambient[0] = 0.3;  ambient[1] = 0.3;  ambient[2] = 0.3;  ambient[3] = 1.0;
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_POSITION, pos);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	glEnable(GL_LIGHT0);
	
}

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, 0.5);
	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, 0.5);
		glVertex2f(-1.0, -0.8);			/* Bottom left */
		glVertex2f(-0.9, -0.8);			/* Bottom right */
		glColor4f(0.8, cg, 0.0, 0.5+0.5*game->radiation);
		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, 0.5);
	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, 0.5);
		glVertex2f(0.9, -0.8);			/* Bottom left */
		glVertex2f(1.0, -0.8);			/* Bottom right */
		glColor4f(cr, cg, cb, 0.5);
		glVertex2f(1.0, -0.8+(1.8*game->fuel));	/* Top right */ 
		glVertex2f(0.9, -0.8+(1.8*game->fuel));	/* Top left */
	glEnd();
	
}

void render_draw(Game *game, Uint32 t) {

	RenderContext *r;
	
	r = game->render;
	
	/* First pass: Looking upwards */
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->fbo);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0, 0, 256, 256);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70.0, 1.0, 0.1, 180.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(game->lander->x, game->lander->y, game->lander->z,
		  game->lander->x, game->lander->y, game->lander->z+10.0,
		  sqrtf(2.0)*sinf(game->lander->yaw), sqrtf(2.0)*cosf(game->lander->yaw), 0.0);
	render_setup_lighting(game);
	render_draw_stuff(game, t);
	
	/* Second pass: Main view */
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	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));	
	render_setup_lighting(game);
	render_draw_stuff(game, t);
	
	/* Finally, draw the lander */
//	glUseProgramObjectARB(r->lighting_program);
//	glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0);
	glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	render_model_instance_draw(game->lander, t, r);
	glPopClientAttrib();
	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_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
//	glUseProgramObjectARB(0);
	
	render_draw_2d(r, game);
	
	SDL_GL_SwapBuffers();
	
}