aboutsummaryrefslogtreecommitdiff
path: root/src/model.c
blob: 1432f62357d449138cf7b5fab080320a625f798e (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
/*
 * model.c
 *
 * Basic functions to handle models
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glew.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

/* Maximum number of vertices per primitive */
#define MAX_VERTICES 100000

ModelContext *model_init() {

	ModelContext *ctx;
	
	ctx = malloc(sizeof(ModelContext));
	if ( ctx == NULL ) return NULL;
	
	ctx->num_models = 0;
	ctx->models = NULL;
	
	return ctx;

}

static int model_add(ModelContext *ctx, Model *model) {

	ctx->models = realloc(ctx->models, (ctx->num_models+1)*sizeof(Model *));
	if ( ctx->models == NULL ) {
		return 1;
	}
	ctx->models[ctx->num_models] = model;
	ctx->num_models++;
	
	return 0;

}

static Model *model_new(const char *name) {

	Model *model;
	
	model = malloc(sizeof(Model));
	if ( model == NULL ) return NULL;
	
	model->num_primitives = 0;
	model->attrib_total = ATTRIB_NONE;
	model->primitives = NULL;
	model->name = strdup(name);
	
	return model;


}

static Primitive *model_add_primitive(Model *model, GLenum type, GLfloat *vertices, GLfloat *normals, GLfloat *texcoords,
					int n, PrimitiveAttrib attribs, GLfloat r, GLfloat g, GLfloat b, char *texture,
					GLfloat radius, GLfloat shininess, int vbos) {
	
	Primitive *p;
	
	/* Sanity check */
	if ( type == PRIMITIVE_HEMISPHERE ) {
		if ( !(attribs & ATTRIB_RADIUS) ) {
			fprintf(stderr, "Radius must be specified for all hemispheres in model '%s'\n", model->name);
			return NULL;
		}
	}
	
	p = malloc(sizeof(Primitive));
	if ( p == NULL ) return NULL;
	
	p->vertices = malloc(n * sizeof(GLfloat) * 3);
	if ( p->vertices == NULL ) {
		free(p);
		return NULL;
	}
	
	p->normals = malloc(n * sizeof(GLfloat) * 3);
	if ( p->normals == NULL ) {
		free(p);
		free(p->vertices);
		return NULL;
	}
	
	p->texcoords = malloc(n * sizeof(GLfloat) * 2);
	if ( p->texcoords == NULL ) {
		free(p);
		free(p->vertices);
		free(p->normals);
		return NULL;
	}
	
	/* Copy the vertices into memory */
	memcpy(p->vertices, vertices, 3*n*sizeof(GLfloat));
	memcpy(p->normals, normals, 3*n*sizeof(GLfloat));
	memcpy(p->texcoords, texcoords, 2*n*sizeof(GLfloat));
	p->num_vertices = n;
	p->type = type;
	p->attribs = attribs;
	p->col_r = r;
	p->col_g = g;
	p->col_b = b;
	p->texture = texture;
	p->radius = radius;
	p->shininess = shininess;
	
	if ( vbos && ( type != PRIMITIVE_HEMISPHERE ) ) {
	
		/* FIXME: Move this snippet to render.c is some tidy way */
		
		glGenBuffers(1, &p->vertices_buffer);
		glBindBuffer(GL_ARRAY_BUFFER, p->vertices_buffer);
		glBufferData(GL_ARRAY_BUFFER, 3*p->num_vertices*sizeof(GLfloat), p->vertices, GL_STATIC_DRAW);
	
		glGenBuffers(1, &p->normals_buffer);
		glBindBuffer(GL_ARRAY_BUFFER, p->normals_buffer);
		glBufferData(GL_ARRAY_BUFFER, 3*p->num_vertices*sizeof(GLfloat), p->normals, GL_STATIC_DRAW);
	
		glGenBuffers(1, &p->texcoords_buffer);
		glBindBuffer(GL_ARRAY_BUFFER, p->texcoords_buffer);
		glBufferData(GL_ARRAY_BUFFER, 2*p->num_vertices*sizeof(GLfloat), p->texcoords, GL_STATIC_DRAW);
	
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	
	}
	
	model->attrib_total = model->attrib_total | attribs;
	model->primitives = realloc(model->primitives, sizeof(Primitive *) * (model->num_primitives+1));
	model->primitives[model->num_primitives] = p;
	model->num_primitives++;
	
	return 0;

}

static void model_calculate_normals(GLfloat *vertices, GLfloat *normals, int first, int last, int centre, int v1, int v2) {
	
	GLfloat ax, ay, az;
	GLfloat bx, by, bz;
	GLfloat nx, ny, nz, n;
	unsigned int i;
	
	ax = vertices[3*v1+0] - vertices[3*centre+0];
	ay = vertices[3*v1+1] - vertices[3*centre+1];
	az = vertices[3*v1+2] - vertices[3*centre+2];
	bx = vertices[3*v2+0] - vertices[3*centre+0];
	by = vertices[3*v2+1] - vertices[3*centre+1];
	bz = vertices[3*v2+2] - vertices[3*centre+2];
	nx = ay*bz - az*by;
	ny = - ax*bz + az*bx;
	nz = ax*by - ay*bx;
	n = sqrtf(nx*nx + ny*ny + nz*nz);
	nx = nx / n;  ny = ny / n;  nz = nz / n;
	
	for ( i=first; i<=last; i++ ) {
		normals[3*i+0] = nx;
		normals[3*i+1] = ny;
		normals[3*i+2] = nz;
	}
	
}

static int model_load(ModelContext *ctx, const char *name, RenderContext *render) {

	FILE *fh;
	char tmp[64];
	Model *model;
	PrimitiveType type;
	int num_vertices;
	GLfloat *vertices;
	GLfloat *normals;
	GLfloat *texcoords;
	GLfloat col_r = 0.0;
	GLfloat col_g = 0.0;
	GLfloat col_b = 0.0;
	GLfloat radius = 0.0;
	GLfloat shininess = 100.0;
	PrimitiveAttrib attribs;
	char *texture;
	int subdiv;
	
	/* Don't subdivide the geometry if we're doing per-fragment lighting anyway */
	if ( render->shaders ) {
		subdiv = 0;
	} else {
		subdiv = 1;
	}
	
	snprintf(tmp, 63, "%s/models/%s", DATADIR, name);
	fh = fopen(tmp, "r");
	if ( fh == NULL ) {
		return -1;
	}
	
	model = model_new(name);
	vertices = malloc(MAX_VERTICES*3*sizeof(GLfloat));
	normals = malloc(MAX_VERTICES*3*sizeof(GLfloat));
	texcoords = malloc(MAX_VERTICES*2*sizeof(GLfloat));
	num_vertices = 0;
	type = PRIMITIVE_TRIANGLES;
	attribs = ATTRIB_NONE;
	texture = NULL;
	while ( !feof(fh) ) {
		
		char line[1024];
		GLfloat x, y, z;
		GLfloat r, g, b;
		GLfloat texx, texy, forget;
		
		texx = 0.0;  texy = 0.0;	/* Default texture coordinates */
		
		fgets(line, 1023, fh);
		
		if ( line[0] == '#' ) {
			continue;
		}
		
		if ( line[0] == '\n' ) {
			if ( num_vertices > 0 ) {
				model_add_primitive(model, type, vertices, normals, texcoords, num_vertices,
							attribs, col_r, col_g, col_b, texture, radius, shininess, render->vbos);
				num_vertices = 0;
				type = PRIMITIVE_TRIANGLES;
				attribs = ATTRIB_NONE;
				texture = NULL;
			}
		}
		
		if ( strncmp(line, "QUADS", 5) == 0 ) {
			type = PRIMITIVE_QUADS;
		}
		if ( strncmp(line, "TRIANGLES", 9) == 0 ) {
			type = PRIMITIVE_TRIANGLES;
		}
		if ( strncmp(line, "HEMISPHERE", 10) == 0 ) {
			type = PRIMITIVE_HEMISPHERE;
		}
		
		if ( sscanf(line, "%f %f %f %f %f", &forget, &forget, &forget, &x, &y) == 5 ) {
			texx = x;  texy = y;
		}
		
		if ( sscanf(line, "%f %f %f", &x, &y, &z) == 3 ) {
			vertices[3*num_vertices+0] = x;
			vertices[3*num_vertices+1] = y;
			vertices[3*num_vertices+2] = z;
			texcoords[2*num_vertices+0] = texx;
			texcoords[2*num_vertices+1] = texy;
			num_vertices++;
			if ( (type == PRIMITIVE_QUADS) && ((num_vertices % 4)==0) ) {
				model_calculate_normals(vertices, normals, num_vertices-4, num_vertices-1,
							num_vertices-4, num_vertices-3, num_vertices-2);
			}
			if ( (type == PRIMITIVE_TRIANGLES) && ((num_vertices % 3)==0) ) {
				model_calculate_normals(vertices, normals, num_vertices-3, num_vertices-1,
							num_vertices-3, num_vertices-2, num_vertices-1);
			}
			if ( num_vertices > MAX_VERTICES ) {
				fprintf(stderr, "Too many vertices in primitive\n");
				return 1;
			}
		}
		
		/* Subdivide the previous face if requested */
		if ( subdiv && (sscanf(line, "subdivide %f %f", &x, &y) == 2) ) {
			if ( type == PRIMITIVE_QUADS ) {
				if ( (num_vertices % 4)==0 ) {
					GLfloat u, v;
					GLfloat v1x, v1y, v1z;
					GLfloat v2x, v2y, v2z;
					GLfloat v3x, v3y, v3z;
					GLfloat v4x, v4y, v4z;
					GLfloat nx, ny, nz;
					GLfloat t1x, t1y;
					GLfloat t2x, t2y;
					GLfloat t3x, t3y;
					GLfloat t4x, t4y;
					nx = normals[3*(num_vertices-1) + 0];
					ny = normals[3*(num_vertices-1) + 1];
					nz = normals[3*(num_vertices-1) + 2];
					v4x = vertices[3*(num_vertices-1) + 0];
					v4y = vertices[3*(num_vertices-1) + 1];
					v4z = vertices[3*(num_vertices-1) + 2];
					v3x = vertices[3*(num_vertices-2) + 0];
					v3y = vertices[3*(num_vertices-2) + 1];
					v3z = vertices[3*(num_vertices-2) + 2];
					v2x = vertices[3*(num_vertices-3) + 0];
					v2y = vertices[3*(num_vertices-3) + 1];
					v2z = vertices[3*(num_vertices-3) + 2];
					v1x = vertices[3*(num_vertices-4) + 0];
					v1y = vertices[3*(num_vertices-4) + 1];
					v1z = vertices[3*(num_vertices-4) + 2];
					t4x = texcoords[2*(num_vertices-1) + 0];
					t4y = texcoords[2*(num_vertices-1) + 1];
					t3x = texcoords[2*(num_vertices-2) + 0];
					t3y = texcoords[2*(num_vertices-2) + 1];
					t2x = texcoords[2*(num_vertices-3) + 0];
					t2y = texcoords[2*(num_vertices-3) + 1];
					t1x = texcoords[2*(num_vertices-4) + 0];
					t1y = texcoords[2*(num_vertices-4) + 1];
					if ( num_vertices - 4 + x*y*4 > MAX_VERTICES ) {
						fprintf(stderr, "Not enough free vertex space to subdivide face\n");
					}
					num_vertices -= 4;
					for ( u=0; u<x; u++ ) {
						for ( v=0; v<y; v++ ) {
							GLfloat l1x, l1y, l1z;
							GLfloat l2x, l2y, l2z;
							GLfloat l3x, l3y, l3z;
							GLfloat l4x, l4y, l4z;
							GLfloat tl1x, tl1y;
							GLfloat tl2x, tl2y;
							GLfloat tl3x, tl3y;
							GLfloat tl4x, tl4y;
							/* Interpolate vertices */
							l1x = v1x + u*(v2x-v1x)/x;
							l1y = v1y + u*(v2y-v1y)/x;
							l1z = v1z + u*(v2z-v1z)/x;
							l2x = v4x + u*(v3x-v4x)/x;
							l2y = v4y + u*(v3y-v4y)/x;
							l2z = v4z + u*(v3z-v4z)/x;
							l3x = v1x + (u+1)*(v2x-v1x)/x;
							l3y = v1y + (u+1)*(v2y-v1y)/x;
							l3z = v1z + (u+1)*(v2z-v1z)/x;
							l4x = v4x + (u+1)*(v3x-v4x)/x;
							l4y = v4y + (u+1)*(v3y-v4y)/x;
							l4z = v4z + (u+1)*(v3z-v4z)/x;
							/* Interpolate texture coords */
							tl1x = t1x + u*(t2x-t1x)/x;
							tl1y = t1y + u*(t2y-t1y)/x;
							tl2x = t4x + u*(t3x-t4x)/x;
							tl2y = t4y + u*(t3y-t4y)/x;
							tl3x = t1x + (u+1)*(t2x-t1x)/x;
							tl3y = t1y + (u+1)*(t2y-t1y)/x;
							tl4x = t4x + (u+1)*(t3x-t4x)/x;
							tl4y = t4y + (u+1)*(t3y-t4y)/x;
							/* Calculate vertices */
							vertices[3*num_vertices+0] = l1x + v*(l2x-l1x)/y;
							vertices[3*num_vertices+1] = l1y + v*(l2y-l1y)/y;
							vertices[3*num_vertices+2] = l1z + v*(l2z-l1z)/y;
							texcoords[2*num_vertices+0] = tl1x + v*(tl2x-tl1x)/y;
							texcoords[2*num_vertices+1] = tl1y + v*(tl2y-tl1y)/y;
							normals[3*num_vertices+0] = nx;
							normals[3*num_vertices+1] = ny;
							normals[3*num_vertices+2] = nz;
							num_vertices++;
							vertices[3*num_vertices+0] = l3x + v*(l4x-l3x)/y;
							vertices[3*num_vertices+1] = l3y + v*(l4y-l3y)/y;
							vertices[3*num_vertices+2] = l3z + v*(l4z-l3z)/y;
							texcoords[2*num_vertices+0] = tl3x + v*(tl4x-tl3x)/y;
							texcoords[2*num_vertices+1] = tl3y + v*(tl4y-tl3y)/y;
							normals[3*num_vertices+0] = nx;
							normals[3*num_vertices+1] = ny;
							normals[3*num_vertices+2] = nz;
							num_vertices++;
							vertices[3*num_vertices+0] = l3x + (v+1)*(l4x-l3x)/y;
							vertices[3*num_vertices+1] = l3y + (v+1)*(l4y-l3y)/y;
							vertices[3*num_vertices+2] = l3z + (v+1)*(l4z-l3z)/y;
							texcoords[2*num_vertices+0] = tl3x + (v+1)*(tl4x-tl3x)/y;
							texcoords[2*num_vertices+1] = tl3y + (v+1)*(tl4y-tl3y)/y;
							normals[3*num_vertices+0] = nx;
							normals[3*num_vertices+1] = ny;
							normals[3*num_vertices+2] = nz;
							num_vertices++;
							vertices[3*num_vertices+0] = l1x + (v+1)*(l2x-l1x)/y;
							vertices[3*num_vertices+1] = l1y + (v+1)*(l2y-l1y)/y;
							vertices[3*num_vertices+2] = l1z + (v+1)*(l2z-l1z)/y;
							texcoords[2*num_vertices+0] = tl1x + (v+1)*(tl2x-tl1x)/y;
							texcoords[2*num_vertices+1] = tl1y + (v+1)*(tl2y-tl1y)/y;
							normals[3*num_vertices+0] = nx;
							normals[3*num_vertices+1] = ny;
							normals[3*num_vertices+2] = nz;
							num_vertices++;
						}
					}
				} else {
					fprintf(stderr, "'subdivide' directive must come at a quad boundary\n");
					return 1;
				}
			} else {
				fprintf(stderr, "Can't subdivide this type of primitive\n");
				return 1;
			}
		}
		
		if ( sscanf(line, "pulse %f %f %f", &r, &g, &b) == 3 ) {
			attribs = attribs | ATTRIB_COLOUR;
			attribs = attribs | ATTRIB_PULSE;
			col_r = r;  col_g = g;  col_b = b;
		}
		if ( sscanf(line, "colour %f %f %f", &r, &g, &b) == 3 ) {
			attribs = attribs | ATTRIB_COLOUR;
			col_r = r;  col_g = g;  col_b = b;
		}
		if ( sscanf(line, "radius %f", &radius) == 1 ) {
			attribs = attribs | ATTRIB_RADIUS;
		}
		if ( sscanf(line, "shiny %f", &shininess) == 1 ) {
			attribs = attribs | ATTRIB_SHINY;
		}
		if ( strncmp(line, "swirly", 6) == 0 ) {
			attribs = attribs | ATTRIB_SWIRLY;
		}
		if ( strncmp(line, "texture", 7) == 0 ) {
			if ( strlen(line) < 9 ) {
				fprintf(stderr, "Invalid texture specification\n");
				return 1;
			}
			texture = strdup(line+8);
			chomp(texture);
			if ( texture_lookup(render, texture) == NULL ) {
				texture_load(render, texture);
				if ( texture_lookup(render, texture) == NULL ) {
					fprintf(stderr, "Couldn't find texture %s\n", texture);
				}
			}
		}

	}
	
	fclose(fh);

	return model_add(ctx, model);
	
}

static Model *model_lookup(ModelContext *ctx, const char *name) {
	
	int i, found;
	
	found = 0;
	for ( i=0; i<ctx->num_models; i++ ) {
		if ( strcmp(ctx->models[i]->name, name) == 0 ) {
			found = 1;
			break;
		}
	}
	
	if ( found == 0 ) {
		return NULL;
	}
	
	return ctx->models[i];
}

ModelInstance *model_instance_new(ModelContext *ctx, const char *name, RenderContext *render) {

	ModelInstance *instance;
	
	instance = malloc(sizeof(ModelInstance));
	instance->model = model_lookup(ctx, name);
	
	if ( instance->model == NULL ) {
		/* Couldn't find model, so try to load it */
		model_load(ctx, name, render);
		instance->model = model_lookup(ctx, name);
	}
	
	instance->vx = 0.0;
	instance->vy = 0.0;
	instance->vz = 0.0;
	instance->yaw = 0.0;
	instance->yawspeed = 0.0;
	instance->landed = 0;
	instance->recharging = 0;
	
	return instance;

}