]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/penguin/lw.cpp
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.
29 #define wxUint32 unsigned int
39 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
40 (((wxUint32)(b))<<16)| \
41 (((wxUint32)(c))<< 8)| \
44 #define ID_FORM MK_ID('F','O','R','M')
45 #define ID_LWOB MK_ID('L','W','O','B')
46 #define ID_PNTS MK_ID('P','N','T','S')
47 #define ID_SRFS MK_ID('S','R','F','S')
48 #define ID_SURF MK_ID('S','U','R','F')
49 #define ID_POLS MK_ID('P','O','L','S')
50 #define ID_COLR MK_ID('C','O','L','R')
52 static wxInt32
read_char(FILE *f
)
58 static wxInt32
read_short(FILE *f
)
60 return (read_char(f
)<<8) | read_char(f
);
63 static wxInt32
read_long(FILE *f
)
65 return (read_char(f
)<<24) | (read_char(f
)<<16) | (read_char(f
)<<8) | read_char(f
);
68 static GLfloat
read_float(FILE *f
)
70 wxInt32 x
= read_long(f
);
74 static int read_string(FILE *f
, char *s
)
80 if (cnt
< LW_MAX_NAME_LEN
)
83 s
[LW_MAX_NAME_LEN
-1] = 0;
86 /* if length of string (including \0) is odd skip another byte */
94 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
96 int guess_cnt
= lwo
->material_cnt
;
101 /* allocate more memory for materials if needed */
102 if (guess_cnt
<= lwo
->material_cnt
) {
103 guess_cnt
+= guess_cnt
/2 + 4;
104 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
106 material
= lwo
->material
+ lwo
->material_cnt
++;
109 nbytes
-= read_string(f
,material
->name
);
116 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
120 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
123 char name
[LW_MAX_NAME_LEN
];
124 lwMaterial
*material
= NULL
;
126 /* read surface name */
127 nbytes
-= read_string(f
,name
);
130 for (i
=0; i
< lwo
->material_cnt
; i
++) {
131 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
132 material
= &lwo
->material
[i
];
139 int id
= read_long(f
);
140 int len
= read_short(f
);
141 nbytes
-= 6 + len
+ (len%2
);
145 material
->r
= read_char(f
) / 255.0;
146 material
->g
= read_char(f
) / 255.0;
147 material
->b
= read_char(f
) / 255.0;
148 read_char(f
); /* dummy */
151 fseek(f
, len
+(len%2
), SEEK_CUR
);
157 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
159 int guess_cnt
= lwo
->face_cnt
;
165 /* allocate more memory for polygons if necessary */
166 if (guess_cnt
<= lwo
->face_cnt
) {
167 guess_cnt
+= guess_cnt
+ 4;
168 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
170 face
= lwo
->face
+ lwo
->face_cnt
++;
172 /* number of points in this face */
173 face
->index_cnt
= read_short(f
);
176 /* allocate space for points */
177 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
180 for (i
=0; i
<face
->index_cnt
; i
++) {
181 face
->index
[i
] = read_short(f
);
185 /* read surface material */
186 face
->material
= read_short(f
);
189 /* skip over detail polygons */
190 if (face
->material
< 0) {
192 face
->material
= -face
->material
;
193 det_cnt
= read_short(f
);
195 while (det_cnt
-- > 0) {
196 int cnt
= read_short(f
);
197 fseek(f
, cnt
*2+2, SEEK_CUR
);
203 /* readjust to true size */
204 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
209 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
212 lwo
->vertex_cnt
= nbytes
/ 12;
213 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
214 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
215 lwo
->vertex
[i
*3+0] = read_float(f
);
216 lwo
->vertex
[i
*3+1] = read_float(f
);
217 lwo
->vertex
[i
*3+2] = read_float(f
);
226 int lw_is_lwobject(const char *lw_file
)
228 FILE *f
= fopen(lw_file
, "rb");
230 wxInt32 form
= read_long(f
);
231 wxInt32 nlen
= read_long(f
);
232 wxInt32 lwob
= read_long(f
);
234 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
241 lwObject
*lw_object_read(const char *lw_file
)
244 lwObject
*lw_object
= NULL
;
246 wxInt32 form_bytes
= 0;
247 wxInt32 read_bytes
= 0;
250 f
= fopen(lw_file
, "rb");
255 /* check for headers */
256 if (read_long(f
) != ID_FORM
) {
260 form_bytes
= read_long(f
);
263 if (read_long(f
) != ID_LWOB
) {
268 /* create new lwObject */
269 lw_object
= (lwObject
*) calloc(sizeof(lwObject
),1);
272 while (read_bytes
< form_bytes
) {
273 wxInt32 id
= read_long(f
);
274 wxInt32 nbytes
= read_long(f
);
275 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
279 read_pnts(f
, nbytes
, lw_object
);
282 read_pols(f
, nbytes
, lw_object
);
285 read_srfs(f
, nbytes
, lw_object
);
288 read_surf(f
, nbytes
, lw_object
);
291 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
301 void lw_object_free(lwObject
*lw_object
)
303 if (lw_object
->face
) {
305 for (i
=0; i
<lw_object
->face_cnt
; i
++)
306 free(lw_object
->face
[i
].index
);
307 free(lw_object
->face
);
309 free(lw_object
->material
);
310 free(lw_object
->vertex
);
318 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
319 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
320 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
321 void lw_object_show(const lwObject
*lw_object
)
324 int prev_index_cnt
= -1;
325 int prev_material
= -1;
330 for (i
=0; i
<lw_object
->face_cnt
; i
++) {
331 GLfloat ax
,ay
,az
,bx
,by
,bz
,nx
,ny
,nz
,r
;
332 const lwFace
*face
= lw_object
->face
+i
;
334 /* ignore faces with less than 3 points */
335 if (face
->index_cnt
< 3)
338 /* calculate normal */
343 bx
= PX(face
->index_cnt
-1) - PX(0);
344 by
= PY(face
->index_cnt
-1) - PY(0);
345 bz
= PZ(face
->index_cnt
-1) - PZ(0);
347 nx
= ay
* bz
- az
* by
;
348 ny
= az
* bx
- ax
* bz
;
349 nz
= ax
* by
- ay
* bx
;
351 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
352 if (r
< 0.000001) /* avoid division by zero */
359 if (prev_index_cnt
!= face
->index_cnt
|| prev_index_cnt
> 4) {
360 if (prev_index_cnt
> 0) glEnd();
361 prev_index_cnt
= face
->index_cnt
;
362 switch (face
->index_cnt
) {
364 glBegin(GL_TRIANGLES
);
374 /* update material if necessary */
375 if (prev_material
!= face
->material
) {
376 prev_material
= face
->material
;
377 glColor3f(lw_object
->material
[face
->material
].r
,
378 lw_object
->material
[face
->material
].g
,
379 lw_object
->material
[face
->material
].b
);
382 /* update normal if necessary */
383 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
387 glNormal3f(nx
,ny
,nz
);
390 /* draw polygon/triangle/quad */
391 for (j
=0; j
<face
->index_cnt
; j
++)
392 glVertex3f(PX(j
),PY(j
),PZ(j
));
396 /* if glBegin was called call glEnd */
397 if (prev_index_cnt
> 0)
402 GLfloat
lw_object_radius(const lwObject
*lwo
)
405 double max_radius
= 0.0;
407 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
408 GLfloat
*v
= &lwo
->vertex
[i
*3];
409 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
413 return sqrt(max_radius
);
416 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
420 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
421 lwo
->vertex
[i
*3+0] *= scale
;
422 lwo
->vertex
[i
*3+1] *= scale
;
423 lwo
->vertex
[i
*3+2] *= scale
;