]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/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.
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
47 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
48 (((wxUint32)(b))<<16)| \
49 (((wxUint32)(c))<< 8)| \
52 #define ID_FORM MK_ID('F','O','R','M')
53 #define ID_LWOB MK_ID('L','W','O','B')
54 #define ID_PNTS MK_ID('P','N','T','S')
55 #define ID_SRFS MK_ID('S','R','F','S')
56 #define ID_SURF MK_ID('S','U','R','F')
57 #define ID_POLS MK_ID('P','O','L','S')
58 #define ID_COLR MK_ID('C','O','L','R')
60 static wxInt32
read_char(FILE *f
)
66 static wxInt32
read_short(FILE *f
)
68 // the execution path was not always correct
69 // when using the direct evaluation in the return statement
70 wxInt32 first
= read_char(f
) ;
71 wxInt32 second
= read_char(f
) ;
73 return (first
<<8) | second
;
76 static wxInt32
read_long(FILE *f
)
78 // the execution path was not always correct
79 // when using the direct evaluation in the return statement
80 wxInt32 first
= read_char(f
) ;
81 wxInt32 second
= read_char(f
) ;
82 wxInt32 third
= read_char(f
) ;
83 wxInt32 fourth
= read_char(f
) ;
84 return (first
<<24) | (second
<<16) | (third
<<8) | fourth
;
87 static GLfloat
read_float(FILE *f
)
89 wxInt32 x
= read_long(f
);
93 static int read_string(FILE *f
, char *s
)
99 if (cnt
< LW_MAX_NAME_LEN
)
102 s
[LW_MAX_NAME_LEN
-1] = 0;
105 /* if length of string (including \0) is odd skip another byte */
113 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
115 int guess_cnt
= lwo
->material_cnt
;
118 lwMaterial
*material
;
120 /* allocate more memory for materials if needed */
121 if (guess_cnt
<= lwo
->material_cnt
) {
122 guess_cnt
+= guess_cnt
/2 + 4;
123 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
125 material
= lwo
->material
+ lwo
->material_cnt
++;
128 nbytes
-= read_string(f
,material
->name
);
135 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
139 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
142 char name
[LW_MAX_NAME_LEN
];
143 lwMaterial
*material
= NULL
;
145 /* read surface name */
146 nbytes
-= read_string(f
,name
);
149 for (i
=0; i
< lwo
->material_cnt
; i
++) {
150 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
151 material
= &lwo
->material
[i
];
158 int id
= read_long(f
);
159 int len
= read_short(f
);
160 nbytes
-= 6 + len
+ (len%2
);
164 material
->r
= read_char(f
) / 255.0;
165 material
->g
= read_char(f
) / 255.0;
166 material
->b
= read_char(f
) / 255.0;
167 read_char(f
); /* dummy */
170 fseek(f
, len
+(len%2
), SEEK_CUR
);
176 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
178 int guess_cnt
= lwo
->face_cnt
;
184 /* allocate more memory for polygons if necessary */
185 if (guess_cnt
<= lwo
->face_cnt
) {
186 guess_cnt
+= guess_cnt
+ 4;
187 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
189 face
= lwo
->face
+ lwo
->face_cnt
++;
191 /* number of points in this face */
192 face
->index_cnt
= read_short(f
);
195 /* allocate space for points */
196 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
199 for (i
=0; i
<face
->index_cnt
; i
++) {
200 face
->index
[i
] = read_short(f
);
204 /* read surface material */
205 face
->material
= read_short(f
);
208 /* skip over detail polygons */
209 if (face
->material
< 0) {
211 face
->material
= -face
->material
;
212 det_cnt
= read_short(f
);
214 while (det_cnt
-- > 0) {
215 int cnt
= read_short(f
);
216 fseek(f
, cnt
*2+2, SEEK_CUR
);
222 /* readjust to true size */
223 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
228 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
231 lwo
->vertex_cnt
= nbytes
/ 12;
232 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
233 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
234 lwo
->vertex
[i
*3+0] = read_float(f
);
235 lwo
->vertex
[i
*3+1] = read_float(f
);
236 lwo
->vertex
[i
*3+2] = read_float(f
);
245 int lw_is_lwobject(const char *lw_file
)
247 FILE *f
= fopen(lw_file
, "rb");
249 wxInt32 form
= read_long(f
);
250 wxInt32 nlen
= read_long(f
);
251 wxInt32 lwob
= read_long(f
);
253 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
260 lwObject
*lw_object_read(const char *lw_file
)
263 lwObject
*lw_object
= NULL
;
265 wxInt32 form_bytes
= 0;
266 wxInt32 read_bytes
= 0;
269 f
= fopen(lw_file
, "rb");
274 /* check for headers */
275 if (read_long(f
) != ID_FORM
) {
279 form_bytes
= read_long(f
);
282 if (read_long(f
) != ID_LWOB
) {
287 /* create new lwObject */
288 lw_object
= (lwObject
*) calloc(sizeof(lwObject
),1);
291 while (read_bytes
< form_bytes
) {
292 wxInt32 id
= read_long(f
);
293 wxInt32 nbytes
= read_long(f
);
294 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
298 read_pnts(f
, nbytes
, lw_object
);
301 read_pols(f
, nbytes
, lw_object
);
304 read_srfs(f
, nbytes
, lw_object
);
307 read_surf(f
, nbytes
, lw_object
);
310 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
320 void lw_object_free(lwObject
*lw_object
)
322 if (lw_object
->face
) {
324 for (i
=0; i
<lw_object
->face_cnt
; i
++)
325 free(lw_object
->face
[i
].index
);
326 free(lw_object
->face
);
328 free(lw_object
->material
);
329 free(lw_object
->vertex
);
337 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
338 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
339 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
340 void lw_object_show(const lwObject
*lw_object
)
343 int prev_index_cnt
= -1;
344 int prev_material
= -1;
349 for (i
=0; i
<lw_object
->face_cnt
; i
++) {
350 GLfloat ax
,ay
,az
,bx
,by
,bz
,nx
,ny
,nz
,r
;
351 const lwFace
*face
= lw_object
->face
+i
;
353 /* ignore faces with less than 3 points */
354 if (face
->index_cnt
< 3)
357 /* calculate normal */
362 bx
= PX(face
->index_cnt
-1) - PX(0);
363 by
= PY(face
->index_cnt
-1) - PY(0);
364 bz
= PZ(face
->index_cnt
-1) - PZ(0);
366 nx
= ay
* bz
- az
* by
;
367 ny
= az
* bx
- ax
* bz
;
368 nz
= ax
* by
- ay
* bx
;
370 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
371 if (r
< 0.000001) /* avoid division by zero */
378 if (prev_index_cnt
!= face
->index_cnt
|| prev_index_cnt
> 4) {
379 if (prev_index_cnt
> 0) glEnd();
380 prev_index_cnt
= face
->index_cnt
;
381 switch (face
->index_cnt
) {
383 glBegin(GL_TRIANGLES
);
393 /* update material if necessary */
394 if (prev_material
!= face
->material
) {
395 prev_material
= face
->material
;
396 glColor3f(lw_object
->material
[face
->material
].r
,
397 lw_object
->material
[face
->material
].g
,
398 lw_object
->material
[face
->material
].b
);
401 /* update normal if necessary */
402 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
406 glNormal3f(nx
,ny
,nz
);
409 /* draw polygon/triangle/quad */
410 for (j
=0; j
<face
->index_cnt
; j
++)
411 glVertex3f(PX(j
),PY(j
),PZ(j
));
415 /* if glBegin was called call glEnd */
416 if (prev_index_cnt
> 0)
421 GLfloat
lw_object_radius(const lwObject
*lwo
)
424 double max_radius
= 0.0;
426 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
427 GLfloat
*v
= &lwo
->vertex
[i
*3];
428 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
432 return sqrt(max_radius
);
435 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
439 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
440 lwo
->vertex
[i
*3+0] *= scale
;
441 lwo
->vertex
[i
*3+1] *= scale
;
442 lwo
->vertex
[i
*3+2] *= scale
;