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] ); } } }
KIHX_API void kiLoadMeshFromFile( const char* filename ) { auto& meshes = GetMeshes(); meshes.clear(); meshes.emplace_back( kih::CreateMeshFromFile( filename ) ); if ( meshes[0] == nullptr ) { meshes.clear(); return; } for ( int i = 0; i < 6 * 6 - 1; ++i ) { auto mesh = meshes[0]->Clone(); if ( mesh ) { meshes.push_back( mesh ); } else { meshes.emplace_back( kih::CreateMeshFromFile( filename ) ); } } }
bool SGMExporter::DoExport(const TCHAR *name, ExpInterface *ei, Interface *max_interface) { scene = GetIGameInterface(); assert(scene != NULL); std::string sceneName = StringUtils::ToNarrow(scene->GetSceneFileName()); sceneName.replace(sceneName.find(".max"), 4, ""); fileName = StringUtils::ToNarrow(name) + sceneName + ".skm"; Log::StartLog(true, false, false); Log::LogT("=== exporting skinned mesh to file '%s'", fileName.c_str()); scene->SetStaticFrame(0); IGameConversionManager *cm = GetConversionManager(); assert(cm != NULL); cm->SetCoordSystem(IGameConversionManager::IGAME_OGL); if (!scene->InitialiseIGame(false)) { Log::LogT("error: couldnt initialize scene"); return false; } meshesCount = 0; std::ofstream fileStream(fileName.c_str(), std::ios::binary); BinaryWriter bw(&fileStream); /* 1.2 - vertex channels in mesh part */ bw.Write("FTSMDL", 6); bw.Write((unsigned short)((1 << 8) | 2)); // version 1.2 bw.Write((int)0); std::vector<Scene3DMesh*> meshes; if (!GetMeshes(meshes, &bw)) return false; fileStream.seekp(8, std::ios::beg); //fileStream.seekp(0, std::ios::beg); bw.Write((int)meshesCount); fileStream.close(); return true; }
void BlendedAnimationsWindow::CreateScene() { mWireState = std::make_shared<RasterizerState>(); mWireState->fillMode = RasterizerState::FILL_WIREFRAME; mScene = std::make_shared<Node>(); mScene->AttachChild(mManager->GetRoot()); // Create a floor to walk on. VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_NORMAL, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mFloor = mf.CreateRectangle(2, 2, 1024.0f, 2048.0f); mFloor->name = "Floor"; mScene->AttachChild(mFloor); std::shared_ptr<VertexBuffer> vbuffer = mFloor->GetVertexBuffer(); vbuffer->SetUsage(Resource::DYNAMIC_UPDATE); unsigned int numVertices = vbuffer->GetNumElements(); Vertex* vertex = vbuffer->Get<Vertex>(); for (unsigned int i = 0; i < numVertices; ++i) { vertex[i].tcoord[0] *= 64.0f; vertex[i].tcoord[1] *= 256.0f; } std::string textureName = mEnvironment.GetPath("Grating.png"); std::shared_ptr<Texture2> texture(WICFileIO::Load(textureName, true)); texture->AutogenerateMipmaps(); std::shared_ptr<Texture2Effect> effect = std::make_shared<Texture2Effect>( mProgramFactory, texture, SamplerState::MIN_L_MAG_L_MIP_L, SamplerState::WRAP, SamplerState::WRAP); mFloor->SetEffect(effect); mCameraRig.Subscribe(mFloor->worldTransform, effect->GetPVWMatrixConstant()); for (auto const& subscriber : mManager->GetSubscribers()) { mCameraRig.Subscribe(subscriber.first->worldTransform, subscriber.second); } GetMeshes(mScene); mTrackball.Attach(mScene); mTrackball.Update(mApplicationTime); }
void BlendedAnimationsWindow::GetMeshes( std::shared_ptr<Spatial> const& object) { Visual* mesh = dynamic_cast<Visual*>(object.get()); if (mesh) { mMeshes.push_back(mesh); return; } Node* node = dynamic_cast<Node*>(object.get()); if (node) { for (int i = 0; i < node->GetNumChildren(); ++i) { auto const& child = node->GetChild(i); if (child) { GetMeshes(child); } } } }
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 ); }
KIHX_API void kiRenderToBuffer( void* buffer, int width, int height, int bpp ) { if ( buffer == nullptr ) { return; } __UNDONE( temporal testing code ); // Create color and depth stencil buffers. ColorFormat colorFormat = kih::GetSuitableColorFormatFromBpp( bpp ); static std::shared_ptr<Texture> renderTarget = Texture::Create( width, height, colorFormat, buffer ); static std::shared_ptr<Texture> depthStencil = Texture::Create( width, height, ColorFormat::D32F, NULL ); static std::shared_ptr<UnorderedAccessView<OutputMergerInputStream>> omUAV = std::make_shared<UnorderedAccessView<OutputMergerInputStream>>( width * height * 2 ); // Create rendering contexts as many of Thread::HardwareConcurrency. const int Concurrency = 8; // HACK static std::array<std::shared_ptr<RenderingContext>, Concurrency> contexts; if ( contexts[0] == nullptr ) { int index = 0; LoopUnroll<Concurrency>::Work( [&index]() { auto context = RenderingDevice::GetInstance()->CreateRenderingContext(); context->SetViewport( 0, 0, static_cast<unsigned short>( renderTarget->Width() ), static_cast<unsigned short>( renderTarget->Height() ) ); context->SetFixedPipelineMode( false ); context->SetRenderTarget( renderTarget, 0 ); context->SetDepthStencil( depthStencil ); contexts[index++] = context; } ); } auto context = contexts[0]; context->Clear( 0, 0, 0, 255 ); context->SetUnorderedAccessView( nullptr ); // Draw the world. const Matrix4& matWorld = RenderingDevice::GetInstance()->GetWorldMatrix(); // Grid arragement if ( grid_scene.Bool() ) { // threading or not if ( concurrency.Int() <= 1 ) { DrawGridScene( context, matWorld ); } else { // Copy array contexts to vector one. StlVector<std::shared_ptr<RenderingContext>> concurrencyContexts; concurrencyContexts.resize( concurrency.Int() - 1 ); // the other thread is for resolve. for ( size_t i = 0; i < concurrencyContexts.size(); ++i ) { contexts[i]->SetUnorderedAccessView( omUAV ); concurrencyContexts[i] = contexts[i]; } DrawGridSceneInParallel( concurrencyContexts, matWorld ); } } else { context->GetSharedConstantBuffer().SetMatrix4( ConstantBuffer::WorldMatrix, matWorld ); context->GetSharedConstantBuffer().SetVector4( ConstantBuffer::DiffuseColor, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) ); context->Draw( GetMeshes()[0] ); } }