Load and Render *.obj file

Started by
8 comments, last by AMDFX 12 years, 11 months ago
Hey!

I try to load and render an obj file with openGL 2.0 but it looks very buggy! I hope somebody can help me!

Source:
void LoadObjects() { ifstream obj; string obj_str; float Vektor[9999][3]; float x, y, z; int counter = 0; int i = 0; obj.open("box.obj", ios::in); while (!obj.eof()) { getline(obj, obj_str); size_t pos = obj_str.find("v "); if (pos == 0) { sscanf(obj_str.c_str(), "v %f %f %f", &x, &y, &z); Vektor[0] = x; Vektor[1] = y; Vektor[2] = z; i++; counter++; }; }; obj.close(); int j = 0; while (j <= counter) { glBegin(GL_TRIANGLES); j++; glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]); j++; glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]); j++; glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]); glEnd(); }; }
Picture is attachet! Sry for bad english but I hope you understand the Problem and can help me :)
Advertisement
please repost the code somehow in a readable manner. Maybe with [ source ] tags.
Oh ok...

void LoadObjects()
{
ifstream obj;
string obj_str;
float Vektor[9999][3];
float x, y, z;
int i = 0, j = 0;
obj.open("box.obj", ios::in);
while (!obj.eof())
{
getline(obj, obj_str);
size_t pos = obj_str.find("v ");
if (pos == 0) {
sscanf(obj_str.c_str(), "v %f %f %f", &x, &y, &z);
Vektor[0] = x;
Vektor[1] = y;
Vektor[2] = z;
i++;
};
};
obj.close();

while (j <= i)
{
glBegin(GL_POLYGON);
j++;
glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]);
j++;
glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]);
j++;
glVertex3f(Vektor[j][0], Vektor[j][1], Vektor[j][2]);
glEnd();
};
}
Well, I suggest you to read up some documentation/tutorials on .obj loading, but maybe you miss some principles about graphics too. I guess you are new to openGL too, can you draw a simple cube for example? Not with glu functions.
Yes I am new to OpenGL but I can draw a cube (without glu) :P

I update the code, looks maybe a little bit better:
void LoadObjects()
{
ifstream obj;
string obj_str;
float x, y, z, x1, y1, z1, x2, y2, z2;
int i = 0;
bool a, b;
obj.open("box.obj", ios::in);
while (!obj.eof())
{
getline(obj, obj_str);
size_t pos = obj_str.find("v ");
if (pos == 0) {
a = false; b = false;
if (i==0) {
sscanf(obj_str.c_str(), "v %f %f %f", &x, &y, &z);
i++;
a = true;
};
if (i==1 && !a) {
sscanf(obj_str.c_str(), "v %f %f %f", &x1, &y1, &z1);
i++;
b = true;
};
if (i==2 && !b)
sscanf(obj_str.c_str(), "v %f %f %f", &x2, &y2, &z2);
if (i == 2 && !a && !b) {
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(x, y, z);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
i = 0;
};
};
};
obj.close();
}

but same problem...
You may find the Wikipedia article on the Wavefront .obj file format helpful. The summary at FileFormat.Info is good too. In particular, check out the section on face definitions.

The simplest approach is to look up vertex positions, normals, and texture coordinates using the face indices and submit them to OpenGL. Be aware that faces can have more than three vertices so you might have to "unroll" them into several triangles.
Ah I understand know... I must use the "f " informations to connect the faces... I will try and report later :)
Ah I understand now... I must use the "f " informations to connect the faces... I will try and report later :)
Few days ago i was trying to make a loader myself, but then i thought that it might be better just to search for source codes on the net.

I found this one http://www.kixor.net/dev/objloader/

It basically gives you data structs to store obj info.

The only drawback that buzzes me a bit is that i have to know myself if model faces have more that 3 vertexes.

So i think it might make your life a bit easier.
Thx for help dude but my own loader works now! :)

I understand know how to use the "f " stuff and yea... great :D I will include the texture support and the first test looks good!
So thx for help guys ;)

This topic is closed to new replies.

Advertisement