GraphGame gg0 10 - Instancing

12
GraphGame gg010-Instancing Geometry instancing Szécsi László

description

GraphGame gg0 10 - Instancing. Geometry instancing Szécsi László. gg0 10 - Instancing project. copy-paste-rename gg00 9 - Gui folder vcxproj , filters átnevezés solution /add existing project rename project working dir: $( SolutionDir ) - PowerPoint PPT Presentation

Transcript of GraphGame gg0 10 - Instancing

Page 1: GraphGame gg0 10 - Instancing

GraphGamegg010-Instancing

Geometry instancing

Szécsi László

Page 2: GraphGame gg0 10 - Instancing

gg010-Instancing project

• copy-paste-rename gg009-Gui folder• vcxproj, filters átnevezés• solution/add existing project• rename project• working dir: $(SolutionDir)• Project Properties/Configuration

Properties/Debugging/Command Arguments--solutionPath:"$(SolutionDir)" --projectPath:"$(ProjectDir)"

• build, run

Page 3: GraphGame gg0 10 - Instancing

Game.cpp#include "Mesh/Instanced.h"

Page 4: GraphGame gg0 10 - Instancing

Mesh::Instanced

• index buffer + vertex bufferek + instance bufferek + elemleírás

• konstruktor összebuherálja az egyes bufferekhez kapott elemleírásokat

• draw metóduscontext->IASetIndexBuffer(indexBuffer, indexFormat, 0);context->IASetVertexBuffers(0, nVertexBuffers,

vertexBuffers, vertexStrides, zeros);context->DrawIndexedInstanced(nIndices, nInstances, 0,

0, 0);

Page 5: GraphGame gg0 10 - Instancing

Game::createResourcesusing namespace Egg::Math;Egg::Math::float4x4 modelMatrices[200];

for(int iInstance=0; iInstance<200; iInstance++ )

modelMatrices[iInstance] = float4x4::translation(

float3::random(0, 20) ).transpose();

Page 6: GraphGame gg0 10 - Instancing

Game::createResourcesD3D11_INPUT_ELEMENT_DESC modelMatrixElements[4] = {{ "INSTANCEMODEL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0

* sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1},{ "INSTANCEMODEL", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 1

* sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1},{ "INSTANCEMODEL", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 2

* sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1},{ "INSTANCEMODEL", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 3

* sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1} };

Egg::Mesh::InstanceBufferDesc instanceBufferDesc;instanceBufferDesc.elements = modelMatrixElements;instanceBufferDesc.nElements = 4;instanceBufferDesc.instanceStride = sizeof(float4x4);instanceBufferDesc.instanceData = modelMatrices;

Page 7: GraphGame gg0 10 - Instancing

Game::createResourcesEgg::Mesh::Instanced::P instancedMesh = Egg::Mesh::Instanced::create( device, 200, &instanceBufferDesc, 1, indexedMesh);

ID3DX11EffectPass* basicPass = effect->GetTechniqueByName("instanced")->GetPassByName("instanced");

Egg::Mesh::Material::P envmappedMaterial = Egg::Mesh::Material::create(basicPass, 0);

shadedMesh = binder->bindMaterial(envmappedMaterial, instancedMesh);

Page 8: GraphGame gg0 10 - Instancing

fx/main.fxstruct IaosInstanced{ float4 pos: POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD; float4x4 instanceModelMatrix: INSTANCEMODEL;};

VsosTrafo vsInstanced(IaosInstanced input) { VsosTrafo output = (VsosTrafo)0; output.worldPos = mul(input.pos, input.instanceModelMatrix); output.pos = mul(output.worldPos, modelViewProjMatrix); output.normal = mul( float4(input.normal.xyz, 0.0), input.instanceModelMatrix); output.tex = input.tex; return output;}

#9.0

modellel nem kéneszorozni,

mert ugyemár megvolt

model inverse transposedcsak akkor egyezik meg a modellel,

ha csak eltolás+forgatás+izometrikus skálázás van

Page 9: GraphGame gg0 10 - Instancing

fx/main.fxtechnique11 instanced{ pass instanced { SetVertexShader ( CompileShader( vs_5_0, vsInstanced() ) ); SetPixelShader( CompileShader( ps_5_0, psEnvMapped() ) ); }}

#9.0

Page 10: GraphGame gg0 10 - Instancing

Zsiráfok

Page 11: GraphGame gg0 10 - Instancing

FELADAT: elforgatott zsiráfok

Page 12: GraphGame gg0 10 - Instancing

FELADAT: forgó zsiráfok

• instancedMesh tagvaltozóba– animateből elérjük, releaseResourcesban reset

• animate– instance buffer lekérése– device->GetImmediateContext– instance buffer mappelése– feltöltése időfüggő (de nem random!) adattal– unmap– context release