1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: demo_Sphere.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/Body/demo_Sphere.h" 17 18 namespace demo 19 { 20 /* ------------------------------------------------------------------------ 21 Sphere class member function 22 ------------------------------------------------------------------------ */ 23 Sphere(void)24 Sphere::Sphere(void) : 25 Body(), 26 m_Radius(0.5f), m_Division(3) 27 { 28 } 29 ~Sphere()30 Sphere::~Sphere() 31 { 32 } 33 Initialize(void)34 void Sphere::Initialize(void) 35 { 36 u32 vertexAttributes = demo::VERTEX_POSITION_ATTRIBUTE | 37 demo::VERTEX_COLOR_ATTRIBUTE | 38 demo::VERTEX_NORMAL_ATTRIBUTE; 39 InitializeSphere(vertexAttributes, 0.5f, 10); 40 } 41 InitializeSphere(const u32 vertexAttributes,const f32 radius,const u32 division)42 void Sphere::InitializeSphere(const u32 vertexAttributes, 43 const f32 radius, const u32 division) 44 { 45 if ( division == 0 ) 46 { 47 NN_TPANIC_("division is 0.\n"); 48 } 49 50 TrianglesRenderData::Initialize(); 51 DEMO_ASSERT_GL_ERROR(); 52 53 m_Radius = radius; 54 m_Division = division; 55 56 u32 circleVerticesNum = 4 * m_Division; 57 u32 verticesNum = circleVerticesNum * circleVerticesNum; 58 u32 trianglesNum = 2 * verticesNum; 59 InitializeVertexBuffers(vertexAttributes, GL_TRIANGLES, 60 verticesNum, trianglesNum); 61 62 SetPackedVerticesNum(verticesNum); 63 SetPackedTrianglesNum(trianglesNum); 64 SetPackedArraysSize(); 65 66 if ( m_VertexAttributes & demo::VERTEX_POSITION_ATTRIBUTE ) 67 { 68 u32 vertexIndex = 0; 69 f32 deltaTheta = 360.0f / (m_Division * 4.0f); 70 f32 deltaPhi = 360.0f / (m_Division * 4.0f); 71 72 for (u32 phiIndex = 0; phiIndex < circleVerticesNum; phiIndex++) 73 { 74 f32 phi = deltaPhi * phiIndex; 75 for (u32 thetaIndex = 0; thetaIndex < circleVerticesNum; thetaIndex++) 76 { 77 f32 sinPhi = 0.0f; 78 f32 cosPhi = 0.0f; 79 nn::math::SinCosDeg(&sinPhi, &cosPhi, phi); 80 81 f32 sinTheta = 0.0f; 82 f32 cosTheta = 0.0f; 83 f32 theta = deltaTheta * thetaIndex; 84 nn::math::SinCosDeg(&sinTheta, &cosTheta, theta); 85 86 nn::math::VEC3 position(m_Radius * cosPhi * sinTheta, 87 m_Radius * cosTheta, m_Radius * sinPhi * sinTheta); 88 89 if ( m_VertexAttributes & demo::VERTEX_NORMAL_ATTRIBUTE ) 90 { 91 nn::math::VEC3 normal(position); 92 nn::math::VEC3Normalize(&normal, &normal); 93 SetNormal(vertexIndex, normal.x, normal.y, normal.z); 94 } 95 SetPosition(vertexIndex, position.x, position.y, position.z); 96 97 vertexIndex += 1; 98 } 99 } 100 } 101 102 if ( m_VertexAttributes & demo::VERTEX_COLOR_ATTRIBUTE ) 103 { 104 demo::TrianglesRenderData::SetColor(1.0f, 0.0f, 0.0f, 1.0f); 105 } 106 107 if ( m_VertexAttributes & demo::VERTEX_TEXCOORD_ATTRIBUTE ) 108 { 109 u32 vertexIndex = 0; 110 f32 deltaST = 1.0f / circleVerticesNum; 111 for (u32 phiIndex = 0; phiIndex < circleVerticesNum; phiIndex++) 112 { 113 f32 s = deltaST * phiIndex; 114 115 for (u32 thetaIndex = 0; thetaIndex < circleVerticesNum; thetaIndex++) 116 { 117 f32 t = deltaST * thetaIndex; 118 119 SetTexcoord(vertexIndex, s, t); 120 vertexIndex += 1; 121 } 122 } 123 } 124 125 if ( m_VertexAttributes & demo::VERTEX_TANGENT_ATTRIBUTE ) 126 { 127 NN_TPANIC_("Tangent attribute is not implemented."); 128 } 129 130 u32 vertexIndex = 0; 131 for (u32 phiIndex = 0; phiIndex < circleVerticesNum; phiIndex++) 132 { 133 for (u32 thetaIndex = 0; thetaIndex < circleVerticesNum; thetaIndex++) 134 { 135 u32 index0 = thetaIndex + phiIndex * circleVerticesNum; 136 u32 index1 = (thetaIndex + 1) % circleVerticesNum + phiIndex * circleVerticesNum; 137 u32 index2 = (thetaIndex + (phiIndex + 1) * circleVerticesNum) % verticesNum; 138 u32 index3 = (thetaIndex + 1 + (phiIndex + 1) * circleVerticesNum) % verticesNum; 139 SetIndex(vertexIndex, index0, index1, index2); 140 vertexIndex += 1; 141 SetIndex(vertexIndex, index1, index3, index2); 142 vertexIndex += 1; 143 } 144 } 145 146 UpdateBuffers(); 147 SetUpdateBufferBeforeDraw(false); 148 149 DEMO_ASSERT_GL_ERROR(); 150 } 151 152 } 153