codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include"stdio.h" #include"stdlib.h" #include"string.h" #include"queue" #include"vector" #include"string" using namespace std; char buffer [1024]; #define out printf("%lf_",ret); #define endln printf("\n"); struct Normal { float vn[3]; }; float MyPow(int i,int j) { float ret=i; for(int i=0;i<j;i++) ret*=0.1; return ret; } bool isNum(char a) { if(a>=48&&a<58) return true; return false; } bool CatchFace(char * str,unsigned int * vi,unsigned int * ui,unsigned int * vni,int pos) { if(str[0]!='f'||str[1]!=' ') return false; int cur; unsigned int ret=0; int state=1,i; int m=strlen(str),a[3],c=0; /* State状态: 001B(1):vi 011B(3):ui,vi 101B(5):vi,vni 111B(7):vi,ui,vni */ vi[pos]=0;ui[pos]=0;vni[pos]=0; for(i=2;i<m&&str[i]!=' ';i++) { if(str[i]=='/') { state+=2; a[c++]=i; } } if(state==5&&a[1]-a[0]!=1) state+=2; cur=0; for(i=2;i<m&&str[i]!=' ';i++) { if(str[i]=='\n'||str[i]=='\0') return true; if(state%2) { for(ret=0;i<m;i++) { if(str[i]=='/'||str[i]==' ') break; if(isNum(str[i])) { ret*=10; ret+=str[i]-48; } } vi[pos+cur]=ret-1; } else vi[pos+cur]=0; if((state/2)%2) { for(ret=0,i+=1;i<m;i++) { if(str[i]=='/'||str[i]==' '||str[i]=='\n') break; if(isNum(str[i])) { ret*=10; ret+=str[i]-48; } } ui[pos+cur]=ret-1; } else { ui[pos+cur]=0; if(state/4%2) i+=1; } if(state/4%2) { for(ret=0,i+=1;i<m;i++) { if(str[i]=='/'||str[i]==' '||str[i]=='\n') break; if(isNum(str[i])) { ret*=10; ret+=str[i]-48; } } vni[pos+cur]=ret-1; } else vni[pos+cur]=0; ++cur; } return true; } bool CatchVertex(char *str,float & v) { if(str[0]!='v'||str[1]!=' ') return false; float ret; int i,m=strlen(str),cur=0; bool False,Float; int pos; for(i=2;i<m;i++) { if(str[i]==' ') continue; for(pos=1,Float=false,False=false,ret=0;i<m&&str[i]!=' ';i++) { if(isNum(str[i])) { if(Float) ret+=MyPow(str[i]-48,pos++); else { ret*=10; ret+=str[i]-48; } } else { if(str[i]=='.') Float=true; if(str[i]=='-') False=true; } } if(False) ret*=-1; *(&v+cur)=ret; cur++; } return true; } bool CatchNormal(char * str,Normal & n) { if(str[0]!='v'||str[1]!='n'||str[2]!=' ') return false; n.vn[0]=0;n.vn[1]=0;n.vn[2]=0; float ret; int i,m=strlen(str),cur=0; bool False,Float; int pos; for(i=3;i<m;i++) { if(str[i]==' ') continue; for(pos=1,Float=false,False=false,ret=0;i<m&&str[i]!=' ';i++) { if(isNum(str[i])) { if(Float) ret+=MyPow(str[i]-48,pos++); else { ret*=10; ret+=str[i]-48; } } else { if(str[i]=='.') Float=true; if(str[i]=='-') False=true; } } if(False) ret*=-1; if(cur<3) n.vn[cur]=ret; cur++; } return true; } bool CatchTex(char * str,float & t) { if(str[0]!='v'||str[1]!='t'||str[2]!=' ') return false; float ret; int i,m=strlen(str),cur=0; bool False,Float; int pos; for(i=2;i<m;i++) { if(str[i]==' ') continue; for(pos=1,Float=false,False=false,ret=0;i<m&&str[i]!=' ';i++) { if(isNum(str[i])) { if(Float) ret+=MyPow(str[i]-48,pos++); else { ret*=10; ret+=str[i]-48; } } else { if(str[i]=='.') Float=true; if(str[i]=='-') False=true; } } if(False) ret*=-1; *(&t+cur)=ret; cur++; } return true; } class Group { public: Group() { f=0;v=0;vt=0;vn=0; } int SendData() { ; } int SendIndex(unsigned int data,string type) { ++f; if(type=="vi") vertexindex.push_back(data); else if(type=="ui") textureindex.push_back(data); else if(type=="vni") normalindex.push_back(data); return 0; } private: unsigned int f; unsigned int v,vt,vn; vector<double>vertex; vector<double>texture; vector<Normal>normal; vector<unsigned int>vertexindex; vector<unsigned int>textureindex; vector<unsigned int>normalindex; }; class Catch { public: Catch(const char * str) { int vicnt=0,vcnt=0,vncnt=0,vtcnt=0; strcpy_s(tem,str); preread(str); fclose(fp); vertex=new float [v*3]; texture=new float [vt*3]; normal=new Normal [vn]; vertexindex=new unsigned int [f*3]; textureindex=new unsigned int [f*3]; normalindex=new unsigned int [f*3]; if((fp=fopen(str,"r"))==NULL) { perror(str); exit(1); } while(fgets(buffer,1024,fp)!=NULL) { if(buffer[0]=='#') continue; buffer[strlen(buffer)-1]=' '; if(CatchVertex(buffer,vertex[vcnt])) vcnt+=3; if(CatchNormal(buffer,normal[vncnt])) vncnt++; if(CatchTex(buffer,texture[vtcnt])) vtcnt+=3; if(CatchFace(buffer,vertexindex,textureindex,normalindex,vicnt)) vicnt+=3; } } ~Catch() { if(vertex) { delete [] vertex; vertex=NULL; } if(texture) { delete [] texture; texture=NULL; } if(normal) { delete [] normal; normal=NULL; } if(vertexindex) { delete [] vertexindex; vertexindex=NULL; } if(textureindex) { delete [] textureindex; textureindex=NULL; } if(normalindex) { delete [] normalindex; normalindex=NULL; } } bool preread(const char * src) { v=0;vn=0;vt=0;f=0; int cnt=0; if((fp=fopen(tem,"r"))==NULL) { perror(tem); exit(1); return false; } /* v表示顶点 vt表示贴图坐标 vn表示法线坐标 f 顶点索引 / uv点索引 / 法线索引 */ while(fgets(buffer,1024,fp)!=NULL) { ++cnt; if(buffer[0]=='f'&&buffer[1]==' ') ++f; if(buffer[0]=='v'&&buffer[1]==' ') ++v; if(buffer[0]=='v'&&buffer[1]=='t'&&buffer[2]==' ') ++vt; if(buffer[0]=='v'&&buffer[1]=='n'&&buffer[2]==' ') ++vn; } fclose(fp); return true; } bool ClearBuffer() { if(vertex) { delete [] vertex; vertex=NULL; } if(texture) { delete [] texture; texture=NULL; } if(normal) { delete [] normal; normal=NULL; } if(vertexindex) { delete [] vertexindex; vertexindex=NULL; } if(textureindex) { delete [] textureindex; textureindex=NULL; } if(normalindex) { delete [] normalindex; normalindex=NULL; } return true; } inline int get_f(){return f;} inline int get_v(){return v;} inline int get_vt(){return vt;} inline int get_vn(){return vn;} inline float * get_vertex(){return vertex;} inline float * get_texture(){return texture;} inline Normal * get_normal(){return normal;} inline unsigned int * get_vi(){return vertexindex;} inline unsigned int * get_ui(){return textureindex;} inline unsigned int * get_vni(){return normalindex;} private: char tem[100]; FILE *fp; vector<FILE *> mtl; unsigned int f; unsigned int v,vt,vn; float *vertex; float *texture; Normal *normal; unsigned int *vertexindex; unsigned int *textureindex; unsigned int *normalindex; };
Private
[
?
]
Run code
Submit