www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: raft on December 27, 2009, 06:58:28 pm

Title: creating an object from FloatBuffer
Post by: raft 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
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen on December 27, 2009, 08:04:23 pm
Maybe it's indexed geometry? Is there a kind of index buffer somewhere?
Title: Re: creating an object from FloatBuffer
Post by: raft on December 27, 2009, 08:34:27 pm
indeed there is. is data structure different in such a case ?
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen on December 27, 2009, 08:54:00 pm
Yes. The index buffers contains the indices into the vertex/texture buffers.
Title: Re: creating an object from FloatBuffer
Post by: raft on December 27, 2009, 09:52:39 pm
so what's the format exactly ? i've googled but didnt help much.
Title: Re: creating an object from FloatBuffer
Post by: raft 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);
}
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen 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.
Title: Re: creating an object from FloatBuffer
Post by: raft 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:
(http://img694.imageshack.us/img694/4130/screenshotjpcttest.png)

and this is how it should look:
(http://img694.imageshack.us/img694/8791/61173158.png)

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
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen 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...
Title: Re: creating an object from FloatBuffer
Post by: raft on December 28, 2009, 12:08:44 am
negate ? you mean 1-u, 1-v ?
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen 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...)
Title: Re: creating an object from FloatBuffer
Post by: raft on December 28, 2009, 12:23:52 am
didnt helped much. i've also tried -u, -v and v, u
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen 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.
Title: Re: creating an object from FloatBuffer
Post by: raft 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..
(http://img192.imageshack.us/img192/8697/screenshotjpcttest1.png)
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen 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?
Title: Re: creating an object from FloatBuffer
Post by: raft on December 28, 2009, 11:43:02 am
yeah, they seem valid.
min: 0.005225
max: 0.996512
Title: Re: creating an object from FloatBuffer
Post by: raft on December 28, 2009, 12:23:40 pm
i've discovered something strange: i flipped the texture vertically with an image editor. i expected the result will be same as (u, 1-v) but it isnt. it's also different from (1-u, v)  ???
Title: Re: creating an object from FloatBuffer
Post by: raft on December 28, 2009, 02:46:35 pm
solved ;D i posted to Ardor3d forums and one said he can generate my screenshot using upper left part of texture (0.5*u, 0.5*v)
and that reminded me calling recreateTextureCoords(). i really forgot these things  :o

here is how it looks now, in SW renderer, without any lights:
(http://img710.imageshack.us/img710/1646/screenshotjpcttest2.png)

r a f t
Title: Re: creating an object from FloatBuffer
Post by: raft on February 04, 2010, 03:39:27 pm
it may help accepting null uv array in new constructor.
Title: Re: creating an object from FloatBuffer
Post by: EgonOlsen on February 04, 2010, 05:21:27 pm
Changed in http://www.jpct.net/download/beta/jpct.jar (http://www.jpct.net/download/beta/jpct.jar) as well... ;D