1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: demo_TrianglesRenderData.cpp 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev: 46365 $ 14 *---------------------------------------------------------------------------*/ 15 16 #include "demo/RenderData/demo_TrianglesRenderData.h" 17 18 namespace demo 19 { 20 21 /* ------------------------------------------------------------------------ 22 TrianglesRenderData class member function 23 ------------------------------------------------------------------------ */ 24 TrianglesRenderData(void)25 TrianglesRenderData::TrianglesRenderData(void) : 26 RenderData(), 27 m_VertexAttributes(VERTEX_NONE_ATTRIBUTE), 28 m_TriangleType(GL_TRIANGLES), 29 m_VerticesNum(0), m_TrianglesNum(0), 30 m_PackedVerticesNum(0), m_PackedTrianglesNum(0), 31 m_PositionArraySize(0), 32 m_ColorArraySize(0), 33 m_TexcoordArraySize(0), 34 m_NormalArraySize(0), 35 m_TangentArraySize(0), 36 m_TotalArraySize(0), m_OldTotalArraySize(0), 37 m_IndexArraySize(0), m_OldIndexArraySize(0), 38 m_PositionArray(NULL), 39 m_ColorArray(NULL), 40 m_TexcoordArray(NULL), 41 m_NormalArray(NULL), 42 m_TangentArray(NULL), 43 m_IndexArray(NULL), 44 m_ArrayBufferId(0), m_ElementArrayBufferId(0), 45 m_InitArrayFlag(false), m_InitBufferFlag(false), 46 m_UpdateBufferFlag(true), m_UpdateAttributesFlag(true), 47 m_PositionX(0.0f), m_PositionY(0.0f), m_PositionZ(0.0f), 48 m_AngleX(0.0f), m_AngleY(0.0f), m_AngleZ(0.0f) 49 { 50 } 51 ~TrianglesRenderData(void)52 TrianglesRenderData::~TrianglesRenderData(void) 53 { 54 Finalize(); 55 } 56 Initialize(void)57 void TrianglesRenderData::Initialize(void) 58 { 59 } 60 Finalize(void)61 void TrianglesRenderData::Finalize(void) 62 { 63 DestroyArrays(); 64 DestroyBuffers(); 65 } 66 InitializeVertexBuffers(const u32 vertexAttributes,const GLenum triangleType,const u32 verticesNum,const u32 trianglesNum)67 void TrianglesRenderData::InitializeVertexBuffers(const u32 vertexAttributes, 68 const GLenum triangleType, const u32 verticesNum, const u32 trianglesNum) 69 { 70 SetVertexAttributes(vertexAttributes); 71 SetTriangleType(triangleType); 72 SetVerticesNum(verticesNum); 73 SetTrianglesNum(trianglesNum); 74 75 CreateArrays(); 76 SetPackedVerticesNum(0); 77 SetPackedTrianglesNum(0); 78 SetPackedArraysSize(); 79 } 80 SetVertexAttributes(const u32 vertexAttributes)81 void TrianglesRenderData::SetVertexAttributes(const u32 vertexAttributes) 82 { 83 m_VertexAttributes = vertexAttributes; 84 } 85 SetTriangleType(const GLenum triangleType)86 void TrianglesRenderData::SetTriangleType(const GLenum triangleType) 87 { 88 m_TriangleType = triangleType; 89 } 90 SetVerticesNum(const u32 verticesNum)91 void TrianglesRenderData::SetVerticesNum(const u32 verticesNum) 92 { 93 m_VerticesNum = verticesNum; 94 } 95 SetTrianglesNum(const u32 trianglesNum)96 void TrianglesRenderData::SetTrianglesNum(const u32 trianglesNum) 97 { 98 m_TrianglesNum = trianglesNum; 99 } 100 CreateArrays(void)101 void TrianglesRenderData::CreateArrays(void) 102 { 103 DestroyArrays(); 104 105 m_TotalArraySize = 0; 106 m_OldTotalArraySize = 0; 107 108 if ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) 109 { 110 m_PositionArray = (f32*)demo::Alloc(m_VerticesNum * 4 * 4); 111 if ( m_PositionArray == NULL ) 112 { 113 NN_TPANIC_("m_PositionArray is NULL.\n"); 114 } 115 116 for (u32 i = 0; i < m_VerticesNum; i++) 117 { 118 m_PositionArray[4 * i] = 0.0f; 119 m_PositionArray[4 * i + 1] = 0.0f; 120 m_PositionArray[4 * i + 2] = 0.0f; 121 m_PositionArray[4 * i + 3] = 1.0f; 122 } 123 } 124 125 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 126 { 127 m_ColorArray = (f32*)demo::Alloc(m_VerticesNum * 4 * 4); 128 if ( m_ColorArray == NULL ) 129 { 130 NN_TPANIC_("m_ColorArray is NULL.\n"); 131 } 132 133 for (u32 i = 0; i < m_VerticesNum; i++) 134 { 135 m_ColorArray[4 * i] = 0.0f; 136 m_ColorArray[4 * i + 1] = 0.0f; 137 m_ColorArray[4 * i + 2] = 0.0f; 138 m_ColorArray[4 * i + 3] = 1.0f; 139 } 140 } 141 142 if ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) 143 { 144 m_TexcoordArray = (f32*)demo::Alloc(m_VerticesNum * 3 * 4); 145 if ( m_TexcoordArray == NULL ) 146 { 147 NN_TPANIC_("m_TexcoordArray is NULL.\n"); 148 } 149 150 for (u32 i = 0; i < m_VerticesNum; i++) 151 { 152 m_TexcoordArray[3 * i] = 0.0f; 153 m_TexcoordArray[3 * i + 1] = 0.0f; 154 m_TexcoordArray[3 * i + 2] = 0.0f; 155 } 156 } 157 158 if ( m_VertexAttributes & VERTEX_NORMAL_ATTRIBUTE ) 159 { 160 m_NormalArray = (f32*)demo::Alloc(m_VerticesNum * 3 * 4); 161 if ( m_NormalArray == NULL ) 162 { 163 NN_TPANIC_("m_NormalArray is NULL.\n"); 164 } 165 166 for (u32 i = 0; i < m_VerticesNum; i++) 167 { 168 m_NormalArray[3 * i] = 0.0f; 169 m_NormalArray[3 * i + 1] = 0.0f; 170 m_NormalArray[3 * i + 2] = 1.0f; 171 } 172 } 173 174 if ( m_VertexAttributes & VERTEX_TANGENT_ATTRIBUTE ) 175 { 176 m_TangentArray = (f32*)demo::Alloc(m_VerticesNum * 3 * 4); 177 if ( m_TangentArray == NULL ) 178 { 179 NN_TPANIC_("m_TangentArray is NULL.\n"); 180 } 181 182 for (u32 i = 0; i < m_VerticesNum; i++) 183 { 184 m_TangentArray[3 * i] = 1.0f; 185 m_TangentArray[3 * i + 1] = 0.0f; 186 m_TangentArray[3 * i + 2] = 0.0f; 187 } 188 } 189 190 m_IndexArraySize = 0; 191 m_OldIndexArraySize = 0; 192 193 m_IndexArray = (GLushort*)demo::Alloc(m_TrianglesNum * 3 * 2); 194 if ( m_IndexArray == NULL ) 195 { 196 NN_TPANIC_("m_IndexArray is NULL.\n"); 197 } 198 199 for (u32 i = 0; i < m_TrianglesNum; i++) 200 { 201 m_IndexArray[3 * i] = 0; 202 m_IndexArray[3 * i + 1] = 0; 203 m_IndexArray[3 * i + 2] = 0; 204 } 205 206 m_InitArrayFlag = true; 207 } 208 DestroyArrays(void)209 void TrianglesRenderData::DestroyArrays(void) 210 { 211 if (! m_InitArrayFlag ) 212 { 213 return; 214 } 215 216 m_InitArrayFlag = false; 217 218 if ( m_PositionArray != NULL ) 219 { 220 demo::Free((void*) m_PositionArray); 221 m_PositionArray = NULL; 222 } 223 224 if ( m_ColorArray != NULL ) 225 { 226 demo::Free((void*) m_ColorArray); 227 m_ColorArray = NULL; 228 } 229 230 if ( m_TexcoordArray != NULL ) 231 { 232 demo::Free((void*) m_TexcoordArray); 233 m_TexcoordArray = NULL; 234 } 235 236 if ( m_NormalArray != NULL ) 237 { 238 demo::Free((void*) m_NormalArray); 239 m_NormalArray = NULL; 240 } 241 242 if ( m_TangentArray != NULL ) 243 { 244 demo::Free((void*) m_TangentArray); 245 m_TangentArray = NULL; 246 } 247 248 if ( m_IndexArray != NULL ) 249 { 250 demo::Free((void*) m_IndexArray); 251 m_IndexArray = NULL; 252 } 253 254 m_PositionArraySize = 0; 255 m_NormalArraySize = 0; 256 m_TexcoordArraySize = 0; 257 m_ColorArraySize = 0; 258 m_TangentArraySize = 0; 259 m_TotalArraySize = 0; 260 m_OldTotalArraySize = 0; 261 262 m_IndexArraySize = 0; 263 m_OldIndexArraySize = 0; 264 } 265 SetPackedVerticesNum(const u32 packedVerticesNum)266 void TrianglesRenderData::SetPackedVerticesNum(const u32 packedVerticesNum) 267 { 268 m_PackedVerticesNum = packedVerticesNum; 269 } 270 AddPackedVerticesNum(const u32 packedVerticesNum)271 void TrianglesRenderData::AddPackedVerticesNum(const u32 packedVerticesNum) 272 { 273 m_PackedVerticesNum += packedVerticesNum; 274 } 275 GetPackedVerticesNum(void) const276 u32 TrianglesRenderData::GetPackedVerticesNum(void) const 277 { 278 return m_PackedVerticesNum; 279 } 280 SetPackedTrianglesNum(const u32 packedTrianglesNum)281 void TrianglesRenderData::SetPackedTrianglesNum(const u32 packedTrianglesNum) 282 { 283 m_PackedTrianglesNum = packedTrianglesNum; 284 } 285 AddPackedTrianglesNum(const u32 packedTrianglesNum)286 void TrianglesRenderData::AddPackedTrianglesNum(const u32 packedTrianglesNum) 287 { 288 m_PackedTrianglesNum += packedTrianglesNum; 289 } 290 GetPackedTrianglesNum(void) const291 u32 TrianglesRenderData::GetPackedTrianglesNum(void) const 292 { 293 return m_PackedTrianglesNum; 294 } 295 DrawPackedTriangles(void)296 void TrianglesRenderData::DrawPackedTriangles(void) 297 { 298 SetPackedArraysSize(); 299 u32 packedTrianglesNum = GetPackedTrianglesNum(); 300 if ( packedTrianglesNum != 0 ) 301 { 302 Draw(); 303 } 304 ClearPackedNum(); 305 } 306 ClearPackedNum(void)307 void TrianglesRenderData::ClearPackedNum(void) 308 { 309 SetPackedVerticesNum(0); 310 SetPackedTrianglesNum(0); 311 } 312 SetPackedArraysSize(void)313 void TrianglesRenderData::SetPackedArraysSize(void) 314 { 315 m_OldTotalArraySize = m_TotalArraySize; 316 m_TotalArraySize = 0; 317 318 if ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) 319 { 320 m_PositionArraySize = m_PackedVerticesNum * VERTEX_POSITION_ATTRIBUTE_SIZE * 4; 321 m_TotalArraySize += m_PositionArraySize; 322 } 323 else 324 { 325 m_PositionArraySize = 0; 326 } 327 328 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 329 { 330 m_ColorArraySize = m_PackedVerticesNum * VERTEX_COLOR_ATTRIBUTE_SIZE * 4; 331 m_TotalArraySize += m_ColorArraySize; 332 } 333 else 334 { 335 m_ColorArraySize = 0; 336 } 337 338 if ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) 339 { 340 m_TexcoordArraySize = m_PackedVerticesNum * VERTEX_TEXCOORD_ATTRIBUTE_SIZE * 4; 341 m_TotalArraySize += m_TexcoordArraySize; 342 } 343 else 344 { 345 m_TexcoordArraySize = 0; 346 } 347 348 if ( m_VertexAttributes & VERTEX_NORMAL_ATTRIBUTE ) 349 { 350 m_NormalArraySize = m_PackedVerticesNum * VERTEX_NORMAL_ATTRIBUTE_SIZE * 4; 351 m_TotalArraySize += m_NormalArraySize; 352 } 353 else 354 { 355 m_NormalArraySize = 0; 356 } 357 358 if ( m_VertexAttributes & VERTEX_TANGENT_ATTRIBUTE ) 359 { 360 m_TangentArraySize = m_PackedVerticesNum * VERTEX_TANGENT_ATTRIBUTE_SIZE * 4; 361 m_TotalArraySize += m_TangentArraySize; 362 } 363 else 364 { 365 m_TangentArraySize = 0; 366 } 367 368 m_OldIndexArraySize = m_IndexArraySize; 369 m_IndexArraySize = m_PackedTrianglesNum * 3 * 2; 370 } 371 UpdateBuffers(void)372 void TrianglesRenderData::UpdateBuffers(void) 373 { 374 u32 array_offset = 0; 375 376 if ( m_ArrayBufferId == 0 ) 377 { 378 glGenBuffers(1, &m_ArrayBufferId); 379 DEMO_ASSERT_GL_ERROR(); 380 } 381 382 glBindBuffer(GL_ARRAY_BUFFER, m_ArrayBufferId); 383 384 // Allocate ARRAY_BUFFER 385 if ( m_TotalArraySize > m_OldTotalArraySize ) 386 { 387 glBufferData(ARRAY_BUFFER_DATA_TYPE, 388 m_TotalArraySize, 389 0, 390 GL_STATIC_DRAW); 391 392 m_OldTotalArraySize = m_TotalArraySize; 393 } 394 395 // Copy POSITION 396 if ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) 397 { 398 glBufferSubData(GL_ARRAY_BUFFER, 399 array_offset, 400 m_PositionArraySize, 401 m_PositionArray); 402 array_offset += m_PositionArraySize; 403 } 404 405 // Copy COLOR 406 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 407 { 408 glBufferSubData(GL_ARRAY_BUFFER, 409 array_offset, 410 m_ColorArraySize, 411 m_ColorArray); 412 array_offset += m_ColorArraySize; 413 } 414 415 // Copy TEXCOORD 416 if ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) 417 { 418 glBufferSubData(GL_ARRAY_BUFFER, 419 array_offset, 420 m_TexcoordArraySize, 421 m_TexcoordArray); 422 array_offset += m_TexcoordArraySize; 423 } 424 425 // Copy NORMAL 426 if ( m_VertexAttributes & VERTEX_NORMAL_ATTRIBUTE ) 427 { 428 glBufferSubData(GL_ARRAY_BUFFER, 429 array_offset, 430 m_NormalArraySize, 431 m_NormalArray); 432 array_offset += m_NormalArraySize; 433 } 434 435 // Copy TANGENT 436 if ( m_VertexAttributes & VERTEX_TANGENT_ATTRIBUTE ) 437 { 438 glBufferSubData(GL_ARRAY_BUFFER, 439 array_offset, 440 m_TangentArraySize, 441 m_TangentArray); 442 array_offset += m_TangentArraySize; 443 } 444 445 if ( m_ElementArrayBufferId == 0 ) 446 { 447 glGenBuffers(1, &m_ElementArrayBufferId); 448 DEMO_ASSERT_GL_ERROR(); 449 } 450 451 DEMO_ASSERT_GL_ERROR(); 452 453 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 454 m_ElementArrayBufferId); 455 456 // Allocate ELEMENT_ARRAY_BUFFER 457 if ( m_IndexArraySize > m_OldIndexArraySize ) 458 { 459 glBufferData(ELEMENT_ARRAY_BUFFER_DATA_TYPE, 460 m_IndexArraySize, 461 NULL, 462 GL_STATIC_DRAW); 463 m_OldIndexArraySize = m_IndexArraySize; 464 465 DEMO_ASSERT_GL_ERROR(); 466 } 467 468 // Copy ELEMENT_ARRAY_BUFFER 469 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 470 0, 471 m_IndexArraySize, 472 m_IndexArray); 473 474 DEMO_ASSERT_GL_ERROR(); 475 476 m_InitBufferFlag = true; 477 } 478 SetUpdateBufferBeforeDraw(const bool updateBufferFlag)479 void TrianglesRenderData::SetUpdateBufferBeforeDraw(const bool updateBufferFlag) 480 { 481 m_UpdateBufferFlag = updateBufferFlag; 482 } 483 DestroyBuffers(void)484 void TrianglesRenderData::DestroyBuffers(void) 485 { 486 if (! m_InitBufferFlag ) 487 { 488 return; 489 } 490 491 DEMO_ASSERT_GL_ERROR(); 492 493 if ( m_ArrayBufferId != 0 ) 494 { 495 glDeleteBuffers(1, &m_ArrayBufferId); 496 m_ArrayBufferId = 0; 497 } 498 499 if ( m_ElementArrayBufferId != 0 ) 500 { 501 glDeleteBuffers(1, &m_ElementArrayBufferId); 502 m_ElementArrayBufferId = 0; 503 } 504 505 DEMO_ASSERT_GL_ERROR(); 506 507 m_InitBufferFlag = false; 508 } 509 SetPosition(const u32 index,const GLfloat positionX,const GLfloat positionY,const GLfloat positionZ)510 void TrianglesRenderData::SetPosition(const u32 index, 511 const GLfloat positionX, const GLfloat positionY, const GLfloat positionZ) 512 { 513 if ( ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 514 { 515 m_PositionArray[4 * index] = positionX; 516 m_PositionArray[4 * index + 1] = positionY; 517 m_PositionArray[4 * index + 2] = positionZ; 518 m_PositionArray[4 * index + 3] = 1.0f; 519 } 520 else 521 { 522 NN_TPANIC_("Invalid SetPosition().\n"); 523 } 524 } 525 SetPosition(const u32 index,const GLfloat positionX,const GLfloat positionY,const GLfloat positionZ,const GLfloat positionW)526 void TrianglesRenderData::SetPosition(const u32 index, 527 const GLfloat positionX, const GLfloat positionY, 528 const GLfloat positionZ, const GLfloat positionW) 529 { 530 if ( ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 531 { 532 m_PositionArray[4 * index] = positionX; 533 m_PositionArray[4 * index + 1] = positionY; 534 m_PositionArray[4 * index + 2] = positionZ; 535 m_PositionArray[4 * index + 3] = positionW; 536 } 537 else 538 { 539 NN_TPANIC_("Invalid SetPosition().\n"); 540 } 541 } 542 SetColor(const u32 index,const GLfloat red,const GLfloat green,const GLfloat blue)543 void TrianglesRenderData::SetColor(const u32 index, 544 const GLfloat red, const GLfloat green, const GLfloat blue) 545 { 546 if ( ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 547 { 548 m_ColorArray[4 * index] = red; 549 m_ColorArray[4 * index + 1] = green; 550 m_ColorArray[4 * index + 2] = blue; 551 m_ColorArray[4 * index + 3] = 1.0f; 552 } 553 else 554 { 555 NN_TPANIC_("Invalid SetColor().\n"); 556 } 557 } 558 SetColor(const u32 index,const GLfloat red,const GLfloat green,const GLfloat blue,const GLfloat alpha)559 void TrianglesRenderData::SetColor(const u32 index, 560 const GLfloat red, const GLfloat green, const GLfloat blue, const GLfloat alpha) 561 { 562 if ( ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 563 { 564 m_ColorArray[4 * index] = red; 565 m_ColorArray[4 * index + 1] = green; 566 m_ColorArray[4 * index + 2] = blue; 567 m_ColorArray[4 * index + 3] = alpha; 568 } 569 else 570 { 571 NN_TPANIC_("Invalid SetColor().\n"); 572 } 573 } 574 SetColor(const GLfloat red,const GLfloat green,const GLfloat blue)575 void TrianglesRenderData::SetColor(const GLfloat red, const GLfloat green, const GLfloat blue) 576 { 577 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 578 { 579 for (u32 index = 0; index < m_VerticesNum; index++) 580 { 581 m_ColorArray[4 * index] = red; 582 m_ColorArray[4 * index + 1] = green; 583 m_ColorArray[4 * index + 2] = blue; 584 m_ColorArray[4 * index + 3] = 1.0f; 585 } 586 } 587 else 588 { 589 NN_TPANIC_("Invalid SetColor().\n"); 590 } 591 } 592 SetColor(const GLfloat red,const GLfloat green,const GLfloat blue,const GLfloat alpha)593 void TrianglesRenderData::SetColor(const GLfloat red, const GLfloat green, const GLfloat blue, 594 const GLfloat alpha) 595 { 596 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 597 { 598 for (u32 index = 0; index < m_VerticesNum; index++) 599 { 600 m_ColorArray[4 * index] = red; 601 m_ColorArray[4 * index + 1] = green; 602 m_ColorArray[4 * index + 2] = blue; 603 m_ColorArray[4 * index + 3] = alpha; 604 } 605 } 606 else 607 { 608 NN_TPANIC_("Invalid SetColor().\n"); 609 } 610 } 611 SetTexcoord(const u32 index,const GLfloat texcoordS,const GLfloat texcoordT)612 void TrianglesRenderData::SetTexcoord(const u32 index, 613 const GLfloat texcoordS, const GLfloat texcoordT) 614 { 615 if ( ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 616 { 617 m_TexcoordArray[3 * index] = texcoordS; 618 m_TexcoordArray[3 * index + 1] = texcoordT; 619 m_TexcoordArray[3 * index + 2] = 1.0f; 620 } 621 else 622 { 623 NN_TPANIC_("Invalid SetTexcoord().\n"); 624 } 625 } 626 SetTexcoord(const u32 index,const GLfloat texcoordS,const GLfloat texcoordT,const GLfloat texcoordR)627 void TrianglesRenderData::SetTexcoord(const u32 index, 628 const GLfloat texcoordS, const GLfloat texcoordT, const GLfloat texcoordR) 629 { 630 if ( ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 631 { 632 m_TexcoordArray[3 * index] = texcoordS; 633 m_TexcoordArray[3 * index + 1] = texcoordT; 634 m_TexcoordArray[3 * index + 2] = texcoordR; 635 } 636 else 637 { 638 NN_TPANIC_("Invalid SetTexcoord().\n"); 639 } 640 } 641 SetNormal(const u32 index,const GLfloat normalX,const GLfloat normalY,const GLfloat normalZ)642 void TrianglesRenderData::SetNormal(const u32 index, 643 const GLfloat normalX, const GLfloat normalY, const GLfloat normalZ) 644 { 645 if ( ( m_VertexAttributes & VERTEX_NORMAL_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 646 { 647 m_NormalArray[3 * index] = normalX; 648 m_NormalArray[3 * index + 1] = normalY; 649 m_NormalArray[3 * index + 2] = normalZ; 650 } 651 else 652 { 653 NN_TPANIC_("Invalid SetNormal().\n"); 654 } 655 } 656 SetTangent(const u32 index,const GLfloat tangentX,const GLfloat tangentY,const GLfloat tangentZ)657 void TrianglesRenderData::SetTangent(const u32 index, 658 const GLfloat tangentX, const GLfloat tangentY, const GLfloat tangentZ) 659 { 660 if ( ( m_VertexAttributes & VERTEX_TANGENT_ATTRIBUTE ) && ( index < m_VerticesNum ) ) 661 { 662 m_TangentArray[3 * index] = tangentX; 663 m_TangentArray[3 * index + 1] = tangentY; 664 m_TangentArray[3 * index + 2] = tangentZ; 665 } 666 else 667 { 668 NN_TPANIC_("Invalid SetTangent().\n"); 669 } 670 } 671 SetIndex(const u32 index,const GLuint index0,const GLuint index1,const GLuint index2)672 void TrianglesRenderData::SetIndex(const u32 index, 673 const GLuint index0, const GLuint index1, const GLuint index2) 674 { 675 if ( index < m_TrianglesNum ) 676 { 677 m_IndexArray[3 * index] = static_cast<GLushort>(index0); 678 m_IndexArray[3 * index + 1] = static_cast<GLushort>(index1); 679 m_IndexArray[3 * index + 2] = static_cast<GLushort>(index2); 680 } 681 else 682 { 683 NN_TPANIC_("Invalid SetIndex().\n"); 684 } 685 } 686 SetSquareIndex(const u32 squareIndex,const GLuint index0,const GLuint index1,const GLuint index2,const GLuint index3)687 void TrianglesRenderData::SetSquareIndex(const u32 squareIndex, 688 const GLuint index0, const GLuint index1, 689 const GLuint index2, const GLuint index3) 690 { 691 u32 squareIndex6 = 6 * squareIndex; 692 693 if ( squareIndex6 <= m_TrianglesNum ) 694 { 695 // Degenerate triangle 696 if ( squareIndex != 0 ) 697 { 698 m_IndexArray[squareIndex6 - 1] = static_cast<GLushort>(index0); 699 } 700 701 m_IndexArray[squareIndex6] = static_cast<GLushort>(index0); 702 m_IndexArray[squareIndex6 + 1] = static_cast<GLushort>(index1); 703 m_IndexArray[squareIndex6 + 2] = static_cast<GLushort>(index2); 704 m_IndexArray[squareIndex6 + 3] = static_cast<GLushort>(index3); 705 706 // Degenerate triangle 707 m_IndexArray[squareIndex6 + 4] = static_cast<GLushort>(index3); 708 m_IndexArray[squareIndex6 + 5] = static_cast<GLushort>(index3); 709 } 710 else 711 { 712 NN_TPANIC_("Invalid SetSquareIndex().\n"); 713 } 714 } 715 SetWorldPosition(const f32 worldPositionX,const f32 worldPositionY,const f32 worldPositionZ)716 void TrianglesRenderData::SetWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ) 717 { 718 m_PositionX = worldPositionX; 719 m_PositionY = worldPositionY; 720 m_PositionZ = worldPositionZ; 721 } 722 TranslateWorldPosition(const f32 worldPositionX,const f32 worldPositionY,const f32 worldPositionZ)723 void TrianglesRenderData::TranslateWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ) 724 { 725 m_PositionX += worldPositionX; 726 m_PositionY += worldPositionY; 727 m_PositionZ += worldPositionZ; 728 } 729 GetWorldPosition(f32 & worldPositionX,f32 & worldPositionY,f32 & worldPositionZ)730 void TrianglesRenderData::GetWorldPosition(f32& worldPositionX, f32& worldPositionY, f32& worldPositionZ) 731 { 732 worldPositionX = m_PositionX; 733 worldPositionY = m_PositionY; 734 worldPositionZ = m_PositionZ; 735 } 736 SetWorldAngle(const f32 worldAngleX,const f32 worldAngleY,const f32 worldAngleZ)737 void TrianglesRenderData::SetWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ) 738 { 739 m_AngleX = worldAngleX; 740 m_AngleY = worldAngleY; 741 m_AngleZ = worldAngleZ; 742 } 743 RotateWorldAngle(const f32 worldAngleX,const f32 worldAngleY,const f32 worldAngleZ)744 void TrianglesRenderData::RotateWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ) 745 { 746 m_AngleX += worldAngleX; 747 m_AngleY += worldAngleY; 748 m_AngleZ += worldAngleZ; 749 } 750 GetWorldAngle(f32 & worldAngleX,f32 & worldAngleY,f32 & worldAngleZ)751 void TrianglesRenderData::GetWorldAngle(f32& worldAngleX, f32& worldAngleY, f32& worldAngleZ) 752 { 753 worldAngleX = m_AngleX; 754 worldAngleY = m_AngleY; 755 worldAngleZ = m_AngleZ; 756 } 757 GetWorldMatrix(void) const758 nn::math::MTX44 TrianglesRenderData::GetWorldMatrix(void) const 759 { 760 nn::math::Vector3 positionVector(m_PositionX, m_PositionY, m_PositionZ); 761 nn::math::MTX34 positionMatrix; 762 MTX34Identity(&positionMatrix); 763 MTX34Translate(&positionMatrix, &positionVector); 764 nn::math::MTX44 worldMatrix(positionMatrix); 765 766 nn::math::MTX44 worldRotMatrix; 767 MTX44RotXYZDeg(&worldRotMatrix, m_AngleX, m_AngleY, m_AngleZ); 768 MTX44Mult(&worldMatrix, &worldMatrix, &worldRotMatrix); 769 770 return worldMatrix; 771 } 772 Draw(void)773 void TrianglesRenderData::Draw(void) 774 { 775 if (! m_InitArrayFlag ) 776 { 777 return; 778 } 779 780 if( m_UpdateBufferFlag ) 781 { 782 UpdateBuffers(); 783 } 784 785 // Setup vertex buffers 786 glBindBuffer(GL_ARRAY_BUFFER, m_ArrayBufferId); 787 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ElementArrayBufferId); 788 789 if ( m_UpdateAttributesFlag ) 790 { 791 EnableVertexAttributes(); 792 } 793 794 DrawElements(); 795 } 796 797 DrawElements(void)798 void TrianglesRenderData::DrawElements(void) 799 { 800 DEMO_ASSERT_GL_ERROR(); 801 802 if ( m_TriangleType == GL_TRIANGLES ) 803 { 804 u32 indices_num = m_PackedTrianglesNum * 3; 805 glDrawElements(GL_TRIANGLES, 806 indices_num, 807 GL_UNSIGNED_SHORT, 0); 808 } 809 else if ( m_TriangleType == GL_TRIANGLE_STRIP ) 810 { 811 u32 indices_num = 6 * (m_PackedTrianglesNum / 4); 812 glDrawElements(GL_TRIANGLE_STRIP, 813 indices_num, 814 GL_UNSIGNED_SHORT, 0); 815 } 816 817 DEMO_ASSERT_GL_ERROR(); 818 } 819 SetEnableVertexAttributesBeforeDraw(const bool updateAttributesFlag)820 void TrianglesRenderData::SetEnableVertexAttributesBeforeDraw(const bool updateAttributesFlag) 821 { 822 m_UpdateAttributesFlag = updateAttributesFlag; 823 } 824 EnableVertexAttributes(void)825 void TrianglesRenderData::EnableVertexAttributes(void) 826 { 827 u32 attrib_index = 0; 828 u32 array_offset = 0; 829 830 DEMO_ASSERT_GL_ERROR(); 831 832 // POSITION 833 if ( m_VertexAttributes & VERTEX_POSITION_ATTRIBUTE ) 834 { 835 glEnableVertexAttribArray(attrib_index); 836 glVertexAttribPointer(attrib_index, 837 VERTEX_POSITION_ATTRIBUTE_SIZE, 838 GL_FLOAT, GL_FALSE, 0, 0); 839 attrib_index += 1; 840 array_offset += m_PositionArraySize; 841 } 842 843 // COLOR 844 if ( m_VertexAttributes & VERTEX_COLOR_ATTRIBUTE ) 845 { 846 glEnableVertexAttribArray(attrib_index); 847 glVertexAttribPointer(attrib_index, 848 VERTEX_COLOR_ATTRIBUTE_SIZE, 849 GL_FLOAT, GL_FALSE, 0, 850 (GLvoid*)array_offset); 851 852 attrib_index += 1; 853 array_offset += m_ColorArraySize; 854 } 855 856 // TEXCOORD 857 if ( m_VertexAttributes & VERTEX_TEXCOORD_ATTRIBUTE ) 858 { 859 glEnableVertexAttribArray(attrib_index); 860 glVertexAttribPointer(attrib_index, 861 VERTEX_TEXCOORD_ATTRIBUTE_SIZE, 862 GL_FLOAT, GL_FALSE, 0, 863 (GLvoid*)array_offset); 864 865 attrib_index += 1; 866 array_offset += m_TexcoordArraySize; 867 } 868 869 // NORMAL 870 if ( m_VertexAttributes & VERTEX_NORMAL_ATTRIBUTE ) 871 { 872 glEnableVertexAttribArray(attrib_index); 873 glVertexAttribPointer(attrib_index, 874 VERTEX_NORMAL_ATTRIBUTE_SIZE, 875 GL_FLOAT, GL_FALSE, 0, 876 (GLvoid*)array_offset); 877 878 attrib_index += 1; 879 array_offset += m_NormalArraySize; 880 } 881 882 // TANGENT 883 if ( m_VertexAttributes & VERTEX_TANGENT_ATTRIBUTE ) 884 { 885 glEnableVertexAttribArray(attrib_index); 886 glVertexAttribPointer(attrib_index, 887 VERTEX_TANGENT_ATTRIBUTE_SIZE, 888 GL_FLOAT, GL_FALSE, 0, 889 (GLvoid*)array_offset); 890 891 attrib_index += 1; 892 array_offset += m_TangentArraySize; 893 } 894 895 for (u32 i = attrib_index; i < 8; i++) 896 { 897 glDisableVertexAttribArray(i); 898 } 899 900 DEMO_ASSERT_GL_ERROR(); 901 } 902 903 } 904