由于某种原因,我的 C++ glm::vec3 数组似乎重置为 null

My C++ glm::vec3 array seems to be resetting to null for some reason

我正在处理本教程中的一些 openGL 代码,并尝试将其应用到我自己的立方体场景中 - http://learnopengl.com/#!Advanced-Lighting/Normal-Mapping

我在一些基本的立方体代码中添加了一个切线贴图,我意识到我必须将立方体的顶点拆分出来,以便将它们重新加工成一个切线法线贴图,用于一些光照计算(基本上是帮助显示纹理的深度) .

真正奇怪的是,在大约一半的时候,nm(正常)向量数组似乎被一个甚至没有触及它的 for 循环完全清空。我唯一的想法是,也许在我改变切线和双切线数组的值的循环中,它以某种方式在内存中重叠。切线似乎也受到同一问题的影响,并且也被取消了。 pos、uv 和 bitangent 值都保持不变,但是...

我已经在更下方的评论中标记了发生变化的地方。 有没有想去哪里寻找或可能导致这种情况的原因?

谢谢, 乔恩

vec3 pos[36];
vec3 nm[36];
vec2 uv[36];
vec3 tangent[36];
vec3 bitangent[36];
GLfloat cubeSet[36 * 14];

GLuint cubeVAO = 0;
GLuint cubeVBO = 0;
void RenderCube()
{
    if (cubeVAO == 0)
    {
        GLfloat cubeVertices[] = {
            // Back face
            -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // Bottom-left
            0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
            0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right         
            0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,  // top-right
            -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,  // bottom-left
            -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,// top-left
            // Front face
            -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
            0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,  // bottom-right
            0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  // top-right
            0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
            -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,  // top-left
            -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,  // bottom-left
            // Left face
            -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
            -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left
            -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,  // bottom-left
            -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
            -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,  // bottom-right
            -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
            // Right face
            0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
            0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
            0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right         
            0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,  // bottom-right
            0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,  // top-left
            0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left     
            // Bottom face
            -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
            0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left
            0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,// bottom-left
            0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
            -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right
            -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
            // Top face
            -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,// top-left
            0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
            0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right     
            0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
            -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,// top-left
            -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left        
            };


        for (int i = 0; i < 36; i++)
        {
            int j = i * 8;
            pos[i] = vec3(cubeVertices[j], cubeVertices[j + 1], cubeVertices[j + 2]);
            nm[i] = vec3(cubeVertices[j+3], cubeVertices[j + 4], cubeVertices[j + 5]);
            uv[i] = vec2(cubeVertices[j+6], cubeVertices[j + 7]);

            /*cout << pos[i].x << pos[i].y << pos[i].z << endl;
            cout << nm[i].x << nm[i].y << nm[i].z << endl;
            cout << uv[i].x << uv[i].y << endl;*/
        }

        cout << nm[35].x << nm[35].y << nm[35].z << endl; // COMES OUT AS 010

        for (int i = 0; i < 36; i++)
        {
            int j = i * 3;

            vec3 edge1 = pos[j + 1] - pos[j];
            vec3 edge2 = pos[j + 2] - pos[j];
            vec2 deltaUV1 = uv[j + 1] - uv[j];
            vec2 deltaUV2 = uv[j + 2] - uv[j];

            vec3 tangent1(0.0f), bitangent1(0.0f);

            GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);

            tangent1.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
            tangent1.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
            tangent1.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
            tangent1 = normalize(tangent1);

            bitangent1.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
            bitangent1.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
            bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
            bitangent1 = normalize(bitangent1);

            tangent[j] = tangent1;
            tangent[j+1] = tangent1;
            tangent[j+2] = tangent1;

            bitangent[j] = bitangent1;
            bitangent[j + 1] = bitangent1;
            bitangent[j + 2] = bitangent1;
        }

        cout << nm[35].x << nm[35].y << nm[35].z << endl; //COMES OUT AS -nan(ind)-nan(ind)-nan(ind)

        for (int i = 0; i < 36; i++)
        {
            int j = i * 14;

            cubeSet[j] = pos[i].x;
            cubeSet[j+1] = pos[i].y;
            cubeSet[j+2] = pos[i].z;

            cubeSet[j + 3] = nm[i].x;
            cubeSet[j + 4] = nm[i].y;
            cubeSet[j + 5] = nm[i].z;

            cubeSet[j + 6] = uv[i].x;
            cubeSet[j + 7] = uv[i].y;

            cubeSet[j + 8] = tangent[i].x;
            cubeSet[j + 9] = tangent[i].y;
            cubeSet[j + 10] = tangent[i].z;

            cubeSet[j + 11] = bitangent[i].x;
            cubeSet[j + 12] = bitangent[i].y;
            cubeSet[j + 13] = bitangent[i].z;
        }

        glGenVertexArrays(1, &cubeVAO);
        glGenBuffers(1, &cubeVBO);

        glBindVertexArray(cubeVAO);
        glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(cubeSet), cubeSet, GL_STATIC_DRAW);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
        glEnableVertexAttribArray(2);
        glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat)));
        glEnableVertexAttribArray(3);
        glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(11 * sizeof(GLfloat)));
        glEnableVertexAttribArray(4);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    glBindVertexArray(cubeVAO);
    glDrawArrays(GL_TRIANGLES, 0, 36);  
    glBindVertexArray(0);
}

您声明...

vec3 tangent[36];

但是在你的第二个循环中你有...

for (int i = 0; i < 36; i++)
{
    int j = i * 3;
        .
        .
        .
    tangent[j] = tangent1;
    tangent[j+1] = tangent1;
    tangent[j+2] = tangent1;

所以您正在访问 tangent[107] 并导致内存损坏。 bitagent.

也是如此