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: 24575 $ 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 namespace nw { 29 30 namespace os { 31 class IAllocator; 32 } 33 34 namespace font { 35 class Font; 36 } 37 38 namespace demo { 39 40 //--------------------------------------------------------------------------- 41 //! @brief デモ用の文字やプリミティブを描画するためのクラスです。 42 //--------------------------------------------------------------------------- 43 class GraphicsDrawing 44 { 45 public: 46 //---------------------------------------- 47 //! @name コンストラクタ/デストラクタ 48 //@{ 49 50 //--------------------------------------------------------------------------- 51 //! @brief スクリーンサイズを設定します。 52 //! 53 //! @param[in] width スクリーンの横幅です。 54 //! @param[in] height スクリーンの縦幅です。 55 //--------------------------------------------------------------------------- SetScreenSize(s32 width,s32 height)56 void SetScreenSize( s32 width, s32 height ) 57 { 58 m_ScreenSize = math::VEC2( static_cast<f32>(width), static_cast<f32>(height) ); 59 } 60 61 //--------------------------------------------------------------------------- 62 //! @brief コンストラクタです。 63 //--------------------------------------------------------------------------- 64 GraphicsDrawing(); 65 66 //--------------------------------------------------------------------------- 67 //! @brief デストラクタです。 68 //--------------------------------------------------------------------------- 69 ~GraphicsDrawing(); 70 71 //@} 72 73 //--------------------------------------------------------------------------- 74 //! @brief リソースの破棄をおこないます。 75 //--------------------------------------------------------------------------- 76 void Finalize(); 77 78 //======================================================================================== 79 //! @name シェイプ描画 80 //@{ 81 82 //--------------------------------------------------------------------------- 83 //! @brief シェーダの初期化をおこないます。 84 //! 85 //! @param[in] allocator アロケータです。 86 //! @param[in] shaderPath シェーダバイナリのファイルパスです。 87 //! 88 //! @return 初期化に成功した場合は true, 失敗した場合は false を返します。 89 //--------------------------------------------------------------------------- 90 bool InitializeShader( os::IAllocator* allocator, const wchar_t* shaderPath ); 91 92 //--------------------------------------------------------------------------- 93 //! @brief シェーダや描画用のセットアップをおこないます。 94 //--------------------------------------------------------------------------- 95 void Setup(); 96 97 //--------------------------------------------------------------------------- 98 //! @brief ラインサイズを設定します。 99 //! 100 //! @param[in] width ラインサイズです。 101 //--------------------------------------------------------------------------- SetLineWidth(f32 width)102 void SetLineWidth(f32 width) { m_LineWidth = width; } 103 104 //--------------------------------------------------------------------------- 105 //! @brief demo::GraphicsDrawing のレンダリングの開始関数です。 106 //--------------------------------------------------------------------------- 107 void BeginDrawingShape(); 108 109 // 図形描画 110 111 //--------------------------------------------------------------------------- 112 //! @brief ラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。 113 //! 114 //! @param[in] x1 ライン開始点の x 座標です。 115 //! @param[in] y1 ライン開始点の y 座標です。 116 //! @param[in] x2 ライン終了点の x 座標です。 117 //! @param[in] y2 ライン終了点の y 座標です。 118 //! @param[in] color カラーです。 119 //--------------------------------------------------------------------------- DrawLine(s32 x1,s32 y1,s32 x2,s32 y2,ut::Color8 color)120 void DrawLine( s32 x1, s32 y1, s32 x2, s32 y2, ut::Color8 color ) 121 { 122 this->DrawLine( 123 math::VEC2(static_cast<f32>(x1), static_cast<f32>(y1)), 124 math::VEC2(static_cast<f32>(x2), static_cast<f32>(y2)), 125 color 126 ); 127 } 128 129 //--------------------------------------------------------------------------- 130 //! @brief ラインを描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。 131 //! 132 //! @param[in] p1 ラインの開始点です。 133 //! @param[in] p2 ラインの終了点です。 134 //! @param[in] color カラーです。 135 //--------------------------------------------------------------------------- 136 void DrawLine( const math::VEC2& p1, const math::VEC2& p2, ut::Color8 color ); 137 138 //--------------------------------------------------------------------------- 139 //! @brief 四角形の線を描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。 140 //! 141 //! @param[in] posh 左上端のx座標です。 142 //! @param[in] posv 左上端のy座標です。 143 //! @param[in] sizeh 四角形のx方向サイズです。 144 //! @param[in] sizev 四角形のy方向サイズです。 145 //! @param[in] color カラーです。 146 //--------------------------------------------------------------------------- DrawRectangle(s32 posh,s32 posv,s32 sizeh,s32 sizev,ut::Color8 color)147 void DrawRectangle( s32 posh, s32 posv, s32 sizeh, s32 sizev, ut::Color8 color ) 148 { 149 this->DrawRectangle( 150 math::VEC2(static_cast<f32>(posh), static_cast<f32>(posv)), 151 math::VEC2(static_cast<f32>(sizeh), static_cast<f32>(sizev)), 152 color 153 ); 154 } 155 156 //--------------------------------------------------------------------------- 157 //! @brief 四角形の線を描画します。 ラインシェーダは使用せず、CPUで頂点を生成します。 158 //! 159 //! @param[in] pos 左上端の座標です。 160 //! @param[in] size 四角形のサイズです。 161 //! @param[in] color カラーです。 162 //--------------------------------------------------------------------------- 163 void DrawRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color ); 164 165 //--------------------------------------------------------------------------- 166 //! @brief 四角形を描画します。 167 //! 168 //! @param[in] posh 左上端のx座標です。 169 //! @param[in] posv 左上端のy座標です。 170 //! @param[in] sizeh 四角形のx方向サイズです。 171 //! @param[in] sizev 四角形のy方向サイズです。 172 //! @param[in] color カラーです。 173 //--------------------------------------------------------------------------- FillRectangle(s32 posh,s32 posv,s32 sizeh,s32 sizev,ut::Color8 color)174 void FillRectangle( s32 posh, s32 posv, s32 sizeh, s32 sizev, ut::Color8 color ) 175 { 176 this->FillRectangle( 177 math::VEC2(static_cast<f32>(posh), static_cast<f32>(posv)), 178 math::VEC2(static_cast<f32>(sizeh), static_cast<f32>(sizev)), 179 color 180 ); 181 } 182 183 //--------------------------------------------------------------------------- 184 //! @brief 四角形を描画します。 185 //! 186 //! @param[in] pos 左上端の座標です。 187 //! @param[in] size 四角形のサイズです。 188 //! @param[in] color カラーです。 189 //--------------------------------------------------------------------------- 190 void FillRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color ); 191 192 //--------------------------------------------------------------------------- 193 //! @brief 三角形を描画します。 194 //! 195 //! @param[in] pt1 頂点1です。 196 //! @param[in] pt2 頂点2です。 197 //! @param[in] pt3 頂点3です。 198 //! @param[in] color カラーです。 199 //--------------------------------------------------------------------------- 200 void FillTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, ut::Color8 color ); 201 202 //--------------------------------------------------------------------------- 203 //! @brief 三角形を描画します。 204 //! 205 //! @param[in] pPointArray 頂点配列へのポインタです。 206 //! @param[in] pointCount 頂点数です。 207 //! @param[in] color カラーです。 208 //--------------------------------------------------------------------------- 209 void FillTriangles( const math::VEC2* pPointArray, s32 pointCount, ut::Color8 color ); 210 211 //@} 212 213 //======================================================================================== 214 //! @name フォント描画 215 //@{ 216 217 //--------------------------------------------------------------------------- 218 //! @brief 描画可能な最大文字数を設定します。 219 //! 220 //! @param[in] maxTextCount 最大文字数です。 221 //--------------------------------------------------------------------------- SetMaxTextCount(s32 maxTextCount)222 void SetMaxTextCount( s32 maxTextCount ) 223 { 224 m_MaxTextCount = maxTextCount; 225 } 226 227 //--------------------------------------------------------------------------- 228 //! @brief フォントの初期化をおこないます。 229 //! 230 //! @param[in] allocator アロケータです。 231 //! @param[in] fontShaderPath フォント用シェーダのパスです。 232 //! @param[in] fontPath フォントファイルのパスです。 233 //! 234 //! @return 初期化に成功した場合は true, 失敗した場合は false を返します。 235 //--------------------------------------------------------------------------- 236 bool InitializeFont( os::IAllocator* allocator, const wchar_t* fontShaderPath, const wchar_t* fontPath ); 237 238 //--------------------------------------------------------------------------- 239 //! @brief フォントの破棄をおこないます。 240 //--------------------------------------------------------------------------- 241 void DestroyFont(); 242 243 //--------------------------------------------------------------------------- 244 //! @brief demo::GraphicsDrawing のフォントレンダリングの開始関数です。 245 //--------------------------------------------------------------------------- 246 void BeginDrawingString(); 247 248 //--------------------------------------------------------------------------- 249 //! @brief 文字列を描画します。 250 //! 251 //! @param[in] format フォーマット文字列です。 252 //! @param[in] ... 出力パラメータです。 253 //! 254 //! @return 出力文字幅を返します。 255 //--------------------------------------------------------------------------- 256 f32 DrawString( const char* format, ... ); 257 258 259 //--------------------------------------------------------------------------- 260 //! @brief 位置指定付きで、文字列を描画します。 261 //! 262 //! @param[in] posh 出力開始位置の x 座標です。 263 //! @param[in] posv 出力開始位置の y 座標です。 264 //! @param[in] format フォーマット文字列です。 265 //! @param[in] ... 出力パラメータです。 266 //! 267 //! @return 出力文字幅を返します。 268 //--------------------------------------------------------------------------- 269 f32 DrawString( s32 posh, s32 posv, const char* format, ... ); 270 271 //--------------------------------------------------------------------------- 272 //! @brief 位置指定付きで、文字列を描画します。 273 //! 274 //! @param[in] posh 出力開始位置の x 座標です。 275 //! @param[in] posv 出力開始位置の y 座標です。 276 //! @param[in] format フォーマット文字列です。 277 //! @param[in] args 出力パラメータです。 278 //! 279 //! @return 出力文字幅を返します。 280 //--------------------------------------------------------------------------- 281 f32 DrawStringArgs( s32 posh, s32 posv, const char* format, std::va_list args ); 282 283 284 //! @brief デモ用のテキストライターを取得します。 GetWriter()285 font::TextWriter& GetWriter() { return m_TextWriter; } GetWriter()286 const font::TextWriter& GetWriter() const { return m_TextWriter; } 287 288 //@} 289 290 //--------------------------------------------------------------------------- 291 //! @brief demo::GraphicsDrawing の描画を Flush します。 292 //--------------------------------------------------------------------------- 293 void FlushDrawing(); 294 295 //--------------------------------------------------------------------------- 296 //! @brief シェーダの初期化をおこないます。 297 //! 298 //! @param[in] pShaderBinary シェーダバイナリのポインタです。 299 //! @param[in] binarySize シェーダバイナリのサイズです。 300 //! 301 //! @return 初期化に成功した場合は true, 失敗した場合は false を返します。 302 //--------------------------------------------------------------------------- 303 bool InitializeShader( void* pShaderBinary, size_t binarySize ); 304 305 //--------------------------------------------------------------------------- 306 //! @brief フォントの初期化をおこないます。 307 //! 308 //! @param[in] shaderBinary シェーダバイナリのポインタです。 309 //! @param[in] shaderSize シェーダバイナリのサイズです。 310 //! @param[in] fontBinary フォントバイナリのポインタです。 311 //! @param[in] fontSize フォントバイナリのサイズです。 312 //! @param[in] drawBuffer 描画バッファのポインタです。 313 //! @param[in] stringBuffer 文字列用バッファです。 314 //! 315 //! @return 初期化に成功した場合は true, 失敗した場合は false を返します。 316 //--------------------------------------------------------------------------- 317 bool InitializeFont( void* shaderBinary, size_t shaderSize, void* fontBinary, size_t fontSize, void* drawBuffer, void* stringBuffer ); 318 319 private: 320 //! @brief デモ描画用のマテリアルセットアップをおこないます。 321 void SetupMaterial(); 322 //! @brief デモ描画用のテクスチャコンバイナ設定のセットアップをおこないます。 323 void SetupTexEnv(); 324 //! @brief プロジェクション、モデルビュー行列を Uniform へ設定します。 325 void SendMatrix(); 326 327 //! @brief 四角形描画用の頂点列を生成します。 328 //! 329 //! @param[in] pt1 頂点座標1です。 330 //! @param[in] pt2 頂点座標2です。 331 //! @param[in] pt3 頂点座標3です。 332 //! @param[out] *pVecArray 頂点列を生成するバッファの先頭アドレスです。 333 //! 334 //! @return 頂点数を返します。 335 s32 BuildTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, math::VEC2 *pVecArray ); 336 337 //! @brief xy軸に平行な四角形描画用の頂点列を生成します。 338 //! 339 //! @param[in] pos 左上座標です。 340 //! @param[in] size 四角形のサイズです。 341 //! @param[out] *pVecArray 頂点列を生成するバッファの先頭アドレスです。 342 //! 343 //! @return 頂点数を返します。 344 s32 BuildRectangle( const math::VEC2& pos, const math::VEC2& size, math::VEC2 *pVecArray ); 345 346 //! @brief 四角形描画用の頂点列を生成します。 347 //! 348 //! 頂点は、pt1, pt2, pt3, pt4 の順に、時計回り または 反時計回りに 349 //! 指定する必要があります。 350 //! 351 //! @param[in] pt1 頂点座標1です。 352 //! @param[in] pt2 頂点座標2です。 353 //! @param[in] pt3 頂点座標3です。 354 //! @param[in] pt4 頂点座標4です。 355 //! @param[out] *pVecArray 頂点列を生成するバッファの先頭アドレスです。 356 //! 357 //! @return 頂点数を返します。 358 s32 BuildRectangle( 359 const math::VEC2& pt1, 360 const math::VEC2& pt2, 361 const math::VEC2& pt3, 362 const math::VEC2& pt4, 363 math::VEC2 *pVecArray ); 364 365 //! @brief ライン描画用の頂点列を生成します。 366 //! 367 //! @param[in] pt1 ラインの開始座標です。 368 //! @param[in] pt2 ラインの終端座標です。 369 //! @param[in] lineWidth ライン幅の指定です。 370 //! @param[out] *pVecArray 頂点列を生成するバッファの先頭アドレスです。 371 //! 372 //! @return 頂点数を返します。 373 s32 BuildLine( const math::VEC2& pt1, const math::VEC2& pt2, f32 lineWidth, math::VEC2 *pVecArray ); 374 375 //! @brief フォントデータを設定します。 376 //! 377 //! @param[in] fontData フォントバイナリへのポインタです。 378 //! @param[in] fontSize フォントバイナリサイズです。 379 //! 380 //! @return 設定に成功した場合は true そうでなければ false を返します。 381 bool InitializeFontResource( void* fontData, size_t fontSize ); 382 383 //! @brief フォント用のシェーダを設定します。 384 //! 385 //! @param[in] shaderData シェーダバイナリへのポインタです。 386 //! @param[in] shaderSize シェーダバイナリサイズです。 387 //! 388 //! @return 設定に成功した場合は true そうでなければ false を返します。 389 bool InitializeFontShader( void* shaderData, size_t shaderSize ); 390 391 //! @brief 文字列表示用にモデルビュー行列と射影行列を設定します。 392 void SendFontMatrix(); 393 394 static const s32 DEFAULT_SCREEN_WIDTH = 320; 395 static const s32 DEFAULT_SCREEN_HEIGHT = 240; 396 static const s32 DEFAULT_MAX_TEXT_COUNT = 512; 397 398 enum 399 { 400 VERTEX_STREAM_SIZE = 24 401 }; 402 403 math::VEC2 m_ScreenSize; 404 f32 m_LineWidth; 405 406 GLuint m_ProgramID; 407 GLuint m_ShaderID; 408 409 void* m_pShaderBinary; 410 411 math::VEC2 m_PositionStream[ VERTEX_STREAM_SIZE ]; 412 math::VEC4 m_ColorStream[ VERTEX_STREAM_SIZE ]; 413 math::VEC2 m_TexCoordStream[ VERTEX_STREAM_SIZE ]; 414 415 nw::font::ResFont m_Font; 416 nw::font::TextWriter m_TextWriter; 417 nw::font::RectDrawer m_Drawer; 418 s32 m_MaxTextCount; 419 void* m_FontCommandBuffer; 420 421 nw::os::IAllocator* m_FontAllocator; 422 nw::os::IAllocator* m_ShapeAllocator; 423 }; 424 425 } /* namespace demo */ 426 } /* namespace nw */ 427 428 /* NW_DEMO_GRAPHICSDRAWING_H_ */ 429 #endif 430