| 119 | } |
| 120 | |
| 121 | Ref<SceneGraph::Node> SceneGraph::createTriangleSphere (const Vec3fa& center, const float radius, size_t N, Ref<MaterialNode> material) |
| 122 | { |
| 123 | unsigned numPhi = unsigned(N); |
| 124 | unsigned numTheta = 2*numPhi; |
| 125 | unsigned numVertices = numTheta*(numPhi+1); |
| 126 | Ref<SceneGraph::TriangleMeshNode> mesh = new SceneGraph::TriangleMeshNode(material,BBox1f(0,1),1); |
| 127 | mesh->positions[0].resize(numVertices); |
| 128 | |
| 129 | /* create sphere geometry */ |
| 130 | const float rcpNumTheta = rcp(float(numTheta)); |
| 131 | const float rcpNumPhi = rcp(float(numPhi)); |
| 132 | for (unsigned int phi=0; phi<=numPhi; phi++) |
| 133 | { |
| 134 | for (unsigned int theta=0; theta<numTheta; theta++) |
| 135 | { |
| 136 | const float phif = phi*float(pi)*rcpNumPhi; |
| 137 | const float thetaf = theta*2.0f*float(pi)*rcpNumTheta; |
| 138 | mesh->positions[0][phi*numTheta+theta].x = center.x + radius*sin(phif)*sin(thetaf); |
| 139 | mesh->positions[0][phi*numTheta+theta].y = center.y + radius*cos(phif); |
| 140 | mesh->positions[0][phi*numTheta+theta].z = center.z + radius*sin(phif)*cos(thetaf); |
| 141 | } |
| 142 | if (phi == 0) continue; |
| 143 | |
| 144 | if (phi == 1) |
| 145 | { |
| 146 | for (unsigned int theta=1; theta<=numTheta; theta++) |
| 147 | { |
| 148 | unsigned int p00 = numTheta-1; |
| 149 | unsigned int p10 = phi*numTheta+theta-1; |
| 150 | unsigned int p11 = phi*numTheta+theta%numTheta; |
| 151 | mesh->triangles.push_back(TriangleMeshNode::Triangle(p10,p00,p11)); |
| 152 | } |
| 153 | } |
| 154 | else if (phi == numPhi) |
| 155 | { |
| 156 | for (unsigned int theta=1; theta<=numTheta; theta++) |
| 157 | { |
| 158 | unsigned int p00 = (phi-1)*numTheta+theta-1; |
| 159 | unsigned int p01 = (phi-1)*numTheta+theta%numTheta; |
| 160 | unsigned int p10 = numPhi*numTheta; |
| 161 | mesh->triangles.push_back(TriangleMeshNode::Triangle(p10,p00,p01)); |
| 162 | } |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | for (unsigned int theta=1; theta<=numTheta; theta++) |
| 167 | { |
| 168 | unsigned int p00 = (phi-1)*numTheta+theta-1; |
| 169 | unsigned int p01 = (phi-1)*numTheta+theta%numTheta; |
| 170 | unsigned int p10 = phi*numTheta+theta-1; |
| 171 | unsigned int p11 = phi*numTheta+theta%numTheta; |
| 172 | mesh->triangles.push_back(TriangleMeshNode::Triangle(p10,p00,p11)); |
| 173 | mesh->triangles.push_back(TriangleMeshNode::Triangle(p01,p11,p00)); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | return mesh.dynamicCast<SceneGraph::Node>(); |
| 178 | } |