]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/penguin/lw.cpp
fa9a3827e13c49f5214362970787086d1a32ece9
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"
41 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
42 (((wxUint32)(b))<<16)| \
43 (((wxUint32)(c))<< 8)| \
46 #define ID_FORM MK_ID('F','O','R','M')
47 #define ID_LWOB MK_ID('L','W','O','B')
48 #define ID_PNTS MK_ID('P','N','T','S')
49 #define ID_SRFS MK_ID('S','R','F','S')
50 #define ID_SURF MK_ID('S','U','R','F')
51 #define ID_POLS MK_ID('P','O','L','S')
52 #define ID_COLR MK_ID('C','O','L','R')
54 static wxInt32
read_char(FILE *f
)
60 static wxInt32
read_short(FILE *f
)
62 // the execution path was not always correct
63 // when using the direct evaluation in the return statement
64 wxInt32 first
= read_char(f
) ;
65 wxInt32 second
= read_char(f
) ;
67 return (first
<<8) | second
;
70 static wxInt32
read_long(FILE *f
)
72 // the execution path was not always correct
73 // when using the direct evaluation in the return statement
74 wxInt32 first
= read_char(f
) ;
75 wxInt32 second
= read_char(f
) ;
76 wxInt32 third
= read_char(f
) ;
77 wxInt32 fourth
= read_char(f
) ;
78 return (first
<<24) | (second
<<16) | (third
<<8) | fourth
;
81 static GLfloat
read_float(FILE *f
)
83 wxInt32 x
= read_long(f
);
87 static int read_string(FILE *f
, char *s
)
93 if (cnt
< LW_MAX_NAME_LEN
)
96 s
[LW_MAX_NAME_LEN
-1] = 0;
99 /* if length of string (including \0) is odd skip another byte */
107 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
109 int guess_cnt
= lwo
->material_cnt
;
112 lwMaterial
*material
;
114 /* allocate more memory for materials if needed */
115 if (guess_cnt
<= lwo
->material_cnt
) {
116 guess_cnt
+= guess_cnt
/2 + 4;
117 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
119 material
= lwo
->material
+ lwo
->material_cnt
++;
122 nbytes
-= read_string(f
,material
->name
);
129 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
133 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
136 char name
[LW_MAX_NAME_LEN
];
137 lwMaterial
*material
= NULL
;
139 /* read surface name */
140 nbytes
-= read_string(f
,name
);
143 for (i
=0; i
< lwo
->material_cnt
; i
++) {
144 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
145 material
= &lwo
->material
[i
];
152 int id
= read_long(f
);
153 int len
= read_short(f
);
154 nbytes
-= 6 + len
+ (len%2
);
158 material
->r
= read_char(f
) / 255.0;
159 material
->g
= read_char(f
) / 255.0;
160 material
->b
= read_char(f
) / 255.0;
161 read_char(f
); /* dummy */
164 fseek(f
, len
+(len%2
), SEEK_CUR
);
170 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
172 int guess_cnt
= lwo
->face_cnt
;
178 /* allocate more memory for polygons if necessary */
179 if (guess_cnt
<= lwo
->face_cnt
) {
180 guess_cnt
+= guess_cnt
+ 4;
181 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
183 face
= lwo
->face
+ lwo
->face_cnt
++;
185 /* number of points in this face */
186 face
->index_cnt
= read_short(f
);
189 /* allocate space for points */
190 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
193 for (i
=0; i
<face
->index_cnt
; i
++) {
194 face
->index
[i
] = read_short(f
);
198 /* read surface material */
199 face
->material
= read_short(f
);
202 /* skip over detail polygons */
203 if (face
->material
< 0) {
205 face
->material
= -face
->material
;
206 det_cnt
= read_short(f
);
208 while (det_cnt
-- > 0) {
209 int cnt
= read_short(f
);
210 fseek(f
, cnt
*2+2, SEEK_CUR
);
216 /* readjust to true size */
217 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
222 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
225 lwo
->vertex_cnt
= nbytes
/ 12;
226 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
227 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
228 lwo
->vertex
[i
*3+0] = read_float(f
);
229 lwo
->vertex
[i
*3+1] = read_float(f
);
230 lwo
->vertex
[i
*3+2] = read_float(f
);
239 bool lw_is_lwobject(const char *lw_file
)
241 FILE *f
= fopen(lw_file
, "rb");
243 wxInt32 form
= read_long(f
);
244 wxInt32 nlen
= read_long(f
);
245 wxInt32 lwob
= read_long(f
);
247 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
254 lwObject
*lw_object_read(const char *lw_file
)
258 FILE *f
= fopen(lw_file
, "rb");
263 /* check for headers */
264 if (read_long(f
) != ID_FORM
) {
269 wxInt32 read_bytes
= 0;
271 wxInt32 form_bytes
= read_long(f
);
274 if (read_long(f
) != ID_LWOB
) {
279 /* create new lwObject */
280 lwObject
*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
;