As i set the vertexbuffers one for the model and the other one for the instance position etc., how does directx knows wich buffer is ment to be the geometrie buffer and which the instance buffer?
I also experienced a graphic bug with using this code:
struct instancedata
{
XMFLOAT3 Position;
};
struct texturedvertex
{
XMFLOAT3 Pos;
XMFLOAT2 uvkoord;
};
struct geometry
{
ID3D11Buffer* vertexbuffer;
int vertsanzahl;
ID3D11Buffer* indicesbuffer;
int indicesanzahl;
};
geometry geometrybuffer;
ID3D11Buffer* instancebuffer = NULL;
ID3D11InputLayout* vertexlayouttextured = NULL;
:::::
init buffers
:::::
inp layout:
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "POSITION", 1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 20, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
};
hr = device->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &vertexlayouttextured );
if( FAILED( hr ) )
return hr;
rendering:
UINT trianglebuff = sizeof(texturedvertex);
UINT instancebuff = sizeof(instancedata);
UINT zerooffset = 0;
immediatecontext->IASetInputLayout( vertexlayouttextured );
immediatecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); //Triangle strip
immediatecontext->IASetVertexBuffers( 0, 1, &geometrybuffer.vertexbuffer ,&trianglebuff , &zerooffset ); //set the geometrybuffer
immediatecontext->IASetVertexBuffers( 1, 1, &instancebuffer ,&instancebuff , &zerooffset ); //set the instance buffer
immediatecontext->VSSetShader( vertexshader, NULL, 0 ); //set vertexshader
immediatecontext->VSSetConstantBuffers( 0, 1, &matrixbuffer ); //containing world view projection matrices
immediatecontext->PSSetShader( pixelshader, NULL, 0 ); //set pixelshader
immediatecontext->PSSetShaderResources(0,1,&textur);
//set textur
immediatecontext->Map( instancebuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
instancedata *data = ( instancedata* )MappedResource.pData;
//map the instance-data
for (int i = 0; i < sys.bodiesl();i++)
{
data[i].Position = XMFLOAT3((float)(sys.hks[i].Position.x / scaler),(float)(sys.hks[i].Position.y / scaler),(float)(sys.hks[i].Position.z / scaler));
}
immediatecontext->Unmap( instancebuffer, 0 );
//update positions
immediatecontext->DrawIndexedInstanced(geometrybuffer.indicesanzahl,sys.bodies(),0,0,0);
//draw as many instances as bodies
hlsl shader:
static const float PI = 3.14159265f;
cbuffer matrix : register( b0 ) //matrices
{
row_major matrix World;
row_major matrix View;
row_major matrix Projection;
};
Texture2D<float4> Textur : register(t0);
SamplerState Meshtexturesampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//textures, sampler
struct VS_INPUT
{
float4 Pos : POSITION0;
float2 Textcoord : TEXCOORD0;
float4 instancepos : POSITION1;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Textcoord : TEXCOORD0;
};
//input structures
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
input.Pos.x += input.instancepos.x;
input.Pos.y += input.instancepos.y;
input.Pos.z += input.instancepos.z;
output.Pos = mul(input.Pos,World);
output.Pos = mul(output.Pos,View);
output.Pos = mul(output.Pos,Projection);
output.Textcoord = input.Textcoord;
return output;
}
float4 PS( PS_INPUT input) : SV_Target
{
return Textur.Sample(Meshtexturesampler,input.Textcoord);
}
Now the problem is that the second instance always stays at the center of the scene but it should move i guess there are some errors in the input layout declaration?

Find content
Not Telling