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,710
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,710
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,710
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,710
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: 399

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,710
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top