I have a function to calculate the normals of vertices in my terrain. I already have the normals for each triangle so I am just adding all the adjacent triangles normals to get a normal for the vertice. I'm getting seg faults, I think from going out of the bounds of my vector. Here is the function
Are there any blatant mistakes, or might it be something obscure?Code:void Map::calcVertexNorms() { /*It works if this get() is uncommented */ //std::cin.get(); float l; pos normal; int pitch = numTrisW*2; /*Adds each of the 6 adjacent triangles' normals*/ for (int i=0; i < pitch*numTrisH; i+=2) { normal.x += mapTris[i].norm.x; normal.y += mapTris[i].norm.y; normal.z += mapTris[i].norm.z; normal.x += mapTris[i+1].norm.x; normal.y += mapTris[i+1].norm.y; normal.z += mapTris[i+1].norm.z; if (i - pitch > 0) { normal.x += mapTris[i-pitch].norm.x; normal.y += mapTris[i-pitch].norm.y; normal.z += mapTris[i-pitch].norm.z; } if (i - pitch > 0 && i % pitch != 509) { normal.x += mapTris[i-pitch-1].norm.x; normal.y += mapTris[i-pitch-1].norm.y; normal.z += mapTris[i-pitch-1].norm.z; normal.x += mapTris[i-pitch-2].norm.x; normal.y += mapTris[i-pitch-2].norm.y; normal.z += mapTris[i-pitch-2].norm.z; } if (i % pitch != 509) { normal.x += mapTris[i-1].norm.x; normal.y += mapTris[i-1].norm.y; normal.z += mapTris[i-1].norm.z; } l = sqrtf((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z)); normal.x /= l; normal.y /= l; normal.z /= l; /*Set the new normal to the proper verts*/ mapTris[i].verts[0].norm = normal; mapTris[i+1].verts[0].norm = normal; if (i - pitch > 0) { mapTris[i-pitch].verts[2].norm = normal; } if (i - pitch > 0 && i % pitch != 509) { mapTris[i-pitch-1].verts[2].norm = normal; mapTris[i-pitch-2].verts[1].norm = normal; } if (i % pitch != 509) { mapTris[i-1].verts[1].norm = normal; } } }
Thanks



LinkBack URL
About LinkBacks



