C ++ - Float数组在某个非常大的索引之后不存储值

时间:2018-05-31 08:57:22

标签: c++ arrays floating-point opengl-3

在我的程序中,我使用Perlin噪音创建地形。地形正在正确生成,现在我正在尝试将计算出的法线存储到名为 normals 的浮点数组中。它正常存储法线直到某个索引,但它只存储0。这是我的代码:

float frequency = 8;
float flagVertices[ N*N*VALS_PER_VERT*VERTICES_PER_QUAD ];
float normals[ N*N*VALS_PER_VERT*VERTICES_PER_QUAD ];
int idxFlag = 0;
int normFlag = 0;
for (int i=0;i < N;i++){
    for (int j=0;j < N;j++){
        // *** Face 1 ***
        // (0,0)
        flagVertices[idxFlag++] = (float)i/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)i/N*frequency, (float)j/N*frequency ) );
        flagVertices[idxFlag++] = (float)j/N;
        glm::vec3 p0 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]);                

        // (0,1)
        flagVertices[idxFlag++] = (float)i/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)i/N*frequency, (float)(j+1)/N*frequency ) );
        flagVertices[idxFlag++] = (float)(j+1)/N; 
        glm::vec3 p1 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]); 

        // (1,1)
        flagVertices[idxFlag++] = (float)(i+1)/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)(i+1)/N*frequency, (float)(j+1)/N*frequency ) );
        flagVertices[idxFlag++] = (float)(j+1)/N; 
        glm::vec3 p2 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]);

        // Adding in normals
        glm::vec3 normal = glm::abs(glm::triangleNormal(p0,p1,p2));
        normals[normFlag++] = normal.x;
        normals[normFlag++] = normal.y;
        normals[normFlag++] = normal.z;

        // *** Face 2 ***
        // (1,1)
        flagVertices[idxFlag++] = (float)(i+1)/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)(i+1)/N*frequency, (float)(j+1)/N*frequency ) );
        flagVertices[idxFlag++] = (float)(j+1)/N;
        glm::vec3 p3 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]); 

        // (1,0)
        flagVertices[idxFlag++] = (float)(i+1)/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)(i+1)/N*frequency, (float)j/N*frequency ) );
        flagVertices[idxFlag++] = (float)j/N;
        glm::vec3 p4 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]); 

        // (0,0)
        flagVertices[idxFlag++] = (float)i/N;
        flagVertices[idxFlag++] = 0.2f*PerlinNoise( glm::vec2( (float)i/N*frequency, (float)j/N*frequency ) );
        flagVertices[idxFlag++] = (float)j/N;
        glm::vec3 p5 = glm::vec3(flagVertices[idxFlag-3], flagVertices[idxFlag-2], flagVertices[idxFlag-1]);

        // Adding in normals
        normal = glm::abs(glm::triangleNormal(p3,p4,p5));
        normals[normFlag++] = normal.x;
        normals[normFlag++] = normal.y;
        normals[normFlag++] = normal.z;
    }
}

我确定这是一个C ++问题,我有,因为我已经尝试在创建它之后打印出法线,我看不到0。但是当我从数组中打印出法线(使用正确的索引)时,我得到的是0。

1 个答案:

答案 0 :(得分:0)

对于normals数组,请将乘法与VALS_PER_VERT一起删除。它会使你的阵列比它需要的大三倍。

您未初始化的元素的值取决于您定义数组的位置:如果它们被定义为全局(或命名空间)变量,它们将被初始化为零;如果它们被定义为函数内的局部变量,则它们的值将是 indeterminate (并且读取它们将导致未定义的行为)。

所以定义应该是

float normals[ N*N*VERTICES_PER_QUAD ];

并记住不要超出界限。这将使所有元素在您显示的循环内初始化。