void DrawGridScene( std::shared_ptr<RenderingContext> context, const Matrix4& matWorld ) { Assert( context ); ConstantBuffer& cbuffer = context->GetSharedConstantBuffer(); float scale = model_scale.Float(); size_t columns = grid_size.Int(); float rowFactor = 1.0f / grid_size.Int(); float colFactor = 1.0f / columns; for ( int row = 0; row < grid_size.Int(); ++row ) { for ( size_t col = 0; col < columns; ++col ) { // world transform Matrix4 matTrans; matTrans.Translate( ( ( col + 1 ) * colFactor - colFactor * ( columns * 0.5f ) ) * columns, ( ( row + 1 ) * rowFactor - rowFactor * ( columns * 0.5f ) ) * columns, ( ( row + 1 ) * rowFactor - rowFactor * ( columns * 0.5f ) ) * columns ); Matrix4 matScale; matScale.Scaling( scale, scale, scale ); Matrix4 matNewWorld = matScale * matWorld * matTrans; cbuffer.SetMatrix4( ConstantBuffer::WorldMatrix, matNewWorld ); // various color cbuffer.SetVector4( ConstantBuffer::DiffuseColor, Vector4( max( 0.2f, matTrans.A41 * 0.5f + 0.8f ), max( 0.4f, matTrans.A42 * 0.5f + 0.6f ), matTrans.A43 * 0.5f + 1.0f, 1.0f ) ); context->Draw( GetMeshes()[row * columns + col] ); } } }
void DrawGridSceneInParallel( StlVector<std::shared_ptr<RenderingContext>>& contexts, const Matrix4& matWorld ) { float scale = model_scale.Float(); size_t rows = grid_size.Int(); size_t columns = grid_size.Int(); float rowFactor = 1.0f / grid_size.Int(); float colFactor = 1.0f / columns; //using FuncPreRender = std::function<void( std::shared_ptr<RenderingContext> context, const std::shared_ptr<IMesh>& mesh, size_t index )>; RenderingContext::FuncPreRender funcPreRender( [=]( std::shared_ptr<RenderingContext> context, size_t index ) { ConstantBuffer& cbuffer = context->GetSharedConstantBuffer(); // world transform size_t row = kih::Clamp<size_t>( index / rows, 0, rows - 1 ); size_t col = kih::Clamp<size_t>( index - row * columns, 0, columns - 1 ); Matrix4 matTrans; matTrans.Translate( ( ( col + 1 ) * colFactor - colFactor * ( columns * 0.5f ) ) * columns, ( ( row + 1 ) * rowFactor - rowFactor * ( columns * 0.5f ) ) * columns, ( ( row + 1 ) * rowFactor - rowFactor * ( columns * 0.5f ) ) * columns ); Matrix4 matScale; matScale.Scaling( scale, scale, scale ); Matrix4 matNewWorld = matScale * matWorld * matTrans; cbuffer.SetMatrix4( ConstantBuffer::WorldMatrix, matNewWorld ); // various color cbuffer.SetVector4( ConstantBuffer::DiffuseColor, Vector4( max( 0.2f, matTrans.A41 * 0.5f + 0.8f ), max( 0.4f, matTrans.A42 * 0.5f + 0.6f ), matTrans.A43 * 0.5f + 1.0f, 1.0f ) ); } ); RenderingContext::DrawInParallel( contexts, GetMeshes(), grid_size.Int() * grid_size.Int(), funcPreRender ); }