File: readlwo.c

package info (click to toggle)
lightspeed 1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,052 kB
  • ctags: 1,193
  • sloc: ansic: 11,604; sh: 327; makefile: 205; sed: 93
file content (477 lines) | stat: -rw-r--r-- 9,748 bytes parent folder | download | duplicates (7)
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
/* readlwo.c */

/*
 * Copyright (C) 1998 Janne Lf <jlof@mail.student.oulu.fi>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This is a heavily modified version of Janne's LWO reader library, as
 * included in the GtkGLArea distribution (gtkglarea-1.0/examples/lw.c)
 */


#include <stdio.h>
#include <math.h>
#include "readlwo.h"

#define MK_ID(a,b,c,d) ((((guint32)(a))<<24)| \
(((guint32)(b))<<16)| \
(((guint32)(c))<< 8)| \
(((guint32)(d))    ))

#define ID_FORM MK_ID('F','O','R','M')
#define ID_LWOB MK_ID('L','W','O','B')
#define ID_PNTS MK_ID('P','N','T','S')
#define ID_SRFS MK_ID('S','R','F','S')
#define ID_SURF MK_ID('S','U','R','F')
#define ID_POLS MK_ID('P','O','L','S')
#define ID_COLR MK_ID('C','O','L','R')

static gint32
read_char(FILE *f)
{
	gint32 c;
	c = fgetc(f);
	g_return_val_if_fail(c != EOF, 0);
	return c;
}

static gint32
read_short(FILE *f)
{
	guint8 bytes[2];
	bytes[0] = read_char(f);
	bytes[1] = read_char(f);
	return (bytes[0] << 8) | bytes[1];
}

static gint32
read_long(FILE *f)
{
	guint8 bytes[4];
	bytes[0] = read_char(f);
	bytes[1] = read_char(f);
	bytes[2] = read_char(f);
	bytes[3] = read_char(f);
	return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}

static float
read_float(FILE *f)
{
	gint32 x;
	x = read_long(f);
	return *(float *)&x;
}

static gint
read_string(FILE *f, char *str)
{
	gint c;
	gint cnt = 0;
	do {
		c = read_char(f);
		if (cnt < LW_MAX_NAME_LEN)
			str[cnt] = c;
		else
			str[LW_MAX_NAME_LEN - 1] = 0;
		cnt++;
	} while (c != 0);
	/* if length of string (including \0) is odd skip another byte */
	if (cnt % 2) {
		read_char(f);
		cnt++;
	}
	return cnt;
}

static void
read_srfs(FILE *f, gint nbytes, lwObject *lwo)
{
	int guess_cnt = lwo->material_cnt;

	while (nbytes > 0) {
		lwMaterial *material;

		/* allocate more memory for materials if needed */
		if (guess_cnt <= lwo->material_cnt) {
			guess_cnt += guess_cnt / 2 + 4;
			lwo->materials = xrealloc(lwo->materials, sizeof(lwMaterial) * guess_cnt);
		}
		material = lwo->materials + lwo->material_cnt++;

		/* read name */
		nbytes -= read_string(f, material->name);

		/* defaults */
		material->r = 0.5;
		material->g = 0.5;
		material->b = 0.5;
	}
	lwo->materials = xrealloc(lwo->materials, sizeof(lwMaterial) * lwo->material_cnt);
}


static void
read_surf(FILE *f, gint nbytes, lwObject *lwo)
{
	int i;
	char name[LW_MAX_NAME_LEN];
	lwMaterial *material = NULL;

	/* read surface name */
	nbytes -= read_string(f, name);

	/* find material */
	for (i = 0; i < lwo->material_cnt; i++) {
		if (strcmp(lwo->materials[i].name, name) == 0) {
			material = &lwo->materials[i];
			break;
		}
	}
	g_return_if_fail(material != NULL);

	/* read values */
	while (nbytes > 0) {
		gint id = read_long(f);
		gint len = read_short(f);
		nbytes -= 6 + len + (len % 2);

		switch (id) {
		case ID_COLR:
			material->r = read_char(f) / 255.0;
			material->g = read_char(f) / 255.0;
			material->b = read_char(f) / 255.0;
			read_char(f);	/* dummy */
			break;

		default:
			fseek(f, len + (len % 2), SEEK_CUR);
		}
	}
}


static void
read_pols(FILE *f, int nbytes, lwObject *lwo)
{
	int guess_cnt = lwo->face_cnt;
	size_t n;

	while (nbytes > 0) {
		lwFace *face;
		int i;

		/* allocate more memory for polygons if necessary */
		if (guess_cnt <= lwo->face_cnt) {
			guess_cnt += guess_cnt + 4;
			lwo->faces = xrealloc(lwo->faces, sizeof(lwFace) * guess_cnt);
		}
		face = lwo->faces + lwo->face_cnt++;

		/* number of points in this face */
		face->index_cnt = read_short(f);
		nbytes -= 2;

		/* allocate space for points */
		n = sizeof(int) * face->index_cnt;
		face->indices = xmalloc(n);

		/* read points in */
		for (i = 0; i < face->index_cnt; i++) {
			face->indices[i] = read_short(f);
			nbytes -= 2;
		}

		/* read surface material */
		face->mat_id = read_short(f);
		nbytes -= 2;

		/* skip over detail  polygons */
		if (face->mat_id < 0) {
			int det_cnt;
			face->mat_id = - face->mat_id;
			det_cnt = read_short(f);
			nbytes -= 2;
			while (det_cnt-- > 0) {
				int cnt = read_short(f);
				fseek(f, cnt * 2 + 2, SEEK_CUR);
				nbytes -= cnt * 2 + 2;
			}
		}
		face->mat_id -= 1;
	}
	/* readjust to true size */
	lwo->faces = xrealloc(lwo->faces, sizeof(lwFace) * lwo->face_cnt);
}



static void
read_pnts(FILE *f, gint nbytes, lwObject *lwo)
{
	int i;
	lwo->vertex_cnt = nbytes / 12;
	lwo->vertices = xmalloc( lwo->vertex_cnt * sizeof(lwPoint) );
	for (i = 0; i < lwo->vertex_cnt; i++) {
		lwo->vertices[i].x = read_float(f);
		lwo->vertices[i].y = read_float(f);
		lwo->vertices[i].z = read_float(f);
	}
}






gint
lw_is_lwobject(const char *lw_file)
{
	FILE *f = fopen(lw_file, "rb");
	if (f) {
		gint32 form = read_long(f);
		gint32 nlen = read_long(f);
		gint32 lwob = read_long(f);
		fclose(f);
		if ((form == ID_FORM) && (nlen != 0) && (lwob == ID_LWOB))
			return TRUE;
	}
	return FALSE;
}


lwObject *
lw_object_read(const char *lw_file)
{
	FILE *f = NULL;
	lwObject *lw_object = NULL;
	size_t n;

	gint32 form_bytes = 0;
	gint32 read_bytes = 0;

	/* open file */
	f = fopen(lw_file, "rb");
	if (f == NULL) {
		/* g_warning("can't open file %s", lw_file); */
		return NULL;
	}

	/* check for headers */
	if (read_long(f) != ID_FORM) {
		/* g_warning("file %s is not an IFF file", lw_file); */
		fclose(f);
		return NULL;
	}
	form_bytes = read_long(f);
	read_bytes += 4;

	if (read_long(f) != ID_LWOB) {
		/* g_warning("file %s is not a LWOB file", lw_file); */
		fclose(f);
		return NULL;
	}

	/* create new lwObject */
	n = sizeof(lwObject);
	lw_object = xmalloc(n);
	memset(lw_object, 0, n);

	/* read chunks */
	while (read_bytes < form_bytes) {
		gint32 id = read_long(f);
		gint32 nbytes = read_long(f);
		read_bytes += 8 + nbytes + (nbytes % 2);

		switch (id) {
		case ID_PNTS:
			read_pnts(f, nbytes, lw_object);
			break;

		case ID_POLS:
			read_pols(f, nbytes, lw_object);
			break;

		case ID_SRFS:
			read_srfs(f, nbytes, lw_object);
			break;

		case ID_SURF:
			read_surf(f, nbytes, lw_object);
			break;

		default:
			fseek(f, nbytes + (nbytes % 2), SEEK_CUR);
		}
	}

	fclose(f);
	return lw_object;
}







void
lw_object_free(lwObject *lw_object)
{
	g_return_if_fail(lw_object != NULL);

	if (lw_object->faces != NULL) {
		int i;
		for (i = 0; i < lw_object->face_cnt; i++)
			xfree(lw_object->faces[i].indices);
		xfree(lw_object->faces);
	}
	xfree(lw_object->materials);
	xfree(lw_object->vertices);
	xfree(lw_object);
}




#if 0 /* don't need this next bit */

#define PX(i) (lw_object->vertices[face->indices[i]].x)
#define PY(i) (lw_object->vertices[face->indices[i]].y)
#define PZ(i) (lw_object->vertices[face->indices[i]].z)
void
lw_object_show(const lwObject *lw_object)
{
	int i, j;
	int prev_index_cnt = -1;
	int prev_material = -1;
	float prev_nx = 0;
	float prev_ny = 0;
	float prev_nz = 0;

	g_return_if_fail(lw_object != NULL);

	for (i = 0; i < lw_object->face_cnt; i++) {
		float ax, ay, az, bx, by, bz, nx, ny, nz, r;
		const lwFace *face = lw_object->faces + i;

		/* ignore faces with less than 3 points */
		if (face->index_cnt < 3)
			continue;

		/* calculate normal */
		ax = PX(1) - PX(0);
		ay = PY(1) - PY(0);
		az = PZ(1) - PZ(0);

		bx = PX(face->index_cnt - 1) - PX(0);
		by = PY(face->index_cnt - 1) - PY(0);
		bz = PZ(face->index_cnt - 1) - PZ(0);

		nx = ay * bz - az * by;
		ny = az * bx - ax * bz;
		nz = ax * by - ay * bx;

		r = sqrt(nx * nx + ny * ny + nz * nz);
		if (r < 0.000001)	/* avoid division by zero */
			continue;
		nx /= r;
		ny /= r;
		nz /= r;

		/* glBegin/glEnd */
		if (prev_index_cnt != face->index_cnt || prev_index_cnt > 4) {
			if (prev_index_cnt > 0)
				glEnd();
			prev_index_cnt = face->index_cnt;
			switch (face->index_cnt) {
			case 3:
				glBegin(GL_TRIANGLES);
				break;

			case 4:
				glBegin(GL_QUADS);
				break;

			default:
				glBegin(GL_POLYGON);
			}
		}

		/* update material if necessary */
		if (prev_material != face->mat_id) {
			prev_material = face->mat_id;
			glColor3f(lw_object->materials[face->mat_id].r,
			          lw_object->materials[face->mat_id].g,
			          lw_object->materials[face->mat_id].b);
		}

		/* update normal if necessary */
		if (nx != prev_nx || ny != prev_ny || nz != prev_nz) {
			prev_nx = nx;
			prev_ny = ny;
			prev_nz = nz;
			glNormal3f(nx, ny, nz);
		}

		/* draw polygon/triangle/quad */
		for (j = 0; j < face->index_cnt; j++)
			glVertex3f(PX(j), PY(j), PZ(j));

	}

	/* if glBegin was called call glEnd */
	if (prev_index_cnt > 0)
		glEnd();
}


float
lw_object_radius(const lwObject *lwo)
{
	int i;
	double max_radius = 0.0;

	g_return_val_if_fail(lwo != NULL, 0.0);

	for (i = 0; i < lwo->vertex_cnt; i++) {
		point *v = &lwo->vertices[i];
		double r = (v->x * v->x) + (v->y * v->y) + (v->z * v->z);
		if (r > max_radius)
			max_radius = r;
	}
	return sqrt(max_radius);
}

void
lw_object_scale(lwObject *lwo, float scale)
{
	int i;

	g_return_if_fail(lwo != NULL);

	for (i = 0; i < lwo->vertex_cnt; i++) {
		lwo->vertices[i].x *= scale;
		lwo->vertices[i].y *= scale;
		lwo->vertices[i].z *= scale;
	}
}

#endif /* 0 */

/* end readlwo.c */