1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_TrianglesRenderData.h
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 #ifndef DEMO_TRIANGLES_RENDER_DATA_H_
17 #define DEMO_TRIANGLES_RENDER_DATA_H_
18 
19 #include <nn/gx.h>
20 #include <nn/math.h>
21 
22 #include "demo/Utility/demo_MemoryManager.h"
23 
24 #include "demo/RenderData/demo_RenderData.h"
25 
26 namespace demo
27 {
28     /*!
29     :private
30 
31     @brief    Enumerated type that indicates vertex attributes
32     */
33     enum VertexAttributes
34     {
35         VERTEX_NONE_ATTRIBUTE = 0,            //!< indicates that vertex attributes have no attributes
36         VERTEX_POSITION_ATTRIBUTE = (1 << 0), //!< Position coordinate attributes of the vertex attributes
37         VERTEX_COLOR_ATTRIBUTE    = (1 << 1), //!< Vertex color attributes of the vertex attributes
38         VERTEX_TEXCOORD_ATTRIBUTE = (1 << 2), //!< Texture coordinate attributes of the vertex attributes
39         VERTEX_NORMAL_ATTRIBUTE   = (1 << 3), //!< Normal attributes of the vertex attributes
40         VERTEX_TANGENT_ATTRIBUTE  = (1 << 4)  //!< Tangent attributes of the vertex attributes
41     };
42 
43     /*!
44     :private
45 
46     @brief Number of elements for the vertex position coordinates (all elements in f32 format as xyzw)
47     */
48     const int VERTEX_POSITION_ATTRIBUTE_SIZE = 4;
49 
50     /*!
51     :private
52 
53     @brief Number of elements for the vertex color (all elements in f32 format as red, green, blue, and alpha)
54     */
55     const int VERTEX_COLOR_ATTRIBUTE_SIZE  = 4;
56 
57     /*!
58     :private
59 
60     @brief Number of elements for the vertex texture coordinates (all elements in f32 format as s, t, and r)
61     */
62     const int VERTEX_TEXCOORD_ATTRIBUTE_SIZE = 3;
63 
64     /*!
65     :private
66 
67     @brief Number of elements for the vertex normal (all elements in f32 format as nx, ny, and nz)
68     */
69     const int VERTEX_NORMAL_ATTRIBUTE_SIZE = 3;
70 
71     /*!
72     :private
73 
74     @brief Number of elements for the vertex tangent (all elements in f32 format as tx, ty, and tz)
75     */
76     const int VERTEX_TANGENT_ATTRIBUTE_SIZE = 3;
77 
78     /*!
79     :private
80 
81     @brief Maximum number of triangles that the TrianglesRenderData class can have as rendering data.
82     */
83     const u32 MAX_TRIANGLES_NUM = 8192;
84 
85     /*!
86     :private
87 
88     @brief Maximum number of quadrangles that the TrianglesRenderData class can have as rendering data
89     */
90     const u32 MAX_SQUARES_NUM = MAX_TRIANGLES_NUM / 2;
91 
92     /*!
93     :private
94 
95     @brief Maximum number of vertices for triangles that the TrianglesRenderData class can have as rendering data.
96     */
97     const u32 MAX_TRIANGLE_VERTICES_NUM = MAX_TRIANGLES_NUM * 3;
98 
99     /*!
100     :private
101 
102     @brief Maximum number of indices for triangles that the TrianglesRenderData class can have as rendering data
103     */
104     const u32 MAX_INDICES_NUM = MAX_TRIANGLES_NUM * 3;
105 
106     /*!
107     :private
108 
109     @brief Vertex buffer data type
110     */
111     const GLenum ARRAY_BUFFER_DATA_TYPE = GL_ARRAY_BUFFER;
112 
113     /*!
114     :private
115 
116     @brief Index buffer data type
117     */
118     const GLenum ELEMENT_ARRAY_BUFFER_DATA_TYPE = GL_ELEMENT_ARRAY_BUFFER;
119 
120     /*!
121     :private
122 
123     @brief Class for triangular mesh that is the rendering data.
124     */
125     class TrianglesRenderData : public RenderData
126     {
127     public:
128         /*!
129         :private
130 
131         @brief    Constructor.
132         */
133         TrianglesRenderData(void);
134 
135         /*!
136         :private
137 
138         @brief    Destructor.
139         */
140         virtual ~TrianglesRenderData(void);
141 
142     public:
143         /*!
144         :private
145 
146         @brief    Initializes the rendering data.
147         */
148         virtual void Initialize(void);
149 
150         /*!
151         :private
152 
153         @brief    Finalizes the rendering data.
154         */
155         virtual void Finalize(void);
156 
157     public:
158         /*!
159         :private
160 
161         @brief    Performs initialization for the vertex buffer.
162 
163         @param[in]    vertexAttributes    Bitwise OR of vertex attributes
164         @param[in]    triangleType        Type of triangle primitive to render
165         @param[in]    verticesNum         Total number of  the vertices of triangular primitive
166         @param[in]    trianglesNum        Total number of triangle primitives
167         */
168         void InitializeVertexBuffers(const u32 vertexAttributes,
169             const GLenum triangleType, const u32 verticesNum, const u32 trianglesNum);
170 
171     protected:
172         void SetVertexAttributes(const u32 vertexAttributes);
173         void SetTriangleType(const GLenum triangleType);
174         void SetVerticesNum(const u32 verticesNum);
175         void SetTrianglesNum(const u32 trianglesNum);
176         void CreateArrays(void);
177         void DestroyArrays(void);
178 
179     public:
180         /*!
181         :private
182 
183         @brief    Sets the number of packed vertices.
184 
185         @param[in]    packedVerticesNum    Number of packed vertices
186         */
187         void SetPackedVerticesNum(const u32 packedVerticesNum);
188 
189         /*!
190         :private
191 
192         @brief    Adds the number of packed vertices.
193 
194         @param[in]    packedVerticesNum    Number of packed vertices to add
195         */
196         void AddPackedVerticesNum(const u32 packedVerticesNum);
197 
198         /*!
199         :private
200 
201         @brief    Gets the number of packed vertices.
202 
203         @return   Number of packed vertices
204         */
205         u32 GetPackedVerticesNum(void) const;
206 
207     public:
208         /*!
209         :private
210 
211         @brief    Sets the number of packed triangles.
212 
213         @param[in]    packedTrianglesNum    Number of packed triangles
214         */
215         void SetPackedTrianglesNum(const u32 packedTrianglesNum);
216 
217         /*!
218         :private
219 
220         @brief    Adds the number of packed triangles.
221 
222         @param[in]    packedTrianglesNum    Number of packed triangles to add
223         */
224         void AddPackedTrianglesNum(const u32 packedTrianglesNum);
225 
226         /*!
227         :private
228 
229         @brief    Gets the number of packed triangles.
230 
231         @return   Number of packed triangles
232         */
233         u32 GetPackedTrianglesNum(void) const;
234 
235     public:
236         /*!
237         :private
238 
239         @brief    Renders the packed triangles.
240 
241         */
242         void DrawPackedTriangles(void);
243 
244     public:
245         /*!
246         :private
247 
248         @brief    Clears the number of packed triangles.
249         */
250         void ClearPackedNum(void);
251 
252         /*!
253         :private
254 
255         @brief    Based on the packed triangle data, finds the size of the vertex buffer to update.
256         */
257         void SetPackedArraysSize(void);
258 
259     public:
260         /*!
261         :private
262 
263         @brief    Performs update of the vertex buffer.
264         */
265         void UpdateBuffers(void);
266 
267         /*!
268         :private
269 
270         @brief    Sets whether to update the vertex buffer before each rendering.
271         */
272         void SetUpdateBufferBeforeDraw(const bool updateBufferFlag);
273     protected:
274         void DestroyBuffers(void);
275 
276     public:
277         /*!
278         :private
279 
280         @brief   Sets the position coordinates in the vertex matrix.
281 
282         @param[in]    index              Triangle index
283         @param[in]    positionX          X-coordinate of position coordinates
284         @param[in]    positionY          Y-coordinate of position coordinates
285         @param[in]    positionZ          Z-coordinate of position coordinates
286         */
287         void SetPosition(const u32 index,
288             const GLfloat positionX, const GLfloat positionY, const GLfloat positionZ);
289 
290         /*!
291         :private
292 
293         @brief   Sets the position coordinates in the vertex matrix.
294 
295         @param[in]    index              Triangle index
296         @param[in]    positionX          X-coordinate of position coordinates
297         @param[in]    positionY          Y-coordinate of position coordinates
298         @param[in]    positionZ          Z-coordinate of position coordinates
299         @param[in]    positionW          W-coordinate of position coordinates
300         */
301         void SetPosition(const u32 index,
302             const GLfloat positionX, const GLfloat positionY,
303             const GLfloat positionZ, const GLfloat positionW);
304 
305         /*!
306         :private
307 
308         @brief   Sets the vertex color in the vertex matrix.
309 
310         @param[in]    index              Triangle index
311         @param[in]    red          Red component of vertex color
312         @param[in]    green        Green component of vertex color
313         @param[in]    blue         Blue component of vertex color
314         */
315         void SetColor(const u32 index,
316             const GLfloat red, const GLfloat green, const GLfloat blue);
317 
318         /*!
319         :private
320 
321         @brief   Sets the vertex color in the vertex matrix.
322 
323         @param[in]    index              Triangle index
324         @param[in]    red          Red component of vertex color
325         @param[in]    green        Green component of vertex color
326         @param[in]    blue         Blue component of vertex color
327         @param[in]    alpha        Alpha component of vertex color
328         */
329         void SetColor(const u32 index,
330             const GLfloat red, const GLfloat green, const GLfloat blue, const GLfloat alpha);
331 
332         /*!
333         :private
334 
335         @brief   Sets the vertex color in the vertex matrix.
336 
337         @param[in]    red          Red component of vertex color
338         @param[in]    green        Green component of vertex color
339         @param[in]    blue         Blue component of vertex color
340         */
341         virtual void SetColor(const GLfloat red, const GLfloat green, const GLfloat blue);
342 
343         /*!
344         :private
345 
346         @brief   Sets the vertex color in the vertex matrix.
347 
348         @param[in]    index              Triangle index
349         @param[in]    red          Red component of vertex color
350         @param[in]    green        Green component of vertex color
351         @param[in]    blue         Blue component of vertex color
352         @param[in]    alpha        Alpha component of vertex color
353         */
354         virtual void SetColor(const GLfloat red, const GLfloat green, const GLfloat blue, const GLfloat alpha);
355 
356         /*!
357         :private
358 
359         @brief   Sets the texture coordinates in the vertex matrix.
360 
361         @param[in]    index              Triangle index
362         @param[in]    texcoordS          S-coordinate of the texture coordinates
363         @param[in]    texcoordT          T-coordinate of the texture coordinates
364         */
365         void SetTexcoord(const u32 index,
366             const GLfloat texcoordS, const GLfloat texcoordT);
367 
368         /*!
369         :private
370 
371         @brief   Sets the texture coordinates in the vertex matrix.
372 
373         @param[in]    index              Triangle index
374         @param[in]    texcoordS          S-coordinate of the texture coordinates
375         @param[in]    texcoordT          T-coordinate of the texture coordinates
376         @param[in]    texcoordR          R-coordinate of the texture coordinates
377         */
378         void SetTexcoord(const u32 index,
379             const GLfloat texcoordS, const GLfloat texcoordT, const GLfloat texcoordR);
380 
381         /*!
382         :private
383 
384         @brief   Sets the normal in the vertex matrix.
385 
386         @param[in]    index              Triangle index
387         @param[in]    normalX            X-coordinate of the normal
388         @param[in]    normalY            Y-coordinate of the normal
389         @param[in]    normalZ            Z-coordinate of the normal
390         */
391         void SetNormal(const u32 index,
392             const GLfloat normalX, const GLfloat normalY, const GLfloat normalZ);
393 
394         /*!
395         :private
396 
397         @brief   Sets the tangent in the vertex matrix.
398 
399         @param[in]    index              Triangle index
400         @param[in]    tangentX            X-coordinate of the tangent
401         @param[in]    tangentY            Y-coordinate of the tangent
402         @param[in]    tangentZ            Z-coordinate of the tangent
403         */
404         void SetTangent(const u32 index,
405             const GLfloat tangentX, const GLfloat tangentY, const GLfloat tangentZ);
406 
407         /*!
408         :private
409 
410         @brief   Sets the triangle index in the index matrix.
411 
412         @param[in]    index              Triangle index
413         @param[in]    index0             Index of the first vertex of the triangle
414         @param[in]    index1             Index of the second vertex of the triangle
415         @param[in]    index2             Index of the third vertex of the triangle
416         */
417         void SetIndex(const u32 index,
418             const GLuint index0, const GLuint index1, const GLuint index2);
419 
420         /*!
421         :private
422 
423         @brief   Sets the index to create a quadrangle with TRIANGLE_STRIP in the index matrix.
424 
425         @param[in]    index              Quadrangle index
426         @param[in]    index0             Index of the first vertex of the quadrangle
427         @param[in]    index1             Index of the second vertex of the quadrangle
428         @param[in]    index2             Index of the third vertex of the quadrangle
429         @param[in]    index3             Index of the fourth vertex of the quadrangle
430         */
431         void SetSquareIndex(const u32 square_index,
432             const GLuint index0, const GLuint index1,
433             const GLuint index2, const GLuint index3);
434 
435     public:
436         /*!
437         :private
438 
439         @brief    Sets the world coordinates.
440 
441         @param[in]    worldPositionX   X-coordinate of world coordinates
442         @param[in]    worldPositionY   Y-coordinate of world coordinates
443         @param[in]    worldPositionZ   Z-coordinate of world coordinates
444         */
445         void SetWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ);
446 
447         /*!
448         :private
449 
450         @brief    Parallel translate by only the world coordinate vectors.
451 
452         @param[in]    worldPositionX   X-coordinate of world coordinate vector
453         @param[in]    worldPositionY   Y-coordinate of world coordinate vector
454         @param[in]    worldPositionZ   Z-coordinate of world coordinate vector
455         */
456         void TranslateWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ);
457 
458         /*!
459         :private
460 
461         @brief    Gets the world coordinates.
462 
463         @param[out]    worldPositionX   X-coordinate of world coordinates
464         @param[out]    worldPositionY   Y-coordinate of world coordinates
465         @param[out]    worldPositionZ   Z-coordinate of world coordinates
466         */
467         void GetWorldPosition(f32& worldPositionX, f32& worldPositionY, f32& worldPositionZ);
468 
469         /*!
470         :private
471 
472         @brief    Sets the rotation angle in the world coordinate system.
473 
474         @param[in]    worldAngleX   Angle around the X-axis
475         @param[in]    worldAngleY   Angle around the Y-axis
476         @param[in]    worldAngleZ   Angle around the Z-axis
477         */
478         void SetWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ);
479 
480         /*!
481         :private
482 
483         @brief    Rotate by only rotation angle in the world coordinate system.
484 
485         @param[in]    worldAngleX   Angle around the X-axis in world coordinates
486         @param[in]    worldAngleY   Angle around the Y-axis in world coordinates
487         @param[in]    worldAngleZ   Angle around the Z-axis in world coordinates
488         */
489         void RotateWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ);
490 
491         /*!
492         :private
493 
494         @brief    Gets the rotation angle in the world coordinate system.
495 
496         @param[in]    worldAngleX   Angle around the X-axis
497         @param[in]    worldAngleY   Angle around the Y-axis
498         @param[in]    worldAngleZ   Angle around the Z-axis
499         */
500         void GetWorldAngle(f32& worldAngleX, f32& worldAngleY, f32& worldAngleZ);
501 
502     public:
503         /*!
504         :private
505 
506         @brief    Gets the world matrix.
507 
508         @return   World matrix
509         */
510         virtual nn::math::MTX44 GetWorldMatrix(void) const;
511 
512     public:
513         /*!
514         :private
515 
516         @brief    Performs rendering using the rendering data.
517         */
518         virtual void Draw(void);
519 
520     protected:
521         virtual void DrawElements(void);
522 
523     public:
524         /*!
525         :private
526 
527         @brief    Enables vertex attributes.
528         */
529         virtual void EnableVertexAttributes(void);
530 
531         /*!
532         :private
533 
534         @brief    Sets whether to enable vertex attributes before rendering.
535 
536         @param[in]    updateAttributesFlag  true if vertex attributes are enabled before rendering, false otherwise.
537         */
538         void SetEnableVertexAttributesBeforeDraw(const bool updateAttributesFlag);
539 
540     protected:
541         u32 m_ShaderType;
542         u32 m_VertexAttributes;
543         GLenum m_TriangleType;
544         u32 m_VerticesNum;
545         u32 m_TrianglesNum;
546         u32 m_PackedVerticesNum;
547         u32 m_PackedTrianglesNum;
548 
549     protected:
550         u32 m_PositionArraySize;
551         u32 m_ColorArraySize;
552         u32 m_TexcoordArraySize;
553         u32 m_NormalArraySize;
554         u32 m_TangentArraySize;
555         u32 m_TotalArraySize;
556         u32 m_OldTotalArraySize;
557 
558         u32 m_IndexArraySize;
559         u32 m_OldIndexArraySize;
560 
561     protected:
562         GLfloat* m_PositionArray;
563         GLfloat* m_ColorArray;
564         GLfloat* m_TexcoordArray;
565         GLfloat* m_NormalArray;
566         GLfloat* m_TangentArray;
567         GLushort* m_IndexArray;
568 
569     protected:
570         GLuint m_ArrayBufferId;
571         GLuint m_ElementArrayBufferId;
572 
573     protected:
574         bool m_InitArrayFlag;
575         bool m_InitBufferFlag;
576         bool m_UpdateBufferFlag;
577         bool m_UpdateAttributesFlag;
578 
579     protected:
580         f32 m_PositionX;
581         f32 m_PositionY;
582         f32 m_PositionZ;
583         f32 m_AngleX;
584         f32 m_AngleY;
585         f32 m_AngleZ;
586     };
587 
588 }
589 
590 #endif
591