Changeset 1507
 Timestamp:
 Jun 21, 2012, 1:41:04 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/orbital/mesh.h
r1506 r1507 7 7 // 8 8 9 /* TODO for this file:10 *  rename "AppendQuadVert" to "AddVertex" or something; it has nothing11 * to do with quads.12 */13 14 9 #if !defined __MESH_H__ 15 10 #define __MESH_H__ … … 23 18 : m_color(0), m_color2(0) 24 19 { 25 m_cursors.Push(0, 0 , 0);20 m_cursors.Push(0, 0); 26 21 } 27 22 … … 30 25 void OpenBrace() 31 26 { 32 m_cursors.Push(m_vert.Count(), m_ quadidx.Count(), m_triidx.Count());27 m_cursors.Push(m_vert.Count(), m_indices.Count()); 33 28 } 34 29 … … 65 60 66 61 Array<uint16_t> indexlist; 67 for (int i = 0; i < m_triidx.Count(); i += 3) 68 { 69 indexlist << m_triidx[i + 0]; 70 indexlist << m_triidx[i + 1]; 71 indexlist << m_triidx[i + 2]; 72 } 73 for (int i = 0; i < m_quadidx.Count(); i += 4) 74 { 75 indexlist << m_quadidx[i + 0]; 76 indexlist << m_quadidx[i + 1]; 77 indexlist << m_quadidx[i + 2]; 78 79 indexlist << m_quadidx[i + 0]; 80 indexlist << m_quadidx[i + 2]; 81 indexlist << m_quadidx[i + 3]; 62 for (int i = 0; i < m_indices.Count(); i += 3) 63 { 64 indexlist << m_indices[i + 0]; 65 indexlist << m_indices[i + 1]; 66 indexlist << m_indices[i + 2]; 82 67 } 83 68 … … 119 104 void SetCurColor2(vec4 const &color) { m_color2 = color; } 120 105 121 void A ppendQuadVert(vec3 const &coord)106 void AddVertex(vec3 const &coord) 122 107 { 123 108 m_vert.Push(coord, vec3(0.f, 1.f, 0.f), m_color); 124 109 } 125 110 126 void A ppendDuplicateQuadVert(int i)111 void AddDuplicateVertex(int i) 127 112 { 128 113 m_vert.Push(m_vert[i].m1, vec3(0.f, 1.f, 0.f), m_vert[i].m3); … … 131 116 void AppendQuad(int i1, int i2, int i3, int i4, int base) 132 117 { 133 m_quadidx << base + i1; 134 m_quadidx << base + i2; 135 m_quadidx << base + i3; 136 m_quadidx << base + i4; 118 m_indices << base + i1; 119 m_indices << base + i2; 120 m_indices << base + i3; 121 122 m_indices << base + i4; 123 m_indices << base + i1; 124 m_indices << base + i3; 137 125 } 138 126 139 127 void AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base) 140 128 { 141 m_quadidx << m_vert.Count(); AppendDuplicateQuadVert(base + i1); 142 m_quadidx << m_vert.Count(); AppendDuplicateQuadVert(base + i2); 143 m_quadidx << m_vert.Count(); AppendDuplicateQuadVert(base + i3); 144 m_quadidx << m_vert.Count(); AppendDuplicateQuadVert(base + i4); 129 m_indices << m_vert.Count(); AddDuplicateVertex(base + i1); 130 m_indices << m_vert.Count(); AddDuplicateVertex(base + i2); 131 m_indices << m_vert.Count(); AddDuplicateVertex(base + i3); 132 133 m_indices << m_vert.Count(); AddDuplicateVertex(base + i4); 134 m_indices << m_vert.Count(); AddDuplicateVertex(base + i1); 135 m_indices << m_vert.Count(); AddDuplicateVertex(base + i3); 145 136 } 146 137 147 138 void AppendTriangle(int i1, int i2, int i3, int base) 148 139 { 149 m_ triidx<< base + i1;150 m_ triidx<< base + i2;151 m_ triidx<< base + i3;140 m_indices << base + i1; 141 m_indices << base + i2; 142 m_indices << base + i3; 152 143 } 153 144 154 145 void AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base) 155 146 { 156 m_ triidx << m_vert.Count(); AppendDuplicateQuadVert(base + i1);157 m_ triidx << m_vert.Count(); AppendDuplicateQuadVert(base + i2);158 m_ triidx << m_vert.Count(); AppendDuplicateQuadVert(base + i3);159 } 160 161 void Compute QuadNormals(int start, int vcount)162 { 163 for (int i = 0; i < vcount; i += 4)164 { 165 vec3 v0 = m_vert[m_ quadidx[start + i + 2]].m1166  m_vert[m_ quadidx[start + i + 0]].m1;167 vec3 v1 = m_vert[m_ quadidx[start + i + 1]].m1168  m_vert[m_ quadidx[start + i + 0]].m1;147 m_indices << m_vert.Count(); AddDuplicateVertex(base + i1); 148 m_indices << m_vert.Count(); AddDuplicateVertex(base + i2); 149 m_indices << m_vert.Count(); AddDuplicateVertex(base + i3); 150 } 151 152 void ComputeNormals(int start, int vcount) 153 { 154 for (int i = 0; i < vcount; i += 3) 155 { 156 vec3 v0 = m_vert[m_indices[start + i + 2]].m1 157  m_vert[m_indices[start + i + 0]].m1; 158 vec3 v1 = m_vert[m_indices[start + i + 1]].m1 159  m_vert[m_indices[start + i + 0]].m1; 169 160 vec3 n = normalize(cross(v1, v0)); 170 161 171 for (int j = 0; j < 4; j++)172 m_vert[m_quadidx[start + i + j]].m2 = n;173 }174 }175 176 void ComputeTriNormals(int start, int vcount)177 {178 for (int i = 0; i < vcount; i += 3)179 {180 vec3 v0 = m_vert[m_triidx[start + i + 2]].m1181  m_vert[m_triidx[start + i + 0]].m1;182 vec3 v1 = m_vert[m_triidx[start + i + 1]].m1183  m_vert[m_triidx[start + i + 0]].m1;184 vec3 n = normalize(cross(v1, v0));185 186 162 for (int j = 0; j < 3; j++) 187 m_vert[m_ triidx[start + i + j]].m2 = n;163 m_vert[m_indices[start + i + j]].m2 = n; 188 164 } 189 165 } … … 266 242 if (s.x * s.y * s.z < 0) 267 243 { 268 for (int i = m_cursors.Last().m2; i < m_quadidx.Count(); i += 2) 269 { 270 uint16_t tmp = m_quadidx[i + 0]; 271 m_quadidx[i + 0] = m_quadidx[i + 1]; 272 m_quadidx[i + 1] = tmp; 273 } 274 275 for (int i = m_cursors.Last().m3; i < m_triidx.Count(); i += 3) 276 { 277 uint16_t tmp = m_triidx[i + 0]; 278 m_triidx[i + 0] = m_triidx[i + 1]; 279 m_triidx[i + 1] = tmp; 244 for (int i = m_cursors.Last().m2; i < m_indices.Count(); i += 3) 245 { 246 uint16_t tmp = m_indices[i + 0]; 247 m_indices[i + 0] = m_indices[i + 1]; 248 m_indices[i + 1] = tmp; 280 249 } 281 250 } … … 289 258 { 290 259 int vlen = m_vert.Count()  m_cursors.Last().m1; 291 int qlen = m_quadidx.Count()  m_cursors.Last().m2; 292 int tlen = m_triidx.Count()  m_cursors.Last().m3; 260 int tlen = m_indices.Count()  m_cursors.Last().m2; 293 261 294 262 for (int i = 0; i < vlen; i++) 295 263 m_vert << m_vert[m_cursors.Last().m1++]; 296 264 297 for (int i = 0; i < qlen; i++)298 m_quadidx << m_quadidx[m_cursors.Last().m2++] + vlen;299 300 265 for (int i = 0; i < tlen; i++) 301 m_ triidx << m_triidx[m_cursors.Last().m3++] + vlen;266 m_indices << m_indices[m_cursors.Last().m2++] + vlen; 302 267 303 268 Scale(s); 304 269 305 270 m_cursors.Last().m1 = vlen; 306 m_cursors.Last().m2 = qlen; 307 m_cursors.Last().m3 = tlen; 271 m_cursors.Last().m2 = tlen; 308 272 } 309 273 … … 327 291 for (int i = 0; i < nsides; i++) 328 292 { 329 A ppendQuadVert(p1); SetCurVertNormal(n);330 A ppendQuadVert(p2); SetCurVertNormal(n);293 AddVertex(p1); SetCurVertNormal(n); 294 AddVertex(p2); SetCurVertNormal(n); 331 295 SetCurVertColor(m_color2); 332 296 … … 344 308 if (!smooth) 345 309 { 346 A ppendQuadVert(p1); SetCurVertNormal(n);347 A ppendQuadVert(p2); SetCurVertNormal(n);310 AddVertex(p1); SetCurVertNormal(n); 311 AddVertex(p2); SetCurVertNormal(n); 348 312 SetCurVertColor(m_color2); 349 313 … … 361 325 ndivisions *= 2; 362 326 363 int ibase = m_ quadidx.Count();327 int ibase = m_indices.Count(); 364 328 int vbase = m_vert.Count(); 365 329 … … 378 342 int i2 = (i + 1) % ndivisions; 379 343 380 A ppendQuadVert(d * vec3(table[i], 1.0f) * table[j].xxy);381 A ppendQuadVert(d * vec3(table[i2], 1.0f) * table[j].xxy);382 A ppendQuadVert(d * vec3(table[i2], 1.0f) * table[j2].xxy);383 A ppendQuadVert(d * vec3(table[i], 1.0f) * table[j2].xxy);344 AddVertex(d * vec3(table[i], 1.0f) * table[j].xxy); 345 AddVertex(d * vec3(table[i2], 1.0f) * table[j].xxy); 346 AddVertex(d * vec3(table[i2], 1.0f) * table[j2].xxy); 347 AddVertex(d * vec3(table[i], 1.0f) * table[j2].xxy); 384 348 } 385 349 … … 387 351 AppendQuad(0, 1, 2, 3, i); 388 352 389 Compute QuadNormals(ibase, m_quadidx.Count()  ibase);353 ComputeNormals(ibase, m_indices.Count()  ibase); 390 354 } 391 355 … … 407 371 void AppendBox(vec3 const &size, float chamf, bool smooth) 408 372 { 409 if (chamf < 0.0f) 410 { 411 AppendBox(size + vec3(chamf * 2.0f), chamf, smooth); 412 return; 413 } 373 if (chamf < 0.0f) 374 { 375 AppendBox(size + vec3(chamf * 2.0f), chamf, smooth); 376 return; 377 } 378 414 379 int vbase = m_vert.Count(); 415 int qibase = m_quadidx.Count(); 416 int tibase = m_triidx.Count(); 380 int ibase = m_indices.Count(); 417 381 418 382 vec3 d = size * 0.5f; 419 383 420 A ppendQuadVert(vec3(d.x, d.y, d.z  chamf));421 A ppendQuadVert(vec3(d.x, +d.y, d.z  chamf));422 A ppendQuadVert(vec3(+d.x, +d.y, d.z  chamf));423 A ppendQuadVert(vec3(+d.x, d.y, d.z  chamf));424 425 A ppendQuadVert(vec3(d.x  chamf, d.y, +d.z));426 A ppendQuadVert(vec3(d.x  chamf, +d.y, +d.z));427 A ppendQuadVert(vec3(d.x  chamf, +d.y, d.z));428 A ppendQuadVert(vec3(d.x  chamf, d.y, d.z));429 430 A ppendQuadVert(vec3(+d.x, d.y, +d.z + chamf));431 A ppendQuadVert(vec3(+d.x, +d.y, +d.z + chamf));432 A ppendQuadVert(vec3(d.x, +d.y, +d.z + chamf));433 A ppendQuadVert(vec3(d.x, d.y, +d.z + chamf));434 435 A ppendQuadVert(vec3(+d.x + chamf, d.y, d.z));436 A ppendQuadVert(vec3(+d.x + chamf, +d.y, d.z));437 A ppendQuadVert(vec3(+d.x + chamf, +d.y, +d.z));438 A ppendQuadVert(vec3(+d.x + chamf, d.y, +d.z));439 440 A ppendQuadVert(vec3(d.x, d.y  chamf, +d.z));441 A ppendQuadVert(vec3(d.x, d.y  chamf, d.z));442 A ppendQuadVert(vec3(+d.x, d.y  chamf, d.z));443 A ppendQuadVert(vec3(+d.x, d.y  chamf, +d.z));444 445 A ppendQuadVert(vec3(d.x, +d.y + chamf, d.z));446 A ppendQuadVert(vec3(d.x, +d.y + chamf, +d.z));447 A ppendQuadVert(vec3(+d.x, +d.y + chamf, +d.z));448 A ppendQuadVert(vec3(+d.x, +d.y + chamf, d.z));384 AddVertex(vec3(d.x, d.y, d.z  chamf)); 385 AddVertex(vec3(d.x, +d.y, d.z  chamf)); 386 AddVertex(vec3(+d.x, +d.y, d.z  chamf)); 387 AddVertex(vec3(+d.x, d.y, d.z  chamf)); 388 389 AddVertex(vec3(d.x  chamf, d.y, +d.z)); 390 AddVertex(vec3(d.x  chamf, +d.y, +d.z)); 391 AddVertex(vec3(d.x  chamf, +d.y, d.z)); 392 AddVertex(vec3(d.x  chamf, d.y, d.z)); 393 394 AddVertex(vec3(+d.x, d.y, +d.z + chamf)); 395 AddVertex(vec3(+d.x, +d.y, +d.z + chamf)); 396 AddVertex(vec3(d.x, +d.y, +d.z + chamf)); 397 AddVertex(vec3(d.x, d.y, +d.z + chamf)); 398 399 AddVertex(vec3(+d.x + chamf, d.y, d.z)); 400 AddVertex(vec3(+d.x + chamf, +d.y, d.z)); 401 AddVertex(vec3(+d.x + chamf, +d.y, +d.z)); 402 AddVertex(vec3(+d.x + chamf, d.y, +d.z)); 403 404 AddVertex(vec3(d.x, d.y  chamf, +d.z)); 405 AddVertex(vec3(d.x, d.y  chamf, d.z)); 406 AddVertex(vec3(+d.x, d.y  chamf, d.z)); 407 AddVertex(vec3(+d.x, d.y  chamf, +d.z)); 408 409 AddVertex(vec3(d.x, +d.y + chamf, d.z)); 410 AddVertex(vec3(d.x, +d.y + chamf, +d.z)); 411 AddVertex(vec3(+d.x, +d.y + chamf, +d.z)); 412 AddVertex(vec3(+d.x, +d.y + chamf, d.z)); 449 413 450 414 /* The 6 quads on each side of the box */ … … 452 416 AppendQuad(i, i + 1, i + 2, i + 3, vbase); 453 417 454 Compute QuadNormals(qibase, m_quadidx.Count()  qibase);455 qibase = m_quadidx.Count();418 ComputeNormals(ibase, m_indices.Count()  ibase); 419 ibase = m_indices.Count(); 456 420 457 421 /* The 8 quads at each edge of the box */ … … 497 461 498 462 if (!smooth) 499 { 500 ComputeQuadNormals(qibase, m_quadidx.Count()  qibase); 501 ComputeTriNormals(tibase, m_triidx.Count()  tibase); 502 } 463 ComputeNormals(ibase, m_indices.Count()  ibase); 503 464 } 504 465 … … 508 469 int vbase = m_vert.Count(); 509 470 510 A ppendQuadVert(vec3(0.f, 0.f, 0.f));471 AddVertex(vec3(0.f, 0.f, 0.f)); 511 472 512 473 mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f); … … 518 479 for (int i = 0; i < nbranches; i++) 519 480 { 520 A ppendQuadVert(p1);481 AddVertex(p1); 521 482 if (fade2) 522 483 SetCurVertColor(m_color2); 523 484 524 A ppendQuadVert(p2);485 AddVertex(p2); 525 486 if (fade) 526 487 SetCurVertColor(m_color2); … … 538 499 int vbase = m_vert.Count(); 539 500 540 A ppendQuadVert(vec3(0.f, 0.f, 0.f));501 AddVertex(vec3(0.f, 0.f, 0.f)); 541 502 542 503 mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f); … … 550 511 for (int i = 0; i < nbranches; i++) 551 512 { 552 A ppendQuadVert(p1);553 A ppendQuadVert(p2);554 A ppendQuadVert(p3); SetCurVertColor(m_color2);555 A ppendQuadVert(p4); SetCurVertColor(m_color2);513 AddVertex(p1); 514 AddVertex(p2); 515 AddVertex(p3); SetCurVertColor(m_color2); 516 AddVertex(p4); SetCurVertColor(m_color2); 556 517 557 518 int j = (i + 1) % nbranches; … … 571 532 int vbase = m_vert.Count(); 572 533 573 A ppendQuadVert(vec3(0.f, 0.f, 0.f));534 AddVertex(vec3(0.f, 0.f, 0.f)); 574 535 575 536 mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f); … … 578 539 for (int i = 0; i < nsides; i++) 579 540 { 580 A ppendQuadVert(p1);541 AddVertex(p1); 581 542 if (fade) 582 543 SetCurVertColor(m_color2); … … 591 552 vec3 p(0.f, 0.f, size); 592 553 593 A ppendQuadVert(p);554 AddVertex(p); 594 555 p = m * p; 595 A ppendQuadVert(p);556 AddVertex(p); 596 557 if (fade) 597 558 SetCurVertColor(m_color2); 598 559 p = m * p; 599 A ppendQuadVert(p);560 AddVertex(p); 600 561 if (fade) 601 562 SetCurVertColor(m_color2); … … 611 572 void AppendSimpleQuad(vec2 p1, vec2 p2, float z = 0.f, int fade = 0) 612 573 { 613 A ppendQuadVert(vec3(p2.x, z, p1.y));614 A ppendQuadVert(vec3(p2.x, z, p2.y));615 A ppendQuadVert(vec3(p1.x, z, p2.y));574 AddVertex(vec3(p2.x, z, p1.y)); 575 AddVertex(vec3(p2.x, z, p2.y)); 576 AddVertex(vec3(p1.x, z, p2.y)); 616 577 if (fade) 617 578 SetCurVertColor(m_color2); 618 A ppendQuadVert(vec3(p1.x, z, p1.y));579 AddVertex(vec3(p1.x, z, p1.y)); 619 580 if (fade) 620 581 SetCurVertColor(m_color2); 621 582 622 583 AppendQuad(3, 2, 1, 0, m_vert.Count()  4); 623 Compute QuadNormals(m_quadidx.Count()  4, 4);584 ComputeNormals(m_indices.Count()  6, 6); 624 585 } 625 586 … … 627 588 float r12, float r22, float sidemul, int offset) 628 589 { 629 int qibase = m_quadidx.Count();590 int ibase = m_indices.Count(); 630 591 int vbase = m_vert.Count(); 631 592 632 A ppendQuadVert(vec3(0.f, h * .5f, 0.f));633 A ppendQuadVert(vec3(0.f, h * .5f, 0.f));593 AddVertex(vec3(0.f, h * .5f, 0.f)); 594 AddVertex(vec3(0.f, h * .5f, 0.f)); 634 595 SetCurVertColor(m_color2); 635 596 … … 662 623 for (int n = 0; n < 24; n++) 663 624 { 664 A ppendQuadVert(p[n / 3]);625 AddVertex(p[n / 3]); 665 626 if (n / 3 >= 4) 666 627 SetCurVertColor(m_color2); … … 685 646 } 686 647 687 Compute QuadNormals(qibase, m_quadidx.Count()  qibase);648 ComputeNormals(ibase, m_indices.Count()  ibase); 688 649 } 689 650 … … 695 656 private: 696 657 vec4 m_color, m_color2; 697 Array<uint16_t> m_ triidx, m_quadidx;658 Array<uint16_t> m_indices; 698 659 Array<vec3, vec3, vec4> m_vert; 699 Array<int, int , int> m_cursors;660 Array<int, int> m_cursors; 700 661 701 662 /* FIXME: put this in a separate class so that we can copy meshes. */
Note: See TracChangeset
for help on using the changeset viewer.