Reputation: 19
I'm working on some openGL code from this tutorial and trying to apply it to my own cube scene - http://learnopengl.com/#!Advanced-Lighting/Normal-Mapping.
Im adding a tangent map to some basic cube code and realise that I had to split the cube vertices out in order to rework them into a tangent normal map for some lighting calculations (basically the help show depth of a texture).
What's really strange is, about halfway down, the nm (normal) vector array seems to be nulled completely by a for loop that doesn't even touch it. My only thought is that perhaps in my loop to change the values of the tangent and bitangent arrays, it's overlapping in the memory somehow. The tangent also seems to have been affected by the same issue and is also being nulled. The pos, uv, and bitangent values all remain intact however...
I've marked where the change occurs with comments further down. Any thoughts where to look or what might be causing this?
Thank you, Jon
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);
}
Upvotes: 0
Views: 597
Reputation: 12879
You declare...
vec3 tangent[36];
But then in your second loop you have...
for (int i = 0; i < 36; i++)
{
int j = i * 3;
.
.
.
tangent[j] = tangent1;
tangent[j+1] = tangent1;
tangent[j+2] = tangent1;
So you're accessing tangent[107]
and causing memory corruption. And likewise for bitagent
.
Upvotes: 3