1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_Models.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_MODELS_H_
17 #define DEMO_MODELS_H_
18 
19 #include <nn/types.h>
20 #include <nn/gx.h>
21 #include <nn/util/util_NonCopyable.h>
22 
23 namespace demo{
24 
25 namespace detail {
26     // The number of component elements in each vertex attribute for each vertex (all elements are defined as f32)
27     const int ATTR_NUM_POS    = 3;  // x, y, z   Data for 1 vertex contains three f32 elements
28     const int ATTR_NUM_NORMAL = 3;  // x, y, z
29     const int ATTR_NUM_COLOR  = 4;  // r, g, b, alpha
30     const int ATTR_NUM_TEX    = 2;  // s, t
31 }  // namespace detail
32 
33 /*
34  * Vertex attributes specified by Initialize function are placed in the following order.
35  * The vertex shader's input register settings must be matched.
36  */
37 
38 /*!
39     @brief Enumerated type representing vertex attributes specified during the initialization of objects
40 */
41     enum AttributeFlag {
42         ATTR_POSITION       = 0x01,
43         ATTR_NORMAL         = 0x02,
44         ATTR_VERTEX_COLOR   = 0x04,
45         ATTR_TEX_COORDINATE = 0x08
46     };
47 
48     /*! @class ModelBase demo_Models.h <nn/demo/demo_Models.h>
49 
50         @brief Base class for simple 3D model objects used in samples.
51     */
52     class ModelBase : private nn::util::NonCopyable<ModelBase> {
53     public:
54         /*!
55         @brief    Destroys the object.
56         */
Finalize()57         virtual void Finalize() { FinalizeImpl(); }
58 
59         /*!
60         @brief    Constructor.
61         */
62         ModelBase();
~ModelBase()63         virtual ~ModelBase() { FinalizeImpl(); }
64         /*!
65         @brief    Destructor.
66         */
67         virtual void Draw();
68 
69     private:
70         void FinalizeImpl();
71 
72     protected:
73         bool   m_Initialized;
74         u8     m_AttributeFlag;   // Vertex attributes applied to model data
75         u8     m_Padding[2];
76 
77         GLuint m_ArrayBufferObjectId;
78         GLuint m_ElementArrayBufferObjectId;
79 
80         size_t m_DataSize[4];
81         size_t m_IndexSize;       // Size of the vertex data (index)
82 
83         void InitializeCore(u16* indices, f32* attr1, f32* attr2, f32* attr3, f32* attr4);
84     };  // class ModelBase
85 
86     /*! @class Cube demo_Models.h <nn/demo/demo_Models.h>
87 
88         @brief Class for rendering cube objects for samples.
89 
90                You can specify the vertex attributes to use.
91     */
92     class CubeModel : public ModelBase {
93     public:
94         /*!
95         @brief    Initializes Cube objects.
96 
97                   Use the AttributeFlag enum to specify the vertex attributes the Cube object has.
98                   When the specified vertex attributes are rendered, they are passed to the vertex shader in the order defined in the AttributeFlag enum, not in the order they were specified.
99                   Always use this function once before using the class.
100 
101         @param[in]   flag      To assign vertex attributes to the Cube object, use a bitwise OR to specify members of the AttributeFlag enum
102         @param[in]   r         Vertex color's R value (only valid when vertex color is specified as a vertex attribute)
103         @param[in]   g         Vertex color's G value (only valid when vertex color is specified as a vertex attribute)
104         @param[in]   b         Vertex color's B value (only valid when vertex color is specified as a vertex attribute)
105         @param[in]   alpha              Vertex color's A value (only valid when vertex color is specified as a vertex attribute)
106         */
107         void Initialize(u8 flag, f32 r = 1.0f, f32 g = 1.0f, f32 b = 1.0f, f32 alpha = 1.0f);
108 
109         /*!
110         @brief    Destroys the object.
111         */
Finalize()112         virtual void Finalize() { ModelBase::Finalize(); }
113 
114         /*!
115         @brief    Constructor.
116         */
CubeModel()117         CubeModel() {}
118 
119         /*!
120         @brief    Destructor.
121         */
~CubeModel()122         virtual ~CubeModel() { }
123     };  // class Cube
124 
125 }
126 
127 #endif
128