//---------------------------------------------------------------------------X_IO *X_ReadMeshWavefront(const char *filename){//--------------------------------------------------------------------------- int iXyz, iUv, iNrm, iTri; int iLine,nLines; X_IO *io = new X_IO; printf("Reading Wavefront from file: %s ...",filename); ifstream file(filename); //Zaehlen der Vertexe und Dreiecke iXyz = iUv = iNrm = iTri = 0; iLine = nLines = 0; if (!file.fail()){ string line, tmp; io->t.nXyz = 0; io->t.nUv = 0; io->t.nNrm = 0; io->t.nTri = 0; while (!file.eof()){ getline(file,line); tmp = line.substr(0,3); if (!strcmp(line.substr(0,2).c_str(),"o ")) {;} if (!strcmp(line.substr(0,2).c_str(),"v ")) {io->t.nXyz++; if (!iXyz) iXyz = iLine; } if (!strcmp(line.substr(0,3).c_str(),"vt ")) {io->t.nUv++; if (!iUv) iUv = iLine; } if (!strcmp(line.substr(0,3).c_str(),"vn ")) {io->t.nNrm++; if (!iNrm) iNrm = iLine; } if (!strcmp(line.substr(0,2).c_str(),"g ")) {;} if (!strcmp(line.substr(0,2).c_str(),"f ")) {io->t.nTri++; if (!iTri) iTri = iLine; } iLine++; } nLines = iLine; io->t.bXyz = true; io->t.bUv = true; io->t.bSmoothNorm = true; io->t.bTri = true; io->t.bSmoothTri = true; io->t.bNorm = true; io->t.bMid = false; io->t.bIndexXyz = false; io->t.bIndexTri = false; io->t.bScale = true; //Keine Skalierung io->t.xyz = new float*[io->t.nXyz]; io->t.uv = new float*[io->t.nUv]; io->t.norm = new float*[io->t.nTri]; io->t.smoothNorm = new float*[io->t.nNrm]; io->t.tri = new int *[io->t.nTri]; io->t.smoothTri = new int **[io->t.nTri]; io->t.mid = new float*[io->t.nTri]; for (int i=0;i<io->t.nXyz;i++) io->t.xyz = new float[3]; for (int i=0;i<io->t.nUv;i++) io->t.uv = new float[3]; for (int i=0;i<io->t.nTri;i++) io->t.norm = new float[3]; for (int i=0;i<io->t.nNrm;i++) io->t.smoothNorm = new float[3]; for (int i=0;i<io->t.nTri;i++) io->t.tri = new int[3]; for (int i=0;i<io->t.nTri;i++) io->t.smoothTri = new int*[3]; for (int i=0;i<io->t.nTri;i++) io->t.mid = new float[3]; for (int i=0;i<io->t.nTri;i++) for (int j=0;j<3;j++) io->t.smoothTri[j] = new int[3]; } file.close(); FILE *fid; fid = fopen(filename,"r"); iLine = 0; if (fid){ while (iLine < nLines){ if (iLine >= nLines) break; char temp_string[256]; if (iLine == iXyz){ for (int i=0;i<io->t.nXyz;i++){ fscanf(fid,"v %f %f %f\n",&io->t.xyz[0],&io->t.xyz[1],&io->t.xyz[2]); iLine++; } } if (iLine == iUv){ for (int i=0;i<io->t.nUv;i++){ fscanf(fid,"vt %f %f\n",&io->t.uv[0],&io->t.uv[1]); iLine++; } } if (iLine == iNrm){ for (int i=0;i<io->t.nNrm;i++){ fscanf(fid,"vn %f %f %f\n",&io->t.smoothNorm[0],&io->t.smoothNorm[1],&io->t.smoothNorm[2]); iLine++; } } if (iLine == iTri){ for (int i=0;i<io->t.nTri;i++){ fscanf(fid,"f %d/%d/%d %d/%d/%d %d/%d/%d\n",&io->t.smoothTri[0][0],&io->t.smoothTri[0][1],&io->t.smoothTri[0][2], &io->t.smoothTri[1][0],&io->t.smoothTri[1][1],&io->t.smoothTri[1][2], &io->t.smoothTri[2][0],&io->t.smoothTri[2][1],&io->t.smoothTri[2][2]); iLine++; } } iLine++; fgets(temp_string,256,fid); } for (int i=0;i<io->t.nTri;i++) for (int j=0;j<3;j++) for (int k=0;k<3;k++) io->t.smoothTri[j][k]--; CheckMesh(io); fclose(fid); printf(" done.\n"); return io; } printf(" error.\n"); return NULL;}
The rendering function looks like this. I use display lists to render the meshes:
//------------------------------------------------------------------------void X_PrepareSmoothMeshList(X_IO *mesh, unsigned int listID){//------------------------------------------------------------------------ float norm1[3],norm2[3],norm3[3]; float xyz1[3],xyz2[3],xyz3[3]; float uv1[2],uv2[2],uv3[2]; int tri1[3], tri2[3], tri3[3]; glNewList(listID,GL_COMPILE); glPushMatrix(); for (int i=0;i<mesh->t.nTri;i++){ tri1[0] = mesh->t.smoothTri[0][0]; tri1[1] = mesh->t.smoothTri[1][0]; tri1[2] = mesh->t.smoothTri[2][0]; tri2[0] = mesh->t.smoothTri[0][1]; tri2[1] = mesh->t.smoothTri[1][1]; tri2[2] = mesh->t.smoothTri[2][1]; tri3[0] = mesh->t.smoothTri[0][2]; tri3[1] = mesh->t.smoothTri[1][2]; tri3[2] = mesh->t.smoothTri[2][2]; norm1[0] = mesh->t.smoothNorm[tri3[0]][0]; norm1[1] = mesh->t.smoothNorm[tri3[0]][1]; norm1[2] = mesh->t.smoothNorm[tri3[0]][2]; norm2[0] = mesh->t.smoothNorm[tri3[1]][0]; norm2[1] = mesh->t.smoothNorm[tri3[1]][1]; norm2[2] = mesh->t.smoothNorm[tri3[1]][2]; norm3[0] = mesh->t.smoothNorm[tri3[2]][0]; norm3[1] = mesh->t.smoothNorm[tri3[2]][1]; norm3[2] = mesh->t.smoothNorm[tri3[2]][2]; xyz1[0] = mesh->t.xyz[tri1[0]][0]; xyz1[1] = mesh->t.xyz[tri1[0]][1]; xyz1[2] = mesh->t.xyz[tri1[0]][2]; xyz2[0] = mesh->t.xyz[tri1[1]][0]; xyz2[1] = mesh->t.xyz[tri1[1]][1]; xyz2[2] = mesh->t.xyz[tri1[1]][2]; xyz3[0] = mesh->t.xyz[tri1[2]][0]; xyz3[1] = mesh->t.xyz[tri1[2]][1]; xyz3[2] = mesh->t.xyz[tri1[2]][2]; uv1[0] = mesh->t.uv[tri2[0]][0]; uv1[1] = mesh->t.uv[tri2[0]][1]; uv2[0] = mesh->t.uv[tri2[1]][0]; uv2[1] = mesh->t.uv[tri2[1]][1]; uv3[0] = mesh->t.uv[tri2[2]][0]; uv3[1] = mesh->t.uv[tri2[2]][1]; glBegin(GL_TRIANGLES); glNormal3fv(norm1); glTexCoord2fv(uv1); glVertex3fv(xyz1); glNormal3fv(norm2); glTexCoord2fv(uv2); glVertex3fv(xyz2); glNormal3fv(norm3); glTexCoord2fv(uv3); glVertex3fv(xyz3); glEnd(); } glPopMatrix(); glEndList(); }