Changeset 1604 for trunk/src/easymesh
 Timestamp:
 Jul 8, 2012, 11:19:11 PM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/easymesh/easymesh.cpp
r1599 r1604 350 350 void EasyMesh::AppendSphere(int ndivisions, vec3 const &size) 351 351 { 352 ndivisions *= 2;353 354 352 int ibase = m_indices.Count(); 355 int vbase = m_vert.Count(); 356 357 vec3 d = size * 0.5f; 358 float const pi = acos(1.0f); 359 360 Array<vec2> table; 361 for (int i = 0; i <= ndivisions; i++) 362 table.Push(vec2(sin(pi * 2 / ndivisions * i) + 1e5f, 363 cos(pi * 2 / ndivisions * i) + 1e5f)); 364 365 for (int j = 0; j <= ndivisions / 2; j++) 366 for (int i = 0; i < ndivisions; i++) 367 { 368 int j2 = j + 1; 369 int i2 = (i + 1) % ndivisions; 370 371 AddVertex(d * vec3(table[i], 1.0f) * table[j].xxy); 372 AddVertex(d * vec3(table[i2], 1.0f) * table[j].xxy); 373 AddVertex(d * vec3(table[i2], 1.0f) * table[j2].xxy); 374 AddVertex(d * vec3(table[i], 1.0f) * table[j2].xxy); 353 354 Array<vec3> vertices; 355 356 float phi = 0.5f + 0.5f * sqrt(5.f); 357 for (int i = 0; i < 4; i++) 358 { 359 float x = (i & 1) ? 0.5f : 0.5f; 360 float y = (i & 2) ? phi * 0.5f : phi * 0.5f; 361 vertices << vec3(x, y, 0.f); 362 vertices << vec3(0.f, x, y); 363 vertices << vec3(y, 0.f, x); 364 } 365 366 static int const trilist[] = 367 { 368 0, 1, 2, 2, 4, 6, 3, 8, 1, 9, 4, 8, 369 7, 0, 5, 7, 11, 3, 10, 5, 6, 10, 9, 11, 370 371 0, 3, 1, 7, 3, 0, 1, 4, 2, 8, 4, 1, 372 2, 5, 0, 6, 5, 2, 6, 9, 10, 4, 9, 6, 373 7, 10, 11, 5, 10, 7, 8, 11, 9, 3, 11, 8 374 }; 375 376 for (unsigned i = 0; i < sizeof(trilist) / sizeof(*trilist); i += 3) 377 { 378 vec3 const &p0 = vertices[trilist[i]]; 379 vec3 const &p1 = vertices[trilist[i + 1]]; 380 vec3 const &p2 = vertices[trilist[i + 2]]; 381 382 vec3 const vx = 1.f / ndivisions * (p1  p0); 383 vec3 const vy = 1.f / ndivisions * (p2  p0); 384 385 int line = ndivisions + 1; 386 387 for (int v = 0, x = 0, y = 0; x < ndivisions + 1; v++) 388 { 389 vec3 p = p0 + x * vx + y * vy; 390 391 /* Add zero, one or two triangles */ 392 if (y < line  1) 393 { 394 AddVertex(normalize(p) * size); 395 AddVertex(normalize(p + vx) * size); 396 AddVertex(normalize(p + vy) * size); 397 AppendTriangle(0, 2, 1, m_vert.Count()  3); 398 } 399 400 if (y < line  2) 401 { 402 AddVertex(normalize(p + vx) * size); 403 AddVertex(normalize(p + vx + vy) * size); 404 AddVertex(normalize(p + vy) * size); 405 AppendTriangle(0, 2, 1, m_vert.Count()  3); 406 } 407 408 y++; 409 if (y == line) 410 { 411 x++; 412 y = 0; 413 line; 414 } 375 415 } 376 377 for (int i = vbase; i < m_vert.Count(); i += 4) 378 AppendQuad(0, 1, 2, 3, i); 416 } 379 417 380 418 ComputeNormals(ibase, m_indices.Count()  ibase);
Note: See TracChangeset
for help on using the changeset viewer.