1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_GraphicsDrawing.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_DEMO_GRAPHICSDRAWING_H_
19 #define NW_DEMO_GRAPHICSDRAWING_H_
20 
21 #include <GLES2/gl2.h>
22 
23 #include <nw/types.h>
24 #include <nw/ut/ut_Color.h>
25 #include <nw/math/math_Types.h>
26 #include <nw/font/font_TextWriter.h>
27 #include <nw/font/font_ResFont.h>
28 #include <nw/font/font_RectDrawer.h>
29 
30 #include <nn/gr.h>
31 
32 namespace nw {
33 
34 namespace os {
35 class IAllocator;
36 }
37 
38 namespace font {
39 class Font;
40 }
41 
42 namespace demo {
43 
44 //---------------------------------------------------------------------------
45 //! @brief デモ用の文字やプリミティブを描画するためのクラスです。
46 //---------------------------------------------------------------------------
47 class GraphicsDrawing
48 {
49 public:
50     //----------------------------------------
51     //! @name コンストラクタ/デストラクタ
52     //@{
53 
54     //---------------------------------------------------------------------------
55     //! @brief        スクリーンサイズを設定します。
56     //!
57     //! @param[in]    width スクリーンの横幅です。
58     //! @param[in]    height スクリーンの縦幅です。
59     //---------------------------------------------------------------------------
SetScreenSize(s32 width,s32 height)60     void SetScreenSize( s32 width, s32 height )
61     {
62         m_ScreenSize = math::VEC2( static_cast<f32>(width), static_cast<f32>(height) );
63     }
64 
65     //---------------------------------------------------------------------------
66     //! @brief        コンストラクタです。
67     //---------------------------------------------------------------------------
68     GraphicsDrawing();
69 
70     //---------------------------------------------------------------------------
71     //! @brief        デストラクタです。
72     //---------------------------------------------------------------------------
73     ~GraphicsDrawing();
74 
75     //@}
76 
77     //---------------------------------------------------------------------------
78     //! @brief        リソースの破棄をおこないます。
79     //---------------------------------------------------------------------------
80     void Finalize();
81 
82     //========================================================================================
83     //! @name シェイプ描画
84     //@{
85 
86     //---------------------------------------------------------------------------
87     //! @brief        シェイプ描画の初期化をおこないます。
88     //!
89     //! @param[in]    allocator     アロケータです。
90     //! @param[in]    shaderPath    シェーダバイナリのファイルパスです。
91     //!
92     //! @return       初期化に成功した場合は true, 失敗した場合は false を返します。
93     //---------------------------------------------------------------------------
94     bool InitializeShape( os::IAllocator* allocator, const wchar_t* shaderPath );
95 
96     //---------------------------------------------------------------------------
97     //! @brief        シェイプ描画用のシェーダの初期化をおこないます。
98     //!
99     //! @param[in]    pShaderBinary シェイプシェーダバイナリのポインタです。
100     //! @param[in]    binarySize    シェイプシェーダバイナリのサイズです。
101     //!
102     //! @return       初期化に成功した場合は true, 失敗した場合は false を返します。
103     //---------------------------------------------------------------------------
104     bool InitializeShape( void* pShaderBinary, size_t binarySize );
105 
106     //---------------------------------------------------------------------------
107     //! @brief        シェイプのリソースの破棄をおこないます。
108     //---------------------------------------------------------------------------
109     void DestroyShape();
110 
111     //---------------------------------------------------------------------------
112     //! @brief        シェイプ描画用のセットアップをおこないます。
113     //---------------------------------------------------------------------------
114     void SetupShape();
115 
116     //---------------------------------------------------------------------------
117     //! @brief        シェイプ描画で描画可能な最大頂点数を設定します。
118     //!
119     //! @param[in]    maxShapeVertexCount 最大頂点数です。
120     //---------------------------------------------------------------------------
SetMaxShapeVertexCount(s32 maxShapeVertexCount)121     void SetMaxShapeVertexCount( s32 maxShapeVertexCount )
122     {
123         m_MaxShapeVertexCount = maxShapeVertexCount;
124     }
125 
126     //---------------------------------------------------------------------------
127     //! @brief        ラインサイズを設定します。
128     //!
129     //! @param[in]    width ラインサイズです。
130     //---------------------------------------------------------------------------
SetLineWidth(f32 width)131     void SetLineWidth( f32 width ) { m_LineWidth = width; }
132 
133     //---------------------------------------------------------------------------
134     //! @brief        シェイプ描画のレンダリングの開始関数です。
135     //---------------------------------------------------------------------------
136     void BeginDrawingShape();
137 
138     //---------------------------------------------------------------------------
139     //! @brief        シェイプ描画のレンダリングの終了関数です。
140     //---------------------------------------------------------------------------
141     void EndDrawingShape();
142 
143     // シェイプ描画
144 
145     //---------------------------------------------------------------------------
146     //! @brief        ラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。
147     //!
148     //! @param[in]    x1      ライン開始点の x 座標です。
149     //! @param[in]    y1      ライン開始点の y 座標です。
150     //! @param[in]    x2      ライン終了点の x 座標です。
151     //! @param[in]    y2      ライン終了点の y 座標です。
152     //! @param[in]    color   カラーです。
153     //---------------------------------------------------------------------------
DrawLine(s32 x1,s32 y1,s32 x2,s32 y2,ut::Color8 color)154     void DrawLine( s32 x1, s32 y1, s32 x2, s32 y2, ut::Color8 color )
155         {
156             this->DrawLine(
157                 math::VEC2(static_cast<f32>(x1), static_cast<f32>(y1)),
158                 math::VEC2(static_cast<f32>(x2), static_cast<f32>(y2)),
159                 color
160             );
161         }
162 
163     //---------------------------------------------------------------------------
164     //! @brief        ラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。
165     //!
166     //! @param[in]    p1      ラインの開始点です。
167     //! @param[in]    p2      ラインの終了点です。
168     //! @param[in]    color   カラーです。
169     //---------------------------------------------------------------------------
170     void DrawLine( const math::VEC2& p1, const math::VEC2& p2, ut::Color8 color );
171 
172     //---------------------------------------------------------------------------
173     //! @brief        四角形のラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。
174     //!
175     //! @param[in]    posh    左上端のx座標です。
176     //! @param[in]    posv    左上端のy座標です。
177     //! @param[in]    sizeh   四角形のx方向サイズです。
178     //! @param[in]    sizev   四角形のy方向サイズです。
179     //! @param[in]    color   カラーです。
180     //---------------------------------------------------------------------------
DrawRectangle(s32 posh,s32 posv,s32 sizeh,s32 sizev,ut::Color8 color)181     void DrawRectangle( s32 posh, s32 posv, s32 sizeh, s32 sizev, ut::Color8 color )
182         {
183             this->DrawRectangle(
184                 math::VEC2(static_cast<f32>(posh), static_cast<f32>(posv)),
185                 math::VEC2(static_cast<f32>(sizeh), static_cast<f32>(sizev)),
186                 color
187             );
188         }
189 
190     //---------------------------------------------------------------------------
191     //! @brief        四角形のラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。
192     //!
193     //! @param[in]    pos     左上端の座標です。
194     //! @param[in]    size    四角形のサイズです。
195     //! @param[in]    color   カラーです。
196     //---------------------------------------------------------------------------
197     void DrawRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color );
198 
199     //---------------------------------------------------------------------------
200     //! @brief        四角形を描画します。
201     //!
202     //! @param[in]    posh    左上端のx座標です。
203     //! @param[in]    posv    左上端のy座標です。
204     //! @param[in]    sizeh   四角形のx方向サイズです。
205     //! @param[in]    sizev   四角形のy方向サイズです。
206     //! @param[in]    color   カラーです。
207     //---------------------------------------------------------------------------
FillRectangle(s32 posh,s32 posv,s32 sizeh,s32 sizev,ut::Color8 color)208     void FillRectangle( s32 posh, s32 posv, s32 sizeh, s32 sizev, ut::Color8 color )
209         {
210             this->FillRectangle(
211                 math::VEC2(static_cast<f32>(posh), static_cast<f32>(posv)),
212                 math::VEC2(static_cast<f32>(sizeh), static_cast<f32>(sizev)),
213                 color
214             );
215         }
216 
217     //---------------------------------------------------------------------------
218     //! @brief        四角形を描画します。
219     //!
220     //! @param[in]    pos     左上端の座標です。
221     //! @param[in]    size    四角形のサイズです。
222     //! @param[in]    color   カラーです。
223     //---------------------------------------------------------------------------
224     void FillRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color );
225 
226     //---------------------------------------------------------------------------
227     //! @brief        三角形を描画します。
228     //!
229     //! @param[in]    pt1     頂点1です。
230     //! @param[in]    pt2     頂点2です。
231     //! @param[in]    pt3     頂点3です。
232     //! @param[in]    color   カラーです。
233     //---------------------------------------------------------------------------
234     void FillTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, ut::Color8 color );
235 
236     //---------------------------------------------------------------------------
237     //! @brief        三角形を描画します。
238     //!
239     //! @param[in]    pPointArray   頂点配列へのポインタです。
240     //! @param[in]    pointCount    頂点数です。
241     //! @param[in]    color         カラーです。
242     //---------------------------------------------------------------------------
243     void FillTriangles( const math::VEC2* pPointArray, s32 pointCount, ut::Color8 color );
244 
245     //@}
246 
247     //========================================================================================
248     //! @name フォント描画
249     //@{
250 
251     //---------------------------------------------------------------------------
252     //! @brief        描画可能な最大文字数を設定します。
253     //!
254     //! @param[in]    maxTextCount 最大文字数です。
255     //---------------------------------------------------------------------------
SetMaxTextCount(s32 maxTextCount)256     void SetMaxTextCount( s32 maxTextCount )
257     {
258         m_MaxTextCount = maxTextCount;
259     }
260 
261     //---------------------------------------------------------------------------
262     //! @brief        フォントの初期化を行います。
263     //!
264     //!               フォントバイナリは DestroyFont で破棄されます。
265     //!
266     //! @param[in]    allocator       アロケータです。
267     //! @param[in]    fontShaderPath  フォント用シェーダのパスです。
268     //! @param[in]    fontPath        フォントファイルのパスです。
269     //!
270     //! @return       初期化に成功した場合は true, 失敗した場合は false を返します。
271     //---------------------------------------------------------------------------
272     bool InitializeFont( os::IAllocator* allocator, const wchar_t* fontShaderPath, const wchar_t* fontPath );
273 
274     //---------------------------------------------------------------------------
275     //! @brief        フォントの初期化を行います。
276     //!
277     //!               フォントバイナリは Finalize 時に破棄されません。
278     //!
279     //! @param[in]    allocator          アロケータです。
280     //! @param[in]    fontShaderPath     フォント用シェーダのパスです。
281     //! @param[in]    fontBinary         フォントバイナリのポインタです。
282     //! @param[in]    fontSize           フォントバイナリのサイズです。
283     //!
284     //! @return       初期化に成功した場合は true, 失敗した場合は false を返します。
285     //---------------------------------------------------------------------------
286     bool InitializeFont( os::IAllocator* allocator, const wchar_t* fontShaderPath, void* fontBinary, size_t fontSize );
287 
288     //---------------------------------------------------------------------------
289     //! @brief        フォントの破棄をおこないます。
290     //---------------------------------------------------------------------------
291     void DestroyFont();
292 
293     //---------------------------------------------------------------------------
294     //! @brief        demo::GraphicsDrawing のフォントレンダリングの開始関数です。
295     //---------------------------------------------------------------------------
296     void BeginDrawingString();
297 
298     //---------------------------------------------------------------------------
299     //! @brief        文字列を描画します。
300     //!
301     //! @param[in]    format  フォーマット文字列です。
302     //! @param[in]    ...     出力パラメータです。
303     //!
304     //! @return       出力文字幅を返します。
305     //---------------------------------------------------------------------------
306     f32 DrawString( const char* format, ... );
307 
308     //---------------------------------------------------------------------------
309     //! @brief        位置指定付きで、文字列を描画します。
310     //!
311     //! @param[in]    posh    出力開始位置の x 座標です。
312     //! @param[in]    posv    出力開始位置の y 座標です。
313     //! @param[in]    format  フォーマット文字列です。
314     //! @param[in]    ...     出力パラメータです。
315     //!
316     //! @return       出力文字幅を返します。
317     //---------------------------------------------------------------------------
318     f32 DrawString( s32 posh, s32 posv, const char* format, ... );
319 
320     //---------------------------------------------------------------------------
321     //! @brief        位置指定付きで、文字列を描画します。
322     //!
323     //! @param[in]    posh    出力開始位置の x 座標です。
324     //! @param[in]    posv    出力開始位置の y 座標です。
325     //! @param[in]    format  フォーマット文字列です。
326     //! @param[in]    args    出力パラメータです。
327     //!
328     //! @return       出力文字幅を返します。
329     //---------------------------------------------------------------------------
330     f32 DrawStringArgs( s32 posh, s32 posv, const char* format, std::va_list args );
331 
332 
333     //! @brief デモ用のテキストライターを取得します。
GetWriter()334     font::TextWriter& GetWriter() { return m_TextWriter; }
GetWriter()335     const font::TextWriter& GetWriter() const { return m_TextWriter; }
336 
337     //---------------------------------------------------------------------------
338     //! @brief        フォントの描画処理を終了します。
339     //---------------------------------------------------------------------------
340     void EndDrawingString();
341 
342     //@}
343 
344 private:
345     //! @brief シェイプ描画用の GR ライブラリのオブジェクトを初期化します。
346     void InitializeShapeGraphicsState();
347 
348     //! @brief シェイプ描画用のマテリアルセットアップをおこないます。
349     void SetupShapeMaterial();
350 
351     //! @brief シェイプ描画用のテクスチャコンバイナ設定のセットアップをおこないます。
352     void SetupShapeTexEnv();
353 
354     //! @brief シェイプ描画用のプロジェクション、モデルビュー行列を Uniform へ設定します。
355     void SendShapeMatrix();
356 
357     //! @brief        四角形描画用の頂点列を生成します。
358     //!
359     //! @param[in]    pt1         頂点座標1です。
360     //! @param[in]    pt2         頂点座標2です。
361     //! @param[in]    pt3         頂点座標3です。
362     //! @param[out]   *pVecArray  頂点列を生成するバッファの先頭アドレスです。
363     //!
364     //! @return       頂点数を返します。
365     s32 BuildTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, math::VEC2 *pVecArray );
366 
367     //! @brief        xy軸に平行な四角形描画用の頂点列を生成します。
368     //!
369     //! @param[in]    pos         左上座標です。
370     //! @param[in]    size        四角形のサイズです。
371     //! @param[out]   *pVecArray  頂点列を生成するバッファの先頭アドレスです。
372     //!
373     //! @return       頂点数を返します。
374     s32 BuildRectangle( const math::VEC2& pos, const math::VEC2& size, math::VEC2 *pVecArray );
375 
376     //! @brief        四角形描画用の頂点列を生成します。
377     //!
378     //!               頂点は、pt1, pt2, pt3, pt4 の順に、時計回り または 反時計回りに
379     //!               指定する必要があります。
380     //!
381     //! @param[in]    pt1         頂点座標1です。
382     //! @param[in]    pt2         頂点座標2です。
383     //! @param[in]    pt3         頂点座標3です。
384     //! @param[in]    pt4         頂点座標4です。
385     //! @param[out]   *pVecArray  頂点列を生成するバッファの先頭アドレスです。
386     //!
387     //! @return       頂点数を返します。
388     s32 BuildRectangle(
389         const math::VEC2& pt1,
390         const math::VEC2& pt2,
391         const math::VEC2& pt3,
392         const math::VEC2& pt4,
393         math::VEC2 *pVecArray );
394 
395     //! @brief        ライン描画用の頂点列を生成します。
396     //!
397     //! @param[in]    pt1         ラインの開始座標です。
398     //! @param[in]    pt2         ラインの終端座標です。
399     //! @param[in]    lineWidth   ライン幅の指定です。
400     //! @param[out]   *pVecArray  頂点列を生成するバッファの先頭アドレスです。
401     //!
402     //! @return       頂点数を返します。
403     s32 BuildLine( const math::VEC2& pt1, const math::VEC2& pt2, f32 lineWidth, math::VEC2 *pVecArray );
404 
405     //! @brief        シェイプ描画用の頂点カラーを生成します。
406     //!
407     //! @param[in]    vertexCount 頂点数です
408     //! @param[in]    color       頂点カラーです
409     //! @param[out]   *pVecArray  頂点カラーを設定するバッファの先頭アドレスです。
410     void BuildColor( const s32 vertexCount, const ut::Color8 color,
411                      f32* pColorArray );
412 
413     //! @brief        バイナリからフォントの初期化を行います。
414     //!
415     //! @param[in]    shaderBinary       シェーダバイナリのポインタです。
416     //! @param[in]    shaderSize         シェーダバイナリのサイズです。
417     //! @param[in]    fontBinary         フォントバイナリのポインタです。
418     //! @param[in]    fontSize           フォントバイナリのサイズです。
419     //! @param[in]    drawBuffer         描画バッファのポインタです。
420     //! @param[in]    stringBuffer       文字列用バッファです。
421     //!
422     //! @return       初期化に成功した場合は true, 失敗した場合は false を返します。
423     bool InitializeFontFromBinary(
424         void* shaderBinary,
425         size_t shaderSize,
426         void* fontBinary,
427         size_t fontSize,
428         void* drawBuffer,
429         void* stringBuffer
430     );
431 
432     //! @brief        フォントデータを設定します。
433     //!
434     //! @param[in]    fontData    フォントバイナリへのポインタです。
435     //! @param[in]    fontSize    フォントバイナリサイズです。
436     //!
437     //! @return       設定に成功した場合は true そうでなければ false を返します。
438     bool InitializeFontResource( void* fontData, size_t fontSize );
439 
440     //! @brief        フォント用のシェーダを設定します。
441     //!
442     //! @param[in]    shaderData    シェーダバイナリへのポインタです。
443     //! @param[in]    shaderSize    シェーダバイナリサイズです。
444     //!
445     //! @return       設定に成功した場合は true そうでなければ false を返します。
446     bool InitializeFontShader( void* shaderData, size_t shaderSize );
447 
448     //! @brief        文字列表示用にモデルビュー行列と射影行列を設定します。
449     void SendFontMatrix();
450 
451     static const s32               DEFAULT_SCREEN_WIDTH           = 320;
452     static const s32               DEFAULT_SCREEN_HEIGHT          = 240;
453     static const s32               DEFAULT_MAX_TEXT_COUNT         = 512;
454     static const s32               DEFAULT_MAX_SHAPE_VERTEX_COUNT = 128;
455 
456     static const s32               SHAPE_DRAW_LINE_VERTEX_COUNT        = 6;
457     static const s32               SHAPE_DRAW_RECTANGLE_VERTEX_COUNT   = 4 * SHAPE_DRAW_LINE_VERTEX_COUNT;
458     static const s32               SHAPE_FILL_TRIANGLE_VERTEX_COUNT    = 3;
459     static const s32               SHAPE_FILL_RECTANGLE_VERTEX_COUNT   = 2 * SHAPE_FILL_TRIANGLE_VERTEX_COUNT;
460 
461     static const f32               FONT_SIZE;
462     static const f32               FONT_FIXED_WIDTH;
463 
464     math::VEC2                     m_ScreenSize;
465 
466     // シェイプ描画用のメンバ変数です。
467     f32                            m_LineWidth;
468 
469     s32                            m_MaxShapeVertexCount;
470     s32                            m_ShapeVertexCount;
471 
472     void*                          m_pShapeShaderBinary;
473     nn::gr::Shader                 m_ShapeShader;
474     nn::gr::BindSymbolVSFloat      m_ShapeBindSymbolProjectionMatrix;
475     nn::gr::BindSymbolVSFloat      m_ShapeBindSymbolModelViewMatrix;
476 
477     nn::gr::CTR::BindSymbolVSInput m_ShapeBindSymbolPos;
478     nn::gr::CTR::BindSymbolVSInput m_ShapeBindSymbolColor;
479 
480     nn::gr::Viewport               m_ShapeViewport;
481     nn::gr::RenderState            m_ShapeRenderState;
482     nn::gr::Combiner               m_ShapeCombiner;
483     nn::gr::Vertex                 m_ShapeVertex;
484     nn::gr::Vertex::IndexStream    m_ShapeIndexStream;
485 
486     math::VEC2*                    m_ShapePositionStreamArray;
487     f32*                           m_ShapeColorStreamArray;
488     u16*                           m_ShapeIndexStreamArray;
489 
490     nw::os::IAllocator*            m_ShapeAllocator;
491 
492     // フォント用のメンバです。
493     nw::font::ResFont              m_Font;
494     nw::font::TextWriter           m_TextWriter;
495     nw::font::RectDrawer           m_Drawer;
496     s32                            m_MaxTextCount;
497     void*                          m_FontCommandBuffer;
498     bool                           m_IsFontOwner;
499 
500     nw::os::IAllocator*            m_FontAllocator;
501 };
502 
503 } /* namespace demo */
504 } /* namespace nw */
505 
506 /* NW_DEMO_GRAPHICSDRAWING_H_ */
507 #endif
508