typedef struct _vertex {
float x,y,z;
} vertex;
typedef struct _vertex2 {
float nx,ny,nz;
} vertex2;
typedef struct _object {
int n;
vertex * vertices;
vertex2 * normales;
unsigned int * indices;
} object;
object head,leftHand,leftHandTwo,leftWrist,
rightWrist,rightHand,rightHandTwo,
chest,
leftLegDown,leftLegUp,leftFoot,
rightFoot,rightLegDown,rightLegUp,
cube;
void loadobj(char *filename, object * o)
{
int vc=0, fc=0, nc=0, tc=0, v=0, n=0, t=0, f=0, sc=0, s=0;
unsigned int v1,v2,v3,n1,n2,n3,t1,t2,t3,mid,mn=0,i,*faces;
float *vertices, *normals, *textures;
char line[64], mtllib[32];
vertex *tmp;
vertex2 *tmp2;
FILE *file;
file = fopen(filename, "r"); // Open file
setvbuf(file, NULL, _IOFBF, 1024*256); // Assume 256 KB buffer
while(!feof(file))
{
fgets(line, 64, file);
switch(line[0])
{
case 'v': switch(line[1])
{
case 'n': ++nc; break; // tu zliczamy
case 't': ++tc; break;
default: ++vc;
}
break;
case 'f': ++fc; break;
}
}
rewind(file);
o->n = fc;
vertices = (GLfloat*)calloc(3*vc, sizeof(GLfloat));
normals = (GLfloat*)calloc(3*nc, sizeof(GLfloat));
textures = (GLfloat*)calloc(2*tc, sizeof(GLfloat));
tmp = (vertex*)calloc(3*fc, sizeof(vertex));
tmp2 = (vertex2*)calloc(3*fc, sizeof(vertex2));
o->indices = (unsigned int*)calloc(3*fc, sizeof(unsigned int));
while(fgets(line, 64, file))
{
switch(line[0])
{
case 'v': switch(line[1])
{
case 'n': sscanf(line+2, "%f %f %f", &normals[n], &normals[n+1], &normals[n+2]); n+=3; break;
case 't': sscanf(line+2, "%f %f %*f", &textures[t], &textures[t+1]); t+=2; break;
default: sscanf(line+2, "%f %f %f", &vertices[v], &vertices[v+1], &vertices[v+2]); v+=3;
}
break;
case 'f':
sscanf(line+2, "%u//%u %u//%u %u//%u", &v1,&n1, &v2, &n2, &v3 , &n3);
//sscanf(line+2, "%u/%u/%u %u/%u/%u %u/%u/%u", &v1,&t1,&n1, &v2,&t2, &n2, &v3,&t3 , &n3);
tmp[s].x = vertices[3*v1-3];
tmp[s].y = vertices[3*v1-2];
tmp[s].z = vertices[3*v1-1]; // sprawdzam debuggerem - zgadza się
tmp2[s].nx = normals[3*n1-3];
tmp2[s].ny = normals[3*n1-2];
tmp2[s].nz = normals[3*n1-1];
std::cout<< v1 << " " <<3*v1-3 << " " << vertices[3*v1-3] << " "
<< vertices[3*v1-2]<< " "
<< vertices[3*v1-1] << endl;
s++;
tmp[s].x = vertices[3*v2-3];
tmp[s].y = vertices[3*v2-2];
tmp[s].z = vertices[3*v2-1];
tmp2[s].nx = normals[3*n2-3];
tmp2[s].ny = normals[3*n2-2];
tmp2[s].nz = normals[3*n2-1];
s++;
tmp[s].x = vertices[3*v3-3];
tmp[s].y = vertices[3*v3-2];
tmp[s].z = vertices[3*v3-1];
tmp2[s].nx = normals[3*n3-3];
tmp2[s].ny = normals[3*n3-2];
tmp2[s].nz = normals[3*n3-1];
s++;
break;
}
}
o->vertices = tmp; // przypisanie wskaźnika też działa
fclose(file);
}