]>
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.
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
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 // the execution path was not always correct
61 // when using the direct evaluation in the return statement
62 wxInt32 first
= read_char(f
) ;
63 wxInt32 second
= read_char(f
) ;
65 return (first
<<8) | second
;
68 static wxInt32
read_long(FILE *f
)
70 // the execution path was not always correct
71 // when using the direct evaluation in the return statement
72 wxInt32 first
= read_char(f
) ;
73 wxInt32 second
= read_char(f
) ;
74 wxInt32 third
= read_char(f
) ;
75 wxInt32 fourth
= read_char(f
) ;
76 return (first
<<24) | (second
<<16) | (third
<<8) | fourth
;
79 static GLfloat
read_float(FILE *f
)
81 wxInt32 x
= read_long(f
);
85 static int read_string(FILE *f
, char *s
)
91 if (cnt
< LW_MAX_NAME_LEN
)
94 s
[LW_MAX_NAME_LEN
-1] = 0;
97 /* if length of string (including \0) is odd skip another byte */
105 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
107 int guess_cnt
= lwo
->material_cnt
;
110 lwMaterial
*material
;
112 /* allocate more memory for materials if needed */
113 if (guess_cnt
<= lwo
->material_cnt
) {
114 guess_cnt
+= guess_cnt
/2 + 4;
115 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
117 material
= lwo
->material
+ lwo
->material_cnt
++;
120 nbytes
-= read_string(f
,material
->name
);
127 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
131 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
134 char name
[LW_MAX_NAME_LEN
];
135 lwMaterial
*material
= NULL
;
137 /* read surface name */
138 nbytes
-= read_string(f
,name
);
141 for (i
=0; i
< lwo
->material_cnt
; i
++) {
142 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
143 material
= &lwo
->material
[i
];
150 int id
= read_long(f
);
151 int len
= read_short(f
);
152 nbytes
-= 6 + len
+ (len%2
);
156 material
->r
= read_char(f
) / 255.0;
157 material
->g
= read_char(f
) / 255.0;
158 material
->b
= read_char(f
) / 255.0;
159 read_char(f
); /* dummy */
162 fseek(f
, len
+(len%2
), SEEK_CUR
);
168 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
170 int guess_cnt
= lwo
->face_cnt
;
176 /* allocate more memory for polygons if necessary */
177 if (guess_cnt
<= lwo
->face_cnt
) {
178 guess_cnt
+= guess_cnt
+ 4;
179 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
181 face
= lwo
->face
+ lwo
->face_cnt
++;
183 /* number of points in this face */
184 face
->index_cnt
= read_short(f
);
187 /* allocate space for points */
188 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
191 for (i
=0; i
<face
->index_cnt
; i
++) {
192 face
->index
[i
] = read_short(f
);
196 /* read surface material */
197 face
->material
= read_short(f
);
200 /* skip over detail polygons */
201 if (face
->material
< 0) {
203 face
->material
= -face
->material
;
204 det_cnt
= read_short(f
);
206 while (det_cnt
-- > 0) {
207 int cnt
= read_short(f
);
208 fseek(f
, cnt
*2+2, SEEK_CUR
);
214 /* readjust to true size */
215 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
220 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
223 lwo
->vertex_cnt
= nbytes
/ 12;
224 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
225 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
226 lwo
->vertex
[i
*3+0] = read_float(f
);
227 lwo
->vertex
[i
*3+1] = read_float(f
);
228 lwo
->vertex
[i
*3+2] = read_float(f
);
237 int lw_is_lwobject(const char *lw_file
)
239 FILE *f
= fopen(lw_file
, "rb");
241 wxInt32 form
= read_long(f
);
242 wxInt32 nlen
= read_long(f
);
243 wxInt32 lwob
= read_long(f
);
245 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
252 lwObject
*lw_object_read(const char *lw_file
)
255 lwObject
*lw_object
= NULL
;
257 wxInt32 form_bytes
= 0;
258 wxInt32 read_bytes
= 0;
261 f
= fopen(lw_file
, "rb");
266 /* check for headers */
267 if (read_long(f
) != ID_FORM
) {
271 form_bytes
= read_long(f
);
274 if (read_long(f
) != ID_LWOB
) {
279 /* create new lwObject */
280 lw_object
= (lwObject
*) calloc(sizeof(lwObject
),1);
283 while (read_bytes
< form_bytes
) {
284 wxInt32 id
= read_long(f
);
285 wxInt32 nbytes
= read_long(f
);
286 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
290 read_pnts(f
, nbytes
, lw_object
);
293 read_pols(f
, nbytes
, lw_object
);
296 read_srfs(f
, nbytes
, lw_object
);
299 read_surf(f
, nbytes
, lw_object
);
302 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
312 void lw_object_free(lwObject
*lw_object
)
314 if (lw_object
->face
) {
316 for (i
=0; i
<lw_object
->face_cnt
; i
++)
317 free(lw_object
->face
[i
].index
);
318 free(lw_object
->face
);
320 free(lw_object
->material
);
321 free(lw_object
->vertex
);
329 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
330 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
331 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
332 void lw_object_show(const lwObject
*lw_object
)
335 int prev_index_cnt
= -1;
336 int prev_material
= -1;
341 for (i
=0; i
<lw_object
->face_cnt
; i
++) {
342 GLfloat ax
,ay
,az
,bx
,by
,bz
,nx
,ny
,nz
,r
;
343 const lwFace
*face
= lw_object
->face
+i
;
345 /* ignore faces with less than 3 points */
346 if (face
->index_cnt
< 3)
349 /* calculate normal */
354 bx
= PX(face
->index_cnt
-1) - PX(0);
355 by
= PY(face
->index_cnt
-1) - PY(0);
356 bz
= PZ(face
->index_cnt
-1) - PZ(0);
358 nx
= ay
* bz
- az
* by
;
359 ny
= az
* bx
- ax
* bz
;
360 nz
= ax
* by
- ay
* bx
;
362 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
363 if (r
< 0.000001) /* avoid division by zero */
370 if (prev_index_cnt
!= face
->index_cnt
|| prev_index_cnt
> 4) {
371 if (prev_index_cnt
> 0) glEnd();
372 prev_index_cnt
= face
->index_cnt
;
373 switch (face
->index_cnt
) {
375 glBegin(GL_TRIANGLES
);
385 /* update material if necessary */
386 if (prev_material
!= face
->material
) {
387 prev_material
= face
->material
;
388 glColor3f(lw_object
->material
[face
->material
].r
,
389 lw_object
->material
[face
->material
].g
,
390 lw_object
->material
[face
->material
].b
);
393 /* update normal if necessary */
394 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
398 glNormal3f(nx
,ny
,nz
);
401 /* draw polygon/triangle/quad */
402 for (j
=0; j
<face
->index_cnt
; j
++)
403 glVertex3f(PX(j
),PY(j
),PZ(j
));
407 /* if glBegin was called call glEnd */
408 if (prev_index_cnt
> 0)
413 GLfloat
lw_object_radius(const lwObject
*lwo
)
416 double max_radius
= 0.0;
418 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
419 GLfloat
*v
= &lwo
->vertex
[i
*3];
420 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
424 return sqrt(max_radius
);
427 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
431 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
432 lwo
->vertex
[i
*3+0] *= scale
;
433 lwo
->vertex
[i
*3+1] *= scale
;
434 lwo
->vertex
[i
*3+2] *= scale
;