So I'm trying to generate terrain using marching cubes algorithm. At this point I'm implementing the diffuse lighting (fragment shader). I calculated normals for each vertex and got this: result
The left side of the picture displays the normals (for each vertex and triangle) and wireframe, to the right is the lighted landscape from the same camera angle.
so, i'm curious, what am I doing wrong?
I calculate normals this way:
for (int t = 0; t < all_triangles.size(); t++) {
Vertex v0 = all_vertices[triangle.get_vertex(0)];
Vertex v1 = all_vertices[triangle.get_vertex(1)];
Vertex v2 = all_vertices[triangle.get_vertex(2)];
QVector3D edge1 = v1 - v0;
QVector3D edge2 = v2 - v0;
QVector3D normal = QVector3D::crossProduct(edge1, edge2);
// triangle.set_normal(normal.normalized());
for (int v = 0; v < 3; v++) {
all_vertices[triangle.get_vertex(v)].add_normal(normal.normalized());
}
}
for (int v = 0; v < all_vertices.size(); v++) {
auto normal = all_vertices[v].get_normal();
normal.normalize();
all_vertices[v].set_normal(normal);
}
upd: vcs