aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: b765e633d5371214f2fdf4b12431026a9d22a16b (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
/*
 * 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;
	
	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);
	glGetInfoLogARB(shader, 4095, &l, text);
	if ( l > 0 ) {
		printf("%s\n", text); fflush(stdout);
	}
	
	return shader;
	
}

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

static void render_delete_shaders(RenderContext *ctx) {

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

}

/* OpenGL initial setup */
RenderContext *render_setup(int width, int height) {
	
	RenderContext *ctx;
	
	ctx = malloc(sizeof(RenderContext));
	if ( ctx == NULL ) return NULL;
	
	ctx->width = width;
	ctx->height = 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, &ctx->fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ctx->fbo);
 	/* Add a (texture) colour buffer to the FBO */
	glGenTextures(1, &ctx->fbotex);
	glBindTexture(GL_TEXTURE_2D, ctx->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, ctx->fbotex, 0);
	/* Add a depth buffer to the FBO */
	glGenRenderbuffersEXT(1, &ctx->fbodepth);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ctx->fbodepth);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, 256, 256);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, ctx->fbodepth);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, ctx->fbodepth);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	
	render_load_shaders(ctx);
	
	ctx->num_textures = 0;
	
	return ctx;
	
}

void render_shutdown(RenderContext *ctx) {
	render_delete_shaders(ctx);
	texture_free_all(ctx);
}

static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderContext *ctx) {

	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);
			glMaterialfv(GL_FRONT, GL_SPECULAR, black);
		} else if ( p->attribs & ATTRIB_COLOUR ) {
			glMaterialfv(GL_FRONT, GL_EMISSION, black);
			glMaterialfv(GL_FRONT, GL_SPECULAR, black);
			glColor3f(p->col_r, p->col_g, p->col_b);
		} else {
			glMaterialfv(GL_FRONT, GL_EMISSION, black);
			glMaterialfv(GL_FRONT, GL_SPECULAR, black);
			glColor3f(1.0, 1.0, 1.0);
		}
		
		/* 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(ctx, 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(p->type, 0, p->num_vertices);
		
		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[] = {0.8, 0.8, 0.8, 1.0};
	GLfloat specular[] = {0.8, 0.8, 0.8, 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);
	
}

void render_draw(Game *game, Uint32 t) {

	/* First pass: Looking upwards */
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, game->render->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, 100.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, game->render->width, game->render->height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(50.0, (GLfloat)game->render->width/(GLfloat)game->render->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 */
	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, game->render);
	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);
	
	glClear(GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glBindTexture(GL_TEXTURE_2D, game->render->fbotex);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0);
		glVertex2f(-1.0, -1.0);
		glTexCoord2f(1.0, 0.0);
		glVertex2f(-0.5, -1.0);
		glTexCoord2f(1.0, 1.0);
		glVertex2f(-0.5, -0.5);
		glTexCoord2f(0.0, 1.0);
		glVertex2f(-1.0, -0.5);
	glEnd();
	glDisable(GL_TEXTURE_2D);
	
	SDL_GL_SwapBuffers();
	
}