📄 bac.cpp
字号:
#include <fstream>
#include "string.h"
#include "stdlib.h"
#include <string>
using namespace std;
int fSize = 0;
int wSize = 0;
/*
int readShort()
{
unsigned char a, b;
fin.get(a);
fin.get(b);
fSize += 2;
return (((int)a) << 8)+b;
}
*/
void writeshort(short i,ofstream &fout)
{
unsigned char a;
a = i&0xff;
fout.put(a);
a = i >> 8;
fout.put(a);
}
void writeString(string str,ofstream &fout)
{
fout.put(str.length());
for (int i=0; i<str.length(); i++)
fout.put(str.at(i));
}
short textureWidth;
class _pnt
{
public:
short x, y, z;
};
short pnts;
_pnt pnt[10000];
class _vct
{
public:
short x,y,z;
};
short vcts;
_vct vct[10000];
class _texture
{
public:
char id;
char name[30];
short w;
short h;
};
short textures;
_texture texture[1000];
class _texcoord
{
public:
short x, y;
};
short texcoords;
_texcoord texcoord[10000];
class _face
{
public:
short p1, p2, p3/*, p4*/;
short t1, t2, t3/*, t4*/;
char mat;//the num of the material
};
short faces;
_face face[10000];
class _mesh
{
public:
short p1, p2, p3/*, p4*/;
short t1, t2, t3/*, t4*/;
};
_mesh mesh[200000];
class _color
{
public:
char r, g, b;
};
short colors;
_color color[10000];
class _material
{
public:
char flag;
char tex_index;
char col_index;// used for the index (the color index or the texture index)
};
short materials;
_material material[10000];
const char keyword[30][30] =
{
"Head", //0
"Figure", //1
"Textures", //2
"Colors", //3
"Materials", //4
"Vertices", //5
"coords", //6
"normals", //7
"TextureCoords", //8
"Polygons;", //9
"material", //10
"textureIndex", //11
"colorIndex", //12
"vct", //13
"pnt", //14
"face", //15
"i2", //16
"i3", //17
"f2", //18
"f3", //19
"blendMode", //20
"doubleFace", //21
"transparent", //22
"i4", //23
};
#define S_HEAD 0
#define S_FIGURE 1
#define S_TEXTURES 2
#define S_COLORS 3
#define S_MATERIALS 4
#define S_VERTICES 5
#define S_CORRDS 6
#define S_NORMALS 7
#define S_TCOORDS 8
#define S_POLYGONS 9
#define TYPE_MATERIAL 10
#define TYPE_TEXTUREIND 11
#define TYPE_COLORIND 12
#define TYPE_VCT 13
#define TYPE_PNT 14
#define TYPE_FACE 15
#define TYPE_I2 16
#define TYPE_I3 17
#define TYPE_F2 18
#define TYPE_F3 19
#define TYPE_BLENDMODE 20
#define TYPE_DOUBLEFACE 21
#define TYPE_TRANSPARENT 22
#define TYPE_I4 23
#define TYPE_COUNT 24
#define MAX_STACK 100
int stack[MAX_STACK];
int stack_start = 0;
int stack_end = MAX_STACK - 1;
int stack_cur = -1;
int pop()
{
if (stack_cur == -1)
return -1;
else
stack_cur--;
return stack_cur;
};
void push(int state)
{
if(stack_cur != MAX_STACK -1)
stack[++stack_cur] = state;
};
int cmp_state(const char* str)
{
int i = 0;
for ( i=0; i<TYPE_COUNT; i++)
{
if(!strcmp(str,keyword[i]))
return i;
}
return -1;
}
void main(int argc, char * argv[])
{
char str[256];
int pos;
int state;
int texture_w;
int texture_h;
int i1,i2,i3,i4;
float f1,f2,f3;
int count_face;
int input_state;
int i;
ifstream fin(argv[1]);
ofstream fout;//,foutdef;
string outname = argv[1];
// prepare for the outfilename
pos = string(argv[1]).find('.');
outname = outname.substr(0,pos);
outname += ".out";
fout.open(outname.c_str(),ios::binary);
//foutdef.open("define.h",ios::app);
textures = faces = texcoords = colors = -1;
materials = -1;
pnts = -1;
vcts = -1;
fin >> str;
while (strcmp(str, "("))
fin >>str;
while(!fin.eof())//if the file is end
{
if(!strcmp(str,"("))
{
fin>>str;
state = cmp_state(str);
push(state);
if (state <= S_POLYGONS)
input_state = state;
}
else if(!strcmp(str,")"))
{
pop();
}
if (stack_cur > -1)
{
switch(stack[stack_cur])
{
case S_HEAD:
break;
case S_FIGURE:
break;
case S_TEXTURES:
break;
case S_COLORS:
break;
case S_MATERIALS:
break;
case S_VERTICES:
break;
case S_CORRDS:
break;
case S_NORMALS:
break;
case S_TCOORDS:
break;
case S_POLYGONS:
break;
case TYPE_MATERIAL:
break;
case TYPE_TEXTUREIND:
materials ++;
fin>>str;
material[materials].tex_index = atoi(str);
break;
case TYPE_COLORIND:
fin>>str;
material[materials].col_index = atoi(str);
break;
case TYPE_VCT:
vcts ++;
fin>>str;
vct[vcts].x = int(atof(str)*1024);
fin>>str;
vct[vcts].y = int(atof(str)*1024);
fin>>str;
vct[vcts].z = int(atof(str)*1024);
break;
case TYPE_PNT:
pnts ++;
fin>>str;
f1 = atof(str);
pnt[pnts].x = int(f1>0?(f1+0.5f): (f1-0.5f));
fin>>str;
f1 = atof(str);
pnt[pnts].y = int(f1>0?(f1+0.5f): (f1-0.5f));
fin>>str;
f1 = atof(str);
pnt[pnts].z = int(f1>0?(f1+0.5f): (f1-0.5f));
break;
case TYPE_FACE:
fin>>str;
if ((!strcmp(str,"("))||(!strcmp(str,")")))
continue;
faces ++;
face[faces].mat = atoi(str);
count_face = 0;
break;
case TYPE_I2:
fin>>str;
i1 = atoi(str);
fin>>str;
i2 = atoi(str);
if (input_state == S_TEXTURES)
{
textures ++;
texture_w = i1;
texture_h = i2;
texture[textures].w = texture_w;
texture[textures].h = texture_h;
fin >> str; //read a ")"
pop();
fin >> str;
fin>>str;
fin>>str;
fin>>texture[textures].name;
texture[textures].name[strlen(texture[textures].name)-1]=0;
strcpy(texture[textures].name, &(texture[textures].name[1]));
}
break;
case TYPE_I3:
fin>>str;
i1 = atoi(str);
fin>>str;
i2 = atoi(str);
fin>>str;
i3 = atoi(str);
if (input_state == S_POLYGONS)
{
if (count_face == 0)
{
face[faces].p1 = i1;
face[faces].p2 = i2;
face[faces].p3 = i3;
count_face +=3;
}
else if (count_face == 3)
{
face[faces].t1 = i1;
face[faces].t2 = i2;
face[faces].t3 = i3;
count_face +=3;
}
else
return ;
}//error msg, the num of the point perface is error
break;
case TYPE_I4:
fin>>str;
i1 = atoi(str);
fin>>str;
i2 = atoi(str);
fin>>str;
i3 = atoi(str);
fin>>str;
i4 = atoi(str);
if (input_state == S_POLYGONS)
{
if (count_face == 0)
{
face[faces].p1 = i1;
face[faces].p2 = i2;
face[faces].p3 = i3;
face[faces+1].p1 = i1;
face[faces+1].p2 = i4;
face[faces+1].p3 = i3;
count_face +=3;
}
else if (count_face == 3)
{
face[faces].t1 = i1;
face[faces].t2 = i2;
face[faces].t3 = i3;
face[faces+1].t1 = i1;
face[faces+1].t2 = i4;
face[faces+1].t3 = i3;
count_face +=3;
faces++;
face[faces].mat = face[faces-1].mat;
}
else
return ;
}//error msg, the num of the point perface is error
break;
case TYPE_F2:
fin>>str;
f1 = atof(str);
fin>>str;
f2 = atof(str);
if (input_state == S_TCOORDS)
{
texcoords ++;
texcoord[texcoords].x = (int)(f1*1024);
texcoord[texcoords].y = (int)(f2*1024);
}
break;
case TYPE_F3:
fin>>str;
f1 = atof(str);
fin>>str;
f2 = atof(str);
fin>>str;
f3 = atof(str);
if (input_state == S_COLORS)
{
colors ++;
color[colors].r = (int)(f1*255);
color[colors].g = (int)(f2*255);
color[colors].b = (int)(f3*255);
}
break;
case TYPE_BLENDMODE:
break;
case TYPE_DOUBLEFACE:
break;
case TYPE_TRANSPARENT:
break;
}
}
fin>>str;
}
int idx1, idx2;
for( i=0;i<=materials;i++)
{
mesh[i*2000].p1 = 1;
}
for(i=0;i<=faces;i++)
{
idx1 = face[i].mat*2000;
idx2 = mesh[idx1].p1;
mesh[idx1+idx2].p1 = face[i].p1;
mesh[idx1+idx2].p2 = face[i].p2;
mesh[idx1+idx2].p3 = face[i].p3;
mesh[idx1+idx2].t1 = face[i].t1;
mesh[idx1+idx2].t2 = face[i].t2;
mesh[idx1+idx2].t3 = face[i].t3;
mesh[face[i].mat*2000].p1++;
}
char flag = 0;//flag for the figure attribute
if (textures != -1)
flag |= 0x80;// if have the texture
if (colors != -1)
flag |= 0x40;// if have the color
if (vcts != -1)
flag |= 0x20;// if have the normal
fout.put(flag); //flag;
writeshort(pnts+1,fout);//pnt
writeshort(vcts+1,fout);//vct
for(i=0;i<=pnts;i++)
{
writeshort(pnt[i].x,fout);
writeshort(pnt[i].y,fout);
writeshort(pnt[i].z,fout);
if(vcts != -1)
{
writeshort(vct[i].x,fout);
writeshort(vct[i].y,fout);
writeshort(vct[i].z,fout);
}
}
if(textures != -1)
{
writeshort(textures+1,fout);
for(i=0;i<=textures;i++)
{
//writeshort(texture[textures].id,fout);
//fout.put(texture[textures].id);
string temp = texture[i].name;
writeString(temp, fout);
writeshort(texture[i].w,fout);
writeshort(texture[i].h,fout);
}
writeshort(texcoords+1,fout);
for(i=0;i<=texcoords;i++)
{
writeshort(texcoord[i].x,fout);
writeshort(texcoord[i].y,fout);
}
}
else if(colors != -1)
{
writeshort(colors+1,fout);
for(i=0;i<=colors;i++)
{
fout.put(color[i].r);
fout.put(color[i].g);
fout.put(color[i].b);
}
}
writeshort(materials+1,fout);
if(textures != -1)
for(i=0;i<=materials;i++)
/*writeshort(material[materials].tex_index,fout);*/
fout.put(material[i].tex_index);
else if (colors != -1)
for(i=0;i<=materials;i++)
/*writeshort(material[materials].col_index,fout);*/
fout.put(material[i].col_index);
// writeshort(faces+1,fout);
for(i=0;i<=materials;i++)
{
writeshort(mesh[i*2000].p1-1,fout);
}
for(int j=0;j<=materials;j++)
{
for(i=1;i<mesh[j*2000].p1;i++)
{
writeshort(mesh[j*2000+i].p1,fout);
writeshort(mesh[j*2000+i].p2,fout);
writeshort(mesh[j*2000+i].p3,fout);
writeshort(mesh[j*2000+i].t1,fout);
writeshort(mesh[j*2000+i].t2,fout);
writeshort(mesh[j*2000+i].t3,fout);
}
}
// fin.close();
// fout.close();
/*
for(i=0;i<textures;i++)
{
for(int j=0;j<=29;j++)
{
if(texture[i].name[j] == '[')texture[i].name[j]='"';
if(texture[i].name[j] == ']')texture[i].name[j]='"';
}
foutdef<<"#define "<<"TEXTURE_"<<i<<" "<<texture[i].name<<endl;
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -