GraphicsPadawan GraphicsPadawan - 3 months ago 6
Java Question

How can I add this float[] vertexData to a Vector3f list?

How I can add vertex data (

float[]
) to a
Vector3f
list? It gives me an error if I try.

float[] vertexData = new float[ allindices2.length * vertexDataSize / 3];
for (int i = 0; i < vertexData.length / vertexDataSize; i++){
vertexData[i * vertexDataSize + 0] = Float.parseFloat(allindices2 [Integer.parseInt(allindices2 [i * source.size() + 0]) * 3 + 0]);
vertexData[i * vertexDataSize + 1] = Float.parseFloat(allpositions2[Integer.parseInt(allindices2[i * source.size() + 0]) * 3 + 1]);
vertexData[i * vertexDataSize + 2] = Float.parseFloat(allpositions2[Integer.parseInt(allindices2[i * source.size() + 0]) * 3 + 2]);

vertices.add(vertexData);
}

Answer

If you don't need the vertexData array for any other reason, you should not create it at all. Instead, you can directly create the required Vector3f instances.

for (int i = 0; i < vertexData.length / vertexDataSize; i++){

      float x = Float.parseFloat(allindices2  [Integer.parseInt(allindices2[i * source.size() + 0]) * 3 + 0]);
      float y = Float.parseFloat(allpositions2[Integer.parseInt(allindices2[i * source.size() + 0]) * 3 + 1]);
      float z = Float.parseFloat(allpositions2[Integer.parseInt(allindices2[i * source.size() + 0]) * 3 + 2]);

      vertices.add(new Vector3f(x,y,z));
}

Nevertheless, all these parse... calls and the general structure look highly dubious. Unless you obtain this data directly from a file or so, you should consider a different data model.

Additionally:

  • Are you sure that the first allindices2 should not be allpositions2?
  • There's no need to do work twice. You can pull out the computation of the index.

Most likely, the code could also be written as

for (int i = 0; i < vertexData.length / vertexDataSize; i++){
      int index = Integer.parseInt(allindices2[i * source.size()]);
      float x = Float.parseFloat(allpositions2[index * 3 + 0]);
      float y = Float.parseFloat(allpositions2[index * 3 + 1]);
      float z = Float.parseFloat(allpositions2[index * 3 + 2]);
      vertices.add(new Vector3f(x,y,z));
}
Comments