顶点数组转换为浮点数组

huangapple go评论87阅读模式
英文:

Vertex array to a float array

问题

我目前正在开发一个游戏引擎,被要求制作一个方法,该方法接受一个顶点数组并输出一个浮点数组。我们已经为Vector3创建了一个类。Vector3是包装了三个浮点数x、y和z的类。顶点只是一个包装了Vector3f的类。因此,如果有一个顶点数组,我该如何将它转换成浮点数组。以下是我目前的代码:

public static float[] VertexArrayToFloatArray(Vertex[] vertices){
    float[] floats = new float[vertices.length * 3]; // 每个顶点需要三个浮点数

    int i = 0;
    for (Vertex v : vertices){
        float x, y, z;
        x = v.getPosition().getX();
        y = v.getPosition().getY();
        z = v.getPosition().getZ();

        floats[i] = x;
        floats[i + 1] = y;
        floats[i + 2] = z;
        
        i += 3; // 每次增加3以处理下一个顶点
    }
    return floats;
}

一个预期的输出示例是将顶点数组放入以创建一个正方形,例如:

Vertex[] vertices = new Vertex[]{

  new Vertex(new Vector3f(0.5f,-0.5f,0)), //右下角
  new Vertex(new Vector3f(-0.5f,0.5f,0)), //左上角
  new Vertex(new Vector3f(0.5f,0.5f,0)),  //右上角
  new Vertex(new Vector3f(-0.5f,-0.5f,0)) //左下角
};

你将会得到结果:

{0.5f,-0.5f,0,-0.5f,0.5f,0,0.5f,0.5f,0 ,-0.5f,-0.5f,0}
英文:

I am currently developing a game engine, and was asked to make a method that takes in a vertex array and outputs a float array. We have a class made for Vector3. Vector3 is a wrapper for 3 floats, x,y and z. The vertex is just a wrapper for a Vector3f. So if their is a vertex array how can i turn it into a float array. This is what i have so far


   public static float[] VertexArrayToFloatArray(Vertex[] vertices){
       float[] floats = new float[vertices.length];

       int i = 0;
       for (Vertex v : vertices){
           float x, y, z;
           x = v.getPosition().getX();
           y = v.getPosition().getY();
           z = v.getPosition().getZ();

           floats[i] = x;
           floats[i + 1] = y;
           floats[i + 2] = z;
       }
       System.out.println(floats);
       return floats;
   }


An expected output would be puting in a vertex array to make a square such as


Vertex[] vertices = new Vertex[]{

 new Vertex(new Vector3f(0.5f,-0.5f,0)), //Bottom Right
 new Vertex(new Vector3f(-0.5f,0.5f,0)), // Top Left
 new Vertex(new Vector3f(0.5f,0.5f,0)),  //Top Right
 new Vertex(new Vector3f(-0.5f,-0.5f,0)) //Bottom Left
}

and you would get

{0.5f,-0.5f,0,-0.5f,0.5f,0,0.5f,0.5f,0 ,-0.5f,-0.5f,0}

as the result

答案1

得分: 1

  1. 在你的示例中,你正在创建一个数组,其大小与输入中顶点的数量相同,但由于每个顶点有3个浮点数,所以你想要创建一个大小是输入大小三倍的数组。
float[] floats = new float[vertices.length * 3];
  1. 然后你正确地使用了for循环,但你从未更改过索引i

这意味着你只在索引0、1和2上进行赋值。
相反,你需要在每次循环时将i增加3(你在ii+1i+2上进行赋值)。

类似这样:

for (Vertex v : vertices) {
    float x, y, z;
    x = v.getPosition().getX();
    y = v.getPosition().getY();
    z = v.getPosition().getZ();

    floats[i] = x;
    floats[i + 1] = y;
    floats[i + 2] = z;
    i += 3;
}

或者你可以使用i++,它将i增加1,并在增加之前返回i的值。
如果我们将这两个建议结合起来,就会得到:

public static float[] VertexArrayToFloatArray(Vertex[] vertices) {
    float[] floats = new float[vertices.length * 3];

    int i = 0;
    for (Vertex v : vertices) {
        float x, y, z;
        x = v.getPosition().getX();
        y = v.getPosition().getY();
        z = v.getPosition().getZ();

        floats[i] = x;
        floats[i++] = y;
        floats[i++] = z;
    }
    System.out.println(floats);
    return floats;
}
英文:

1. In your example, you are creating an array with the size of the number of vertices from the input, but since every Vertex has 3 floats you want to create an array with triple the size of the input

float[] floats = new float[vertices.length*3];

2. Then you are correctly doing for loop but you are never changing the index i.

That means that you are only overwriting index 0,1 and 2.
Instead of that, you need to increment the i by 3 (You are adding on i,i+1, and i+2) every loop

Something like this:

for (Vertex v : vertices){
           float x, y, z;
           x = v.getPosition().getX();
           y = v.getPosition().getY();
           z = v.getPosition().getZ();

           floats[i] = x;
           floats[i + 1] = y;
           floats[i + 2] = z;
           i+=3;
}

Or you can use i++ which increase i by 1 and returns the value of i BEFORE increasing
If we then combine those 2 advise we get

public static float[] VertexArrayToFloatArray(Vertex[] vertices){
       float[] floats = new float[vertices.length*3];

       int i = 0;
       for (Vertex v : vertices){
           float x, y, z;
           x = v.getPosition().getX();
           y = v.getPosition().getY();
           z = v.getPosition().getZ();

           floats[i] = x;
           floats[i++] = y;
           floats[i++] = z;
       }
       System.out.println(floats);
       return floats;
   }

huangapple
  • 本文由 发表于 2020年10月21日 05:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64453676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定