├── README.md └── main.c /README.md: -------------------------------------------------------------------------------- 1 | `gcc -c main.c` 2 | 3 | `gcc -o main main.o -lpng` 4 | 5 | It will try to look for mesh files in the current directory. 6 | 7 | `main mapNNN` without extensions. 8 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | // #include 2 | // #include 3 | // #include 4 | // #include 5 | // #include 6 | // #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // using namespace std; 16 | 17 | #define PNG_DEBUG 3 18 | #include 19 | 20 | typedef unsigned short int usint; 21 | 22 | typedef union usi 23 | { 24 | unsigned char b[2]; 25 | unsigned short int hl; 26 | } us_int; 27 | 28 | typedef union uli 29 | { 30 | unsigned char b[4]; 31 | unsigned long int hl; 32 | } ul_int; 33 | 34 | // void abort_(const char * s, ...) 35 | // { 36 | // va_list args; 37 | // va_start(args, s); 38 | // vfprintf(stderr, s, args); 39 | // fprintf(stderr, "\n"); 40 | // va_end(args); 41 | // abort(); 42 | // } 43 | 44 | int check_mesh( char *filename ) 45 | { 46 | FILE *file; 47 | ul_int mesh_start; 48 | 49 | file = fopen(filename, "r"); 50 | 51 | if(file == NULL) 52 | { 53 | printf( "Error opening %s\n", filename ); 54 | return -1; 55 | } 56 | 57 | fseek( file, 0x40, SEEK_SET ); 58 | 59 | fread( &mesh_start.b, 4, 1, file ); 60 | 61 | if( mesh_start.b[0] != 0xc4 ) 62 | { 63 | fclose( file ); 64 | return -1; 65 | } 66 | return 0; 67 | } 68 | 69 | int convert( char *filename, char *mapname ) 70 | { 71 | printf( "Converting %s with %s\n", filename, mapname ); 72 | if( check_mesh( filename ) != 0 ) 73 | { 74 | printf( "Not a mesh file.\n" ); 75 | return -1; 76 | } 77 | 78 | FILE *file; 79 | file = fopen(filename, "r"); 80 | 81 | unsigned long int meshstart; 82 | 83 | fseek( file, 0x40, SEEK_SET ); 84 | unsigned long int mesh_start; 85 | fread( &mesh_start, 4, 1, file ); 86 | 87 | if( mesh_start != 196 ) 88 | { 89 | printf( "Not a mesh file.\n" ); 90 | fclose( file ); 91 | return -1; 92 | } 93 | 94 | fseek( file, mesh_start, SEEK_SET ); 95 | 96 | //read header... 97 | usint header[ 4 ]; 98 | header[ 0 ] = 0; 99 | header[ 1 ] = 0; 100 | header[ 2 ] = 0; 101 | header[ 3 ] = 0; 102 | 103 | us_int h_tmp; 104 | 105 | fread( &h_tmp.b[0], 1, 1, file ); 106 | fread( &h_tmp.b[1], 1, 1, file ); 107 | header[ 0 ] = (usint) h_tmp.hl; 108 | // printf( "%i %i\n", h_tmp.b[0], h_tmp.hl ); 109 | 110 | fread( &h_tmp.b[0], 1, 1, file ); 111 | fread( &h_tmp.b[1], 1, 1, file ); 112 | header[ 1 ] = (usint) h_tmp.hl; 113 | // printf( "%i %i\n", h_tmp.b[0], h_tmp.hl ); 114 | 115 | fread( &h_tmp.b[0], 1, 1, file ); 116 | fread( &h_tmp.b[1], 1, 1, file ); 117 | header[ 2 ] = (usint) h_tmp.hl; 118 | // printf( "%i %i\n", h_tmp.b[0], h_tmp.hl ); 119 | 120 | fread( &h_tmp.b[0], 1, 1, file ); 121 | fread( &h_tmp.b[1], 1, 1, file ); 122 | header[ 3 ] = (usint) h_tmp.hl; 123 | // printf( "%i %i\n", h_tmp.b[0], h_tmp.hl ); 124 | 125 | usint N = header[ 0 ]; 126 | usint P = header[ 1 ]; 127 | usint Q = header[ 2 ]; 128 | usint R = header[ 3 ]; 129 | 130 | printf( "%i\n", h_tmp.hl ); 131 | if( N == 0 && P == 0 && Q == 0 && R == 0 ) 132 | { 133 | printf( "Not a mesh file.\n" ); 134 | fclose( file ); 135 | return -1; 136 | } 137 | 138 | printf( "Triangles in Map: %i\n", N ); 139 | printf( "Quads in Map: %i\n", P ); 140 | printf( "Untextured Triangles in Map: %i\n", Q ); 141 | printf( "Untextured Quads in Map: %i\n", R ); 142 | 143 | // 144 | // Read in textured triangles... 145 | // 146 | float triangles[ N+(P*2) ][ 3 ][ 3 ]; 147 | for( usint x = 0; x < N; x++ ) 148 | { 149 | for( usint y = 0; y < 3; y++ ) 150 | { 151 | for( usint z = 0; z < 3; z++ ) 152 | { 153 | short int v; 154 | us_int v_tmp; 155 | fread( &v_tmp.b[0], 1, 1, file ); 156 | fread( &v_tmp.b[1], 1, 1, file ); 157 | v = (short int) v_tmp.hl; 158 | if( z == 1 ) v = -v; 159 | triangles[ x ][ y ][ z ] = (float)v / 100; 160 | } 161 | } 162 | } 163 | // 164 | // Read in textured quads, converting to triangles. 165 | // 166 | for( usint x = N; x < N+(P*2); ) 167 | { 168 | //first triangle 169 | for( usint y = 0; y < 3; y++ ) 170 | { 171 | for( usint z = 0; z < 3; z++ ) 172 | { 173 | short int v; 174 | us_int v_tmp; 175 | fread( &v_tmp.b[0], 1, 1, file ); 176 | fread( &v_tmp.b[1], 1, 1, file ); 177 | v = (short int) v_tmp.hl; 178 | if( z == 1 ) v = -v; 179 | triangles[ x ][ y ][ z ] = (float)v / 100; 180 | } 181 | } 182 | x++; 183 | // 184 | // Second triangle 185 | // 186 | // first point 187 | triangles[ x ][ 2 ][ 0 ] = triangles[ x-1 ][ 2 ][ 0 ]; 188 | triangles[ x ][ 2 ][ 1 ] = triangles[ x-1 ][ 2 ][ 1 ]; 189 | triangles[ x ][ 2 ][ 2 ] = triangles[ x-1 ][ 2 ][ 2 ]; 190 | // second point 191 | triangles[ x ][ 0 ][ 0 ] = triangles[ x-1 ][ 1 ][ 0 ]; 192 | triangles[ x ][ 0 ][ 1 ] = triangles[ x-1 ][ 1 ][ 1 ]; 193 | triangles[ x ][ 0 ][ 2 ] = triangles[ x-1 ][ 1 ][ 2 ]; 194 | // third point 195 | for( usint z = 0; z < 3; z++ ) 196 | { 197 | short int v; 198 | us_int v_tmp; 199 | fread( &v_tmp.b[0], 1, 1, file ); 200 | fread( &v_tmp.b[1], 1, 1, file ); 201 | v = (short int) v_tmp.hl; 202 | if( z == 1 ) v = -v; 203 | triangles[ x ][ 1 ][ z ] = (float)v / 100; 204 | } 205 | x++; 206 | } 207 | // 208 | // Untextured stuff 209 | // 210 | // 211 | // Read in untextured triangles... 212 | // 213 | float ut_tri[ Q+(R*2) ][ 3 ][ 3 ]; 214 | for( usint x = 0; x < Q; x++ ) 215 | { 216 | for( usint y = 0; y < 3; y++ ) 217 | { 218 | for( usint z = 0; z < 3; z++ ) 219 | { 220 | short int v; 221 | us_int v_tmp; 222 | fread( &v_tmp.b[0], 1, 1, file ); 223 | fread( &v_tmp.b[1], 1, 1, file ); 224 | v = (short int) v_tmp.hl; 225 | if( z == 1 ) v = -v; 226 | ut_tri[ x ][ y ][ z ] = (float)v / 100; 227 | } 228 | } 229 | } 230 | // 231 | // Read in untextured quads, converting to triangles. 232 | // 233 | for( usint x = Q; x < Q+(R*2); ) 234 | { 235 | //first triangle 236 | for( usint y = 0; y < 3; y++ ) 237 | { 238 | for( usint z = 0; z < 3; z++ ) 239 | { 240 | short int v; 241 | us_int v_tmp; 242 | fread( &v_tmp.b[0], 1, 1, file ); 243 | fread( &v_tmp.b[1], 1, 1, file ); 244 | v = (short int) v_tmp.hl; 245 | if( z == 1 ) v = -v; 246 | ut_tri[ x ][ y ][ z ] = (float)v / 100; 247 | } 248 | } 249 | x++; 250 | // 251 | // Second triangle 252 | // 253 | // first point 254 | ut_tri[ x ][ 2 ][ 0 ] = ut_tri[ x-1 ][ 2 ][ 0 ]; 255 | ut_tri[ x ][ 2 ][ 1 ] = ut_tri[ x-1 ][ 2 ][ 1 ]; 256 | ut_tri[ x ][ 2 ][ 2 ] = ut_tri[ x-1 ][ 2 ][ 2 ]; 257 | // second point 258 | ut_tri[ x ][ 0 ][ 0 ] = ut_tri[ x-1 ][ 1 ][ 0 ]; 259 | ut_tri[ x ][ 0 ][ 1 ] = ut_tri[ x-1 ][ 1 ][ 1 ]; 260 | ut_tri[ x ][ 0 ][ 2 ] = ut_tri[ x-1 ][ 1 ][ 2 ]; 261 | // third point 262 | for( usint z = 0; z < 3; z++ ) 263 | { 264 | short int v; 265 | us_int v_tmp; 266 | fread( &v_tmp.b[0], 1, 1, file ); 267 | fread( &v_tmp.b[1], 1, 1, file ); 268 | v = (short int) v_tmp.hl; 269 | if( z == 1 ) v = -v; 270 | ut_tri[ x ][ 1 ][ z ] = (float)v / 100; 271 | } 272 | x++; 273 | } 274 | // 275 | // Read in normals... 276 | // 277 | float tnorms[ N+(P*2) ][ 3 ][ 3 ]; 278 | for( usint x = 0; x < N; x++ ) 279 | { 280 | for( usint y = 0; y < 3; y++ ) 281 | { 282 | for( usint z = 0; z < 3; z++ ) 283 | { 284 | short int v; 285 | us_int v_tmp; 286 | fread( &v_tmp.b[0], 1, 1, file ); 287 | fread( &v_tmp.b[1], 1, 1, file ); 288 | v = (short int) v_tmp.hl; 289 | if( z == 1 ) v = -v; 290 | tnorms[ x ][ y ][ z ] = (float)v / 100; 291 | } 292 | } 293 | } 294 | // 295 | // Read in normals for quads, converting to triangles. 296 | // 297 | for( usint x = N; x < N+(P*2); ) 298 | { 299 | //first triangle 300 | for( usint y = 0; y < 3; y++ ) 301 | { 302 | for( usint z = 0; z < 3; z++ ) 303 | { 304 | short int v; 305 | us_int v_tmp; 306 | fread( &v_tmp.b[0], 1, 1, file ); 307 | fread( &v_tmp.b[1], 1, 1, file ); 308 | v = (short int) v_tmp.hl; 309 | if( z == 1 ) v = -v; 310 | tnorms[ x ][ y ][ z ] = (float)v / 100; 311 | } 312 | } 313 | x++; 314 | // 315 | // Second triangle 316 | // 317 | // first point 318 | tnorms[ x ][ 2 ][ 0 ] = tnorms[ x-1 ][ 2 ][ 0 ]; 319 | tnorms[ x ][ 2 ][ 1 ] = tnorms[ x-1 ][ 2 ][ 1 ]; 320 | tnorms[ x ][ 2 ][ 2 ] = tnorms[ x-1 ][ 2 ][ 2 ]; 321 | // second point 322 | tnorms[ x ][ 0 ][ 0 ] = tnorms[ x-1 ][ 1 ][ 0 ]; 323 | tnorms[ x ][ 0 ][ 1 ] = tnorms[ x-1 ][ 1 ][ 1 ]; 324 | tnorms[ x ][ 0 ][ 2 ] = tnorms[ x-1 ][ 1 ][ 2 ]; 325 | // third point 326 | for( usint z = 0; z < 3; z++ ) 327 | { 328 | short int v; 329 | us_int v_tmp; 330 | fread( &v_tmp.b[0], 1, 1, file ); 331 | fread( &v_tmp.b[1], 1, 1, file ); 332 | v = (short int) v_tmp.hl; 333 | if( z == 1 ) v = -v; 334 | tnorms[ x ][ 1 ][ z ] = (float)v / 100; 335 | } 336 | x++; 337 | } 338 | 339 | // 340 | // Read in Texture data... 341 | // 342 | short int uv_coords[ N+(P*2) ][ 3 ][ 2 ]; 343 | short int palt_page[ N+(P*2) ][ 2 ]; 344 | for( usint x = 0; x < N; x++ ) 345 | { 346 | for( usint y = 0; y < 3; y++ ) 347 | { 348 | short int v; 349 | us_int v_tmp; 350 | fread( &v_tmp.b[0], 1, 1, file ); 351 | fread( &v_tmp.b[1], 1, 1, file ); 352 | uv_coords[ x ][ y ][ 0 ] = (short int) v_tmp.b[0]; 353 | uv_coords[ x ][ y ][ 1 ] = (short int) v_tmp.b[1]; 354 | 355 | if( y == 0 ) 356 | { 357 | us_int v_tmp; 358 | fread( &v_tmp.b[0], 1, 1, file ); 359 | fread( &v_tmp.b[1], 1, 1, file ); 360 | 361 | palt_page[ x ][ 0 ] = v_tmp.b[0]; 362 | } 363 | if( y == 1 ) 364 | { 365 | // page number 366 | us_int v_tmp; 367 | fread( &v_tmp.b[0], 1, 1, file ); 368 | fread( &v_tmp.b[1], 1, 1, file ); 369 | 370 | v_tmp.b[ 0 ] = v_tmp.b[ 0 ] & 3; 371 | 372 | v = (short int) v_tmp.hl; 373 | palt_page[ x ][ 1 ] = v *256; 374 | } 375 | } 376 | uv_coords[ x ][ 0 ][ 1 ] = uv_coords[ x ][ 0 ][ 1 ] + palt_page[ x ][ 1 ]; 377 | uv_coords[ x ][ 1 ][ 1 ] = uv_coords[ x ][ 1 ][ 1 ] + palt_page[ x ][ 1 ]; 378 | uv_coords[ x ][ 2 ][ 1 ] = uv_coords[ x ][ 2 ][ 1 ] + palt_page[ x ][ 1 ]; 379 | } 380 | // 381 | // Read in texture for quads, converting to triangles. 382 | // 383 | for( usint x = N; x < N+(P*2); ) 384 | { 385 | //First triangle 386 | for( usint y = 0; y < 3; y++ ) 387 | { 388 | short int v; 389 | us_int v_tmp; 390 | fread( &v_tmp.b[0], 1, 1, file ); 391 | fread( &v_tmp.b[1], 1, 1, file ); 392 | uv_coords[ x ][ y ][ 0 ] = (short int) v_tmp.b[0]; 393 | uv_coords[ x ][ y ][ 1 ] = (short int) v_tmp.b[1]; 394 | 395 | if( y == 0 ) 396 | { 397 | us_int v_tmp; 398 | fread( &v_tmp.b[0], 1, 1, file ); 399 | fread( &v_tmp.b[1], 1, 1, file ); 400 | 401 | palt_page[ x ][ 0 ] = v_tmp.b[0]; 402 | } 403 | if( y == 1 ) 404 | { 405 | // page number 406 | us_int v_tmp; 407 | fread( &v_tmp.b[0], 1, 1, file ); 408 | fread( &v_tmp.b[1], 1, 1, file ); 409 | 410 | v_tmp.b[ 0 ] = v_tmp.b[ 0 ] & 3; 411 | 412 | v = (short int) v_tmp.hl; 413 | palt_page[ x ][ 1 ] = v *256; 414 | //uv_coords[ x ][ y ][ 1 ] = uv_coords[ x ][ y ][ 1 ] + 415 | // palt_page[ x ][ 1 ]; 416 | } 417 | } 418 | uv_coords[ x ][ 0 ][ 1 ] = uv_coords[ x ][ 0 ][ 1 ] + palt_page[ x ][ 1 ]; 419 | uv_coords[ x ][ 1 ][ 1 ] = uv_coords[ x ][ 1 ][ 1 ] + palt_page[ x ][ 1 ]; 420 | uv_coords[ x ][ 2 ][ 1 ] = uv_coords[ x ][ 2 ][ 1 ] + palt_page[ x ][ 1 ]; 421 | x++; 422 | // 423 | // Second triangle 424 | // 425 | palt_page[ x ][ 0 ] = palt_page[ x-1 ][0]; 426 | short int p = palt_page[ x ][ 1 ] = palt_page[ x-1 ][1]; 427 | 428 | uv_coords[ x ][ 2 ][ 0 ] = uv_coords[ x-1 ][ 2 ][ 0 ]; 429 | uv_coords[ x ][ 2 ][ 1 ] = uv_coords[ x-1 ][ 2 ][ 1 ]; 430 | 431 | uv_coords[ x ][ 0 ][ 0 ] = uv_coords[ x-1 ][ 1 ][ 0 ]; 432 | uv_coords[ x ][ 0 ][ 1 ] = uv_coords[ x-1 ][ 1 ][ 1 ]; 433 | 434 | us_int v_tmp; 435 | fread( &v_tmp.b[0], 1, 1, file ); 436 | fread( &v_tmp.b[1], 1, 1, file ); 437 | 438 | uv_coords[ x ][ 1 ][ 0 ] = ( short int ) v_tmp.b[0]; 439 | uv_coords[ x ][ 1 ][ 1 ] = ( short int ) v_tmp.b[1] + p; 440 | 441 | x++; 442 | } 443 | fseek( file, 4 * Q + 4 * R, SEEK_CUR ); 444 | fseek( file, 2 * N + 2 * P, SEEK_CUR ); 445 | unsigned short int palettes[ 16 ][ 16 ][4]; 446 | 447 | // char zero = true; 448 | for( int x = 0; x < 16; x++ ) 449 | { 450 | for( int y = 0; y < 16; y++ ) 451 | { 452 | us_int v_tmp; 453 | fread( &v_tmp.b[0], 1, 1, file ); 454 | fread( &v_tmp.b[1], 1, 1, file ); 455 | unsigned char A, R, G, B; 456 | A = (v_tmp.b[1] & 0x80) >> 8; // one or zero 457 | B = (v_tmp.b[1] & 0x7c) >> 2; 458 | unsigned char t = (v_tmp.b[1] & 0x03) << 3; 459 | G = (v_tmp.b[0] & 0xe0) >> 5; 460 | G = G + t; 461 | R = (v_tmp.b[0] & 0x1F); 462 | palettes[ x ][ y ][0] = A*255; 463 | palettes[ x ][ y ][1] = R*7.96875f; // put the values into 255 color 464 | palettes[ x ][ y ][2] = G*7.96875f; 465 | palettes[ x ][ y ][3] = B*7.96875f; 466 | } 467 | } 468 | 469 | fclose( file ); 470 | 471 | printf( "Converting %s with %s\n", filename, mapname ); 472 | 473 | FILE *mf; 474 | mf = fopen(mapname, "r"); 475 | 476 | // png_byte color_type; 477 | // png_byte bit_depth; 478 | 479 | char texfile[50]; 480 | 481 | // for( int px = 0; px < 16; px++ ) 482 | // { 483 | int px = 0; 484 | png_structp png_ptr; 485 | png_infop info_ptr; 486 | int number_of_passes; 487 | png_bytep * row_pointers; 488 | 489 | row_pointers = (png_bytep*)malloc( sizeof(png_bytep) * 1024); 490 | for (int y=0; y < 1024; y++) 491 | row_pointers[y] = (png_byte*) malloc(sizeof(png_bytep) * 256 * 4); 492 | 493 | sprintf( texfile, "%s_tex_%i.png", mapname, px ); 494 | FILE *nf; 495 | nf = fopen( texfile , "w"); 496 | 497 | for( int yy = 1023; yy > 0; yy-- ) 498 | { 499 | int r = 0; 500 | for( int xx = 0; xx < 256*4; xx+=8 ) 501 | { 502 | unsigned char p; 503 | fread( &p, 1, 1, mf ); 504 | unsigned char p1 = (p & 0xF0) >> 4; 505 | unsigned char p2 = (p & 0x0F); 506 | 507 | row_pointers[ yy ][ r+0 ] = (unsigned char)palettes[ px ][ p1 ][1]; 508 | row_pointers[ yy ][ r+1 ] = (unsigned char)palettes[ px ][ p1 ][2]; 509 | row_pointers[ yy ][ r+2 ] = (unsigned char)palettes[ px ][ p1 ][3]; 510 | 511 | row_pointers[ yy ][ r+3 ] = (unsigned char)palettes[ px ][ p2 ][1]; 512 | row_pointers[ yy ][ r+4 ] = (unsigned char)palettes[ px ][ p2 ][2]; 513 | row_pointers[ yy ][ r+5 ] = (unsigned char)palettes[ px ][ p2 ][3]; 514 | 515 | r+=6; 516 | } 517 | } 518 | 519 | if (!nf) exit(-1); 520 | 521 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 522 | 523 | if (!png_ptr) exit(-1); 524 | 525 | info_ptr = png_create_info_struct(png_ptr); 526 | 527 | if (!info_ptr) exit(-1); 528 | 529 | if (setjmp(png_jmpbuf(png_ptr))) exit(-1); 530 | 531 | png_init_io(png_ptr, nf); 532 | 533 | if (setjmp(png_jmpbuf(png_ptr))) exit(-1); 534 | 535 | png_set_IHDR(png_ptr, info_ptr, 256, 1024, 8, 536 | PNG_COLOR_TYPE_RGB, 537 | PNG_INTERLACE_NONE, 538 | PNG_COMPRESSION_TYPE_BASE, 539 | PNG_FILTER_TYPE_BASE); 540 | 541 | png_write_info(png_ptr, info_ptr); 542 | 543 | if (setjmp(png_jmpbuf(png_ptr))) exit(-1); 544 | 545 | png_write_image(png_ptr, row_pointers); 546 | 547 | if (setjmp(png_jmpbuf(png_ptr))) exit(-1); 548 | 549 | png_write_end(png_ptr, NULL); 550 | 551 | 552 | for(int y=0; y < 1024; y++ ) free( row_pointers[ y ] ); 553 | free( row_pointers ); 554 | 555 | fclose( nf ); 556 | 557 | // 558 | // Convert all this stuff to OBJ format 559 | // 560 | char mtlfilename[50]; 561 | sprintf( mtlfilename, "%s_%s.mtl", filename, mapname ); 562 | 563 | FILE *file2; 564 | char outpf[80]; 565 | sprintf( outpf, "%s_%s.obj", filename, mapname ); 566 | file2 = fopen( outpf, "w" ); 567 | 568 | sprintf( outpf, "mtllib %s\n", mtlfilename ); 569 | fwrite( outpf, 1, strlen( outpf ), file2 ); 570 | // fwrite( "mtllib ", 1, 7, file2 ); 571 | // fwrite( filename, 1, strlen(filename), file2 ); 572 | // fwrite( ".mtl", 1, 4, file2); 573 | sprintf( outpf, "o terrain\n" ); 574 | fwrite( outpf, 1, strlen( outpf ), file2 ); 575 | // file2 << "o terrain" << endl; 576 | //Output triangles first. 577 | for( usint x =0; x < N+(P*2); x++ ) 578 | { 579 | for( usint y = 0; y < 3; y++ ) 580 | { 581 | fwrite( "v ", 1, 2, file2 ); 582 | sprintf( outpf, "%f %f %f\n", triangles[ x ][ y ][ 0 ], 583 | triangles[ x ][ y ][ 1 ], 584 | triangles[ x ][ y ][ 2 ] ); 585 | fwrite( outpf, 1, strlen( outpf ), file2 ); 586 | } 587 | //file2 << endl; 588 | } 589 | for( usint x =0; x < N+(P*2); x++ ) 590 | { 591 | for( usint y = 0; y < 3; y++ ) 592 | { 593 | fwrite( "vn ", 1, 3, file2 ); 594 | // file2 << "vn "; 595 | sprintf( outpf, "%f %f %f\n", tnorms[ x ][ y ][ 0 ], 596 | tnorms[ x ][ y ][ 1 ], 597 | tnorms[ x ][ y ][ 2 ] ); 598 | fwrite( outpf, 1, strlen( outpf ), file2 ); 599 | } 600 | //file2 << endl; 601 | } 602 | for( usint x =0; x < N+(P*2); x++ ) 603 | { 604 | for( usint y = 0; y < 3; y++ ) 605 | { 606 | fwrite( "vt ", 1, 3, file2 ); 607 | // file2 << "vt "; 608 | sprintf( outpf, "%f %f \n", ((float)uv_coords[ x ][ y ][ 0 ]/ 256), 609 | ((float)uv_coords[ x ][ y ][ 1 ]/1024)); 610 | fwrite( outpf, 1, strlen( outpf ), file2 ); 611 | } 612 | //file2 << endl; 613 | } 614 | fwrite( "g tex\n", 1, 6, file2 ); 615 | // file2 << "g tex" << endl; 616 | fwrite( "usemtl blah\n", 1, 12, file2 ); 617 | // file2 << "usemtl blah" << endl; 618 | unsigned short int n = 1; 619 | for( usint x = 0; x < N+(P*2); x++ ) 620 | { 621 | fwrite( "f ", 1, 2, file2 ); 622 | // file2 << "f "; 623 | for( usint y = 0; y < 3; y++ ) 624 | { 625 | sprintf( outpf, "%d/%d/%d ", n, n, n ); 626 | fwrite( outpf, 1, strlen( outpf ), file2 ); 627 | // file2 << n << "/" << n << "/" << n << " "; 628 | //file2 << n << " "; 629 | n++; 630 | } 631 | // file2 << endl; 632 | fwrite( "\n", 1, 1, file2 ); 633 | } 634 | fwrite( "o skirt\n", 1, 8, file2);// << endl; 635 | for( usint x =0; x < Q+(R*2); x++ ) 636 | { 637 | for( usint y = 0; y < 3; y++ ) 638 | { 639 | sprintf( outpf, "v %f %f %f\n", ut_tri[ x ][ y ][ 0 ], 640 | ut_tri[ x ][ y ][ 1 ], 641 | ut_tri[ x ][ y ][ 2 ] ); 642 | fwrite( outpf, 1, strlen(outpf), file2 ); 643 | } 644 | //file2 << endl; 645 | } 646 | for( usint x = 0; x < Q+(R*2); x++ ) 647 | { 648 | fwrite( "f ", 1, 2, file2 ); 649 | for( usint y = 0; y < 3; y++ ) 650 | { 651 | sprintf( outpf, "%d//%d ", n, n ); 652 | fwrite( outpf, 1, strlen(outpf), file2 ); 653 | //file2 << n << " "; 654 | n++; 655 | } 656 | fwrite( "\n", 1, 1, file2 ); 657 | } 658 | fclose( file2 ); 659 | 660 | FILE *mtlfile; 661 | mtlfile = fopen( mtlfilename, "w" ); 662 | sprintf( outpf, "newmtl blah\n" ); 663 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 664 | 665 | sprintf( outpf, "Ns 100.000\n" ); 666 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 667 | 668 | sprintf( outpf, "d 1.00000\n" ); 669 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 670 | 671 | sprintf( outpf, "illum 2\n" ); 672 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 673 | 674 | sprintf( outpf, "Kd 1.00000 1.00000 1.00000\n" ); 675 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 676 | 677 | sprintf( outpf, "Ka 1.00000 1.00000 1.00000\n" ); 678 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 679 | 680 | sprintf( outpf, "Ks 1.00000 1.00000 1.00000\n" ); 681 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 682 | 683 | sprintf( outpf, "Ke 1.00000 1.00000 1.00000\n" ); 684 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 685 | 686 | sprintf( outpf, "map_Kd %s\n", texfile ); 687 | fwrite( outpf, 1, strlen(outpf), mtlfile ); 688 | fclose( mtlfile ); 689 | } 690 | 691 | int walkdir(const char *path) 692 | { 693 | if( path == NULL ) return -1; 694 | int path_size; 695 | char *filename; 696 | char *curname; 697 | 698 | char **mnames; 699 | int num_mnames; 700 | 701 | char **tnames; 702 | int num_tnames; 703 | 704 | struct dirent *entry; 705 | DIR *dp; 706 | 707 | path_size = sizeof( char ) * strlen( path ); 708 | filename = (char*)malloc( path_size + 3 ); //+".xx" 709 | strcpy( filename, path ); 710 | 711 | mnames = (char**)malloc( sizeof( char* ) * 1 ); 712 | num_mnames = 0; 713 | 714 | tnames = (char**)malloc( sizeof( char* ) * 1 ); 715 | num_tnames = 0; 716 | 717 | 718 | 719 | dp = opendir("."); 720 | if (dp == NULL) 721 | { 722 | perror("opendir"); 723 | return -1; 724 | } 725 | 726 | while( (entry = readdir(dp)) ) 727 | { 728 | if( strncmp( entry->d_name, path, path_size ) == 0 ) 729 | { 730 | int i = strlen(entry->d_name); 731 | 732 | if( strncmp( entry->d_name + (i-3), "obj", 3 ) == 0 ) continue; 733 | if( strncmp( entry->d_name + (i-3), "gns", 3 ) == 0 ) continue; 734 | 735 | if( strncmp( entry->d_name + (i-3), "mtl", 3 ) == 0 ) continue; 736 | if( strncmp( entry->d_name + (i-3), "png", 3 ) == 0 ) continue; 737 | 738 | if( check_mesh( entry->d_name ) == 0 ) 739 | { 740 | num_mnames++; 741 | mnames = (char**)realloc( mnames, sizeof( char* )*num_mnames ); 742 | mnames[ num_mnames-1 ] = (char*)malloc( sizeof( char ) * strlen( entry->d_name ) + 1 ); 743 | strncpy( mnames[ num_mnames-1 ], entry->d_name, strlen( entry->d_name ) +1 ); 744 | } 745 | else 746 | { 747 | num_tnames++; 748 | tnames = (char**)realloc( tnames, sizeof( char* )*num_tnames ); 749 | tnames[ num_tnames-1 ] = (char*)malloc( sizeof( char ) * strlen( entry->d_name ) +1 ); 750 | strncpy( tnames[ num_tnames-1 ], entry->d_name, strlen( entry->d_name ) +1 ); 751 | } 752 | } 753 | } 754 | 755 | closedir(dp); 756 | printf( "%i meshes found\n", num_mnames ); 757 | printf( "%i possible textures\n", num_tnames ); 758 | for( int i=0; i < num_mnames; i++ ) 759 | { 760 | for( int j=0; j < num_tnames; j++ ) 761 | { 762 | printf( "%s > %s\n", mnames[ i ], tnames[ j ] ); 763 | convert( mnames[ i ], tnames[ j ] ); 764 | } 765 | } 766 | return 0; 767 | } 768 | 769 | int main(int argc, char **argv) 770 | { 771 | walkdir( argv[1] ); 772 | } 773 | 774 | --------------------------------------------------------------------------------