1 /*
2 * Copyright (C) 1998 Janne L�f <jlof@mail.student.oulu.fi>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19
20 #include "lw.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24
25 #define MK_ID(a,b,c,d) ((((guint32)(a))<<24)| \
26 (((guint32)(b))<<16)| \
27 (((guint32)(c))<< 8)| \
28 (((guint32)(d)) ))
29
30 #define ID_FORM MK_ID('F','O','R','M')
31 #define ID_LWOB MK_ID('L','W','O','B')
32 #define ID_PNTS MK_ID('P','N','T','S')
33 #define ID_SRFS MK_ID('S','R','F','S')
34 #define ID_SURF MK_ID('S','U','R','F')
35 #define ID_POLS MK_ID('P','O','L','S')
36 #define ID_COLR MK_ID('C','O','L','R')
37
read_char(FILE * f)38 static gint32 read_char(FILE *f)
39 {
40 int c = fgetc(f);
41 g_return_val_if_fail(c != EOF, 0);
42 return c;
43 }
44
read_short(FILE * f)45 static gint32 read_short(FILE *f)
46 {
47 return (read_char(f)<<8) | read_char(f);
48 }
49
read_long(FILE * f)50 static gint32 read_long(FILE *f)
51 {
52 return (read_char(f)<<24) | (read_char(f)<<16) | (read_char(f)<<8) | read_char(f);
53 }
54
read_float(FILE * f)55 static GLfloat read_float(FILE *f)
56 {
57 gint32 x = read_long(f);
58 return *(GLfloat*)&x;
59 }
60
read_string(FILE * f,char * s)61 static gint read_string(FILE *f, char *s)
62 {
63 gint c;
64 gint cnt = 0;
65 do {
66 c = read_char(f);
67 if (cnt < LW_MAX_NAME_LEN)
68 s[cnt] = c;
69 else
70 s[LW_MAX_NAME_LEN-1] = 0;
71 cnt++;
72 } while (c != 0);
73 /* if length of string (including \0) is odd skip another byte */
74 if (cnt%2) {
75 read_char(f);
76 cnt++;
77 }
78 return cnt;
79 }
80
read_srfs(FILE * f,gint nbytes,lwObject * lwo)81 static void read_srfs(FILE *f, gint nbytes, lwObject *lwo)
82 {
83 int guess_cnt = lwo->material_cnt;
84
85 while (nbytes > 0) {
86 lwMaterial *material;
87
88 /* allocate more memory for materials if needed */
89 if (guess_cnt <= lwo->material_cnt) {
90 guess_cnt += guess_cnt/2 + 4;
91 lwo->material = g_realloc(lwo->material, sizeof(lwMaterial)*guess_cnt);
92 }
93 material = lwo->material + lwo->material_cnt++;
94
95 /* read name */
96 nbytes -= read_string(f,material->name);
97
98 /* defaults */
99 material->r = 0.7;
100 material->g = 0.7;
101 material->b = 0.7;
102 }
103 lwo->material = g_realloc(lwo->material, sizeof(lwMaterial)*lwo->material_cnt);
104 }
105
106
read_surf(FILE * f,gint nbytes,lwObject * lwo)107 static void read_surf(FILE *f, gint nbytes, lwObject *lwo)
108 {
109 int i;
110 char name[LW_MAX_NAME_LEN];
111 lwMaterial *material = NULL;
112
113 /* read surface name */
114 nbytes -= read_string(f,name);
115
116 /* find material */
117 for (i=0; i< lwo->material_cnt; i++) {
118 if (strcmp(lwo->material[i].name,name) == 0) {
119 material = &lwo->material[i];
120 break;
121 }
122 }
123 g_return_if_fail(material != NULL);
124
125 /* read values */
126 while (nbytes > 0) {
127 gint id = read_long(f);
128 gint len = read_short(f);
129 nbytes -= 6 + len + (len%2);
130
131 switch (id) {
132 case ID_COLR:
133 material->r = read_char(f) / 255.0;
134 material->g = read_char(f) / 255.0;
135 material->b = read_char(f) / 255.0;
136 read_char(f); /* dummy */
137 break;
138 default:
139 fseek(f, len+(len%2), SEEK_CUR);
140 }
141 }
142 }
143
144
read_pols(FILE * f,int nbytes,lwObject * lwo)145 static void read_pols(FILE *f, int nbytes, lwObject *lwo)
146 {
147 int guess_cnt = lwo->face_cnt;
148
149 while (nbytes > 0) {
150 lwFace *face;
151 int i;
152
153 /* allocate more memory for polygons if necessary */
154 if (guess_cnt <= lwo->face_cnt) {
155 guess_cnt += guess_cnt + 4;
156 lwo->face = g_realloc(lwo->face, sizeof(lwFace)*guess_cnt);
157 }
158 face = lwo->face + lwo->face_cnt++;
159
160 /* number of points in this face */
161 face->index_cnt = read_short(f);
162 nbytes -= 2;
163
164 /* allocate space for points */
165 face->index = g_malloc0(sizeof(int)*face->index_cnt);
166
167 /* read points in */
168 for (i=0; i<face->index_cnt; i++) {
169 face->index[i] = read_short(f);
170 nbytes -= 2;
171 }
172
173 /* read surface material */
174 face->material = read_short(f);
175 nbytes -= 2;
176
177 /* skip over detail polygons */
178 if (face->material < 0) {
179 int det_cnt;
180 face->material = -face->material;
181 det_cnt = read_short(f);
182 nbytes -= 2;
183 while (det_cnt-- > 0) {
184 int cnt = read_short(f);
185 fseek(f, cnt*2+2, SEEK_CUR);
186 nbytes -= cnt*2+2;
187 }
188 }
189 face->material -= 1;
190 }
191 /* readjust to true size */
192 lwo->face = g_realloc(lwo->face, sizeof(lwFace)*lwo->face_cnt);
193 }
194
195
196
read_pnts(FILE * f,gint nbytes,lwObject * lwo)197 static void read_pnts(FILE *f, gint nbytes, lwObject *lwo)
198 {
199 int i;
200 lwo->vertex_cnt = nbytes / 12;
201 lwo->vertex = g_malloc0(sizeof(GLfloat)*lwo->vertex_cnt*3);
202 for (i=0; i<lwo->vertex_cnt; i++) {
203 lwo->vertex[i*3+0] = read_float(f);
204 lwo->vertex[i*3+1] = read_float(f);
205 lwo->vertex[i*3+2] = read_float(f);
206 }
207 }
208
209
210
211
212
213
lw_is_lwobject(const char * lw_file)214 gint lw_is_lwobject(const char *lw_file)
215 {
216 FILE *f = fopen(lw_file, "rb");
217 if (f) {
218 gint32 form = read_long(f);
219 gint32 nlen = read_long(f);
220 gint32 lwob = read_long(f);
221 fclose(f);
222 if (form == ID_FORM && nlen != 0 && lwob == ID_LWOB)
223 return TRUE;
224 }
225 return FALSE;
226 }
227
228
lw_object_read(const char * lw_file)229 lwObject *lw_object_read(const char *lw_file)
230 {
231 FILE *f = NULL;
232 lwObject *lw_object = NULL;
233
234 gint32 form_bytes = 0;
235 gint32 read_bytes = 0;
236
237 /* open file */
238 f = fopen(lw_file, "rb");
239 if (f == NULL) {
240 g_warning("can't open file %s", lw_file);
241 return NULL;
242 }
243
244 /* check for headers */
245 if (read_long(f) != ID_FORM) {
246 g_warning("file %s is not an IFF file", lw_file);
247 fclose(f);
248 return NULL;
249 }
250 form_bytes = read_long(f);
251 read_bytes += 4;
252
253 if (read_long(f) != ID_LWOB) {
254 g_warning("file %s is not a LWOB file", lw_file);
255 fclose(f);
256 return NULL;
257 }
258
259 /* create new lwObject */
260 lw_object = g_malloc0(sizeof(lwObject));
261
262 /* read chunks */
263 while (read_bytes < form_bytes) {
264 gint32 id = read_long(f);
265 gint32 nbytes = read_long(f);
266 read_bytes += 8 + nbytes + (nbytes%2);
267
268 switch (id) {
269 case ID_PNTS:
270 read_pnts(f, nbytes, lw_object);
271 break;
272 case ID_POLS:
273 read_pols(f, nbytes, lw_object);
274 break;
275 case ID_SRFS:
276 read_srfs(f, nbytes, lw_object);
277 break;
278 case ID_SURF:
279 read_surf(f, nbytes, lw_object);
280 break;
281 default:
282 fseek(f, nbytes + (nbytes%2), SEEK_CUR);
283 }
284 }
285
286 fclose(f);
287 return lw_object;
288 }
289
290
291
292
293
294
295
lw_object_free(lwObject * lw_object)296 void lw_object_free(lwObject *lw_object)
297 {
298 g_return_if_fail(lw_object != NULL);
299
300 if (lw_object->face) {
301 int i;
302 for (i=0; i<lw_object->face_cnt; i++)
303 g_free(lw_object->face[i].index);
304 g_free(lw_object->face);
305 }
306 g_free(lw_object->material);
307 g_free(lw_object->vertex);
308 g_free(lw_object);
309 }
310
311
312
313
314
315 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
316 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
317 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
lw_object_show(const lwObject * lw_object)318 void lw_object_show(const lwObject *lw_object)
319 {
320 int i,j;
321 int prev_index_cnt = -1;
322 int prev_material = -1;
323 GLfloat prev_nx = 0;
324 GLfloat prev_ny = 0;
325 GLfloat prev_nz = 0;
326
327 g_return_if_fail(lw_object != NULL);
328
329 for (i=0; i<lw_object->face_cnt; i++) {
330 GLfloat ax,ay,az,bx,by,bz,nx,ny,nz,r;
331 const lwFace *face = lw_object->face+i;
332
333 /* ignore faces with less than 3 points */
334 if (face->index_cnt < 3)
335 continue;
336
337 /* calculate normal */
338 ax = PX(1) - PX(0);
339 ay = PY(1) - PY(0);
340 az = PZ(1) - PZ(0);
341
342 bx = PX(face->index_cnt-1) - PX(0);
343 by = PY(face->index_cnt-1) - PY(0);
344 bz = PZ(face->index_cnt-1) - PZ(0);
345
346 nx = ay * bz - az * by;
347 ny = az * bx - ax * bz;
348 nz = ax * by - ay * bx;
349
350 r = sqrt(nx*nx + ny*ny + nz*nz);
351 if (r < 0.000001) /* avoid division by zero */
352 continue;
353 nx /= r;
354 ny /= r;
355 nz /= r;
356
357 /* glBegin/glEnd */
358 if (prev_index_cnt != face->index_cnt || prev_index_cnt > 4) {
359 if (prev_index_cnt > 0) glEnd();
360 prev_index_cnt = face->index_cnt;
361 switch (face->index_cnt) {
362 case 3:
363 glBegin(GL_TRIANGLES);
364 break;
365 case 4:
366 glBegin(GL_QUADS);
367 break;
368 default:
369 glBegin(GL_POLYGON);
370 }
371 }
372
373 /* update material if necessary */
374 if (prev_material != face->material) {
375 prev_material = face->material;
376 glColor3f(lw_object->material[face->material].r,
377 lw_object->material[face->material].g,
378 lw_object->material[face->material].b);
379 }
380
381 /* update normal if necessary */
382 if (nx != prev_nx || ny != prev_ny || nz != prev_nz) {
383 prev_nx = nx;
384 prev_ny = ny;
385 prev_nz = nz;
386 glNormal3f(nx,ny,nz);
387 }
388
389 /* draw polygon/triangle/quad */
390 for (j=0; j<face->index_cnt; j++)
391 glVertex3f(PX(j),PY(j),PZ(j));
392
393 }
394
395 /* if glBegin was called call glEnd */
396 if (prev_index_cnt > 0)
397 glEnd();
398 }
399
400
lw_object_radius(const lwObject * lwo)401 GLfloat lw_object_radius(const lwObject *lwo)
402 {
403 int i;
404 double max_radius = 0.0;
405
406 g_return_val_if_fail(lwo != NULL, 0.0);
407
408 for (i=0; i<lwo->vertex_cnt; i++) {
409 GLfloat *v = &lwo->vertex[i*3];
410 double r = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
411 if (r > max_radius)
412 max_radius = r;
413 }
414 return sqrt(max_radius);
415 }
416
lw_object_scale(lwObject * lwo,GLfloat scale)417 void lw_object_scale(lwObject *lwo, GLfloat scale)
418 {
419 int i;
420
421 g_return_if_fail(lwo != NULL);
422
423 for (i=0; i<lwo->vertex_cnt; i++) {
424 lwo->vertex[i*3+0] *= scale;
425 lwo->vertex[i*3+1] *= scale;
426 lwo->vertex[i*3+2] *= scale;
427 }
428 }
429
430
431