This sucks: OBJ Parser on Android, and trying to display a cube via OpenGL ES 2.0 with shaders

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,663
I've been tackling this since day 1, and my progress sucks. I'm stuck on parsing a simple OBJ file, with a cube mesh.

I could *ragequit* all day, all thanks to that "f". Even if I got them parsed, I don't even know what to do with them for use in OpenGL ES 2.0. Can't use the good ol' ByteBuffer (that's for ES 1.0/1.1).

If lightly possible, can someone give hints on the next step? I want to read up on the theories, and then I show my code that's follow the theories, and then hopefully, I get to see where I did wrong, and finally correct it with pin-point accuracy.

Thanks in advance.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Without actually knowing what you're doing or what you don't know, it's a bit hard to comment on that.

I'm also not too sure what you mean by reading theories, then showing your code to do something...

Buuuut I know the problem with the faces. They're a bit annoying to get into a data format that OGL likes.

The easiest way to parse Obj is to:

Parse all "v" and put them into an array (v).
Parse all "vt" and put them into an array (vt).
Parse all "nv" and put them into an array (vn)

Create three arrays to hold your final vertex information. One for vertices, one for texture coords and one for normals (fv, fvt, fvn).

"f" usually has the format: <vert> <vert> <vert>
vert usually has the format: <number> / <number> / <number>

Code:
For every face:
    For every vert in face:
        Read the 3 numbers (a,b,c)
        push v[a-1] onto fv
        push vt[b-1] onto fvt
        push vn[c-1] onto fvn

You can potentially reduce the amount of data by using indexed buffers, but the design of Obj (= seperating position, texture UV and normal from each other) makes that pretty tedious.

Biggest problem is that Obj isn't a very strict format. Differences like having 3 or 4 verts in a face (triangle or quad), or not having a vt index.
So at the start I'd suggest limiting yourself to a strict rule set.

I'm not familiar with OGL:ES, nor do I know which language you're using. But I'm sure you can use some sort of buffer object since all modern OGL versions seem to operate with them.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,663
Do you happen to know what I should do with two of each kind (v and fv, vt and fvt, vn and fvn)?

Why OBJ couldn't just use the first three types available? (Just by using v, vt, and vn only when trying to load the model and blitting it to the display)?

I have also seen an implementation that use the vertex and normal indices to point towards the target vertices and normals, then calculate the cross product of two vectors (each vector is the difference of two vertices), and finally normalize them. The result is then placed in the normal array, where the normal indices are pointing at. Is this redundant if there is already normals (vn) included in the file?

Otherwise, I don't see the point and usage of the vertex, normal, and texture indices. What are they for?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
v, vt and vn are not needed after you populate the other arrays. They're just temporarily needed so you know which pos/tex/normal belongs to which index.
fv, fvt and fvn contain the position, texture and normal data just how OpenGL wants it. So they are ready to be uploaded to OGL through buffer objects.

Taking the cross product of the vectors that comprise the triangle gives you the normal. So if you have vn entries in your Obj file then that's redundant.

At the end of the day, the Obj format is completely arbitrary. Whoever invented it simply thought it useful to use indices.
Maybe because the Obj files would be smaller this way, or maybe because some graphics modeling programs organize their data like this.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,663
Thinking about this when I'm in a calm, collected state of mind makes me feel dumb.

In short, the most important part of the OBJ is the f. The rest are just for temporary loading the exact values for the f and its elements.

Thanks for helping me break down the OBJ into understandable concepts.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,663
I have come across some problems I'm not able to explain very well without you guys trying it out.

I'm unable to get a cube to load correctly. I was able to make it rotate nicely on all axes.

The only way to make this program work is to install it on an Android device with the OS version >= 2.2 (Gingerbread or above). It runs OpenGL 2.0.

This is what it looks like right now:

JnKJbXn.png


This is the expected outcome:

lC0H6Rr.png


Here's the Pastebin of my GLSurfaceView.Renderer portion:
http://pastebin.com/KE5nETFg

Here's the Pastebin of the Cube class, with the OBJ loader included:
http://pastebin.com/JuukRGku

Anyone had ideas? Thanks in advance.
 

Attachments

  • 3DOpenGL_test.apk
    455 KB · Views: 398

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I don't have an Android device, but..

Code:
vertexIndices.add(Short.valueOf(line.split(" ")[1]));
vertexIndices.add(Short.valueOf(line.split(" ")[2]));
 vertexIndices.add(Short.valueOf(line.split(" ")[3]));

You're parsing faces here, and you try to split at " ". So you try to call things like Short.valueOf("2/3/4"). So even IF that returns a value, it's not what you want.
Or have you changed the Obj file in some way?

Next, in Obj indices start at 1. In OpenGL they start at 0. So you have to subtract -1 from the values in your ShortBuffer.

Also, after you've read all data from the Obj file, print them to the console and manually check if they're correct. Because I don't think they are.


Code:
vertices[i] = (f != null ? f : Float.NaN);
..
indices[i] = (s != null ? s : 1);
You can't just do that .. if one entry in your array equals null then that's a parsing error or an invalid Obj file.
You're covering up errors by swapping them with wrong(!) index or position data.
At the very least you should print out an error, or log the error in a logfile, when that happens.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,663
When I export my OBJ files, the "f" isn't set to have "v/vt/vn" format, rather it just shows "v".

f vx vy vz.

I didn't use "Triangulate faces" option when I'm exporting.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    Actually I was just playing with having some kind of mention of the food forum and recipes on the main page to test and see if it would engage some of those people to post something. It is just weird to get so much traffic and no engagement
  • The Helper The Helper:
    So what it really is me trying to implement some kind of better site navigation not change the whole theme of the site
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top