main.cpp
上传用户:qccn516
上传日期:2013-05-02
资源大小:3382k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /* 3d2obj utile
  2.  *
  3.  * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include <stdio.h>
  20. #include "mesh.h"
  21. /*
  22.  */
  23. void exportObj(const char *name,Mesh *mesh) {
  24. FILE *file = fopen(name,"wb");
  25. if(!file) {
  26. fprintf(stderr,"exportObj(): error create "%s" filen",name);
  27. return;
  28. }
  29. fprintf(file,"#####                           #####n");
  30. fprintf(file,"#                                   #n");
  31. fprintf(file,"# file was produced by 3d2obj utile #n");
  32. fprintf(file,"# http://frustum.org                #n");
  33. fprintf(file,"#                                   #n");
  34. fprintf(file,"#####                           #####n");
  35. int num_vertex = 0;
  36. for(int i = 0; i < mesh->getNumSurfaces(); i++) {
  37. Mesh::Vertex *vertex = mesh->getVertex(i);
  38. int *indices = mesh->getIndices(i);
  39. fprintf(file,"ng %sn",*mesh->getSurfaceName(i) == '' ? name : mesh->getSurfaceName(i));
  40. for(int j = 0; j < mesh->getNumVertex(i); j++) {
  41. fprintf(file,"v %f %f %fn",vertex[j].xyz.x,vertex[j].xyz.z,vertex[j].xyz.y);
  42. }
  43. for(int j = 0; j < mesh->getNumVertex(i); j++) {
  44. fprintf(file,"vn %f %f %fn",vertex[j].normal.x,vertex[j].normal.z,vertex[j].normal.y);
  45. }
  46. for(int j = 0; j < mesh->getNumVertex(i); j++) {
  47. fprintf(file,"vt %f %fn",vertex[j].texcoord.x,1.0f - vertex[j].texcoord.y);
  48. }
  49. for(int j = 0; j < mesh->getNumStrips(i); j++) {
  50. for(int k = 2; k < indices[0]; k++) {
  51. int v0 = indices[k - 2 + 1] + 1 + num_vertex;
  52. int v1 = indices[k - 1 + 1] + 1 + num_vertex;
  53. int v2 = indices[k - 0 + 1] + 1 + num_vertex;
  54. if(k % 2 == 1) fprintf(file,"f %d/%d/%d %d/%d/%d %d/%d/%dn",v0,v0,v0,v1,v1,v1,v2,v2,v2);
  55. else fprintf(file,"f %d/%d/%d %d/%d/%d %d/%d/%dn",v1,v1,v1,v0,v0,v0,v2,v2,v2);
  56. }
  57. indices += indices[0] + 1;
  58. }
  59. num_vertex += mesh->getNumVertex(i);
  60. }
  61. fclose(file);
  62. }
  63. /*
  64.  */
  65. int main(int argc,char **argv) {
  66. if(argc < 2) {
  67. printf("3d (.3ds or .mesh) to Maya OBJ formatn");
  68. printf("usage: %s <3d file> ...n",argv[0]);
  69. printf("written by Alexander Zaprjagaevn");
  70. printf("frustum@frustum.orgn");
  71. printf("http://frustum.orgn");
  72. return 0;
  73. }
  74. for(int i = 1; i < argc; i++) {
  75. char name[1024];
  76. strcpy(name,argv[i]);
  77. char *s = strrchr(name,'.');
  78. if(s) strcpy(s,".obj");
  79. else strcat(name,".obj");
  80. Mesh *mesh = new Mesh(argv[i]);
  81. if(mesh->getNumSurfaces() != 0) {
  82. printf("%s -> %sn",argv[i],name);
  83. exportObj(name,mesh);
  84. }
  85. delete mesh;
  86. }
  87. return 0;
  88. }