here's a pic:
www.freewebs.com/ttotto/snap037.bmp
heres the code:
Code: Select all
EZ_model* EZ_3d::Load_obj(char *fname)
{
FILE* fp;
fp=fopen(fname,"rb");
if (fp==NULL) return NULL;
int t_verts=0, t_faces=-1;
bool normals=false;// if they are defined on the file or not
bool textures=false;
char buffer[256];
int t_norms=0;
EZ_model* model =(EZ_model*)malloc( sizeof(EZ_model));
model->verts=0;
model->t_verts=0;
model->t_faces=0;
while(!feof(fp))
{
fgets(buffer, sizeof(buffer), fp);
if(buffer[0] == 'v' && buffer[1] == ' ')
{
t_verts++;
}
else if(buffer[0] == 'f' && buffer[1] == ' ')
{
t_faces++;
}
else if(buffer[0] == 'v' && buffer[1] == 'n')
{
normals=true;
t_norms++;
}
else if(buffer[0] == 'v' && buffer[1] == 't')
{
textures=true;
}
}
fseek ( fp , 0 , SEEK_SET );
allVertex tverts[t_verts];
model->t_verts=t_verts;
model->t_faces=t_faces;
model->list = (unsigned int *) malloc(t_faces*3*10);
model->indices=(short*) memalign(16, t_faces * 3 * sizeof(short));
model->verts = (allVertex*) memalign(16, t_faces * 3 * sizeof(allVertex));
short index[t_faces*3];
char tu[4][32];
float tempv[3];
float tempn[3];
int tempf[4];
int tn[3];
int v=0;
int r=0;
int h=0;
int i=0;
while(!feof(fp))
{
fgets(buffer, sizeof(buffer), fp);
if(buffer[0] == 'v' && buffer[1] == ' ')
{
sscanf(buffer,"%*s %s %s %s",tu[0],tu[1],tu[2]);
tempv[0]=atof(tu[0]);
tempv[1]=atof(tu[1]);
tempv[2]=atof(tu[2]);
model->verts[v].x=tempv[0];
model->verts[v].y=tempv[1];
model->verts[v].z=tempv[2];
model->verts[v].color=WHITE(255);
v++;
}
else if(buffer[0] == 'v' && buffer[1] == 'n')
{
sscanf(buffer,"%*s %s %s %s",tu[0],tu[1],tu[2]);
tempn[0]=atof(tu[0]);
tempn[1]=atof(tu[1]);
tempn[2]=atof(tu[2]);
model->verts[h].nx=tempn[0];
model->verts[h].ny=tempn[1];
model->verts[h].nz=tempn[2];
h++;
}
else if(buffer[0] == 'f' && buffer[1] == ' ')
{
if (r<t_faces){
if (normals && !textures){
sscanf(buffer,"%*s %d//%d %d//%d %d//%d",&tempf[0],&tn[0],&tempf[1],&tn[1],&tempf[2],&tn[2]);
i++;
}
else {
sscanf(buffer,"%*s %hd %hd %hd",&index[i*3],&index[i*3+1],&index[i*3+2]);
model->indices[i*3]=index[i*3]-1;
model->indices[i*3+1]=index[i*3+1]-1;
model->indices[i*3+2]=index[i*3+2]-1;
i++;
}
r++;
}
}
}
fclose(fp);
return model;
}
void EZ_model::Draw(){
sceGumDrawArray( GU_TRIANGLES, GU_COLOR_8888|GU_INDEX_16BIT|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
3*t_faces, indices, verts );
}