]>
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"
40 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
41 (((wxUint32)(b))<<16)| \
42 (((wxUint32)(c))<< 8)| \
45 #define ID_FORM MK_ID('F','O','R','M')
46 #define ID_LWOB MK_ID('L','W','O','B')
47 #define ID_PNTS MK_ID('P','N','T','S')
48 #define ID_SRFS MK_ID('S','R','F','S')
49 #define ID_SURF MK_ID('S','U','R','F')
50 #define ID_POLS MK_ID('P','O','L','S')
51 #define ID_COLR MK_ID('C','O','L','R')
53 static wxInt32
read_char(FILE *f
)
59 static wxInt32
read_short(FILE *f
)
61 // the execution path was not always correct
62 // when using the direct evaluation in the return statement
63 wxInt32 first
= read_char(f
) ;
64 wxInt32 second
= read_char(f
) ;
66 return (first
<<8) | second
;
69 static wxInt32
read_long(FILE *f
)
71 // the execution path was not always correct
72 // when using the direct evaluation in the return statement
73 wxInt32 first
= read_char(f
) ;
74 wxInt32 second
= read_char(f
) ;
75 wxInt32 third
= read_char(f
) ;
76 wxInt32 fourth
= read_char(f
) ;
77 return (first
<<24) | (second
<<16) | (third
<<8) | fourth
;
80 static GLfloat
read_float(FILE *f
)
82 wxInt32 x
= read_long(f
);
86 static int read_string(FILE *f
, char *s
)
92 if (cnt
< LW_MAX_NAME_LEN
)
95 s
[LW_MAX_NAME_LEN
-1] = 0;
98 /* if length of string (including \0) is odd skip another byte */
106 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
108 int guess_cnt
= lwo
->material_cnt
;
111 lwMaterial
*material
;
113 /* allocate more memory for materials if needed */
114 if (guess_cnt
<= lwo
->material_cnt
) {
115 guess_cnt
+= guess_cnt
/2 + 4;
116 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
118 material
= lwo
->material
+ lwo
->material_cnt
++;
121 nbytes
-= read_string(f
,material
->name
);
128 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
132 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
135 char name
[LW_MAX_NAME_LEN
];
136 lwMaterial
*material
= NULL
;
138 /* read surface name */
139 nbytes
-= read_string(f
,name
);
142 for (i
=0; i
< lwo
->material_cnt
; i
++) {
143 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
144 material
= &lwo
->material
[i
];
151 int id
= read_long(f
);
152 int len
= read_short(f
);
153 nbytes
-= 6 + len
+ (len%2
);
157 material
->r
= read_char(f
) / 255.0;
158 material
->g
= read_char(f
) / 255.0;
159 material
->b
= read_char(f
) / 255.0;
160 read_char(f
); /* dummy */
163 fseek(f
, len
+(len%2
), SEEK_CUR
);
169 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
171 int guess_cnt
= lwo
->face_cnt
;
177 /* allocate more memory for polygons if necessary */
178 if (guess_cnt
<= lwo
->face_cnt
) {
179 guess_cnt
+= guess_cnt
+ 4;
180 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
182 face
= lwo
->face
+ lwo
->face_cnt
++;
184 /* number of points in this face */
185 face
->index_cnt
= read_short(f
);
188 /* allocate space for points */
189 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
192 for (i
=0; i
<face
->index_cnt
; i
++) {
193 face
->index
[i
] = read_short(f
);
197 /* read surface material */
198 face
->material
= read_short(f
);
201 /* skip over detail polygons */
202 if (face
->material
< 0) {
204 face
->material
= -face
->material
;
205 det_cnt
= read_short(f
);
207 while (det_cnt
-- > 0) {
208 int cnt
= read_short(f
);
209 fseek(f
, cnt
*2+2, SEEK_CUR
);
215 /* readjust to true size */
216 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
221 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
224 lwo
->vertex_cnt
= nbytes
/ 12;
225 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
226 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
227 lwo
->vertex
[i
*3+0] = read_float(f
);
228 lwo
->vertex
[i
*3+1] = read_float(f
);
229 lwo
->vertex
[i
*3+2] = read_float(f
);
238 bool lw_is_lwobject(const char *lw_file
)
240 FILE *f
= fopen(lw_file
, "rb");
242 wxInt32 form
= read_long(f
);
243 wxInt32 nlen
= read_long(f
);
244 wxInt32 lwob
= read_long(f
);
246 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
253 lwObject
*lw_object_read(const char *lw_file
)
257 FILE *f
= fopen(lw_file
, "rb");
262 /* check for headers */
263 if (read_long(f
) != ID_FORM
) {
268 wxInt32 read_bytes
= 0;
270 wxInt32 form_bytes
= read_long(f
);
273 if (read_long(f
) != ID_LWOB
) {
278 /* create new lwObject */
279 lwObject
*lw_object
= (lwObject
*) calloc(sizeof(lwObject
),1);
282 while (read_bytes
< form_bytes
) {
283 wxInt32 id
= read_long(f
);
284 wxInt32 nbytes
= read_long(f
);
285 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
289 read_pnts(f
, nbytes
, lw_object
);
292 read_pols(f
, nbytes
, lw_object
);
295 read_srfs(f
, nbytes
, lw_object
);
298 read_surf(f
, nbytes
, lw_object
);
301 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
311 void lw_object_free(lwObject
*lw_object
)
313 if (lw_object
->face
) {
315 for (i
=0; i
<lw_object
->face_cnt
; i
++)
316 free(lw_object
->face
[i
].index
);
317 free(lw_object
->face
);
319 free(lw_object
->material
);
320 free(lw_object
->vertex
);
328 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
329 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
330 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
331 void lw_object_show(const lwObject
*lw_object
)
334 int prev_index_cnt
= -1;
335 int prev_material
= -1;
340 for (i
=0; i
<lw_object
->face_cnt
; i
++) {
341 GLfloat ax
,ay
,az
,bx
,by
,bz
,nx
,ny
,nz
,r
;
342 const lwFace
*face
= lw_object
->face
+i
;
344 /* ignore faces with less than 3 points */
345 if (face
->index_cnt
< 3)
348 /* calculate normal */
353 bx
= PX(face
->index_cnt
-1) - PX(0);
354 by
= PY(face
->index_cnt
-1) - PY(0);
355 bz
= PZ(face
->index_cnt
-1) - PZ(0);
357 nx
= ay
* bz
- az
* by
;
358 ny
= az
* bx
- ax
* bz
;
359 nz
= ax
* by
- ay
* bx
;
361 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
362 if (r
< 0.000001) /* avoid division by zero */
369 if (prev_index_cnt
!= face
->index_cnt
|| prev_index_cnt
> 4) {
370 if (prev_index_cnt
> 0) glEnd();
371 prev_index_cnt
= face
->index_cnt
;
372 switch (face
->index_cnt
) {
374 glBegin(GL_TRIANGLES
);
384 /* update material if necessary */
385 if (prev_material
!= face
->material
) {
386 prev_material
= face
->material
;
387 glColor3f(lw_object
->material
[face
->material
].r
,
388 lw_object
->material
[face
->material
].g
,
389 lw_object
->material
[face
->material
].b
);
392 /* update normal if necessary */
393 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
397 glNormal3f(nx
,ny
,nz
);
400 /* draw polygon/triangle/quad */
401 for (j
=0; j
<face
->index_cnt
; j
++)
402 glVertex3f(PX(j
),PY(j
),PZ(j
));
406 /* if glBegin was called call glEnd */
407 if (prev_index_cnt
> 0)
412 GLfloat
lw_object_radius(const lwObject
*lwo
)
415 double max_radius
= 0.0;
417 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
418 GLfloat
*v
= &lwo
->vertex
[i
*3];
419 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
423 return sqrt(max_radius
);
426 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
430 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
431 lwo
->vertex
[i
*3+0] *= scale
;
432 lwo
->vertex
[i
*3+1] *= scale
;
433 lwo
->vertex
[i
*3+2] *= scale
;