1 /* -----------------------------------------------------------------------------
5 Copyright (c) 2003, Randy Reddig & seaw0lf
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
18 Neither the names of the copyright holders nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ----------------------------------------------------------------------------- */
36 #include "picointernal.h"
42 unsigned char id_length, colormap_type, image_type;
43 unsigned short colormap_index, colormap_length;
44 unsigned char colormap_size;
45 unsigned short x_origin, y_origin, width, height;
46 unsigned char pixel_size, attributes;
53 _terrain_load_tga_buffer()
54 loads a tga image into a newly allocated image buffer
55 fixme: replace/clean this function
58 void _terrain_load_tga_buffer( unsigned char *buffer, unsigned char **pic, int *width, int *height ) {
60 int columns, rows, numPixels;
61 unsigned char *pixbuf;
64 unsigned char *targa_rgba;
69 if ( buffer == NULL ) {
75 targa_header.id_length = *buf_p++;
76 targa_header.colormap_type = *buf_p++;
77 targa_header.image_type = *buf_p++;
79 targa_header.colormap_index = _pico_little_short( *(short*)buf_p );
81 targa_header.colormap_length = _pico_little_short( *(short*) buf_p );
83 targa_header.colormap_size = *buf_p++;
84 targa_header.x_origin = _pico_little_short( *(short*) buf_p );
86 targa_header.y_origin = _pico_little_short( *(short*) buf_p );
88 targa_header.width = _pico_little_short( *(short*) buf_p );
90 targa_header.height = _pico_little_short( *(short*) buf_p );
92 targa_header.pixel_size = *buf_p++;
93 targa_header.attributes = *buf_p++;
95 if ( targa_header.image_type != 2 && targa_header.image_type != 10 && targa_header.image_type != 3 ) {
96 _pico_printf( PICO_ERROR, "Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n" );
101 if ( targa_header.colormap_type != 0 ) {
102 _pico_printf( PICO_ERROR, "Indexed color TGA images not supported\n" );
106 if ( targa_header.pixel_size != 32 && targa_header.pixel_size != 24 && targa_header.image_type != 3 ) {
107 _pico_printf( PICO_ERROR, "Only 32 or 24 bit TGA images supported (not indexed color)\n" );
112 columns = targa_header.width;
113 rows = targa_header.height;
114 numPixels = columns * rows;
123 targa_rgba = _pico_alloc( numPixels * 4 );
126 if ( targa_header.id_length != 0 ) {
127 buf_p += targa_header.id_length; // skip TARGA image comment
130 if ( targa_header.image_type == 2 || targa_header.image_type == 3 ) {
131 // Uncompressed RGB or gray scale image
132 for ( row = rows - 1; row >= 0; row-- )
134 pixbuf = targa_rgba + row * columns * 4;
135 for ( column = 0; column < columns; column++ )
137 unsigned char red,green,blue,alphabyte;
138 switch ( targa_header.pixel_size )
164 alphabyte = *buf_p++;
168 *pixbuf++ = alphabyte;
177 /* rle encoded pixels */
178 else if ( targa_header.image_type == 10 ) {
179 unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
186 for ( row = rows - 1; row >= 0; row-- ) {
187 pixbuf = targa_rgba + row * columns * 4;
188 for ( column = 0; column < columns; ) {
189 packetHeader = *buf_p++;
190 packetSize = 1 + ( packetHeader & 0x7f );
191 if ( packetHeader & 0x80 ) { // run-length packet
192 switch ( targa_header.pixel_size ) {
203 alphabyte = *buf_p++;
206 //Error("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
210 for ( j = 0; j < packetSize; j++ ) {
214 *pixbuf++ = alphabyte;
216 if ( column == columns ) { // run spans across rows
224 pixbuf = targa_rgba + row * columns * 4;
228 else { // non run-length packet
229 for ( j = 0; j < packetSize; j++ ) {
230 switch ( targa_header.pixel_size ) {
244 alphabyte = *buf_p++;
248 *pixbuf++ = alphabyte;
251 //Sysprintf("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
255 if ( column == columns ) { // pixel packet run spans across rows
263 pixbuf = targa_rgba + row * columns * 4;
272 /* fix vertically flipped image */
273 if ( ( targa_header.attributes & ( 1 << 5 ) ) ) {
275 for ( row = 0; row < .5f * rows; row++ )
277 for ( column = 0; column < columns; column++ )
279 flip = *( (int*)targa_rgba + row * columns + column );
280 *( (int*)targa_rgba + row * columns + column ) = *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column );
281 *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column ) = flip;
291 validates a picoterrain file
294 static int _terrain_canload( PM_PARAMS_CANLOAD ) {
298 /* create pico parser */
299 p = _pico_new_parser( (const picoByte_t*) buffer, bufSize );
301 return PICO_PMV_ERROR_MEMORY;
304 /* get first token */
305 if ( _pico_parse_first( p ) == NULL ) {
306 return PICO_PMV_ERROR_IDENT;
309 /* check first token */
310 if ( _pico_stricmp( p->token, "picoterrain" ) ) {
311 _pico_free_parser( p );
312 return PICO_PMV_ERROR_IDENT;
315 /* free the pico parser object */
316 _pico_free_parser( p );
318 /* file seems to be a valid picoterrain file */
326 loads a picoterrain file
329 static picoModel_t *_terrain_load( PM_PARAMS_LOAD ) {
330 int i, j, v, pw[ 5 ], r;
333 char *shader, *heightmapFile, *colormapFile;
334 picoVec3_t scale, origin;
336 unsigned char *imageBuffer;
337 int imageBufSize, w, h, cw, ch;
338 unsigned char *heightmap, *colormap, *heightPixel, *colorPixel;
340 picoModel_t *picoModel;
341 picoSurface_t *picoSurface;
342 picoShader_t *picoShader;
343 picoVec3_t xyz, normal;
348 /* create pico parser */
349 p = _pico_new_parser( (const picoByte_t*) buffer, bufSize );
354 /* get first token */
355 if ( _pico_parse_first( p ) == NULL ) {
359 /* check first token */
360 if ( _pico_stricmp( p->token, "picoterrain" ) ) {
361 _pico_printf( PICO_ERROR, "Invalid PicoTerrain model" );
362 _pico_free_parser( p );
367 shader = heightmapFile = colormapFile = NULL;
368 _pico_set_vec( scale, 512, 512, 32 );
370 /* parse ase model file */
373 /* get first token on line */
374 if ( !_pico_parse_first( p ) ) {
378 /* skip empty lines */
379 if ( !p->token || !p->token[ 0 ] ) {
384 if ( !_pico_stricmp( p->token, "shader" ) ) {
385 if ( _pico_parse( p, 0 ) && p->token[ 0 ] ) {
386 if ( shader != NULL ) {
387 _pico_free( shader );
389 shader = _pico_clone_alloc( p->token );
394 else if ( !_pico_stricmp( p->token, "heightmap" ) ) {
395 if ( _pico_parse( p, 0 ) && p->token[ 0 ] ) {
396 if ( heightmapFile != NULL ) {
397 _pico_free( heightmapFile );
399 heightmapFile = _pico_clone_alloc( p->token );
404 else if ( !_pico_stricmp( p->token, "colormap" ) ) {
405 if ( _pico_parse( p, 0 ) && p->token[ 0 ] ) {
406 if ( colormapFile != NULL ) {
407 _pico_free( colormapFile );
409 colormapFile = _pico_clone_alloc( p->token );
414 else if ( !_pico_stricmp( p->token, "scale" ) ) {
415 _pico_parse_vec( p, scale );
418 /* skip unparsed rest of line and continue */
419 _pico_parse_skip_rest( p );
422 /* ----------------------------------------------------------------- */
425 heightmap = imageBuffer = NULL;
426 _pico_load_file( heightmapFile, &imageBuffer, &imageBufSize );
427 _terrain_load_tga_buffer( imageBuffer, &heightmap, &w, &h );
428 _pico_free( heightmapFile );
429 _pico_free_file( imageBuffer );
431 if ( heightmap == NULL || w < 2 || h < 2 ) {
432 _pico_printf( PICO_ERROR, "PicoTerrain model with invalid heightmap" );
433 if ( shader != NULL ) {
434 _pico_free( shader );
436 if ( colormapFile != NULL ) {
437 _pico_free( colormapFile );
439 _pico_free_parser( p );
443 /* set origin (bottom lowest corner of terrain mesh) */
444 _pico_set_vec( origin, ( w / -2 ) * scale[ 0 ], ( h / -2 ) * scale[ 1 ], -128 * scale[ 2 ] );
447 colormap = imageBuffer = NULL;
448 _pico_load_file( colormapFile, &imageBuffer, &imageBufSize );
449 _terrain_load_tga_buffer( imageBuffer, &colormap, &cw, &ch );
450 _pico_free( colormapFile );
451 _pico_free_file( imageBuffer );
453 if ( cw != w || ch != h ) {
454 _pico_printf( PICO_WARNING, "PicoTerrain colormap/heightmap size mismatch" );
455 _pico_free( colormap );
459 /* ----------------------------------------------------------------- */
461 /* create new pico model */
462 picoModel = PicoNewModel();
463 if ( picoModel == NULL ) {
464 _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
469 PicoSetModelFrameNum( picoModel, frameNum );
470 PicoSetModelNumFrames( picoModel, 1 ); /* sea */
471 PicoSetModelName( picoModel, fileName );
472 PicoSetModelFileName( picoModel, fileName );
474 /* allocate new pico surface */
475 picoSurface = PicoNewSurface( picoModel );
476 if ( picoSurface == NULL ) {
477 _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
478 PicoFreeModel( picoModel ); /* sea */
482 /* terrain surfaces are triangle meshes */
483 PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
485 /* set surface name */
486 PicoSetSurfaceName( picoSurface, "picoterrain" );
488 /* create new pico shader */
489 picoShader = PicoNewShader( picoModel );
490 if ( picoShader == NULL ) {
491 _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
492 PicoFreeModel( picoModel );
493 _pico_free( shader );
497 /* detox and set shader name */
498 _pico_setfext( shader, NULL );
499 _pico_unixify( shader );
500 PicoSetShaderName( picoShader, shader );
501 _pico_free( shader );
503 /* associate current surface with newly created shader */
504 PicoSetSurfaceShader( picoSurface, picoShader );
506 /* make bogus normal */
507 _pico_set_vec( normal, 0.0f, 0.0f, 0.0f );
510 for ( j = 0; j < h; j++ )
512 for ( i = 0; i < w; i++ )
516 heightPixel = heightmap + v * 4;
517 colorPixel = colormap
522 _pico_set_vec( xyz, origin[ 0 ] + scale[ 0 ] * i,
523 origin[ 1 ] + scale[ 1 ] * j,
524 origin[ 2 ] + scale[ 2 ] * heightPixel[ 0 ] );
525 PicoSetSurfaceXYZ( picoSurface, v, xyz );
528 PicoSetSurfaceNormal( picoSurface, v, normal );
533 PicoSetSurfaceST( picoSurface, 0, v, st );
536 if ( colorPixel != NULL ) {
537 _pico_set_color( color, colorPixel[ 0 ], colorPixel[ 1 ], colorPixel[ 2 ], colorPixel[ 3 ] );
540 _pico_copy_color( picoColor_white, color );
542 PicoSetSurfaceColor( picoSurface, 0, v, color );
544 /* set triangles (zero alpha in heightmap suppresses this quad) */
545 if ( i < ( w - 1 ) && j < ( h - 1 ) && heightPixel[ 3 ] >= 128 ) {
547 pw[ 0 ] = i + ( j * w );
548 pw[ 1 ] = i + ( ( j + 1 ) * w );
549 pw[ 2 ] = i + 1 + ( ( j + 1 ) * w );
550 pw[ 3 ] = i + 1 + ( j * w );
551 pw[ 4 ] = i + ( j * w ); /* same as pw[ 0 ] */
556 /* make first triangle */
557 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 0 ), (picoIndex_t) pw[ r + 0 ] );
558 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 1 ), (picoIndex_t) pw[ r + 1 ] );
559 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 2 ), (picoIndex_t) pw[ r + 2 ] );
561 /* make second triangle */
562 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 3 ), (picoIndex_t) pw[ r + 0 ] );
563 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 4 ), (picoIndex_t) pw[ r + 2 ] );
564 PicoSetSurfaceIndex( picoSurface, ( v * 6 + 5 ), (picoIndex_t) pw[ r + 3 ] );
570 _pico_free_parser( p );
571 _pico_free( heightmap );
572 _pico_free( colormap );
574 /* return the new pico model */
580 /* pico file format module definition */
581 const picoModule_t picoModuleTerrain =
583 "1.3", /* module version string */
584 "PicoTerrain", /* module display name */
585 "Randy Reddig", /* author's name */
586 "2003 Randy Reddig", /* module copyright */
588 "picoterrain", NULL, NULL, NULL /* default extensions to use */
590 _terrain_canload, /* validation routine */
591 _terrain_load, /* load routine */
592 NULL, /* save validation routine */
593 NULL /* save routine */