Jump to content
  • Advertisement
Sign in to follow this  
AMDFX

OpenGL Load and Render *.obj file

This topic is 2703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Advertisement
please repost the code somehow in a readable manner. Maybe with [ source ] tags.

Share this post


Link to post
Share on other sites
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();
};
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ah I understand know... I must use the "f " informations to connect the faces... I will try and report later :)

Share this post


Link to post
Share on other sites
Ah I understand now... I must use the "f " informations to connect the faces... I will try and report later :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!