Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Quick Question about 3D model format

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Quick Question about 3D model format
Dario ff
Member #10,065
August 2008
avatar

Trying to load up a an specific 3D binary model, and I'm having some trouble with face-reading.

Only documentation I got is that the faces are supposed to be read as strips, and that I should read the last reference point of the last face every time I read a new reference point and create a new face.

But I thought faces are supposed to be created with only 3 indices? How could I possibly create one from 2 reference points? ???

The face indices are just unsigned short ints in big endian.

TL;DR;, how do you read faces indices as strips and create the appropiate faces in a format like (index 1, index 2, index 3)?

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Arthur Kalliokoski
Member #5,540
February 2005
avatar

I think they mean you only read in one new vertex and use the previous two vertices as the other two points for a face, hence the term strip.

http://en.wikipedia.org/wiki/Triangle_strip

I really admire the U.S. Constitution. It's so much better than what we have now.

verthex
Member #11,340
September 2009
avatar

Dario ff said:

TL;DR;, how do you read faces indices as strips and create the appropiate faces in a format like (index 1, index 2, index 3)?

I always thought the indices were completely arbitrary and its up to the programmer to decide what they are. But you have to be consistent once you choose your coordinate frame.

gnolam
Member #2,030
March 2002
avatar

Dario ff said:

But I thought faces are supposed to be created with only 3 indices? How could I possibly create one from 2 reference points? ???

As you said: they're strips - a list of connected triangles/quads, so you keep track of the last couple of vertices. For a triangle strip for instance, a new triangle is created from each vertex after the first two.
For example, a triangle strip with vertex indices 0, 1, 2, 3, 4 would result in the following triangles:
(0, 1, 2)
(2, 1, 3)
(2, 3, 4)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Dario ff
Member #10,065
August 2008
avatar

Ok so according to your answers, the following code should work I think...

#SelectExpand
1 unsigned short int face_1=0; 2 unsigned short int face_2=0; 3 unsigned short int face_3=0; 4 5 for (int i=0; i<face_count; i++) { 6 face_3 = face_2; 7 face_2 = face_1; 8 9 fread(&face_1, sizeof(unsigned short int), 1, fp); 10 face_1 = _byteswap_ushort(face_1); 11 12 13 if (i==2) base_indices.push_back(Ogre::Vector3(face_1, face_2, face_3)); 14 else if (i > 2) { 15 Ogre::Vector3 overt(face_1, face_2, face_3); 16 base_indices.push_back(overt); 17 } 18 }

But it looks like this:
{"name":"605556","src":"http:\/\/static.allegro.cc\/image\/cache\/d\/a\/da1ca4a727f6bc3ce803415892c2d5ff.png","w":405,"h":373,"tn":"http:\/\/static.allegro.cc\/image\/cache\/d\/a\/da1ca4a727f6bc3ce803415892c2d5ff"}605556

Perhaps...

  • the vector indices aren't right,

  • or the vectors are supposed to be ordered in the inverse order(as in, pushing to the front of the list),

  • or the above code has a bug I haven't noticed yet? ???

EDIT: It's supposed to be some kind of tree. :P

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

verthex
Member #11,340
September 2009
avatar

What is it supposed to look like a squid?

gnolam
Member #2,030
March 2002
avatar

If you're creating triangles out of triangle strips yourself, note that odd and even triangles have different vertex orderings (to keep a CW/CCW order).

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Arthur Kalliokoski
Member #5,540
February 2005
avatar

These short ints are indices to x, y, z floating point numbers? Could you post the vertex list?

I really admire the U.S. Constitution. It's so much better than what we have now.

Dario ff
Member #10,065
August 2008
avatar

Could you post the vertex list?

Vertex list, Faces list(in the order of the binary file)

Here's how the vertices(or is it vertexes? :P) alone look like.
{"name":"605559","src":"http:\/\/static.allegro.cc\/image\/cache\/e\/8\/e86ed7a9a167b7422a069fdebc9f4553.png","w":590,"h":536,"tn":"http:\/\/static.allegro.cc\/image\/cache\/e\/8\/e86ed7a9a167b7422a069fdebc9f4553"}605559

EDIT: The "65535" face index is probably a 0 that I just endian swapped.

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Arthur Kalliokoski
Member #5,540
February 2005
avatar

I'm playing with it, I think the 65535 is just a marker to start a new strip.

I really admire the U.S. Constitution. It's so much better than what we have now.

Dario ff
Member #10,065
August 2008
avatar

I think you might be on to something. But perhaps I just need to add a new face at the end of each strip?
{"name":"605560","src":"http:\/\/static.allegro.cc\/image\/cache\/0\/4\/04a3228cc210bbb3ff6de5f4678e19c2.png","w":822,"h":669,"tn":"http:\/\/static.allegro.cc\/image\/cache\/0\/4\/04a3228cc210bbb3ff6de5f4678e19c2"}605560

#SelectExpand
1 unsigned short int face_1=0; 2 unsigned short int face_2=0; 3 unsigned short int face_3=0; 4 5 int new_strip=3; 6 7 for (int i=0; i<face_count; i++) { 8 unsigned short int t=0; 9 fread(&t, sizeof(unsigned short int), 1, fp); 10 t = _byteswap_ushort(t); 11 if (t == 65535) { 12 new_strip = 3; 13 } 14 else { 15 new_strip -= 1; 16 17 face_3 = face_2; 18 face_2 = face_1; 19 face_1 = t; 20 21 if (new_strip==0) { 22 Ogre::Vector3 overt(face_3, face_2, face_1); 23 base_indices.push_back(overt); 24 new_strip = 1; 25 } 26 } 27 }

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Arthur Kalliokoski
Member #5,540
February 2005
avatar

I got this far with it, but Blender barfs on it. I was assuming vertex winding wouldn't matter (I don't remember where the setting is). You've already got something showing so I'll stop now.

I really admire the U.S. Constitution. It's so much better than what we have now.

Trent Gamblin
Member #261
April 2000
avatar

Just looking at the model, it might be what gnolam said, the triangles alternate ccw and cw so every second one is getting culled.

If eating hotdogs is wrong, I don't wanna be right.

Arthur Kalliokoski
Member #5,540
February 2005
avatar

If it's OpenGL you could do
glDisable(GL_CULL_FACE);

I really admire the U.S. Constitution. It's so much better than what we have now.

Dario ff
Member #10,065
August 2008
avatar

Indeed it seems to be... when I enable the 2-sided faces on Ogre it looks like this:
{"name":"605562","src":"http:\/\/static.allegro.cc\/image\/cache\/f\/5\/f54888b990b3c09174fcb3e49e2f0177.png","w":786,"h":578,"tn":"http:\/\/static.allegro.cc\/image\/cache\/f\/5\/f54888b990b3c09174fcb3e49e2f0177"}605562

How do I fix the winding problem? Swap the indices order at every odd/even index? Forcing culling off is not really the right way to do it. :P

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Paul whoknows
Member #5,081
September 2004
avatar

Are you using Ogre? still working with Allegro5? ???

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

gnolam
Member #2,030
March 2002
avatar

Dario ff said:

How do I fix the winding problem? Swap the indices order at every odd/even index?

Yes.

[EDIT]
I realized I had it handy, so here comes an explanatory diagram for why this is done. Triangle strip representation of 2 connected quads:
{"name":"605563","src":"http:\/\/static.allegro.cc\/image\/cache\/9\/3\/932f02495b960569a060561c122247e8.png","w":542,"h":300,"tn":"http:\/\/static.allegro.cc\/image\/cache\/9\/3\/932f02495b960569a060561c122247e8"}605563
As you can see, if the winding order isn't reversed, every second triangle will appear to face the wrong way.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Arthur Kalliokoski
Member #5,540
February 2005
avatar

This page has a bit to say about it just past halfway down. You might have to rewrite the data in a different order to ship with the game or whatever.

http://www.codercorner.com/Strips.htm

I really admire the U.S. Constitution. It's so much better than what we have now.

Trent Gamblin
Member #261
April 2000
avatar

If you're reading the model yourself it shouldn't be hard to fix yourself.

read 2 vertices
int t = 0, v = 2
while not done
   read vertex
   if (t % 2 != 0)
      new triangle(v, v-1, v-2)
   else
      new triangle(v-2, v-1, v)
   t++
   v++

If eating hotdogs is wrong, I don't wanna be right.

Dario ff
Member #10,065
August 2008
avatar

Yeah I fixed it with just switching faces around every odd sub-index(as in, index from the last strip).

Thanks a lot. :)

{"name":"605564","src":"http:\/\/static.allegro.cc\/image\/cache\/b\/8\/b80c5ee4220d1d8e154c41f553a87b4c.png","w":713,"h":663,"tn":"http:\/\/static.allegro.cc\/image\/cache\/b\/8\/b80c5ee4220d1d8e154c41f553a87b4c"}605564

Another model.

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Steve Terry
Member #1,989
March 2002
avatar

See attachment ;D

___________________________________
[ My Pictures ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Dario ff
Member #10,065
August 2008
avatar

8mb of Goatsee? :-/

EDIT: Mr. Potato head in A4. :o

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Arthur Kalliokoski
Member #5,540
February 2005
avatar

See attachment

I tried some of the examples with my OBJ loader, the space shuttle example didn't have a mtl file, the triceratops had more than 4 vertices for a face, and when the f5e_05 example had 3 texture vertices I gave up. :'(

[EDIT]

If I clean them up by running them through Blender they work.

{"name":"605568","src":"http:\/\/static.allegro.cc\/image\/cache\/9\/7\/9789eafdcc5469610f0a17f72ee90ba1.png","w":1000,"h":800,"tn":"http:\/\/static.allegro.cc\/image\/cache\/9\/7\/9789eafdcc5469610f0a17f72ee90ba1"}605568

I really admire the U.S. Constitution. It's so much better than what we have now.

Steve Terry
Member #1,989
March 2002
avatar

Don't know what you are talking about, all loaded by my utility just fine.

___________________________________
[ My Pictures ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

gnolam
Member #2,030
March 2002
avatar

I tried some of the examples with my OBJ loader, the space shuttle example didn't have a mtl file, the triceratops had more than 4 vertices for a face, and when the f5e_05 example had 3 texture vertices I gave up. :'(

All of which are perfectly legal for an OBJ. :)

(Well, I'm only pretty sure about the first (it's been a while since I did anything with my OBJ loader). The latter two are definitely legal, though - faces can have any number of vertices, and 3D textures are supported.)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

 1   2 


Go to: