Author Topic: creating an object from FloatBuffer  (Read 8620 times)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
creating an object from FloatBuffer
« on: December 27, 2009, 06:58:28 pm »
hi,

i'm trying to reconstruct an Ardor3d object in jPCT. it internally stores mesh data (vertices and texture coords) as FloatBuffers. it then directly feeds GL with that float buffers:
Code: [Select]
GL11.glVertexPointer(3, 0, vertexBuffer);
and this is what i do in an Object3D constructor. read the data from vertex and textures buffers and create polygons out of them:

Code: [Select]
        FloatBufferData vertexCoords = mesh.getMeshData().getVertexCoords();
FloatBuffer vertexBuffer = mesh.getMeshData().getVertexBuffer();
vertexBuffer.rewind();

        FloatBufferData textureCoords = mesh.getMeshData().getTextureCoords().get(0);
FloatBuffer textureBuffer = textureCoords.getBuffer();
textureBuffer.rewind();

       
        final float[] vertexData = new float[3];
        final float[] textureData = new float[6];
       
for (int i = 0; i < vertexCoords.getTupleCount()/3; i++) {
System.out.println(i);

vertexBuffer.get(vertexData);
SimpleVector vert1 = new SimpleVector(vertexData);

vertexBuffer.get(vertexData);
SimpleVector vert2 = new SimpleVector(vertexData);
vertexBuffer.get(vertexData);

SimpleVector vert3 = new SimpleVector(vertexData);
textureBuffer.get(textureData);

addTriangle(vert1, textureData[0], textureData[1],
vert2, textureData[2], textureData[3],
vert3, textureData[4], textureData[5]);
}

something is definetly wrong as the constructed object is totally shapeless. any ideas ?

thanks,
r a f t

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #1 on: December 27, 2009, 08:04:23 pm »
Maybe it's indexed geometry? Is there a kind of index buffer somewhere?

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #2 on: December 27, 2009, 08:34:27 pm »
indeed there is. is data structure different in such a case ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #3 on: December 27, 2009, 08:54:00 pm »
Yes. The index buffers contains the indices into the vertex/texture buffers.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #4 on: December 27, 2009, 09:52:39 pm »
so what's the format exactly ? i've googled but didnt help much.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #5 on: December 27, 2009, 10:42:22 pm »
i expected each 3 elements in index buffer, specifies the positions of 3 vertices of a polygon in vertex buffer. but seems as that's not the case. or i'm massing something
so without texture coordinates:

Code: [Select]
final int[] indexData = new int[3];
       
for (int i = 0; i < indexBufferData.getBufferLimit()/3; i++) {
indexBuffer.get(indexData);

int index = indexData[0] * 3;
SimpleVector vert1 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);

index = indexData[1] * 3;
SimpleVector vert2 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);

index = indexData[2] * 3;
SimpleVector vert3 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);

addTriangle(vert1, vert2, vert3);
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #6 on: December 27, 2009, 10:49:00 pm »
The index always points to the whole vertex, not to its components, i.e. if you have vertices like

(0,0,0), (1,1,1), (1,1,1)

for your (in this case distorted...) triangle,

you would have

0,0,0,1,1,1 in your vertex buffer

and

0,1,1

in your index buffer.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #7 on: December 27, 2009, 11:41:22 pm »
yeap, almost there :) there were more than one mesh, it faked me.

meshes are correctly loaded but i have a problem with texture, possibly coords.

this is how it looks now:


and this is how it should look:


and the code to load a mesh:
Code: [Select]
for (int i = 0; i < indexBufferData.getBufferLimit()/3; i++) {
indexBuffer.get(indexData);

int index = indexData[0] * 3;
SimpleVector vert1 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);

index = indexData[1] * 3;
SimpleVector vert2 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);

index = indexData[2] * 3;
SimpleVector vert3 = new SimpleVector(vertexData[index], vertexData[index+1], vertexData[index+2]);


index = indexData[0] * 2;
float u1 = textureData[index]; float v1 =textureData[index + 1]; 

index = indexData[1] * 2;
float u2 = textureData[index]; float v2 =textureData[index + 1]; 

index = indexData[2] * 2;
float u3 = textureData[index]; float v3 =textureData[index + 1]; 

addTriangle(vert1, u1, v1,
vert2, u2, v2,
vert3, u3, v3);
}

if there is an index buffer, is it necessarily used for texture coords also  ?

btw, the original texture was TGA which java cannot load by default AFAIK. Ardor3d do somehow load it. I've converted it to a PNG.
and is this textures coordinates thing universal ? are there any coordination system differences ? Ardor3D loads this model from a Collada file.

r a f t

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #8 on: December 27, 2009, 11:53:55 pm »
Yes, the indices usually apply to everything including textures. The coordinates look reversed or something. Try to negate u or v and see what happens then...

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #9 on: December 28, 2009, 12:08:44 am »
negate ? you mean 1-u, 1-v ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #10 on: December 28, 2009, 12:09:51 am »
Yes, something like that. I remember that i had to do this for some file formats in the past (MD2 for example...)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #11 on: December 28, 2009, 12:23:52 am »
didnt helped much. i've also tried -u, -v and v, u

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #12 on: December 28, 2009, 12:37:56 am »
Hmm...but judging from the screen shot, it looks like something like that. Reminds me of textures that were accidently loaded upside down.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: creating an object from FloatBuffer
« Reply #13 on: December 28, 2009, 01:04:31 am »
debugging Ardor3d's code, i've found it loads TGA's vertically flipped. i dont know why..
so i've tried u, 1-v and u, -v but neither helped

this is how it looks like with u, 1-v. seems better, but stil i miss something..

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12298
    • View Profile
    • http://www.jpct.net
Re: creating an object from FloatBuffer
« Reply #14 on: December 28, 2009, 10:42:44 am »
Have you verified the u/v-range? Maybe it's not between 0..1 so that the 1-u will go wrong in that case?