PDA

View Full Version : Reading in a .obj file



MXA92
03-09-2012, 03:43 PM
I'm using Microsoft Visual Studio with OpenGL/C++.
I've been searching the web for days on tutorials, but there doesn't seem to be any that still work / that I can follow and get working.

I'm just trying to display a Cube for now, but can't seem to read in the correct values T_T.

I'm currently reading in Colours, Vertices, and Indices from my own created file, and drawing Polygons to make a cube, but I can't seem to expand this to the format of a .obj file.

Here's what I currently have:

int read_file (const string fileName)
{
ifstream inFile;
inFile.open(fileName.c_str());

inFile >> NUM_VERTS;
vertices = new point3D[NUM_VERTS];

for (int i=0; i < NUM_VERTS; i++)
{
inFile >> vertices[i].x;
inFile >> vertices[i].y;
inFile >> vertices[i].z;
}

inFile >> NUM_COL;
colours = new colour[NUM_COL];

for (int i=0; i < NUM_COL; i++)
{
inFile >> colours[i].r;
inFile >> colours[i].g;
inFile >> colours[i].b;
}

inFile >> NUM_POLY;
indices = new polygon[NUM_POLY];

for (int i=0; i < NUM_POLY; i++)
{
inFile >> indices[i].a;
inFile >> indices[i].b;
inFile >> indices[i].c;
}

inFile.close();
return 0;
}

void drawCube()
{
for (int i = 0; i < NUM_POLY; i++)
{
glColor3f(colours[i].r, colours[i].g, colours[i].b);
drawPolygon(indices[i].a, indices[i].b, indices[i].c);
}
}


void drawPolygon(int a, int b, int c)
{
glBegin(GL_TRIANGLES);
glVertex3fv(&amp;vertices[a].x); //Top
glVertex3fv(&amp;vertices[B].x); //Bottom left
glVertex3fv(&amp;vertices[c].x); //Bottomg right

glEnd();
}

Any help would be massively appreciated, as I'm really confused.

DanielSaucen
03-10-2012, 06:44 PM
Irrelevant to the above code:
I hate to recommend another file format, but it does offer a much wider range implementation, and is extremely simple to parse.

The Collada file format uses the XML schema, and once you understand Collada, or XML, you can use a parser such as TinyXML to write and read other data files, such as config files, and other user-defined variables including models.


Relevant to your code:
As for your code, tell it to print everything it's getting.
Then reference the outputted data with the file's data.
Make sure everything is getting read right.
(may use cout, I prefer printf("%s\n",Data); )

Debugging is a very nice thing to have.

Also, is your OpenGl version set below 3.2?
If not, glVertex3f may not work, as it has been removed from 3.2 forward.

V-man
03-11-2012, 05:29 AM
Also, is your OpenGl version set below 3.2?
If not, glVertex3f may not work, as it has been removed from 3.2 forward.

I have GL 3.3 and every old program works fine.
All the old GL functions are still there and will be there forever.