creating an object from FloatBuffer

Started by raft, December 27, 2009, 06:58:28 PM

Previous topic - Next topic

raft

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:
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:

        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

EgonOlsen

Maybe it's indexed geometry? Is there a kind of index buffer somewhere?

raft

indeed there is. is data structure different in such a case ?

EgonOlsen

Yes. The index buffers contains the indices into the vertex/texture buffers.

raft

so what's the format exactly ? i've googled but didnt help much.

raft

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:

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);
}

EgonOlsen

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.

raft

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:
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

EgonOlsen

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...

raft


EgonOlsen

Yes, something like that. I remember that i had to do this for some file formats in the past (MD2 for example...)

raft

didnt helped much. i've also tried -u, -v and v, u

EgonOlsen

Hmm...but judging from the screen shot, it looks like something like that. Reminds me of textures that were accidently loaded upside down.

raft

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..

EgonOlsen

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?