diff --git a/src/planet/PlanetFace.cpp b/src/planet/PlanetFace.cpp index af8f8bf..3b63715 100644 --- a/src/planet/PlanetFace.cpp +++ b/src/planet/PlanetFace.cpp @@ -4,11 +4,17 @@ // Correlates directly to Face enum const glm::vec3 FACE_NORMALS[6] = { + // FACE_BOTTOM glm::vec3(0.0f, -1.0f, 0.0f), + // FACE_TOP glm::vec3(0.0f, 1.0f, 0.0f), + // FACE_LEFT glm::vec3(-1.0f, 0.0f, 0.0f), + // FACE_RIGHT glm::vec3(1.0f, 0.0f, 0.0f), + // FACE_FRONT glm::vec3(0.0f, 0.0f, -1.0f), + // FACE_BACK glm::vec3(0.0f, 0.0f, 1.0f) }; @@ -16,6 +22,9 @@ PlanetFaceNode::PlanetFaceNode(Planet* planet, PlanetFace* face, glm::vec3 posit m_planet(planet), m_planetFace(face), m_pos(position), m_index(index), m_level(level), m_generated(false), m_dirty(true), m_leaf(true) { glm::vec3 normal = face->getNormal(); + + // normal 0 1 0 left 1 0 0 forward 0 0 -1 + // normal 0 0 -1 left 0 -1 0 forward -1 0 0 m_left = glm::vec3(normal.y, normal.z, normal.x); m_forward = glm::cross(normal, m_left); @@ -100,17 +109,21 @@ void PlanetFaceNode::generate() { for (int j = 0; j < RESOLUTION; j++) { + // Get the 2D index of the vertex on the plane from zero to one (1 = RESOLUTION - 1) glm::vec2 index = glm::vec2(i, j) / (RESOLUTION - 1.0f); - // From the left and forward vectors, we can calculate an oriented unit vertex + // Generate the vertices on the plane using left and forward vectors. + // here 2 * index - 1 is used to convert 0 - 1 to -1 - 1, that is to + // generate the vertices starting from the center point of the unit plane. glm::vec3 iv = (m_forward * (2.0f * index.x - 1.0f)) / divisionLevel; glm::vec3 jv = (m_left * (2.0f * index.y - 1.0f)) / divisionLevel; - // Add the unit left and forward vectors to the origin + // Add the unit left and forward vectors to the origin, m_pos here + // being the center point, which in division level zero is the offset + // normal from the center of the cube. glm::vec3 vertex = m_pos + jv + iv; - // Normalize and multiply by radius to create a spherical mesh - // glm::vec3 vertNormal = glm::normalize(vertex); + // Normalize and multiply by radius to create a spherical mesh (unit sphere) float x2 = vertex.x * vertex.x; float y2 = vertex.y * vertex.y; float z2 = vertex.z * vertex.z; @@ -120,7 +133,9 @@ void PlanetFaceNode::generate() vertex.z * sqrt(1.0f - ((x2 + y2) / 2.0f) + ((x2 * y2) / 3.0f)) ); - glm::vec3 pos = -(m_planet->getNoise().fractal(8, point.x, point.y, point.z) * 20.0f + radius) * point; + // Get noise height and multiply by radius + float height = m_planet->getNoise().fractal(8, point.x, point.y, point.z) * 20.0f; + glm::vec3 pos = -(height + radius) * point; // Add vertex vertices.push_back({ pos, point, glm::vec2(j * (1.0 / RESOLUTION), i * (1.0 / RESOLUTION)) }); @@ -150,6 +165,7 @@ bool PlanetFaceNode::subdivide() int lv = m_level + 1; + // Calculate distance to move the vertices on the unit plane based on division level glm::vec3 stepLeft = m_left * (1.0f / (float)pow(2, lv)); glm::vec3 stepForward = m_forward * (1.0f / (float)pow(2, lv));