VertexBuffer - Too little elements?

Hey,



I guess I’m doing something wrong when trying to get the positions of the vertices of a mesh. I’m not at home so I don’t have the code at hand, thus I’m gonna quickly write something that might not be correct but it’s not about the syntax:



[java]

Sphere mesh = new Sphere(32, 32, 10, false, true);

Geometry sphere = new Geometry("", mesh);



VertexBuffer vB = mesh.getBuffer(Type.Position);

System.out.println(vB.getNumElements());

[/java]



In the left bottom corner I now see that the sphere has something like 1900 vertices. But getNumElements() says that the Buffer has something like 900 Elements. Am I making any common mistakes or something?

Did you took account of the vertices of the status text itself, that is also something like 914 vertices.

Check index buffer, many vertexes are shared between triangles. (probably all, since its a sphere)

What do you need the positions for? Digging around in the buffers is normally (not always) a bad approach to whatever one is trying to do.

I want to change the shape of meshes through algorithms: Topic Link

ah k, sorry… just checking.

Thanks for the help so far!



Is it possible that a sphere is build up on two parts? I have a strange symetrically behaviour when changing vertices in a certain distance to a point in the mesh (took from the vertex buffer). Or is this happening because of triangles sharing vertices?



http://i.imgur.com/PzUCa.jpg



In the picture you see two bulges in the exact shape and mirror inverted. Here is some code:



[java]

VertexBuffer vertexPositions = sphereMesh.getBuffer(VertexBuffer.Type.Position);

System.out.println(vertexPositions.getNumElements());





int i = 0;

int p = 0;



Vector3f outVector = new Vector3f();





while(i < vertexPositions.getNumElements() - 1)

{

for (int k = 0; k < 2; k++)

{

switch (k)

{

case 0:

outVector.x = (Float)vertexPositions.getElementComponent(i, k);

break;

case 1:

outVector.y = (Float)vertexPositions.getElementComponent(i, k);

break;

case 2:

outVector.z = (Float)vertexPositions.getElementComponent(i, k);

break;



}

}









if (getVectorDistance(outVector, new Vector3f((Float)vertexPositions.getElementComponent(200, 0), (Float)vertexPositions.getElementComponent(200, 1),

(Float)vertexPositions.getElementComponent(200, 2))) < 9.0f)

{

for (int k = 0; k < 2; k++)

{

switch (k) {

case 0:

vertexPositions.setElementComponent(i, k, (Float) outVector.x * 1.2f);

break;

case 1:

vertexPositions.setElementComponent(i, k, (Float) outVector.y * 1.2f);

break;

case 2:

vertexPositions.setElementComponent(i, k, (Float) outVector.z * 1.2f);

break;



}

}

}





i++;



}



sphereMesh.clearBuffer(VertexBuffer.Type.Position);

sphereMesh.setBuffer(vertexPositions);

}





private float getVectorDistance(Vector3f v1, Vector3f v2)

{

float xd = v2.x - v1.x;

float yd = v2.y - v1.y;

float zd = v2.z - v1.z;



return FastMath.sqrt((xd * xd) + (yd * yd) + (zd * zd));

}

[/java]

Vertex sharing isn’t your issue. My guess is that the distance between the one side and the other is the same to the picked point.



Just some random asides:

getVectorDistance() appears to be doing the exact same thing as Vector3f’s distance() method.



The for/switch loop is a little verbose when you could do the same thing in three lines just grabbing the values.



Instead of:

[java]

for (int k = 0; k < 2; k++)

{

switch (k)

{

case 0:

outVector.x = (Float)vertexPositions.getElementComponent(i, k);

break;

case 1:

outVector.y = (Float)vertexPositions.getElementComponent(i, k);

break;

case 2:

outVector.z = (Float)vertexPositions.getElementComponent(i, k);

break;



}

}[/java]



Just do:

[java]

outVector.x = (Float)vertexPositions.getElementComponent(i, 0);

outVector.y = (Float)vertexPositions.getElementComponent(i, 1);

outVector.z = (Float)vertexPositions.getElementComponent(i, 2);



[/java]

1 Like

Nice man :slight_smile: Thanks!



After cleaned up the code according to your tips it worked!



http://imgur.com/pTX0K