我正在研究本教程中的一些openGL代码,并尝试将其应用到我自己的多维数据集场景 - http://learnopengl.com/#!Advanced-Lighting/Normal-Mapping。
我将切线贴图添加到一些基本的立方体代码中,并意识到我必须将立方体顶点拆分出来,以便将它们重新加工成相切法线贴图以进行某些光照计算(基本上是帮助显示纹理的深度)。 / p>
真正奇怪的是,大约一半的时候,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);
}
答案 0 :(得分:3)
你声明......
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
。