iDevGames Forums
Wings3D .obj file -> OpenGL problem - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Wings3D .obj file -> OpenGL problem (/thread-4626.html)

Pages: 1 2


Wings3D .obj file -> OpenGL problem - WhatMeWorry - Jan 8, 2006 11:47 PM

I went into wings3D and created a simple cube. I then
exported it to a .obj file. This is what was created:


Code:
# Exported from Wings 3D 0.98.32a
mtllib Cube.mtl
o /Users/kyle/Desktop/Square.ai
#8 vertices, 6 faces
v 0.25416750 -0.18833250 0.10000000
v 0.25416750 0.18833250 0.10000000
v -0.25416750 0.18833250 0.10000000
v -0.25416750 -0.18833250 0.10000000
v 0.25416750 -0.18833250 -0.10000000
v 0.25416750 0.18833250 -0.10000000
v -0.25416750 0.18833250 -0.10000000
v -0.25416750 -0.18833250 -0.10000000
vn 0.57735027 -0.57735027 0.57735027
vn 0.57735027 0.57735027 0.57735027
vn -0.57735027 0.57735027 0.57735027
vn -0.57735027 -0.57735027 0.57735027
vn 0.57735027 -0.57735027 -0.57735027
vn 0.57735027 0.57735027 -0.57735027
vn -0.57735027 0.57735027 -0.57735027
vn -0.57735027 -0.57735027 -0.57735027
g /Users/kyle/Desktop/Square.ai_default
usemtl default
f 1//1 5//5 6//6 2//2
f 2//2 3//3 4//4 1//1
f 2//2 6//6 7//7 3//3
f 3//3 7//7 8//8 4//4
f 4//4 8//8 5//5 1//1
f 5//5 8//8 7//7 6//6


The .obj is then read into my OpenGL file for a wire frame object.

Not sure what I'm doing, but I looked at a StanfordBunny example for
a starting place with the following code:

Code:
int listID = glGenLists(1);   // start the display list
   glNewList(listID, GL_COMPILE);
  
   glLineWidth( 3.0 );
  
   glBegin(GL_LINE_LOOP);
  
   while ( inFile >> token )
   {
         inFile >> x >> y >> z;  // read in vertex
         glVertex2d( x, y, z );  
      }        
   }      
   glEnd ();  
   glEndList();


At least I get something, but It's not a cube; more like two
upright parallel squares with a connecting X at their bottom face.

I also try and create a solid object from the same data. With
the following:

Code:
int listID = glGenLists(1);   // start the display list
    glNewList(listID, GL_COMPILE);

    int maxNum = faceIndices.size();
    
    //glBegin (GL_TRIANGLES);
    //glBegin (GL_LINE_LOOP);
    glBegin (GL_QUADS);
    for( int i = 0; i < maxNum; i=i+6 )
    {
       int vx = faceIndices[i];  
       int vnx = faceIndices[i+1];
      
       int vy = faceIndices[i+2];  
       int vny = faceIndices[i+3];
      
       int vz = faceIndices[i+4];  
       int vnz = faceIndices[i+5];
              
       glVertex3d(vertices[vx], vertices[vy], vertices[vz]);
            
       glNormal3d( vertexNormals[vnx], vertexNormals[vny], vertexNormals[vnz]);                                        
    }
    glEnd ();
    glEndList();


Instead of solidcube, I get sort of two bent solid triangles kinda arranged
about a cube.

Anybody see my mistake(s)?

thanks.


Wings3D .obj file -&gt; OpenGL problem - akb825 - Jan 9, 2006 12:05 AM

I don't think you understand the concept of obj files very well. You have the vertex list, then you have the faces. The faces are what you use to create the actual shape. What you will need to do is read in all the vertices, vertex texture coordinates, and vertex normals. Once you do that, you will need to read in the indices that the faces give you (those indices start at 1 for some odd reason), and resolve those into the correct vertices, vertex texture coordinates, and normals. This is no small task, mainly because they are stupid and don't provide the item counts for obj files. (Mad) I just completed the obj loader for a program I'm making that will assist me in putting models into games. (the final format will be a binary format of my making, though, which will be much faster in loading as well as smaller) The function to load the obj files is 900 lines long, and I have 2 specially created linked lists, one to find the counts that I need for the indices for the vertex arrays, and one to assist in resolving which index points to which vertex. (and also removes copies of vertices) This also includes material file importing, though.


Wings3D .obj file -&gt; OpenGL problem - Leisure Suit Lurie - Jan 9, 2006 06:02 AM

Some apps do put those counts in a comment block.


Wings3D .obj file -&gt; OpenGL problem - Cochrane - Jan 9, 2006 09:28 AM

The first code should have glVertex3d instead of glVertex2d.


Wings3D .obj file -&gt; OpenGL problem - longjumper - Jan 9, 2006 11:11 AM

Now, for some actual help:

1. Go here: http://www.sfu.ca/~kberg/Code/sourcecode.html
2. Download the source for the wavefront obj loader.

You won't be able to write your own, and this one works.


Wings3D .obj file -&gt; OpenGL problem - Fenris - Jan 9, 2006 11:14 AM

Also, don't forget that .obj starts indexing at one, not zero.


Wings3D .obj file -&gt; OpenGL problem - ThemsAllTook - Jan 9, 2006 12:06 PM

LongJumper Wrote:You won't be able to write your own, and this one works.

A rudimentary .obj loader is trivial to write. I did it in an evening. If you want a full-featured loader, it would take a little bit more effort, but really not that much.


Wings3D .obj file -&gt; OpenGL problem - WhatMeWorry - Jan 9, 2006 04:19 PM

Quote:A rudimentary .obj loader is trivial to write. I did it in an evening.

Maybe for you, but it would take me a month and even
then not work right Smile

I'll try the loader first. Also study the code; should learn alot.

thanks all!


Wings3D .obj file -&gt; OpenGL problem - longjumper - Jan 9, 2006 04:55 PM

I was trying to relate to his abilities at this point in time. That "you" was not a universal quantifier Wink

Good luck, that .obj loader was one of the big stepping stones for learning OpenGL for me.


Wings3D .obj file -&gt; OpenGL problem - WhatMeWorry - Jan 10, 2006 12:48 AM

Just for closure:

I Downloaded that .obj loader (just 2 files - ModelType.h and ModelType.cpp),
moved it into my Xcode project, put in a LoadObj() and Draw() method
calls into my code, recompiled, and bam! Perfect openGL 3D drawings.
It doesn't get better that this.

BTW, I did look at the loader and code and yes it has me stymied. I'll study and undersand it eventually, but right now I'm just having too much 3D fun.

If any 3D newbie is reading this, I can't recommend highly enough Wings3D
and the above link to the (.obj loader) ModelType class.

Thanks again all!


Wings3D .obj file -&gt; OpenGL problem - eShad0w - Jun 5, 2006 10:47 AM

WhatMeWorry Wrote:I Downloaded that .obj loader (just 2 files - ModelType.h and ModelType.cpp),
moved it into my Xcode project, put in a LoadObj() and Draw() method
calls into my code, recompiled, and bam! Perfect openGL 3D drawings.
It doesn't get better that this.

Thanks again all!

Huh Can you please be more specific with that, what and where exactly should I put in my project in order for it to load (call) my obj file?


Wings3D .obj file -&gt; OpenGL problem - Ingemar - Jun 9, 2006 11:53 PM

LongJumper Wrote:Now, for some actual help:

1. Go here: http://www.sfu.ca/~kberg/Code/sourcecode.html
2. Download the source for the wavefront obj loader.

You won't be able to write your own, and this one works.
Writing an OBJ loader, at least a basic one that loads straight mesh data is very straight forward. (OBJs hold some more data, like spline representations, but I never touched them.) I have looked at several OBJ samples, but still find it easier to write my own, since I otherwise must rewrite much of someone else's code to adjust the output format and add postprocessing. I have actually done the whole thing twice - with post processing. (I wasn't happy with the first module so I wrote a new one rather than fixing the old one.)

With post processing, I mean breaking down polygons to triangles (so I can use glDrawElements and similar), and calculating texture coordinates and normal vectors, in case they are not provided in the OBJ. That's a lot more work, but still quite managable.


Wings3D .obj file -&gt; OpenGL problem - flash - Jun 24, 2006 02:59 AM

I'm trying to do a similar thing - read a .obj file and display it. I'm using GLUT and C++ atm.

I've been using models with no texture data to start off with and I got to the stage (in an evening) where I could read in all the vertices, normals and face information fine and various models all displayed happily using GL_LINE as the polymode for back and front faces (wireframe).

Now I'm trying to display these models with GL_FILL and basic lighting and I've run into a problem: the vertices for the faces don't seem to be consistently specified in order in the .obj - i.e. some are clockwise, some are anticlockwise. So my display routine draws some faces the right way round, some the wrong way round and the normals are reversed for some and not for others.

I've loaded the same models into Blender and it displays them fine, so presumably there is a way of fixing this.

Is there an obvious way of telling which faces are specified anti-clockwise and which are clockwise so I can reverse them? Is there another kind of solution? Or does it sound like something else is wrong?

Any help would be appreciated...

G


Wings3D .obj file -&gt; OpenGL problem - unknown - Jun 24, 2006 03:28 AM

flash: are you remembering that the vertices you read in will probably be stored in a 0 index array and the polygons in an obj file reference vertices with a starting index of 1?


Wings3D .obj file -&gt; OpenGL problem - OneSadCookie - Jun 24, 2006 04:03 AM

flash: I've never had a problem with Wings' export, but maybe Blender doesn't do such a good job.

You can tell which way the polygon is wound by looking at the dot product of the vertex normals with the face normal, I think.