]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/penguin/lw.c
2 * Copyright (C) 1998 Janne Löf <jlof@mail.student.oulu.fi>
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.
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.
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.
28 #define wxUint32 unsigned int
38 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
39 (((wxUint32)(b))<<16)| \
40 (((wxUint32)(c))<< 8)| \
43 #define ID_FORM MK_ID('F','O','R','M')
44 #define ID_LWOB MK_ID('L','W','O','B')
45 #define ID_PNTS MK_ID('P','N','T','S')
46 #define ID_SRFS MK_ID('S','R','F','S')
47 #define ID_SURF MK_ID('S','U','R','F')
48 #define ID_POLS MK_ID('P','O','L','S')
49 #define ID_COLR MK_ID('C','O','L','R')
51 static wxInt32
read_char(FILE *f
)
57 static wxInt32
read_short(FILE *f
)
59 return (read_char(f
)<<8) | read_char(f
);
62 static wxInt32
read_long(FILE *f
)
64 return (read_char(f
)<<24) | (read_char(f
)<<16) | (read_char(f
)<<8) | read_char(f
);
67 static GLfloat
read_float(FILE *f
)
69 wxInt32 x
= read_long(f
);
73 static int read_string(FILE *f
, char *s
)
79 if (cnt
< LW_MAX_NAME_LEN
)
82 s
[LW_MAX_NAME_LEN
-1] = 0;
85 /* if length of string (including \0) is odd skip another byte */
93 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
95 int guess_cnt
= lwo
->material_cnt
;
100 /* allocate more memory for materials if needed */
101 if (guess_cnt
<= lwo
->material_cnt
) {
102 guess_cnt
+= guess_cnt
/2 + 4;
103 lwo
->material
= realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
105 material
= lwo
->material
+ lwo
->material_cnt
++;
108 nbytes
-= read_string(f
,material
->name
);
115 lwo
->material
= realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
119 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
122 char name
[LW_MAX_NAME_LEN
];
123 lwMaterial
*material
= NULL
;
125 /* read surface name */
126 nbytes
-= read_string(f
,name
);
129 for (i
=0; i
< lwo
->material_cnt
; i
++) {
130 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
131 material
= &lwo
->material
[i
];
138 int id
= read_long(f
);
139 int len
= read_short(f
);
140 nbytes
-= 6 + len
+ (len%2
);
144 material
->r
= read_char(f
) / 255.0;
145 material
->g
= read_char(f
) / 255.0;
146 material
->b
= read_char(f
) / 255.0;
147 read_char(f
); /* dummy */
150 fseek(f
, len
+(len%2
), SEEK_CUR
);
156 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
158 int guess_cnt
= lwo
->face_cnt
;
164 /* allocate more memory for polygons if necessary */
165 if (guess_cnt
<= lwo
->face_cnt
) {
166 guess_cnt
+= guess_cnt
+ 4;
167 lwo
->face
= realloc(lwo
->face
, sizeof(lwFace
)*guess_cnt
);
169 face
= lwo
->face
+ lwo
->face_cnt
++;
171 /* number of points in this face */
172 face
->index_cnt
= read_short(f
);
175 /* allocate space for points */
176 face
->index
= calloc(sizeof(int)*face
->index_cnt
,1);
179 for (i
=0; i
<face
->index_cnt
; i
++) {
180 face
->index
[i
] = read_short(f
);
184 /* read surface material */
185 face
->material
= read_short(f
);
188 /* skip over detail polygons */
189 if (face
->material
< 0) {
191 face
->material
= -face
->material
;
192 det_cnt
= read_short(f
);
194 while (det_cnt
-- > 0) {
195 int cnt
= read_short(f
);
196 fseek(f
, cnt
*2+2, SEEK_CUR
);
202 /* readjust to true size */
203 lwo
->face
= realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
208 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
211 lwo
->vertex_cnt
= nbytes
/ 12;
212 lwo
->vertex
= calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
213 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
214 lwo
->vertex
[i
*3+0] = read_float(f
);
215 lwo
->vertex
[i
*3+1] = read_float(f
);
216 lwo
->vertex
[i
*3+2] = read_float(f
);
225 int lw_is_lwobject(const char *lw_file
)
227 FILE *f
= fopen(lw_file
, "rb");
229 wxInt32 form
= read_long(f
);
230 wxInt32 nlen
= read_long(f
);
231 wxInt32 lwob
= read_long(f
);
233 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
240 lwObject
*lw_object_read(const char *lw_file
)
243 lwObject
*lw_object
= NULL
;
245 wxInt32 form_bytes
= 0;
246 wxInt32 read_bytes
= 0;
249 f
= fopen(lw_file
, "rb");
254 /* check for headers */
255 if (read_long(f
) != ID_FORM
) {
259 form_bytes
= read_long(f
);
262 if (read_long(f
) != ID_LWOB
) {
267 /* create new lwObject */
268 lw_object
= calloc(sizeof(lwObject
),1);
271 while (read_bytes
< form_bytes
) {
272 wxInt32 id
= read_long(f
);
273 wxInt32 nbytes
= read_long(f
);
274 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
278 read_pnts(f
, nbytes
, lw_object
);
281 read_pols(f
, nbytes
, lw_object
);
284 read_srfs(f
, nbytes
, lw_object
);
287 read_surf(f
, nbytes
, lw_object
);
290 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
300 void lw_object_free(lwObject
*lw_object
)
302 if (lw_object
->face
) {
304 for (i
=0; i
<lw_object
->face_cnt
; i
++)
305 free(lw_object
->face
[i
].index
);
306 free(lw_object
->face
);
308 free(lw_object
->material
);
309 free(lw_object
->vertex
);
317 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
318 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
319 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
320 void lw_object_show(const lwObject
*lw_object
)
323 int prev_index_cnt
= -1;
324 int prev_material
= -1;
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
;
333 /* ignore faces with less than 3 points */
334 if (face
->index_cnt
< 3)
337 /* calculate normal */
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);
346 nx
= ay
* bz
- az
* by
;
347 ny
= az
* bx
- ax
* bz
;
348 nz
= ax
* by
- ay
* bx
;
350 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
351 if (r
< 0.000001) /* avoid division by zero */
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
) {
363 glBegin(GL_TRIANGLES
);
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
);
381 /* update normal if necessary */
382 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
386 glNormal3f(nx
,ny
,nz
);
389 /* draw polygon/triangle/quad */
390 for (j
=0; j
<face
->index_cnt
; j
++)
391 glVertex3f(PX(j
),PY(j
),PZ(j
));
395 /* if glBegin was called call glEnd */
396 if (prev_index_cnt
> 0)
401 GLfloat
lw_object_radius(const lwObject
*lwo
)
404 double max_radius
= 0.0;
406 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
407 GLfloat
*v
= &lwo
->vertex
[i
*3];
408 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
412 return sqrt(max_radius
);
415 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
419 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
420 lwo
->vertex
[i
*3+0] *= scale
;
421 lwo
->vertex
[i
*3+1] *= scale
;
422 lwo
->vertex
[i
*3+2] *= scale
;