importing model problem

Member
Posts: 94
Joined: 2008.08
Post: #1
Hi there,

Im attempting to import a 3d .obj model into my game. Ive followed the instructions listed here http://www.phoboslab.org/log/2008/07/yuc...-3d-models but seem to be having a number of problems.

I think the model has been loaded into memory as when I output the number of vertices I get 12336, so that seems about right. But all I get is a blank screen- when I click on buttons etc I still get my debug printed, so Im sure the main game loop is running still. When I remove this line, the rest of my custom models etc work fine:

Code:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*numVertices, vertexData, GL_STATIC_DRAW);

Thanks for any help you can give.
Quote this message in a reply
Moderator
Posts: 3,554
Joined: 2003.06
Post: #2
Can't really say much about what might be happening without more code. One thing I could note is that on the page you linked to, the author converts the obj models beforehand, using an unknown custom script, and you haven't mentioned whether you've done that as well or not. Simply loading an unparsed obj file won't work.

Side-note: Individual triangle strips as the author seems to suggest would actually be a bad idea because of the increase in GL calls (have to draw each strip). Better to simply send triangles. Beyond that, from what I've seen on iPhone so far, triangle strip *ordering* of vertices seems to gain no noticeable improvement in speed. Also, VBOs don't seem to help performance much either, if at all, although I haven't tested that extensively. The performance listed on that page sounds about like what I get for the same number of triangles, and I'm using strip-ordered models with VBOs. So I guess what I'm suggesting is that you could probably just as well skip the VBO approach and simply use glDrawElements or glDrawArrays normally. ... But you have to make sure you've loaded in your model properly first.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #3
Well I also used php to convert the ascii into binary and then saved it to a new file. Im doing this code to load the file in:

Code:
    NSString *fileString;
    fileString = [[NSBundle mainBundle] pathForResource:@"new" ofType:@"obj"];

    printf("file: %s \n\n", fileString);
    
    FILE * fh = fopen([fileString UTF8String], "rb");
    if (fh == NULL)
    {
        printf("Can't open file.");
    }
    
    // Determine the number of vertices in this file

    fread(&numVertices, sizeof(short), 1, fh);
    
    // Load the vertices into memory
    Vertex * vertexData = malloc(sizeof(Vertex)*numVertices);
    fread(vertexData, sizeof(Vertex), numVertices, fh);
    
    fclose(fh);

So if I don t use the VBO code, what should I be doing to load it in?
Thanks!
Quote this message in a reply
Moderator
Posts: 3,554
Joined: 2003.06
Post: #4
wonza Wrote:So if I don t use the VBO code, what should I be doing to load it in?

Same as if you don't use VBOs: glVertexPointer(3, GL_FLOAT, 0, vertexData); tells the GL where to load your geometry data from every time you call glDrawArrays or glDrawElements.

I hope I'm not totally full of crap here, but the idea with VBOs is that once it loads it to video memory once, it doesn't have to continue to fetch it every time. The data has to get to video memory at some point, but since the iPhone is using system RAM for video memory, I don't know if it makes any difference to use VBOs or not. It seems very similar to the way the GMA's work on desktop Macs.

Here is some pseudo-code, using glDrawElements, with or without VBOs for comparison:
Code:
// HEADER

// data arrangement, num floats per coord: 2 texcoord, 3 normal, 3 vert
#define STRIDE    sizeof(GLfloat) * 8

GLuint     myNumIndices;
GLuint      myIndicesVBO;
GLuint      myGeometryVBO;
GLfloat     *myGeometryData;
GLushort     *myIndices;

// INIT

// setup VBOs
glGenBuffers(1, &myIndicesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, myIndicesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, myNumIndices * sizeof(GLushort), myIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &myGeometryVBO);
glBindBuffer(GL_ARRAY_BUFFER, myGeometryVBO);
glBufferData(GL_VERTEX_ARRAY_BUFFER_BINDING, geometryDataSize, myGeometryData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);


// DRAW

// no VBOs
glTexCoordPointer(2, GL_FLOAT, STRIDE, myGeometryData);
glVertexPointer(3, GL_FLOAT, STRIDE, &myGeometryData[5]);
glNormalPointer(GL_FLOAT, STRIDE, &myGeometryData[2]);
glDrawElements(GL_TRIANGLES, myNumIndices, GL_UNSIGNED_SHORT, myIndices);

// using VBOs
glBindBuffer(GL_ARRAY_BUFFER, myGeometryVBO); // added bind
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, myIndicesVBO); // added bind
glTexCoordPointer(2, GL_FLOAT, STRIDE, myGeometryData); // same
glVertexPointer(3, GL_FLOAT, STRIDE, &myGeometryData[5]); // same
glNormalPointer(GL_FLOAT, STRIDE, &myGeometryData[2]); // same
glDrawElements(GL_TRIANGLES, myNumIndices, GL_UNSIGNED_SHORT, nil); // using nil instead of myIndices
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #5
Im wondering if Im not parsing the data correctly. I purchased a model from a exchange3d.com and format is in:

# Max2Obj Version 4.0 Mar 10th, 2001
#
mtllib ./SS-15.mtl
g
# object SS-15 to come ...
#
v -149.355331 -28.648027 8.021841
v -97.125496 -32.692585 22.875225
v -5.666856 -35.851944 31.510717
.......etc
vt 0.488907 0.395400 0.776172
vt 0.496528 0.743260 0.454311
vt 0.503345 0.604908 0.787663
vt 0.163673 0.597230 0.589655
vt 0.222625 0.397290 0.589487
vt 0.255536 0.456202 0.597109
........etc
vn 2.703199 0.000000 0.000000
vn 2.703196 0.000000 0.000000
vn -2.188640 0.000000 -3.292108
vn -2.188640 0.000000 -3.292108
vn -2.882682 -4.660741 1.548545
........etc
s 1
f 84/2418/84 75/2419/75 76/2420/76 83/2417/83
f 72/58/72 63/49/63 64/50/64 71/57/71
f 60/46/60 45/31/45 46/32/46 59/45/59
f 77/2421/77 78/2422/78 85/2423/85 86/2424/86
f 66/52/66 73/59/73 74/60/74 65/51/65
f 44/30/44 61/47/61 62/48/62 43/29/43
s 2
.......etc
Quote this message in a reply
Moderator
Posts: 3,554
Joined: 2003.06
Post: #6
It *looks* okay on a glance, but that doesn't mean it is to spec. I can say for certain that I've tested lots and lots of obj files from different sources off turbosquid, and at least half of them don't follow the obj spec. You can get all kinds of funky stuff. I made a loader/converter to handle all kinds of special cases that I discovered, but it was a major PITA to develop, so the best advice I can offer for that is to use a third-party program like Blender, Cheetah3D, Wings3D, etc. to load your model and re-export it as an obj again so that it comes out in a more predictable and valid format. Plus it will allow you to verify that the model you acquired is actually not garbage (I've encountered completely invalid obj files which were clearly exported from a buggy program and not even checked by the artist before posting online).

[Edit] Actually, I just noticed that the VT's have three coords a piece. That does look suspicious. Should only be two. (unless it's for a 3D texture, which is unusual)
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #7
Im think I need to understand how the following code actually parses the data into the vertexData:

Code:
    fread(&numVertices, sizeof(short), 1, fh);
// Load the vertices into memory
Vertex * vertexData = malloc(sizeof(Vertex)*numVertices);
fread(vertexData, sizeof(Vertex), numVertices, fh);

Like, how does in 'know' to ignore the v's and vt's at the start of the lines etc.

This is the code I used to convert into binary from ascii in php:
Code:
function asc2bin($in) {
  //syntax - asc2bin("text to convert");
  $out = '';
  for ($i = 0, $len = strlen($in); $i < $len; $i++) {
       $out .= sprintf("%08b",ord($in{$i}));
  }
  return $out;
}
Quote this message in a reply
Moderator
Posts: 3,554
Joined: 2003.06
Post: #8
wonza Wrote:Like, how does in 'know' to ignore the v's and vt's at the start of the lines etc.

It doesn't. Which is why I've been suspecting from the beginning that you aren't loading in a properly parsed model. Simply converting it to binary will not parse it and format it into something usable by your program.

You have to read all those lines of v's and vt's and vn's an f's and convert it all into something that makes sense to your program. Google for obj loader or opengl obj loader or something like that. There are tons of them out there.

For iPhone especially, I would suggest writing a converter with one of those obj loaders out there (or your own), to convert what you load into your own binary file format which will load nicely into something like what I showed above:

GLuint myNumIndices;
GLfloat *myGeometryData;
GLushort *myIndices;

It'll probably be a bit larger file size because you won't be sharing vertices where possible like obj does, but it'll load much faster than parsing on the spot.


BTW, if this seems confusing at first, don't worry. Learning how to load obj files is pretty easy in the grander scheme of things, but that doesn't make it a no-brainer. It's worth going through the hassle of learning this though, because it'll open up a huge amount of new things you can do with OpenGL.


Here's a good link for the obj spec:

http://www.martinreddy.net/gfx/3d/OBJ.spec

It shouldn't take more than an afternoon to write your own basic obj parser. But again, be forewarned that not all obj files are valid, so what you think works for you on your first attempt won't work for many obj files. That's why I suggested earlier to be sure to re-export whatever obj files you get from a known program so you get a consistent file to work with every time.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #9
Thanks, I think thats what I needed to hear to get me started. Shame it wasnt as easy as I thought it was going to be, but hey what is? Smile
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #10
I ended up using Blender to import the .obj file and export it as .raw file... which proved alot easier to parse. I didnt have to worry about poly's, normals or colors when importing the model then; which suited my needs just fine. thanks Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Importing in Objective-C Nick 2 3,285 Jul 17, 2004 04:13 PM
Last Post: Nick