/*---------------------------------------------------------------------------* Project: NintendoWare File: demo_GraphicsDrawing.cpp Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo and/or its licensed developers and are protected by national and international copyright laws. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. The content herein is highly confidential and should be handled accordingly. $Revision: 31311 $ *---------------------------------------------------------------------------*/ #include #include #include #include #include #include namespace nw { namespace demo { const f32 GraphicsDrawing::FONT_SIZE = 16.0f; const f32 GraphicsDrawing::FONT_FIXED_WIDTH = 10.5f; //-------------------------------------------------------------------------- /* ctor */ GraphicsDrawing::GraphicsDrawing() : m_ScreenSize( f32(DEFAULT_SCREEN_WIDTH), f32(DEFAULT_SCREEN_HEIGHT) ), m_LineWidth( 2.0f ), m_MaxShapeVertexCount( DEFAULT_MAX_SHAPE_VERTEX_COUNT ), m_ShapeVertexCount( 0 ), m_pShapeShaderBinary( NULL ), m_ShapeShader(), m_ShapeBindSymbolProjectionMatrix(), m_ShapeBindSymbolModelViewMatrix(), m_ShapeBindSymbolPos(), m_ShapeBindSymbolColor(), m_ShapeViewport(), m_ShapeRenderState(), m_ShapeCombiner(), m_ShapeVertex(), m_ShapeIndexStream(), m_ShapePositionStreamArray( NULL ), m_ShapeColorStreamArray( NULL ), m_ShapeIndexStreamArray( NULL ), m_ShapeAllocator( NULL ), m_Font(), m_TextWriter(), m_Drawer(), m_MaxTextCount( DEFAULT_MAX_TEXT_COUNT ), m_FontCommandBuffer( NULL ), m_IsFontOwner( true ), m_FontAllocator( NULL ) { } //-------------------------------------------------------------------------- GraphicsDrawing::~GraphicsDrawing() { } //-------------------------------------------------------------------------- void GraphicsDrawing::Finalize() { this->DestroyShape(); this->DestroyFont(); m_FontAllocator = NULL; m_ShapeAllocator = NULL; } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeShape( void* pShaderBinary, size_t binarySize ) { bool result = true; m_pShapeShaderBinary = pShaderBinary; NN_UNUSED_VAR( binarySize ); // シェーダーバイナリを指定して初期化 // 0番目の実行イメージを頂点シェーダーとして有効化 m_ShapeShader.SetupBinary( pShaderBinary, 0, -1 ); // 文字列で、Uniform を検索 result = m_ShapeShader.SearchBindSymbol( &m_ShapeBindSymbolProjectionMatrix, "uProjection" ); NW_ASSERT( result ); result = m_ShapeShader.SearchBindSymbol( &m_ShapeBindSymbolModelViewMatrix, "uModelView" ); NW_ASSERT( result ); // 文字列による location の検索 result = m_ShapeShader.SearchBindSymbol( &m_ShapeBindSymbolPos, "aPosition" ); NW_ASSERT( result ); result = m_ShapeShader.SearchBindSymbol( &m_ShapeBindSymbolColor, "aColor" ); NW_ASSERT( result ); // 頂点ストリームの設定 uptr physicalAddr = nngxGetPhysicalAddr( reinterpret_cast< uptr >( m_ShapePositionStreamArray ) ); m_ShapeVertex.EnableAttrAsArray( m_ShapeBindSymbolPos, physicalAddr, PICA_DATA_SIZE_2_FLOAT ); physicalAddr = nngxGetPhysicalAddr( reinterpret_cast< uptr >( m_ShapeColorStreamArray ) ); m_ShapeVertex.EnableAttrAsArray( m_ShapeBindSymbolColor, physicalAddr, PICA_DATA_SIZE_4_FLOAT ); // インデックスストリームの設定 physicalAddr = nngxGetPhysicalAddr( reinterpret_cast< uptr >( m_ShapeIndexStreamArray ) ); m_ShapeIndexStream.physicalAddr = physicalAddr; m_ShapeIndexStream.isUnsignedByte = false; return result; } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeShape( os::IAllocator* allocator, const wchar_t* shaderPath ) { m_ShapeAllocator = allocator; ut::MoveArray buffer = nw::demo::Utility::LoadFile( allocator, shaderPath ); if ( buffer.empty() ) { return false; } s32 size = buffer.size(); void* pBuffer = buffer.release(); u32 shapePositionStreamArraySize = 2 * 4 * m_MaxShapeVertexCount; u32 shapeColorStreamArraySize = 4 * 4 * m_MaxShapeVertexCount; u32 shapeIndexStreamArraySize = 2 * m_MaxShapeVertexCount; m_ShapePositionStreamArray = static_cast( m_ShapeAllocator->Alloc( shapePositionStreamArraySize, 128 ) ); m_ShapeColorStreamArray = static_cast( m_ShapeAllocator->Alloc( shapeColorStreamArraySize, 128 ) ); m_ShapeIndexStreamArray = static_cast( m_ShapeAllocator->Alloc( shapeIndexStreamArraySize, 128 ) ); for ( s32 index = 0; index < m_MaxShapeVertexCount; index++ ) { m_ShapePositionStreamArray[ index ] = math::VEC2( 0.0f, 0.0f ); u32 colorIndex = 4 * index; m_ShapeColorStreamArray[ colorIndex ] = 1.0f; m_ShapeColorStreamArray[ colorIndex + 1 ] = 1.0f; m_ShapeColorStreamArray[ colorIndex + 2 ] = 1.0f; m_ShapeColorStreamArray[ colorIndex + 3 ] = 1.0f; } for ( s32 index = 0; index < m_MaxShapeVertexCount; index++ ) { m_ShapeIndexStreamArray[ index ] = static_cast( index ); } InitializeShapeGraphicsState(); return InitializeShape( pBuffer, size ); } //-------------------------------------------------------------------------- void GraphicsDrawing::DestroyShape() { m_ShapeVertex.DisableAll(); if ( m_pShapeShaderBinary && m_ShapeAllocator ) { os::SafeFree( m_pShapeShaderBinary, m_ShapeAllocator ); } if ( m_ShapeAllocator ) { os::SafeFree( m_ShapePositionStreamArray, m_ShapeAllocator ); os::SafeFree( m_ShapeColorStreamArray, m_ShapeAllocator ); os::SafeFree( m_ShapeIndexStreamArray, m_ShapeAllocator ); } } //-------------------------------------------------------------------------- void GraphicsDrawing::SetupShape() { this->SetupShapeMaterial(); this->SetupShapeTexEnv(); } //-------------------------------------------------------------------------- void GraphicsDrawing::BeginDrawingShape() { SetupShape(); m_ShapeVertexCount = 0; m_ShapeViewport.Set( 0, 0, m_ScreenSize.y, m_ScreenSize.x ); bit32* command; nngxGetCmdlistParameteri( NN_GX_CMDLIST_CURRENT_BUFADDR, reinterpret_cast( &command ) ); bit32* commandStart = command; command = m_ShapeViewport.MakeCommand( command ); nngxMoveCommandbufferPointer( ( command - commandStart ) * sizeof(u32) ); this->SendShapeMatrix(); } //-------------------------------------------------------------------------- void GraphicsDrawing::EndDrawingShape( void ) { m_ShapeIndexStream.drawVtxNum = m_ShapeVertexCount; u32 shapePositionStreamArraySize = 2 * 4 * m_ShapeVertexCount; u32 shapeColorStreamArraySize = 4 * 4 * m_ShapeVertexCount; u32 shapeIndexStreamArraySize = 2 * m_ShapeVertexCount; nngxUpdateBuffer( m_ShapePositionStreamArray, shapePositionStreamArraySize ); nngxUpdateBuffer( m_ShapeColorStreamArray, shapeColorStreamArraySize ); nngxUpdateBuffer( m_ShapeIndexStreamArray, shapeIndexStreamArraySize ); bit32* command; nngxGetCmdlistParameteri( NN_GX_CMDLIST_CURRENT_BUFADDR, reinterpret_cast( &command ) ); bit32* commandStart = command; command = nn::gr::CTR::Vertex::MakeDisableCommand( command ); command = m_ShapeVertex.MakeEnableAttrCommand( command ); command = m_ShapeVertex.MakeDrawCommand( command, m_ShapeIndexStream ); command = m_ShapeVertex.MakeDisableAttrCommand( command ); command = nn::gr::CTR::Vertex::MakeDisableCommand( command ); nngxMoveCommandbufferPointer( ( command - commandStart ) * sizeof(u32) ); m_ShapeVertexCount = 0; } //-------------------------------------------------------------------------- void GraphicsDrawing::DrawLine( const math::VEC2& p1, const math::VEC2& p2, ut::Color8 color ) { NW_ASSERT( ( m_ShapeVertexCount + SHAPE_DRAW_LINE_VERTEX_COUNT ) <= m_MaxShapeVertexCount ); s32 count = this->BuildLine( p1, p2, m_LineWidth, &m_ShapePositionStreamArray[ m_ShapeVertexCount ] ); u32 index = 4 * m_ShapeVertexCount; BuildColor( count, color, &m_ShapeColorStreamArray[ index ] ); m_ShapeVertexCount += count; } //-------------------------------------------------------------------------- void GraphicsDrawing::DrawRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color ) { NW_ASSERT( ( m_ShapeVertexCount + SHAPE_DRAW_RECTANGLE_VERTEX_COUNT ) <= m_MaxShapeVertexCount ); s32 count = 0; count += this->BuildLine( pos, math::VEC2( pos.x + size.x, pos.y ), m_LineWidth, &m_ShapePositionStreamArray[ m_ShapeVertexCount + count ] ); count += this->BuildLine( math::VEC2( pos.x + size.x, pos.y ), pos + size, m_LineWidth, &m_ShapePositionStreamArray[ m_ShapeVertexCount + count ] ); count += this->BuildLine( pos + size, math::VEC2( pos.x, pos.y + size.y ), m_LineWidth, &m_ShapePositionStreamArray[ m_ShapeVertexCount + count ] ); count += this->BuildLine(math::VEC2( pos.x, pos.y + size.y ), pos, m_LineWidth, &m_ShapePositionStreamArray[ m_ShapeVertexCount + count ] ); u32 index = 4 * m_ShapeVertexCount; BuildColor( count, color, &m_ShapeColorStreamArray[ index ] ); m_ShapeVertexCount += count; } //-------------------------------------------------------------------------- void GraphicsDrawing::FillRectangle( const math::VEC2& pos, const math::VEC2& size, ut::Color8 color ) { NW_ASSERT( ( m_ShapeVertexCount + SHAPE_FILL_RECTANGLE_VERTEX_COUNT ) <= m_MaxShapeVertexCount ); s32 count = this->BuildRectangle(pos, size, &m_ShapePositionStreamArray[ m_ShapeVertexCount ]); u32 index = 4 * m_ShapeVertexCount; BuildColor( count, color, &m_ShapeColorStreamArray[ index ] ); m_ShapeVertexCount += count; } //-------------------------------------------------------------------------- void GraphicsDrawing::FillTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, ut::Color8 color ) { NW_ASSERT( ( m_ShapeVertexCount + SHAPE_FILL_TRIANGLE_VERTEX_COUNT ) <= m_MaxShapeVertexCount ); s32 count = this->BuildTriangle( pt1, pt2, pt3, &m_ShapePositionStreamArray[ m_ShapeVertexCount ] ); u32 index = 4 * m_ShapeVertexCount; BuildColor( count, color, &m_ShapeColorStreamArray[ index ] ); m_ShapeVertexCount += count; } //-------------------------------------------------------------------------- void GraphicsDrawing::FillTriangles( const math::VEC2* pPointArray, s32 pointCount, ut::Color8 color ) { NW_NULL_ASSERT( pPointArray ); NW_ASSERT( pointCount > 0 ); NW_ASSERT( ( m_ShapeVertexCount + pointCount ) <= m_MaxShapeVertexCount ); s32 size = 2 * 4 * pointCount; std::memcpy( &m_ShapePositionStreamArray[ m_ShapeVertexCount ], pPointArray, size ); u32 index = 4 * m_ShapeVertexCount; BuildColor( pointCount, color, &m_ShapeColorStreamArray[ index ] ); m_ShapeVertexCount += pointCount; } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeFont( os::IAllocator* allocator, const wchar_t* fontShaderPath, const wchar_t* fontPath ) { m_FontAllocator = allocator; m_IsFontOwner = true; // フォントリソースをロードします。 ut::MoveArray fontData = nw::demo::Utility::LoadFile( allocator, fontPath, nw::font::GlyphDataAlignment ); ut::MoveArray fontShaderData = nw::demo::Utility::LoadFile( allocator, fontShaderPath ); s32 fontSize = fontData.size(); void* fontBuffer = fontData.release(); s32 shaderSize = fontShaderData.size(); void* shaderBuffer = fontShaderData.release(); const u32 vtxBufCommandBufferSize = nw::font::RectDrawer::GetVertexBufferCommandBufferSize(shaderBuffer, shaderSize); m_FontCommandBuffer = allocator->Alloc(vtxBufCommandBufferSize); NN_NULL_ASSERT(m_FontCommandBuffer); const u32 drawBufferSize = nw::font::ResFont::GetDrawBufferSize(fontBuffer); void* drawBuffer = allocator->Alloc(drawBufferSize, 4); NW_NULL_ASSERT(drawBuffer); const u32 stringBufferSize = nw::font::CharWriter::GetDispStringBufferSize(m_MaxTextCount); void *const stringBuffer = allocator->Alloc(stringBufferSize); NN_NULL_ASSERT(stringBuffer); bool result = false; if ( fontBuffer != NULL && shaderBuffer != NULL ) { result = this->InitializeFontFromBinary( shaderBuffer, shaderSize, fontBuffer, fontSize, drawBuffer, stringBuffer ); } os::SafeFree(shaderBuffer, allocator); if ( ! result ) { os::SafeFree(fontBuffer, allocator); } return result; } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeFont( os::IAllocator* allocator, const wchar_t* fontShaderPath, void* fontBinary, size_t fontSize ) { m_FontAllocator = allocator; m_IsFontOwner = false; ut::MoveArray fontShaderData = nw::demo::Utility::LoadFile( allocator, fontShaderPath ); s32 shaderSize = fontShaderData.size(); void* shaderBuffer = fontShaderData.release(); const u32 vtxBufCommandBufferSize = nw::font::RectDrawer::GetVertexBufferCommandBufferSize(shaderBuffer, shaderSize); m_FontCommandBuffer = allocator->Alloc(vtxBufCommandBufferSize); NN_NULL_ASSERT(m_FontCommandBuffer); const u32 drawBufferSize = nw::font::ResFont::GetDrawBufferSize(fontBinary); void* drawBuffer = allocator->Alloc(drawBufferSize, 4); NW_NULL_ASSERT(drawBuffer); const u32 stringBufferSize = nw::font::CharWriter::GetDispStringBufferSize(m_MaxTextCount); void *const stringBuffer = allocator->Alloc(stringBufferSize); NN_NULL_ASSERT(stringBuffer); bool result = this->InitializeFontFromBinary( shaderBuffer, shaderSize, fontBinary, fontSize, drawBuffer, stringBuffer ); os::SafeFree(shaderBuffer, allocator); return result; } //-------------------------------------------------------------------------- void GraphicsDrawing::DestroyFont() { m_Drawer.Finalize(); os::SafeFree(m_FontCommandBuffer, m_FontAllocator); nw::font::DispStringBuffer* stringBuffer = m_TextWriter.GetDispStringBuffer(); os::SafeFree(stringBuffer, m_FontAllocator); // フォントの破棄 if (!m_Font.IsManaging(NULL)) { void* drawBuffer = m_Font.SetDrawBuffer(NULL); void* resource = m_Font.RemoveResource(); if (m_IsFontOwner) { os::SafeFree(resource, m_FontAllocator); } os::SafeFree(drawBuffer, m_FontAllocator); } } //-------------------------------------------------------------------------- void GraphicsDrawing::BeginDrawingString() { // カラーバッファ情報 // LCDの向きに合わせて、幅と高さを入れ替えています。 const nw::font::ColorBufferInfo colBufInfo = { m_ScreenSize.y, m_ScreenSize.x, PICA_DATA_DEPTH24_STENCIL8_EXT }; const u32 screenSettingCommands[] = { // ビューポートの設定 NW_FONT_CMD_SET_VIEWPORT( 0, 0, colBufInfo.width, colBufInfo.height ), // シザー処理を無効 NW_FONT_CMD_SET_DISABLE_SCISSOR( colBufInfo ), // wバッファの無効化 // デプスレンジの設定 // ポリゴンオフセットの無効化 NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET( 0.0f, // wScale : 0.0 でWバッファが無効 0.0f, // depth range near 1.0f, // depth range far 0, // polygon offset units : 0.0 で ポリゴンオフセットが無効 colBufInfo), }; nngxAdd3DCommand(screenSettingCommands, sizeof(screenSettingCommands), true); static const u32 s_InitCommands[] = { // カリングを無効 NW_FONT_CMD_SET_CULL_FACE( NW_FONT_CMD_CULL_FACE_DISABLE ), // ステンシルテストを無効 NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(), // デプステストを無効 // カラーバッファの全ての成分を書き込み可 NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK( false, // isDepthTestEnabled 0, // depthFunc true, // depthMask true, // red true, // green true, // blue true), // alpha // アーリーデプステストを無効 NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST( false ), // フレームバッファアクセス制御 NW_FONT_CMD_SET_FBACCESS( true, // colorRead true, // colorWrite false, // depthRead false, // depthWrite false, // stencilRead false), // stencilWrite }; nngxAdd3DCommand(s_InitCommands, sizeof(s_InitCommands), true); m_Drawer.DrawBegin(); this->SendFontMatrix(); m_TextWriter.SetCursor( 0, 0 ); } //-------------------------------------------------------------------------- f32 GraphicsDrawing::DrawString( const char* format, ... ) { NW_NULL_ASSERT( format ); std::va_list vargs; va_start(vargs, format); m_TextWriter.StartPrint(); f32 width = m_TextWriter.VPrintf(format, vargs); m_TextWriter.EndPrint(); m_Drawer.BuildTextCommand(&m_TextWriter); m_TextWriter.UseCommandBuffer(); va_end(vargs); return width; } //-------------------------------------------------------------------------- f32 GraphicsDrawing::DrawString( s32 posh, s32 posv, const char* format, ... ) { NW_NULL_ASSERT( format ); m_TextWriter.SetCursor( static_cast(posh), static_cast(posv) ); std::va_list vargs; va_start(vargs, format); f32 width = this->DrawStringArgs( posh, posv, format, vargs); va_end(vargs); return width; } //-------------------------------------------------------------------------- f32 GraphicsDrawing::DrawStringArgs( s32 posh, s32 posv, const char* format, std::va_list args ) { NW_NULL_ASSERT( format ); m_TextWriter.SetCursor( static_cast(posh), static_cast(posv) ); m_TextWriter.StartPrint(); f32 width = m_TextWriter.VPrintf(format, args); m_TextWriter.EndPrint(); m_Drawer.BuildTextCommand(&m_TextWriter); m_TextWriter.UseCommandBuffer(); return width; } //-------------------------------------------------------------------------- void GraphicsDrawing::EndDrawingString() { m_Drawer.DrawEnd(); } //-------------------------------------------------------------------------- void GraphicsDrawing::InitializeShapeGraphicsState() { // レンダリングステートの設定 m_ShapeRenderState.blend.isEnable = true; m_ShapeRenderState.blend.eqRgb = PICA_DATA_BLEND_EQUATION_ADD; m_ShapeRenderState.blend.eqAlpha = PICA_DATA_BLEND_EQUATION_ADD; m_ShapeRenderState.blend.srcRgb = PICA_DATA_BLEND_FUNC_SRC_ALPHA; m_ShapeRenderState.blend.srcAlpha = PICA_DATA_BLEND_FUNC_ONE_MINUS_SRC_ALPHA; m_ShapeRenderState.blend.dstRgb = PICA_DATA_BLEND_FUNC_ONE_MINUS_SRC_ALPHA; m_ShapeRenderState.blend.dstAlpha = PICA_DATA_BLEND_FUNC_ONE_MINUS_SRC_ALPHA; m_ShapeRenderState.stencilTest.isEnable = false; m_ShapeRenderState.depthTest.isEnable = false; m_ShapeRenderState.depthTest.isEnableWrite = false; m_ShapeRenderState.cullingTest.isEnable = false; // テクスチャコンバイナの設定 for ( u32 stageIndex = 0; stageIndex < nn::gr::CTR::Combiner::COMBINER_STAGE_MAX; stageIndex++ ) { m_ShapeCombiner.stage[ stageIndex ].rgb.combine = PICA_DATA_TEX_ENV_COMBINE_REPLACE; m_ShapeCombiner.stage[ stageIndex ].alpha.combine = PICA_DATA_TEX_ENV_COMBINE_REPLACE; for ( u32 sourceIndex = 0; sourceIndex < 3; sourceIndex++) { if ( ( stageIndex == 0 ) && ( sourceIndex == 0 ) ) { m_ShapeCombiner.stage[ stageIndex ].rgb.source[ sourceIndex ] = PICA_DATA_TEX_ENV_SRC_RGBA_PRIMARY_COLOR; m_ShapeCombiner.stage[ stageIndex ].alpha.source[ sourceIndex ] = PICA_DATA_TEX_ENV_SRC_RGBA_PRIMARY_COLOR; } else { m_ShapeCombiner.stage[ stageIndex ].rgb.source[ sourceIndex ] = PICA_DATA_TEX_ENV_SRC_RGBA_PREVIOUS; m_ShapeCombiner.stage[ stageIndex ].alpha.source[ sourceIndex ] = PICA_DATA_TEX_ENV_SRC_RGBA_PREVIOUS; } m_ShapeCombiner.stage[ stageIndex ].rgb.operand[ sourceIndex ] = PICA_DATA_OPE_RGB_SRC_COLOR; m_ShapeCombiner.stage[ stageIndex ].alpha.operand[ sourceIndex ] = PICA_DATA_OPE_ALPHA_SRC_ALPHA; } m_ShapeCombiner.stage[ stageIndex ].rgb.scale = PICA_DATA_TEX_ENV_SCALE_1; m_ShapeCombiner.stage[ stageIndex ].alpha.scale = PICA_DATA_TEX_ENV_SCALE_1; } } //-------------------------------------------------------------------------- void GraphicsDrawing::SetupShapeMaterial() { bit32* command; nngxGetCmdlistParameteri( NN_GX_CMDLIST_CURRENT_BUFADDR, reinterpret_cast( &command ) ); bit32* commandStart = command; command = m_ShapeShader.MakeFullCommand( command ); command = m_ShapeRenderState.MakeCommand( command ) ; nngxMoveCommandbufferPointer( ( command - commandStart ) * sizeof(u32) ); } //-------------------------------------------------------------------------- void GraphicsDrawing::SetupShapeTexEnv() { bit32* command; nngxGetCmdlistParameteri( NN_GX_CMDLIST_CURRENT_BUFADDR, reinterpret_cast( &command ) ); bit32* commandStart = command; command = m_ShapeCombiner.MakeCommand( command ); nngxMoveCommandbufferPointer( ( command - commandStart ) * sizeof(u32) ); } //-------------------------------------------------------------------------- void GraphicsDrawing::SendShapeMatrix() { nw::math::MTX44 projMtx; nw::math::MTX44 viewMtx; const nw::math::VEC3 camPos( 0.0f, 0.0f, 1.0f ); const nw::math::VEC3 aimPos( 0.0f, 0.0f, 0.0f ); const nw::math::VEC3 camUp( 0.0f, 1.0f, 0.0f ); f32 width = m_ScreenSize.y; f32 height = m_ScreenSize.x; const f32 left = 0.f; f32 right = m_ScreenSize.x; f32 bottom = m_ScreenSize.y; const f32 top = 0.f; const f32 znear = - 100.0f; const f32 zfar = 100.0f; math::MTX44OrthoPivot( &projMtx, left, right, bottom, top, znear, zfar, math::PIVOT_UPSIDE_TO_TOP ); math::MTX44Identity( &viewMtx ); math::MTX34LookAt( reinterpret_cast(&viewMtx), &camPos, &camUp, &aimPos ); bit32* command; nngxGetCmdlistParameteri( NN_GX_CMDLIST_CURRENT_BUFADDR, reinterpret_cast( &command ) ); bit32* commandStart = command; // シェーダのUniformを設定 { command = m_ShapeBindSymbolProjectionMatrix.MakeUniformCommand( command, projMtx ); command = m_ShapeBindSymbolModelViewMatrix.MakeUniformCommand( command, viewMtx ); } nngxMoveCommandbufferPointer( ( command - commandStart ) * sizeof(u32) ); } //-------------------------------------------------------------------------- s32 GraphicsDrawing::BuildTriangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, math::VEC2 *pVecArray ) { NW_NULL_ASSERT( pVecArray ); pVecArray[ 0 ] = pt1; pVecArray[ 1 ] = pt2; pVecArray[ 2 ] = pt3; return 3; } //-------------------------------------------------------------------------- s32 GraphicsDrawing::BuildRectangle( const math::VEC2& pt1, const math::VEC2& pt2, const math::VEC2& pt3, const math::VEC2& pt4, math::VEC2 *pVecArray ) { NW_NULL_ASSERT( pVecArray ); s32 count = 0; count += this->BuildTriangle( pt1, pt2, pt3, &pVecArray[count] ); count += this->BuildTriangle( pt1, pt3, pt4, &pVecArray[count] ); return count; } //-------------------------------------------------------------------------- s32 GraphicsDrawing::BuildRectangle( const math::VEC2& pos, const math::VEC2& size, math::VEC2 *pVecArray ) { NW_NULL_ASSERT( pVecArray ); s32 count = 0; count += this->BuildRectangle( pos, math::VEC2(pos.x + size.x, pos.y), pos + size, math::VEC2(pos.x, pos.y + size.y), pVecArray ); return count; } //-------------------------------------------------------------------------- s32 GraphicsDrawing::BuildLine( const math::VEC2& pt1, const math::VEC2& pt2, f32 lineWidth, math::VEC2 *pVecArray ) { NW_ASSERT( lineWidth >= 0.f ); NW_NULL_ASSERT( pVecArray ); math::VEC2 direction = pt1 - pt2; math::VEC2 ortho = math::VEC2( direction.y, - direction.x ); VEC2Normalize( &ortho, &ortho ); ortho *= lineWidth; s32 count = 0; count += this->BuildRectangle( pt1 - ortho / 2, pt1 + ortho / 2, pt2 + ortho / 2, pt2 - ortho / 2, pVecArray ); return count; } //-------------------------------------------------------------------------- void GraphicsDrawing::BuildColor( const s32 vertexCount, const ut::Color8 color, f32* pColorArray ) { ut::FloatColor floatColor = color; for ( s32 index = 0; index < vertexCount; index++ ) { s32 colorIndex = 4 * index; pColorArray[ colorIndex ] = floatColor.r; pColorArray[ colorIndex + 1] = floatColor.g; pColorArray[ colorIndex + 2] = floatColor.b; pColorArray[ colorIndex + 3] = floatColor.a; } } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeFontFromBinary( void* shaderBinary, size_t shaderSize, void* fontBinary, size_t fontSize, void* drawBuffer, void* stringBuffer ) { if ( ! this->InitializeFontResource( fontBinary, fontSize ) ) { return false; } if ( ! this->InitializeFontShader( shaderBinary, shaderSize ) ) { return false; } // 描画用バッファを設定します。 m_Font.SetDrawBuffer(drawBuffer); m_TextWriter.SetFont( &m_Font ); m_TextWriter.SetFontSize( FONT_SIZE, FONT_SIZE ); m_TextWriter.EnableFixedWidth( true ); m_TextWriter.SetFixedWidth( FONT_FIXED_WIDTH ); nw::font::DispStringBuffer *const pDrawStringBuf = nw::font::CharWriter::InitDispStringBuffer(stringBuffer, m_MaxTextCount); m_TextWriter.SetDispStringBuffer(pDrawStringBuf); return true; } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeFontResource( void* fontData, size_t fontSize ) { NW_UNUSED_VARIABLE( fontSize ); // フォントリソースをセットします。 return m_Font.SetResource( fontData ); } //-------------------------------------------------------------------------- bool GraphicsDrawing::InitializeFontShader( void* shaderBinary, size_t shaderSize ) { m_Drawer.Initialize(m_FontCommandBuffer, shaderBinary, shaderSize); return true; } //-------------------------------------------------------------------------- void GraphicsDrawing::SendFontMatrix() { //---- 射影行列を正射影に設定 { // 左上原点とし、Y軸とZ軸の向きが逆になるように設定します。 math::MTX44 proj; const f32 znear = 0.0f; const f32 zfar = - 1.0f; const f32 t = 0; f32 b = static_cast( m_ScreenSize.y ); const f32 l = 0; f32 r = static_cast( m_ScreenSize.x ); math::MTX44OrthoPivot( &proj, l, r, b, t, znear, zfar, math::PIVOT_UPSIDE_TO_TOP ); m_Drawer.SetProjectionMtx( proj ); } //---- モデルビュー行列を単位行列に設定 { nn::math::MTX34 mv; nn::math::MTX34Identity(&mv); m_Drawer.SetViewMtxForText(mv); } } } /* namespace demo */ } /* namespace nw */