]>
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.
29 // does this not give redefine errors on other platforms ?
31 #define wxUint32 unsigned int
42 #define MK_ID(a,b,c,d) ((((wxUint32)(a))<<24)| \
43 (((wxUint32)(b))<<16)| \
44 (((wxUint32)(c))<< 8)| \
47 #define ID_FORM MK_ID('F','O','R','M')
48 #define ID_LWOB MK_ID('L','W','O','B')
49 #define ID_PNTS MK_ID('P','N','T','S')
50 #define ID_SRFS MK_ID('S','R','F','S')
51 #define ID_SURF MK_ID('S','U','R','F')
52 #define ID_POLS MK_ID('P','O','L','S')
53 #define ID_COLR MK_ID('C','O','L','R')
55 static wxInt32
read_char(FILE *f
)
61 static wxInt32
read_short(FILE *f
)
63 // the execution path was not always correct
64 // when using the direct evaluation in the return statement
65 wxInt32 first
= read_char(f
) ;
66 wxInt32 second
= read_char(f
) ;
68 return (first
<<8) | second
;
71 static wxInt32
read_long(FILE *f
)
73 // the execution path was not always correct
74 // when using the direct evaluation in the return statement
75 wxInt32 first
= read_char(f
) ;
76 wxInt32 second
= read_char(f
) ;
77 wxInt32 third
= read_char(f
) ;
78 wxInt32 fourth
= read_char(f
) ;
79 return (first
<<24) | (second
<<16) | (third
<<8) | fourth
;
82 static GLfloat
read_float(FILE *f
)
84 wxInt32 x
= read_long(f
);
88 static int read_string(FILE *f
, char *s
)
94 if (cnt
< LW_MAX_NAME_LEN
)
97 s
[LW_MAX_NAME_LEN
-1] = 0;
100 /* if length of string (including \0) is odd skip another byte */
108 static void read_srfs(FILE *f
, int nbytes
, lwObject
*lwo
)
110 int guess_cnt
= lwo
->material_cnt
;
113 lwMaterial
*material
;
115 /* allocate more memory for materials if needed */
116 if (guess_cnt
<= lwo
->material_cnt
) {
117 guess_cnt
+= guess_cnt
/2 + 4;
118 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*guess_cnt
);
120 material
= lwo
->material
+ lwo
->material_cnt
++;
123 nbytes
-= read_string(f
,material
->name
);
130 lwo
->material
= (lwMaterial
*) realloc(lwo
->material
, sizeof(lwMaterial
)*lwo
->material_cnt
);
134 static void read_surf(FILE *f
, int nbytes
, lwObject
*lwo
)
137 char name
[LW_MAX_NAME_LEN
];
138 lwMaterial
*material
= NULL
;
140 /* read surface name */
141 nbytes
-= read_string(f
,name
);
144 for (i
=0; i
< lwo
->material_cnt
; i
++) {
145 if (strcmp(lwo
->material
[i
].name
,name
) == 0) {
146 material
= &lwo
->material
[i
];
153 int id
= read_long(f
);
154 int len
= read_short(f
);
155 nbytes
-= 6 + len
+ (len%2
);
159 material
->r
= read_char(f
) / 255.0;
160 material
->g
= read_char(f
) / 255.0;
161 material
->b
= read_char(f
) / 255.0;
162 read_char(f
); /* dummy */
165 fseek(f
, len
+(len%2
), SEEK_CUR
);
171 static void read_pols(FILE *f
, int nbytes
, lwObject
*lwo
)
173 int guess_cnt
= lwo
->face_cnt
;
179 /* allocate more memory for polygons if necessary */
180 if (guess_cnt
<= lwo
->face_cnt
) {
181 guess_cnt
+= guess_cnt
+ 4;
182 lwo
->face
= (lwFace
*) realloc((void*) lwo
->face
, sizeof(lwFace
)*guess_cnt
);
184 face
= lwo
->face
+ lwo
->face_cnt
++;
186 /* number of points in this face */
187 face
->index_cnt
= read_short(f
);
190 /* allocate space for points */
191 face
->index
= (int*) calloc(sizeof(int)*face
->index_cnt
,1);
194 for (i
=0; i
<face
->index_cnt
; i
++) {
195 face
->index
[i
] = read_short(f
);
199 /* read surface material */
200 face
->material
= read_short(f
);
203 /* skip over detail polygons */
204 if (face
->material
< 0) {
206 face
->material
= -face
->material
;
207 det_cnt
= read_short(f
);
209 while (det_cnt
-- > 0) {
210 int cnt
= read_short(f
);
211 fseek(f
, cnt
*2+2, SEEK_CUR
);
217 /* readjust to true size */
218 lwo
->face
= (lwFace
*) realloc(lwo
->face
, sizeof(lwFace
)*lwo
->face_cnt
);
223 static void read_pnts(FILE *f
, int nbytes
, lwObject
*lwo
)
226 lwo
->vertex_cnt
= nbytes
/ 12;
227 lwo
->vertex
= (float*) calloc(sizeof(GLfloat
)*lwo
->vertex_cnt
*3, 1);
228 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
229 lwo
->vertex
[i
*3+0] = read_float(f
);
230 lwo
->vertex
[i
*3+1] = read_float(f
);
231 lwo
->vertex
[i
*3+2] = read_float(f
);
240 int lw_is_lwobject(const char *lw_file
)
242 FILE *f
= fopen(lw_file
, "rb");
244 wxInt32 form
= read_long(f
);
245 wxInt32 nlen
= read_long(f
);
246 wxInt32 lwob
= read_long(f
);
248 if (form
== ID_FORM
&& nlen
!= 0 && lwob
== ID_LWOB
)
255 lwObject
*lw_object_read(const char *lw_file
)
258 lwObject
*lw_object
= NULL
;
260 wxInt32 form_bytes
= 0;
261 wxInt32 read_bytes
= 0;
264 f
= fopen(lw_file
, "rb");
269 /* check for headers */
270 if (read_long(f
) != ID_FORM
) {
274 form_bytes
= read_long(f
);
277 if (read_long(f
) != ID_LWOB
) {
282 /* create new lwObject */
283 lw_object
= (lwObject
*) calloc(sizeof(lwObject
),1);
286 while (read_bytes
< form_bytes
) {
287 wxInt32 id
= read_long(f
);
288 wxInt32 nbytes
= read_long(f
);
289 read_bytes
+= 8 + nbytes
+ (nbytes%2
);
293 read_pnts(f
, nbytes
, lw_object
);
296 read_pols(f
, nbytes
, lw_object
);
299 read_srfs(f
, nbytes
, lw_object
);
302 read_surf(f
, nbytes
, lw_object
);
305 fseek(f
, nbytes
+ (nbytes%2
), SEEK_CUR
);
315 void lw_object_free(lwObject
*lw_object
)
317 if (lw_object
->face
) {
319 for (i
=0; i
<lw_object
->face_cnt
; i
++)
320 free(lw_object
->face
[i
].index
);
321 free(lw_object
->face
);
323 free(lw_object
->material
);
324 free(lw_object
->vertex
);
332 #define PX(i) (lw_object->vertex[face->index[i]*3+0])
333 #define PY(i) (lw_object->vertex[face->index[i]*3+1])
334 #define PZ(i) (lw_object->vertex[face->index[i]*3+2])
335 void lw_object_show(const lwObject
*lw_object
)
338 int prev_index_cnt
= -1;
339 int prev_material
= -1;
344 for (i
=0; i
<lw_object
->face_cnt
; i
++) {
345 GLfloat ax
,ay
,az
,bx
,by
,bz
,nx
,ny
,nz
,r
;
346 const lwFace
*face
= lw_object
->face
+i
;
348 /* ignore faces with less than 3 points */
349 if (face
->index_cnt
< 3)
352 /* calculate normal */
357 bx
= PX(face
->index_cnt
-1) - PX(0);
358 by
= PY(face
->index_cnt
-1) - PY(0);
359 bz
= PZ(face
->index_cnt
-1) - PZ(0);
361 nx
= ay
* bz
- az
* by
;
362 ny
= az
* bx
- ax
* bz
;
363 nz
= ax
* by
- ay
* bx
;
365 r
= sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
);
366 if (r
< 0.000001) /* avoid division by zero */
373 if (prev_index_cnt
!= face
->index_cnt
|| prev_index_cnt
> 4) {
374 if (prev_index_cnt
> 0) glEnd();
375 prev_index_cnt
= face
->index_cnt
;
376 switch (face
->index_cnt
) {
378 glBegin(GL_TRIANGLES
);
388 /* update material if necessary */
389 if (prev_material
!= face
->material
) {
390 prev_material
= face
->material
;
391 glColor3f(lw_object
->material
[face
->material
].r
,
392 lw_object
->material
[face
->material
].g
,
393 lw_object
->material
[face
->material
].b
);
396 /* update normal if necessary */
397 if (nx
!= prev_nx
|| ny
!= prev_ny
|| nz
!= prev_nz
) {
401 glNormal3f(nx
,ny
,nz
);
404 /* draw polygon/triangle/quad */
405 for (j
=0; j
<face
->index_cnt
; j
++)
406 glVertex3f(PX(j
),PY(j
),PZ(j
));
410 /* if glBegin was called call glEnd */
411 if (prev_index_cnt
> 0)
416 GLfloat
lw_object_radius(const lwObject
*lwo
)
419 double max_radius
= 0.0;
421 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
422 GLfloat
*v
= &lwo
->vertex
[i
*3];
423 double r
= v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2];
427 return sqrt(max_radius
);
430 void lw_object_scale(lwObject
*lwo
, GLfloat scale
)
434 for (i
=0; i
<lwo
->vertex_cnt
; i
++) {
435 lwo
->vertex
[i
*3+0] *= scale
;
436 lwo
->vertex
[i
*3+1] *= scale
;
437 lwo
->vertex
[i
*3+2] *= scale
;