   My .obj model loader
  &bHello. This is my first post in this forum. I hope I find all the help I need here... This is the problem I have. I am trying to create a .obj model loader (from .obj files). Searching the internet, I found the specification, and I started programming based on that. here is what I have done so far:ObjLoaders.h#ifndef OBJLOADERS_H#define OBJLOADERS_H#include <iostream>#include <vector>#include <string>#include <OpenGL/OpenGL.h>#include <OpenGL/glu.h>using namespace std;typedef float vector3f[3];class OBJFileInfo{public:unsigned numVertices;unsigned numTriangles;unsigned numVertexNormals;unsigned numTextureVertex;OBJFileInfo(){numVertices = 0;numTriangles = 0;numVertexNormals = 0;numTextureVertex = 0;}};class Triangle{public:unsigned TriangleEdges[3];unsigned TriangleTextureCoords[3];unsigned TriangleNormals[3];Triangle(const Triangle& otherTriangle){for (int i=0; i<3; i++) {TriangleEdges = otherTriangle.TriangleEdges;TriangleTextureCoords = otherTriangle.TriangleTextureCoords;TriangleNormals = otherTriangle.TriangleNormals;}}Triangle(){reset();}void reset(){for (int i=0; i<3; i++) {TriangleEdges = 0;TriangleTextureCoords = 0;TriangleNormals = 0;}}};class OBJModel{OBJFileInfo fileinfo;public:vector3f* vertices;vector3f* vertexNormals;vector3f* textureVertices;//Triangle* triangles;vector<Triangle> triangles;OBJModel(OBJFileInfo info){fileinfo = info;vertices=new vector3f[info.numVertices];vertexNormals =new vector3f[info.numVertexNormals];textureVertices =new vector3f[info.numTextureVertex];//triangles =new Triangle[info.numTriangles];}void vertexAtIndex(unsigned index, vector3f resultBuffer){resultBuffer[0] = vertices[index][0];resultBuffer[1] = vertices[index][1];resultBuffer[1] = vertices[index][1];}void drawVertexAtIndex(unsigned index){vector3f vertexToDraw;vertexToDraw[0] = vertices[index][0];vertexToDraw[1] = vertices[index][1];vertexToDraw[1] = vertices[index][1];glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);}//TO BE COMPLETED!!!void drawFaceAtIndex(unsigned index){unsigned triangleVertexPositions[3];triangleVertexPositions[0] = triangles[index].TriangleEdges[0];triangleVertexPositions[1] = triangles[index].TriangleEdges[1];triangleVertexPositions[2] = triangles[index].TriangleEdges[2];//cout << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << '\n';vector3f v1, v2, v3;v1[0] = vertices[triangleVertexPositions[0]][0];v1[1] = vertices[triangleVertexPositions[0]][1];v1[2] = vertices[triangleVertexPositions[0]][2];v2[0] = vertices[triangleVertexPositions[1]][0];v2[1] = vertices[triangleVertexPositions[1]][1];v2[2] = vertices[triangleVertexPositions[1]][2];v3[0] = vertices[triangleVertexPositions[2]][0];v3[1] = vertices[triangleVertexPositions[2]][1];v3[2] = vertices[triangleVertexPositions[2]][2];cout << v1[0] << " " << v1[1] << " " << v1[2] << '\n';cout << v2[0] << " " << v2[1] << " " << v2[2] << '\n';cout << v3[0] << " " << v3[1] << " " << v3[2] << '\n';glBegin(GL_TRIANGLES);glVertex3f(v1[0], v1[1], v1[2]);glVertex3f(v2[0], v2[1], v2[2]);glVertex3f(v3[0], v3[1], v3[2]);glEnd();}~OBJModel(){delete [] vertices;delete [] vertexNormals;delete [] textureVertices;//delete [] triangles;}unsigned numVertices(){return fileinfo.numVertices;}unsigned numTriangles(){return fileinfo.numTriangles;}unsigned numVertexNormals(){return fileinfo.numVertexNormals;}unsigned numTextureVertex(){return fileinfo.numTextureVertex;}};void firstPass(char *filename, OBJFileInfo& info);void secondPass(char *filename, OBJModel *model);#endifObjLoaders.cpp#include "ObjLoaders.h"void firstPass(char *filename, OBJFileInfo& info){char buf[128];FILE *filePointer = fopen(filename, "r");while ( fscanf(filePointer, "%s", buf) != EOF){switch (buf[0]) {case 'v':switch (buf[1]) {case 't':fgets(buf, sizeof(buf), filePointer);info.numTextureVertex++;break;case '\0':fgets(buf, sizeof(buf), filePointer);info.numVertices++;break;case 'n':fgets(buf, sizeof(buf), filePointer);info.numVertexNormals++;break;default:break;}break;case '#':fgets(buf, sizeof(buf), filePointer);break;case 'f':info.numTriangles++;//NSLog(@"not implemented yet! %i", info.numTriangles);break;default:fgets(buf, sizeof(buf), filePointer);break;}}fclose(filePointer);info.numTriangles--;}void secondPass(char *filename, OBJModel *model){char buf[128];FILE *filePointer = fopen(filename, "r");unsigned currentVertexIndex = 0;unsigned currentvertexNormalIndex = 0;unsigned currentTextureVertexIndex = 0;unsigned currentTriangleIndex = 0;while ( fscanf(filePointer, "%s", buf) != EOF ) {switch (buf[0]) {case 'v':{switch (buf[1]) {case '\0':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, &b, &c);model->vertices[currentVertexIndex][0] = a;model->vertices[currentVertexIndex][1] = b;model->vertices[currentVertexIndex][2] = c;//cout << a << " " << b << " " << c << '\n';currentVertexIndex++;}break;case 'n':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, &b, &c);//cout << a << ' ' << b << ' ' << c << '\n';model->vertexNormals[currentvertexNormalIndex][0] = a;model->vertexNormals[currentvertexNormalIndex][1] = b;model->vertexNormals[currentvertexNormalIndex][2] = c;currentvertexNormalIndex++;}break;case 't':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, , &c);model->textureVertices[currentTextureVertexIndex][0] = a;model->textureVertices[currentTextureVertexIndex][1] = b;model->textureVertices[currentTextureVertexIndex][2] = c;currentTextureVertexIndex++;}break;default:break;}}break;case 'f':{Triangle currentTriangle;int v,t,n = 0;fscanf(filePointer, "%s", buf);if ( strstr(buf, "//") ) {sscanf(buf, "%d//%d", &v, &n);currentTriangle.TriangleEdges[0] = v;currentTriangle.TriangleNormals[0] = n;fscanf(filePointer, "%d//%d", &v, &n);currentTriangle.TriangleEdges[1] = v;currentTriangle.TriangleNormals[1] = n;fscanf(filePointer, "%d//%d", &v, &n);currentTriangle.TriangleEdges[2] = v;currentTriangle.TriangleNormals[2] = n;}else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3){currentTriangle.TriangleEdges[0] = v;currentTriangle.TriangleTextureCoords[0] = t;currentTriangle.TriangleNormals[0] = n;fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleTextureCoords[1]);fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleNormals[1]);}else if (sscanf(buf, "%d/%d", &v, &t) == 2){currentTriangle.TriangleTextureCoords[0] = t;currentTriangle.TriangleEdges[0] = v;fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1]);fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[2], &currentTriangle.TriangleTextureCoords[2]);}else {sscanf(buf, "%d", &v);currentTriangle.TriangleEdges[0] = v;fscanf(filePointer, "%d", currentTriangle.TriangleEdges[1]);fscanf(filePointer, "%d", currentTriangle.TriangleEdges[2]);}model->triangles.push_back(currentTriangle);currentTriangleIndex++;}break;default:fgets(buf, sizeof(buf), filePointer);break;}}}the code to draw:OBJFileInfo objFile;firstPass(FILENAME2, objFile);OBJModel model(objFile);secondPass(FILENAME2, &model);model.drawFaceAtIndex(1);Although something is drawn to the screen, I only see garbage! The file is parsed correctly, and all classes end up in having the correct variables stored in them. So it must be a logic error, and not a programming one (and logic errors are hard to spot!). For simplicity reasons I haven't implemented texture or material support, and I am trying to load a file that only contains vertex and vertex normals data.This is the file, in case you want to run the code yourself:#  #  Wavefront OBJ generated by GLM library#  #  GLM library#  Nate Robins#  ndr@pobox.com#  http://www.pobox.com/~ndr#  mtllib lightcycle.mtl# 91 verticesv -0.675745 -4.181760 0.828977v -0.675746 -0.260560 1.854730v 0.677798 -2.070860 -0.100226v 0.669888 -0.260560 1.854730v 0.669888 -4.181760 0.828977v 0.203011 3.536160 -0.847399v 0.203011 3.851470 -0.511977v 0.203011 3.207430 -0.525406v 0.203011 3.536160 -0.150241v -0.200718 3.536160 -0.150241v -0.200718 3.851470 -0.511977v -0.200718 3.207430 -0.538833v -0.200718 3.536160 -0.847399v 1.257510 -0.361758 0.887388v 0.610035 -0.187286 1.704330v 0.610035 -3.311140 0.887388v -1.257510 -0.361758 0.888085v -0.602038 -3.311140 0.888085v -0.602039 -0.187286 1.705020v 0.571984 -3.371230 -0.150160v 0.571984 -3.692720 -0.507515v 0.571984 -3.054550 -0.512684v 0.571984 -3.371230 -0.847315v -0.569690 -3.371230 -0.150160v -0.569690 -3.054550 -0.512684v -0.569690 -3.692720 -0.507515v -0.569690 -3.371230 -0.847315v -0.675746 2.560710 1.165440v -0.675504 1.051360 -0.101380v 0.669887 2.561520 1.165440v 0.677798 1.051100 -0.101111v 0.301519 2.480440 -0.547424v 0.301519 3.536160 0.565185v 0.301519 3.536160 -1.562820v 0.301519 4.538160 -0.493712v 0.002783 3.536160 -1.987540v 0.002783 5.000000 -0.491226v 0.002783 3.536160 0.989902v -0.299225 3.536160 -1.562820v -0.299225 4.538160 -0.493712v -0.299225 3.536160 0.565185v -0.299225 2.480440 -0.533995v 0.002783 1.978340 -0.547779v -0.545389 1.646580 -1.553560v -0.545389 3.002350 -0.509387v -0.544671 3.530940 -1.150170v -0.545389 3.530570 -1.559590v 0.548401 3.530670 -1.150440v 0.547683 3.532990 -1.559590v 0.547683 3.002350 -0.508849v -1.074710 -3.371230 0.597294v -1.074710 -4.529150 -0.143543v 0.011553 -5.000000 -0.143341v 0.011553 -3.371230 0.989982v 1.077010 -3.371230 0.597294v 1.077010 -4.529150 -0.143543v 1.077010 -2.298130 -0.853935v 0.011553 -1.807680 -0.867211v -1.074710 -2.298130 -0.853935v 1.077010 -3.840490 -1.664290v 0.011553 -3.840490 -2.056980v -1.074710 -3.840490 -1.664290v -0.545389 3.529330 0.160689v 0.547683 3.529330 0.160689v 0.547683 1.646580 -1.553560v -0.389670 3.132820 1.122540v 0.392881 3.134730 1.125790v 0.393451 2.943360 0.805744v -0.389097 2.941480 0.802474v 0.392881 -0.187721 2.056980v 0.393451 -0.187888 1.694550v -0.389669 -0.189631 2.053730v -0.389096 -0.189771 1.691110v 0.392881 -3.121660 1.598650v 0.393451 -2.951540 1.251250v -0.389669 -3.123570 1.595410v -0.389096 -2.953420 1.247980v 0.392881 -4.460940 0.882128v 0.393451 -4.109510 0.815935v -0.389669 -4.462850 0.878881v -0.389096 -4.109460 0.815965v -0.545389 -2.722760 -1.553560v -0.542968 -2.722760 1.299390v -0.545389 1.273530 0.348329v 0.547683 1.273530 0.348329v 0.547683 -2.722760 1.299390v 0.549168 -2.723830 -1.553400v -0.545389 1.905600 1.092650v -0.545389 3.529330 1.092650v 0.547683 3.529330 1.092650v 0.547683 1.905600 1.092650# 161 normalsvn -1.000000 0.000043 -0.000164vn -0.695529 0.228198 0.681296vn 0.999989 -0.000698 0.004692vn 0.999989 -0.000071 0.004740vn 0.999985 -0.001397 0.005339vn 1.000000 0.000000 0.000000vn -0.727607 0.485071 0.485071vn -1.000000 -0.000000 -0.000000vn -0.762493 0.457496 0.457496vn -0.816497 0.408248 0.408248vn 0.755312 -0.165813 0.634042vn -0.650458 0.433360 0.623782vn -0.751311 -0.166972 0.638477vn -0.689812 0.328116 0.645368vn -0.870411 0.348168 0.348085vn -1.000000 -0.000013 -0.000146vn -1.000000 -0.000000 -0.000191vn -0.000037 -0.008133 0.999967vn -0.000143 0.237274 0.971443vn -0.000071 0.237306 0.971435vn 0.999990 0.000592 0.004441vn 0.999988 0.001182 0.004837vn 0.990735 0.135771 0.003072vn 0.990950 0.134093 0.006023vn 0.991671 -0.000341 -0.128797vn 0.990653 -0.000193 -0.136404vn 0.989251 -0.053024 0.136276vn 0.990060 0.038876 0.135166vn 0.988948 -0.142679 -0.040308vn 0.988451 -0.142925 0.050369vn 0.718758 0.502329 -0.480679vn 0.739721 0.481013 -0.470574vn 0.697080 0.523144 -0.490306vn 0.728037 0.492626 0.476741vn 0.706889 0.503081 0.497209vn 0.748475 0.481689 0.455807vn -0.722445 0.499236 -0.478369vn -0.743007 0.488333 -0.457681vn -0.701182 0.509654 -0.498594vn -0.716825 0.501381 0.484540vn -0.694993 0.522259 0.494196vn -0.737935 0.479999 0.474397vn -0.989280 -0.052461 0.136279vn -0.989474 0.041017 0.138778vn -0.988451 -0.142924 0.050369vn -0.988948 -0.142678 -0.040308vn -0.991921 -0.001821 -0.126842vn -0.990653 -0.001044 -0.136403vn -0.990692 0.135907 0.007691vn -0.990815 0.134357 0.015289vn -0.734282 -0.467560 -0.492156vn -0.709626 -0.491743 -0.504598vn -0.757948 -0.442747 -0.479052vn 0.740924 -0.460565 -0.488786vn 0.722193 -0.469473 -0.507969vn 0.759063 -0.451290 -0.469212vn -1.000000 0.000107 0.000214vn -1.000000 0.000176 -0.000228vn -1.000000 0.000267 0.000534vn -0.999998 0.000006 0.001754vn -0.000984 0.999997 0.002385vn -0.002214 0.999981 0.005674vn 0.000247 1.000000 -0.000905vn 0.000017 0.771683 0.636008vn 0.000348 0.771406 0.636343vn -0.000313 0.771959 0.635672vn -0.256207 -0.536497 0.804070vn -0.227626 -0.524786 0.820235vn 0.002409 -0.555019 0.831834vn 0.002659 -0.571153 0.820840vn 0.260864 -0.535801 0.803036vn 0.231834 -0.524251 0.819399vn 0.270297 0.756003 0.596153vn -0.082418 0.775548 0.625886vn 0.107381 0.774012 0.623999vn 0.214051 0.785420 0.580773vn -0.281550 0.752851 0.594933vn -0.335186 0.757543 0.560159vn 0.261686 0.468109 -0.844034vn 0.002771 0.505124 -0.863042vn 0.002409 0.485013 -0.874503vn 0.219633 0.453754 -0.863637vn -0.257025 0.468725 -0.845124vn -0.215624 0.454170 -0.864428vn -0.249256 -0.857477 -0.450116vn -0.147496 -0.900986 -0.408006vn -0.105259 -0.870728 -0.480367vn 0.074871 -0.872314 -0.483180vn 0.282281 -0.847936 -0.448689vn 0.373382 -0.845068 -0.382683vn -0.613902 -0.614155 -0.495921vn -0.660614 -0.603626 -0.446346vn -0.564274 -0.621767 -0.543140vn -0.826467 0.357975 -0.434519vn -0.863654 0.374751 -0.337140vn -0.780066 0.337209 -0.527055vn 0.613898 -0.614158 -0.495923vn 0.660610 -0.603629 -0.446349vn 0.564270 -0.621769 -0.543142vn 0.774472 0.417297 -0.475454vn 0.831994 0.298993 -0.467322vn 0.703497 0.528353 -0.475326vn 0.620156 -0.494198 0.609241vn 0.791443 -0.284296 0.541104vn 0.400472 -0.665533 0.629832vn -0.782366 -0.365083 0.504597vn -0.840669 -0.393338 0.372237vn -0.707072 -0.328899 0.625998vn -0.828812 0.471259 0.301639vn -0.872628 0.354766 0.335650vn -0.771197 0.579907 0.262607vn 0.828809 0.471263 0.301642vn 0.872626 0.354769 0.335653vn 0.771194 0.579911 0.262609vn 0.000152 0.785917 -0.618331vn 0.000304 0.786038 -0.618178vn 0.000000 0.785797 -0.618485vn 1.000000 -0.000267 -0.000535vn 1.000000 -0.000105 0.000137vn 1.000000 -0.000048 -0.000251vn 0.999998 -0.000006 -0.001755vn 0.000060 0.858295 -0.513157vn 0.000037 0.858270 -0.513198vn 0.000082 0.858319 -0.513116vn 0.999999 0.000437 0.001549vn 0.999999 0.000446 0.001573vn 0.999999 0.000428 0.001526vn -0.999999 -0.000439 -0.001557vn -0.999999 -0.000430 -0.001533vn -0.999999 -0.000449 -0.001580vn 0.999999 -0.000245 0.001547vn 0.999999 -0.000244 0.001521vn 0.999999 -0.000246 0.001573vn -0.999999 0.000246 -0.001555vn -0.999999 0.000247 -0.001580vn -0.999999 0.000245 -0.001529vn 0.999997 -0.000888 0.002087vn 0.999995 -0.001081 0.002875vn 0.999999 -0.000695 0.001300vn -0.999997 0.000899 -0.002123vn -0.999999 0.000699 -0.001307vn -0.999995 0.001098 -0.002939vn 0.002240 -0.180194 -0.983629vn -0.000049 -0.175279 -0.984519vn 0.004529 -0.185104 -0.982709vn -1.000000 -0.000202 0.000424vn -1.000000 -0.000404 0.000849vn -1.000000 -0.000135 0.000283vn 1.000000 0.000116 0.000147vn 1.000000 0.000124 0.000520vn 1.000000 0.000232 0.000294vn -0.003720 -0.154342 0.988010vn -0.003714 -0.154344 0.988010vn -0.003726 -0.154340 0.988011vn 0.000000 -0.253076 0.967446vn -0.429191 0.320573 0.844410vn 0.000000 1.000000 0.000000vn -0.004658 0.269870 0.962886vn -0.002503 -0.471736 0.881736vn -0.002507 -0.471734 0.881737vn -0.002499 -0.471738 0.881735# 126 faces (triangles)g  defaultusemtl Hullf 1//1 1//1 2//2f 3//3 4//4 5//5usemtl Chassisf 6//6 7//6 8//6f 9//6 8//6 7//6f 10//7 11//8 12//9f 12//9 11//8 13//10usemtl Windowf 14//11 15//11 16//11f 17//12 18//13 19//14usemtl Chassisf 20//6 21//6 22//6f 21//6 23//6 22//6f 24//7 25//8 26//9f 26//9 25//8 27//10usemtl Hullf 1//15 2//2 28//16f 28//16 29//17 1//15f 4//18 30//19 28//20f 28//20 2//2 4//18f 31//21 30//22 4//4f 4//4 3//3 31//21usemtl Windowf 32//23 8//24 9//25f 9//25 33//26 32//23f 32//23 34//27 6//28f 6//28 8//24 32//23f 34//27 35//29 7//30f 7//30 6//28 34//27f 35//29 33//26 9//25f 9//25 7//30 35//29usemtl Hullf 36//31 37//32 35//31f 35//31 34//33 36//31f 37//34 38//35 33//34f 33//34 35//36 37//34f 39//37 40//38 37//37f 37//37 36//39 39//37f 40//40 41//41 38//40f 38//40 37//42 40//40usemtl Windowf 39//43 13//44 11//45f 11//45 40//46 39//43f 40//46 11//45 10//47f 10//47 41//48 40//46f 42//49 41//48 10//47f 10//47 12//50 42//49f 42//49 12//50 13//44f 13//44 39//43 42//49usemtl Hullf 42//51 39//52 36//51f 36//51 43//53 42//51f 43//54 36//55 34//54f 34//54 32//56 43//54usemtl Chassisf 44//57 45//58 46//59f 46//59 47//60 44//57f 48//61 49//62 47//61f 47//61 46//63 48//61f 48//64 46//65 45//64f 45//64 50//66 48//64usemtl Hullf 51//67 52//68 53//69f 53//69 54//70 51//67f 55//71 54//70 53//69f 53//69 56//72 55//71f 57//73 58//74 54//75f 54//75 55//76 57//73f 51//77 54//75 58//74f 58//74 59//78 51//77f 60//79 61//80 58//81f 58//81 57//82 60//79f 62//83 59//84 58//81f 58//81 61//80 62//83f 52//85 62//86 61//87f 61//87 53//88 52//85f 60//89 56//90 53//88f 53//88 61//87 60//89usemtl Windowf 24//91 51//92 59//91f 59//91 25//93 24//91f 24//94 26//95 52//94f 52//94 51//96 24//94f 57//97 55//98 20//97f 20//97 22//99 57//97f 55//100 56//101 21//100f 21//100 20//102 55//100f 60//103 57//104 22//103f 22//103 23//105 60//103f 27//106 25//107 59//106f 59//106 62//108 27//106f 26//109 27//110 62//109f 62//109 52//111 26//109f 60//112 23//113 21//112f 21//112 56//114 60//112usemtl Chassisf 50//115 45//116 63//115f 63//115 64//117 50//115f 48//118 50//119 65//120f 65//120 49//121 48//118usemtl Hullf 66//122 67//123 68//122f 68//122 69//124 66//122f 70//125 71//126 68//125f 68//125 67//127 70//125f 72//128 66//129 69//128f 69//128 73//130 72//128usemtl Windowf 74//131 75//132 71//131f 71//131 70//133 74//131f 76//134 72//135 73//134f 73//134 77//136 76//134usemtl Hullf 78//137 79//138 75//137f 75//137 74//139 78//137f 80//140 76//141 77//140f 77//140 81//142 80//140f 80//143 81//144 79//143f 79//143 78//145 80//143usemtl Chassisf 82//146 83//147 84//148f 84//148 44//57 82//146f 85//149 86//150 87//151f 87//151 65//120 85//149usemtl Windowf 76//152 74//153 70//152f 70//152 72//154 76//152usemtl Hullf 4//18 2//2 1//155f 1//155 5//156 4//18usemtl Chassisf 88//8 45//58 44//57f 44//57 84//148 88//8f 64//157 63//157 89//157f 89//157 90//157 64//157f 88//8 89//8 63//8f 63//8 45//58 88//8f 50//119 64//6 90//6f 90//6 91//6 50//119f 85//149 65//120 50//119f 50//119 91//6 85//149usemtl Hullf 72//158 70//158 67//158f 67//158 66//158 72//158f 80//159 78//160 74//159f 74//159 76//161 80//159The result should be a lightcycle (remember the movie "Tron"?) or at least something that looks like it. But instead I see garbage... Can anyone help me?   
  Step through the program with the debugger and make sure the values are read in correctly. This will at least ensure that your loading code is correct. Then you can move on to the drawing code.Also, your code can be made more simple by using std::vector and std::ifstream. It will also make it less error-prone (it will reduce the chance for memory leaks, buffer overruns, etc.).   ;  I haven't review your code deeply yet. But perhaps you should know that OBJ face indices start from 1, so the vertex at position 0 is referenced with 1 and so on. May your code starts the index counting from 0.   ;  I will look into it but I doubt this is the issue here. The result is sooo wrong that it can't be the fault of one misplaced vertex only...   ;  Anyone?   ;Quote:Original post by SoulstormgrAnyone?Did you follow my suggestion?Also:void vertexAtIndex(unsigned index, vector3f resultBuffer){resultBuffer[0] = vertices[index][0];resultBuffer[1] = vertices[index][1];resultBuffer[1] = vertices[index][1];}void drawVertexAtIndex(unsigned index){vector3f vertexToDraw;vertexToDraw[0] = vertices[index][0];vertexToDraw[1] = vertices[index][1];vertexToDraw[1] = vertices[index][1];glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);}Shouldn't the third index be 2?   ;Quote:Did you follow my suggestion? To be honest, no. I didn't. But I am pretty sure (after extensive tests) that all values are read in correctly, and there is no memory leak so the problem lies elsewhere. I corrected the last mistake you pointed out, though. To no effect.A guy in  another forum has managed to have the correct output, but when I use his/her code, I am not getting the same result at all.Here is my latest code.ObjLoaders.h#ifndef OBJLOADERS_H#define OBJLOADERS_H#include <iostream>#include <vector>#include <string>#include <OpenGL/OpenGL.h>#include <OpenGL/glu.h>using namespace std;typedef float vector3f[3];class OBJFileInfo{public:unsigned numVertices;unsigned numTriangles;unsigned numVertexNormals;unsigned numTextureVertex;OBJFileInfo(){numVertices = 0;numTriangles = 0;numVertexNormals = 0;numTextureVertex = 0;}};class Triangle{public:unsigned TriangleEdges[3];unsigned TriangleTextureCoords[3];unsigned TriangleNormals[3];Triangle(const Triangle& otherTriangle){for (int i=0; i<3; i++) {TriangleEdges = otherTriangle.TriangleEdges;TriangleTextureCoords = otherTriangle.TriangleTextureCoords;TriangleNormals = otherTriangle.TriangleNormals;}}Triangle(){reset();}void reset(){for (int i=0; i<3; i++) {TriangleEdges = 0;TriangleTextureCoords = 0;TriangleNormals = 0;}}};class OBJModel{OBJFileInfo fileinfo;public:vector3f* vertices;vector3f* vertexNormals;vector3f* textureVertices;//Triangle* triangles;vector<Triangle> triangles;OBJModel(OBJFileInfo info){setInfo(info);}void setInfo(OBJFileInfo info){fileinfo = info;vertices=new vector3f[info.numVertices];vertexNormals =new vector3f[info.numVertexNormals];textureVertices =new vector3f[info.numTextureVertex];//triangles =new Triangle[info.numTriangles];}void vertexAtIndex(unsigned index, vector3f resultBuffer){resultBuffer[0] = vertices[index][0];resultBuffer[1] = vertices[index][1];resultBuffer[2] = vertices[index][2];}void drawVertexAtIndex(unsigned index){vector3f vertexToDraw;vertexToDraw[0] = vertices[index][0];vertexToDraw[1] = vertices[index][1];vertexToDraw[2] = vertices[index][2];glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);}//TO BE COMPLETED!!!void drawFaceAtIndex(unsigned index){unsigned triangleVertexPositions[3];triangleVertexPositions[0] = triangles[index].TriangleEdges[0];triangleVertexPositions[1] = triangles[index].TriangleEdges[1];triangleVertexPositions[2] = triangles[index].TriangleEdges[2];unsigned triangleNormalPositions[3];triangleNormalPositions[0] = triangles[index].TriangleNormals[0];triangleNormalPositions[1] = triangles[index].TriangleNormals[1];triangleNormalPositions[2] = triangles[index].TriangleNormals[2];//cout << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << '\n';vector3f v1, v2, v3;vector3f n1, n2, n3;for(unsigned i=0; i<3; ++i){v1 = vertices[triangleVertexPositions[0]];v2 = vertices[triangleVertexPositions[1]];v3 = vertices[triangleVertexPositions[2]];n1 = vertexNormals[triangleNormalPositions[0]];n2 = vertexNormals[triangleNormalPositions[1]];n3 = vertexNormals[triangleNormalPositions[2]];}#define normalize(n) {float mag = sqrtf(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]); for(unsigned i=0; i<3;++i)n/=mag;}normalize(n1);normalize(n2);normalize(n3);glBegin(GL_TRIANGLES);glNormal3fv(n1);glVertex3fv(v1);glNormal3fv(n2);glVertex3fv(v2);glNormal3fv(n3);glVertex3fv(v3);glEnd();}~OBJModel(){delete [] vertices;delete [] vertexNormals;delete [] textureVertices;//delete [] triangles;}unsigned numVertices(){return fileinfo.numVertices;}unsigned numTriangles(){return fileinfo.numTriangles;}unsigned numVertexNormals(){return fileinfo.numVertexNormals;}unsigned numTextureVertex(){return fileinfo.numTextureVertex;}};//void firstPass(char *filename, OBJFileInfo& info);//void secondPass(char *filename, OBJModel *model);static inline void firstPass(char *filename, OBJFileInfo& info){char buf[128];FILE *filePointer = fopen(filename, "r");while ( fscanf(filePointer, "%s", buf) != EOF){switch (buf[0]) {case 'v':switch (buf[1]) {case 't':fgets(buf, sizeof(buf), filePointer);info.numTextureVertex++;break;case '\0':fgets(buf, sizeof(buf), filePointer);info.numVertices++;break;case 'n':fgets(buf, sizeof(buf), filePointer);info.numVertexNormals++;break;default:break;}break;case '#':fgets(buf, sizeof(buf), filePointer);break;case 'f':info.numTriangles++;//NSLog(@"not implemented yet! %i", info.numTriangles);break;default:fgets(buf, sizeof(buf), filePointer);break;}}fclose(filePointer);info.numTriangles;//--;}static inline void secondPass(char *filename, OBJModel *model){char buf[128];FILE *filePointer = fopen(filename, "r");unsigned currentVertexIndex = 0;unsigned currentvertexNormalIndex = 0;unsigned currentTextureVertexIndex = 0;unsigned currentTriangleIndex = 0;while ( fscanf(filePointer, "%s", buf) != EOF ) {switch (buf[0]) {case 'v':{switch (buf[1]) {case '\0':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, &b, &c);model->vertices[currentVertexIndex][0] = a;model->vertices[currentVertexIndex][1] = b;model->vertices[currentVertexIndex][2] = c;//cout << a << " " << b << " " << c << '\n';currentVertexIndex++;}break;case 'n':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, &b, &c);cout << a << ' ' << b << ' ' << c << '\n';model->vertexNormals[currentvertexNormalIndex][0] = a;model->vertexNormals[currentvertexNormalIndex][1] = b;model->vertexNormals[currentvertexNormalIndex][2] = c;currentvertexNormalIndex++;}break;case 't':{float a,b,c;fscanf(filePointer, "%f %f %f", &a, &b, &c);model->textureVertices[currentTextureVertexIndex][0] = a;model->textureVertices[currentTextureVertexIndex][1] = b;model->textureVertices[currentTextureVertexIndex][2] = c;currentTextureVertexIndex++;}break;default:break;}}break;case 'f':{Triangle currentTriangle;int v,t,n = 0;fscanf(filePointer, "%s", buf);if ( strstr(buf, "//") ) {sscanf(buf, "%d//%d", &v, &n);printf("%d %d\n", v, n);--v; --n;currentTriangle.TriangleEdges[0] = v;currentTriangle.TriangleNormals[0] = n;fscanf(filePointer, "%d//%d", &v, &n);printf("%d %d\n", v, n);--v; --n;currentTriangle.TriangleEdges[1] = v;currentTriangle.TriangleNormals[1] = n;fscanf(filePointer, "%d//%d", &v, &n);printf("%d %d\n", v, n);--v; --n;currentTriangle.TriangleEdges[2] = v;currentTriangle.TriangleNormals[2] = n;}else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3){currentTriangle.TriangleEdges[0] = v;currentTriangle.TriangleTextureCoords[0] = t;currentTriangle.TriangleNormals[0] = n;fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleTextureCoords[1]);fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleNormals[1]);}else if (sscanf(buf, "%d/%d", &v, &t) == 2){currentTriangle.TriangleTextureCoords[0] = t;currentTriangle.TriangleEdges[0] = v;fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1]);fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[2], &currentTriangle.TriangleTextureCoords[2]);}else {sscanf(buf, "%d", &v);currentTriangle.TriangleEdges[0] = v;fscanf(filePointer, "%d", currentTriangle.TriangleEdges[1]);fscanf(filePointer, "%d", currentTriangle.TriangleEdges[2]);}//cout << currentTriangleIndex << ": ";//cout <<  "f " << currentTriangle.TriangleEdges[0] << "/" << currentTriangle.TriangleTextureCoords[0] << "/" << currentTriangle.TriangleNormals[0];//cout << currentTriangle.TriangleEdges[1] << "/" << currentTriangle.TriangleTextureCoords[1] << "/" << currentTriangle.TriangleNormals[0];//cout << currentTriangle.TriangleEdges[2] << "/" << currentTriangle.TriangleTextureCoords[2] << "/" << currentTriangle.TriangleNormals[2] << '\n';//cout <<  "f " << currentTriangle.TriangleEdges[0] << "//" << currentTriangle.TriangleNormals[0] << " ";//cout << currentTriangle.TriangleEdges[1] << "//" << currentTriangle.TriangleNormals[1] << " ";//cout << currentTriangle.TriangleEdges[2] << "//" << currentTriangle.TriangleNormals[2] << "\n";//model->triangles[currentTriangleIndex++] = currentTriangle;model->triangles.push_back(currentTriangle);currentTriangleIndex++;}break;default:fgets(buf, sizeof(buf), filePointer);break;}}}#endifFor your facilitation, I include a .obj file that represents a solid cone using only a few vertices. It is simpler to debug.cone.obj:g objectusemtl Texturev -21.2132 0 21.2132v -21.2132 0 21.2132v -15.9099 0 15.9099v -30 0 2.62268e-06v -30 0 2.62268e-06v 3.57746e-07 0 30v 3.57746e-07 0 30v -18.5616 7.5 18.5616v -22.5 0 1.96701e-06v 2.6831e-07 0 22.5v -10.6066 0 10.6066v -26.25 7.5 2.29485e-06v 3.13028e-07 7.5 26.25v -15.9099 15 15.9099v 1.78873e-07 0 15v -15 0 1.31134e-06v -5.3033 0 5.3033v -22.5 15 1.96701e-06v -7.5 0 6.55671e-07v 8.94366e-08 0 7.5v 2.6831e-07 15 22.5v -13.2582 22.5 13.2583v 15.9099 0 15.9099v 21.2132 0 21.2132v 21.2132 0 21.2132v 10.6066 0 10.6066v -21.2132 0 -21.2132v -10.6066 0 -10.6066v 0 0 -0v -5.3033 0 -5.3033v 5.3033 0 5.3033v -15.9099 0 -15.9099v -21.2132 0 -21.2132v -18.75 22.5 1.63918e-06v 2.23592e-07 22.5 18.75v 18.5616 7.5 18.5616v -18.5616 7.5 -18.5616v 7.5 0 -0v -3.27835e-07 0 -7.5v -10.6066 30 10.6066v 5.3033 0 -5.3033v 15.9099 15 15.9099v -15 30 1.31134e-06v -15.9099 15 -15.9099v -6.55671e-07 0 -15v 15 0 -0v 1.78873e-07 30 15v 10.6066 0 -10.6066v -7.95495 37.5 7.95495v 13.2582 22.5 13.2583v -9.83506e-07 0 -22.5v -13.2583 22.5 -13.2583v 22.5 0 -0v -11.25 37.5 9.83506e-07v 1.34155e-07 37.5 11.25v 10.6066 30 10.6066v -1.31134e-06 0 -30v -1.31134e-06 0 -30v -10.6066 30 -10.6066v 30 0 -0v 30 0 -0v 15.9099 0 -15.9099v -1.14742e-06 7.5 -26.25v 26.25 7.5 -0v -5.3033 45 5.3033v 7.95495 37.5 7.95495v -9.83506e-07 15 -22.5v -7.5 45 6.55671e-07v -7.95495 37.5 -7.95495v 8.94366e-08 45 7.5v 22.5 15 -0v -8.19589e-07 22.5 -18.75v 18.75 22.5 -0v 21.2132 0 -21.2132v 21.2132 0 -21.2132v 18.5616 7.5 -18.5616v 5.3033 45 5.3033v -6.55671e-07 30 -15v -5.3033 45 -5.3033v 15 30 -0v 15.9099 15 -15.9099v -2.65165 52.5 2.65165v -3.75 52.5 3.27835e-07v -4.91753e-07 37.5 -11.25v 4.47183e-08 52.5 3.75v 11.25 37.5 -0v 13.2583 22.5 -13.2583v 10.6066 30 -10.6066v -2.65165 52.5 -2.65165v -3.27835e-07 45 -7.5v 2.65165 52.5 2.65165v 7.5 45 -0v 7.95495 37.5 -7.95495v 5.3033 45 -5.3033v -1.63918e-07 52.5 -3.75v 3.75 52.5 -0v 2.65165 52.5 -2.65165v 0 60 -0vn -0.632455 0.447214 0.632456vn 0 -1 -0vn 0 -1 -0vn -0.894427 0.447214 7.81933e-08vn 0 -1 -0vn 0 -1 -0vn 1.06659e-08 0.447214 0.894427vn -0.632455 0.447214 0.632456vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn -0.894427 0.447214 7.81933e-08vn 1.06659e-08 0.447214 0.894427vn -0.632455 0.447214 0.632456vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn -0.894427 0.447214 7.81933e-08vn 0 -1 -0vn 0 -1 -0vn 1.06659e-08 0.447214 0.894427vn -0.632455 0.447214 0.632456vn 0 -1 -0vn 0 -1 -0vn 0.632455 0.447214 0.632456vn 0 -1 -0vn -0.632456 0.447214 -0.632456vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn 0 -1 -0vn -0.894427 0.447214 7.81933e-08vn 1.06659e-08 0.447214 0.894427vn 0.632455 0.447214 0.632456vn -0.632456 0.447214 -0.632456vn 0 -1 -0vn 0 -1 -0vn -0.632455 0.447214 0.632456vn 0 -1 -0vn 0.632455 0.447214 0.632456vn -0.894427 0.447214 7.81933e-08vn -0.632456 0.447214 -0.632456vn 0 -1 -0vn 0 -1 -0vn 1.06659e-08 0.447214 0.894427vn 0 -1 -0vn -0.632455 0.447214 0.632456vn 0.632455 0.447214 0.632456vn 0 -1 -0vn -0.632456 0.447214 -0.632456vn 0 -1 -0vn -0.894427 0.447214 7.81933e-08vn 1.06659e-08 0.447214 0.894427vn 0.632455 0.447214 0.632456vn -3.90967e-08 0.447214 -0.894427vn 0 -1 -0vn -0.632456 0.447214 -0.632456vn 0 -1 -0vn 0.894427 0.447214 -0vn 0 -1 -0vn -3.90967e-08 0.447214 -0.894427vn 0.894427 0.447214 -0vn -0.632455 0.447214 0.632456vn 0.632455 0.447214 0.632456vn -3.90967e-08 0.447214 -0.894427vn -0.894427 0.447214 7.81933e-08vn -0.632456 0.447214 -0.632456vn 1.06659e-08 0.447214 0.894427vn 0.894427 0.447214 -0vn -3.90967e-08 0.447214 -0.894427vn 0.894427 0.447214 -0vn 0 -1 -0vn 0.632456 0.447214 -0.632456vn 0.632456 0.447214 -0.632456vn 0.632455 0.447214 0.632456vn -3.90967e-08 0.447214 -0.894427vn -0.632456 0.447214 -0.632456vn 0.894427 0.447214 -0vn 0.632456 0.447214 -0.632456vn -0.632455 0.447214 0.632456vn -0.894427 0.447214 7.81933e-08vn -3.90967e-08 0.447214 -0.894427vn 1.06659e-08 0.447214 0.894427vn 0.894427 0.447214 -0vn 0.632456 0.447214 -0.632456vn 0.632456 0.447214 -0.632456vn -0.632456 0.447214 -0.632456vn -3.90967e-08 0.447214 -0.894427vn 0.632455 0.447214 0.632456vn 0.894427 0.447214 -0vn 0.632456 0.447214 -0.632456vn 0.632456 0.447214 -0.632456vn -3.90967e-08 0.447214 -0.894427vn 0.894427 0.447214 -0vn 0.632456 0.447214 -0.632456vn 0 1 -0f 98//98 96//96 97//97 f 98//98 97//97 95//95 f 98//98 95//95 89//89 f 98//98 89//89 83//83 f 98//98 83//83 82//82 f 98//98 82//82 85//85 f 98//98 85//85 91//91 f 98//98 91//91 96//96 f 96//96 92//92 94//94 f 96//96 94//94 97//97 f 97//97 94//94 90//90 f 97//97 90//90 95//95 f 95//95 90//90 79//79 f 95//95 79//79 89//89 f 89//89 79//79 68//68 f 89//89 68//68 83//83 f 83//83 68//68 65//65 f 83//83 65//65 82//82 f 82//82 65//65 70//70 f 82//82 70//70 85//85 f 85//85 70//70 77//77 f 85//85 77//77 91//91 f 91//91 77//77 92//92 f 91//91 92//92 96//96 f 92//92 86//86 93//93 f 92//92 93//93 94//94 f 94//94 93//93 84//84 f 94//94 84//84 90//90 f 90//90 84//84 69//69 f 90//90 69//69 79//79 f 79//79 69//69 54//54 f 79//79 54//54 68//68 f 68//68 54//54 49//49 f 68//68 49//49 65//65 f 65//65 49//49 55//55 f 65//65 55//55 70//70 f 70//70 55//55 66//66 f 70//70 66//66 77//77 f 77//77 66//66 86//86 f 77//77 86//86 92//92 f 86//86 80//80 88//88 f 86//86 88//88 93//93 f 93//93 88//88 78//78 f 93//93 78//78 84//84 f 84//84 78//78 59//59 f 84//84 59//59 69//69 f 69//69 59//59 43//43 f 69//69 43//43 54//54 f 54//54 43//43 40//40 f 54//54 40//40 49//49 f 49//49 40//40 47//47 f 49//49 47//47 55//55 f 55//55 47//47 56//56 f 55//55 56//56 66//66 f 66//66 56//56 80//80 f 66//66 80//80 86//86 f 80//80 73//73 87//87 f 80//80 87//87 88//88 f 88//88 87//87 72//72 f 88//88 72//72 78//78 f 78//78 72//72 52//52 f 78//78 52//52 59//59 f 59//59 52//52 34//34 f 59//59 34//34 43//43 f 43//43 34//34 22//22 f 43//43 22//22 40//40 f 40//40 22//22 35//35 f 40//40 35//35 47//47 f 47//47 35//35 50//50 f 47//47 50//50 56//56 f 56//56 50//50 73//73 f 56//56 73//73 80//80 f 73//73 71//71 81//81 f 73//73 81//81 87//87 f 87//87 81//81 67//67 f 87//87 67//67 72//72 f 72//72 67//67 44//44 f 72//72 44//44 52//52 f 52//52 44//44 18//18 f 52//52 18//18 34//34 f 34//34 18//18 14//14 f 34//34 14//14 22//22 f 22//22 14//14 21//21 f 22//22 21//21 35//35 f 35//35 21//21 42//42 f 35//35 42//42 50//50 f 50//50 42//42 71//71 f 50//50 71//71 73//73 f 71//71 64//64 76//76 f 71//71 76//76 81//81 f 81//81 76//76 63//63 f 81//81 63//63 67//67 f 67//67 63//63 37//37 f 67//67 37//37 44//44 f 44//44 37//37 12//12 f 44//44 12//12 18//18 f 18//18 12//12 8//8 f 18//18 8//8 14//14 f 14//14 8//8 13//13 f 14//14 13//13 21//21 f 21//21 13//13 36//36 f 21//21 36//36 42//42 f 42//42 36//36 64//64 f 42//42 64//64 71//71 f 64//64 61//61 75//75 f 64//64 75//75 76//76 f 76//76 75//75 57//57 f 76//76 57//57 63//63 f 63//63 57//57 27//27 f 63//63 27//27 37//37 f 37//37 27//27 4//4 f 37//37 4//4 12//12 f 12//12 4//4 1//1 f 12//12 1//1 8//8 f 8//8 1//1 7//7 f 8//8 7//7 13//13 f 13//13 7//7 25//25 f 13//13 25//25 36//36 f 36//36 25//25 61//61 f 36//36 61//61 64//64 f 29//29 31//31 20//20 f 29//29 20//20 17//17 f 29//29 17//17 19//19 f 29//29 19//19 30//30 f 29//29 30//30 39//39 f 29//29 39//39 41//41 f 29//29 41//41 38//38 f 29//29 38//38 31//31 f 31//31 26//26 15//15 f 31//31 15//15 20//20 f 20//20 15//15 11//11 f 20//20 11//11 17//17 f 17//17 11//11 16//16 f 17//17 16//16 19//19 f 19//19 16//16 28//28 f 19//19 28//28 30//30 f 30//30 28//28 45//45 f 30//30 45//45 39//39 f 39//39 45//45 48//48 f 39//39 48//48 41//41 f 41//41 48//48 46//46 f 41//41 46//46 38//38 f 38//38 46//46 26//26 f 38//38 26//26 31//31 f 26//26 23//23 10//10 f 26//26 10//10 15//15 f 15//15 10//10 3//3 f 15//15 3//3 11//11 f 11//11 3//3 9//9 f 11//11 9//9 16//16 f 16//16 9//9 32//32 f 16//16 32//32 28//28 f 28//28 32//32 51//51 f 28//28 51//51 45//45 f 45//45 51//51 62//62 f 45//45 62//62 48//48 f 48//48 62//62 53//53 f 48//48 53//53 46//46 f 46//46 53//53 23//23 f 46//46 23//23 26//26 f 23//23 24//24 6//6 f 23//23 6//6 10//10 f 10//10 6//6 2//2 f 10//10 2//2 3//3 f 3//3 2//2 5//5 f 3//3 5//5 9//9 f 9//9 5//5 33//33 f 9//9 33//33 32//32 f 32//32 33//33 58//58 f 32//32 58//58 51//51 f 51//51 58//58 74//74 f 51//51 74//74 62//62 f 62//62 74//74 60//60 f 62//62 60//60 53//53 f 53//53 60//60 24//24 f 53//53 24//24 23//23 Any ideas?   ;  maybe try even something simpler than a cone, a cube for instance, draw its vertices as billboard quads, draw the edges as lines as well as the polygons? this way you can visually see where the vert's should be.This is what I did when loading in '.3ds' meshes for the first time once the cube worked so did everything else :D   ;  Thank you all for your answers, you have been very helpful. I found the problem.The problem was the one that superoptimo said. I fixed that, and I changed some other variables that concerned the way I have my OpenGL set up in my project. Sorry I could give you my entire project file, but I thought you would be using Xcode anyway...Thanks a lot!   