1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_Cylinder.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_Cylinder.h"
17 
18 namespace demo
19 {
20     /* ------------------------------------------------------------------------
21         Cylinder class  member function
22     ------------------------------------------------------------------------ */
23 
Cylinder(void)24     Cylinder::Cylinder(void) :
25     Body(),
26     m_Radius(0.5f), m_Height(0.5f), m_Division(3)
27     {
28     }
29 
~Cylinder()30     Cylinder::~Cylinder()
31     {
32     }
33 
Initialize(void)34     void Cylinder::Initialize(void)
35     {
36         u32 vertexAttributes = demo::VERTEX_POSITION_ATTRIBUTE |
37             demo::VERTEX_COLOR_ATTRIBUTE |
38             demo::VERTEX_NORMAL_ATTRIBUTE;
39         InitializeCylinder(vertexAttributes, 0.5f, 1.5f, 10);
40     }
41 
InitializeCylinder(const u32 vertexAttributes,const f32 radius,const f32 height,const u32 division)42     void Cylinder::InitializeCylinder(const u32 vertexAttributes,
43         const f32 radius, const f32 height, const u32 division)
44     {
45         TrianglesRenderData::Initialize();
46         DEMO_ASSERT_GL_ERROR();
47 
48         m_Radius = radius;
49         m_Height = height;
50         m_Division = division;
51 
52         f32 halfHeight = 0.5f * m_Height;
53 
54         u32 circleVerticesNum = 4 * m_Division;
55         u32 verticesNum = 6 * circleVerticesNum + 2;
56         u32 trianglesNum = 4 * circleVerticesNum;
57         InitializeVertexBuffers(vertexAttributes, GL_TRIANGLES,
58             verticesNum, trianglesNum);
59 
60         SetPackedVerticesNum(verticesNum);
61         SetPackedTrianglesNum(trianglesNum);
62         SetPackedArraysSize();
63 
64         u32 bottomVertexIndex = verticesNum - 2;
65         u32 topVertexIndex = verticesNum - 1;
66 
67         if ( m_VertexAttributes & demo::VERTEX_POSITION_ATTRIBUTE )
68         {
69             f32 deltaPhi = 360.0f / (m_Division * 4.0f);
70             for (u32 vertexIndex = 0; vertexIndex < circleVerticesNum; vertexIndex++)
71             {
72                 f32 phi = deltaPhi * vertexIndex;
73                 f32 sinValue = 0.0f;
74                 f32 cosValue = 0.0f;
75                 nn::math::SinCosDeg(&sinValue, &cosValue, phi);
76                 f32 x = m_Radius * sinValue;
77                 f32 y = (- halfHeight);
78                 f32 z = m_Radius * cosValue;
79 
80                 // -Y face
81                 SetPosition(vertexIndex, x, y, z);
82                 // side face bottom vertices
83                 SetPosition(2 * circleVerticesNum + vertexIndex,
84                     x, y, z);
85                 SetPosition(3 * circleVerticesNum + vertexIndex,
86                     x, y, z);
87 
88                 // +Y face
89                 y = halfHeight;
90                 SetPosition(circleVerticesNum + vertexIndex,
91                     x, y, z);
92                 // side face top vertices
93                 SetPosition(4 * circleVerticesNum + vertexIndex,
94                     x, y, z);
95                 SetPosition(5 * circleVerticesNum + vertexIndex,
96                     x, y, z);
97             }
98 
99             SetPosition(bottomVertexIndex,
100                 0.0f, (- halfHeight), 0.0f);
101             SetPosition(topVertexIndex,
102                 0.0f, halfHeight, 0.0f);
103         }
104 
105         if ( m_VertexAttributes & demo::VERTEX_COLOR_ATTRIBUTE )
106         {
107             demo::TrianglesRenderData::SetColor(1.0f, 0.0f, 0.0f, 1.0f);
108         }
109 
110         if ( m_VertexAttributes & demo::VERTEX_NORMAL_ATTRIBUTE )
111         {
112             f32 deltaPhi = 360.0f / (m_Division * 4.0f);
113             for (s32 vertexIndex = 0; vertexIndex < circleVerticesNum; vertexIndex++)
114             {
115                 f32 phi = static_cast<u32>((deltaPhi * (vertexIndex - 1) + 360.0f)) % 360;
116                 f32 sinValue0 = 0.0f;
117                 f32 cosValue0 = 0.0f;
118                 nn::math::SinCosDeg(&sinValue0, &cosValue0, phi);
119 
120                 phi = deltaPhi * vertexIndex;
121                 f32 sinValue1 = 0.0f;
122                 f32 cosValue1 = 0.0f;
123                 nn::math::SinCosDeg(&sinValue1, &cosValue1, phi);
124 
125                 phi = static_cast<u32>(deltaPhi * (vertexIndex + 1)) % 360;
126                 f32 sinValue2 = 0.0f;
127                 f32 cosValue2 = 0.0f;
128                 nn::math::SinCosDeg(&sinValue2, &cosValue2, phi);
129 
130                 nn::math::VEC3 normal0(m_Radius * (sinValue0 + sinValue1),
131                     0.0f, m_Radius * (cosValue0 + cosValue1) );
132                 nn::math::VEC3Normalize(&normal0, &normal0);
133 
134                 nn::math::VEC3 normal1(m_Radius * (sinValue1 + sinValue2),
135                     0.0f, m_Radius * (cosValue1 + cosValue2) );
136                 nn::math::VEC3Normalize(&normal1, &normal1);
137 
138                 // -Y face
139                 SetNormal(vertexIndex,
140                     0.0f, -1.0f, 0.0f);
141                 // side face bottom vertices
142                 SetNormal(2 * circleVerticesNum + vertexIndex,
143                     normal0.x, normal0.y, normal0.z);
144                 SetNormal(3 * circleVerticesNum + vertexIndex,
145                     normal1.x, normal1.y, normal1.z);
146 
147                 // +Y face
148                 SetNormal(circleVerticesNum + vertexIndex,
149                     0.0f, 1.0f, 0.0f);
150                 // side face top vertices
151                 SetNormal(4 * circleVerticesNum + vertexIndex,
152                     normal0.x, normal0.y, normal0.z);
153                 SetNormal(5 * circleVerticesNum + vertexIndex,
154                     normal1.x, normal1.y, normal1.z);
155             }
156 
157             SetNormal(bottomVertexIndex,
158                 0.0f, -1.0f, 0.0f);
159             SetNormal(topVertexIndex,
160                 0.0f, 1.0f, 0.0f);
161         }
162 
163         if ( m_VertexAttributes & demo::VERTEX_TEXCOORD_ATTRIBUTE )
164         {
165             NN_TPANIC_("Texcoord attribute is not implemented.");
166         }
167 
168         if ( m_VertexAttributes & demo::VERTEX_TANGENT_ATTRIBUTE )
169         {
170             NN_TPANIC_("Tangent attribute is not implemented.");
171         }
172 
173         for (u32 vertexIndex = 0; vertexIndex < circleVerticesNum; vertexIndex++)
174         {
175             // -Y face
176             u32 vertexIndex0 = vertexIndex;
177             u32 vertexIndex1 = (vertexIndex + 1) % circleVerticesNum;
178             SetIndex(vertexIndex,
179                 bottomVertexIndex, vertexIndex1, vertexIndex0);
180 
181             // +Y face
182             vertexIndex0 = vertexIndex + circleVerticesNum;
183             vertexIndex1 = (vertexIndex + 1) % circleVerticesNum + circleVerticesNum;
184             SetIndex(vertexIndex + circleVerticesNum,
185                 topVertexIndex, vertexIndex0, vertexIndex1);
186 
187             // side face
188             vertexIndex0 = 3 * circleVerticesNum + vertexIndex;
189             vertexIndex1 = 5 * circleVerticesNum + vertexIndex;
190             u32 vertexIndex2 = 2 * circleVerticesNum + (vertexIndex + 1) % circleVerticesNum;
191             u32 vertexIndex3 = 4 * circleVerticesNum + (vertexIndex + 1) % circleVerticesNum;
192 
193             u32 vertexOffset = 2 * circleVerticesNum;
194             SetIndex(vertexOffset + vertexIndex, vertexIndex0, vertexIndex3, vertexIndex1);
195 
196             vertexOffset = 3 * circleVerticesNum;
197             SetIndex(vertexOffset + vertexIndex, vertexIndex0, vertexIndex2, vertexIndex3);
198         }
199 
200         UpdateBuffers();
201         SetUpdateBufferBeforeDraw(false);
202 
203         DEMO_ASSERT_GL_ERROR();
204     }
205 
206 }
207