aboutsummaryrefslogtreecommitdiff
path: root/src/model.c
blob: 5607e9544294c61cd96ad901fe3946c79f060785 (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
/*
 * 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 <gl.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 4096

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) {

	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;
	
	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;
	
	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);
				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;
			}
		}
		
		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, "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;
	
	return instance;

}