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