0
\$\begingroup\$

I have an object and I can render it but I want to use its vertices twice but I don't know how to.

this is my code reading object from txt:

fin.open("piyon.txt");
fin >> vertexCountpiyon;
verticespiyon = new SimpleVertex[vertexCountpiyon];

for(int i=0; i<vertexCountpiyon; i++)
{
    fin >> verticespiyon[i].Pos.x >> verticespiyon[i].Pos.y >> verticespiyon[i].Pos.z;
    fin >> verticespiyon[i].Tex.x >> verticespiyon[i].Tex.y;
    fin >> verticespiyon[i].Normal.x >> verticespiyon[i].Normal.y >> verticespiyon[i].Normal.z;
}   
fin.close();

bd.ByteWidth = sizeof( SimpleVertex ) * vertexCountpiyon;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = verticespiyon;
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer_piyon );
if( FAILED( hr ) )     return hr;

and my render code:

  g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer_piyon, &stride, &offset );
    cBuffer.vMeshColor = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ); 
    XMMATRIX mTranslateBeyazPiyon = XMMatrixTranslation( -17.5F, 0, -12.5F );
    cBuffer.mWorld = XMMatrixTranspose( mTranslateBeyazPiyon );
    g_World_Piyon = mTranslateBeyazPiyon;
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cBuffer, 0, 0 );
    g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
    g_pImmediateContext->VSSetConstantBuffers( 2, 1, &g_pConstantBuffer );
    g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
    g_pImmediateContext->PSSetConstantBuffers( 2, 1, &g_pConstantBuffer );
    g_pImmediateContext->Draw( 7050, 0 );
\$\endgroup\$
  • \$\begingroup\$ Are you talking about instancing? \$\endgroup\$ – MichaelHouse May 12 '14 at 14:32
1
\$\begingroup\$

You must call IASetVertexBuffers function once on the vertices, but call the Draw function twice. Also set the transformation matrix before each draw call and update the constant buffer (UpdateSubResource). You don't need to set your shaders or constant buffers twice.
That is it if you want to render your vertices twice, if you want to use them twice for something else on the CPU you should duplicate them. The best would be if you didn't duplicate them and used them only on the GPU if you could get away with that.

Update:
Seems like you also have to write hlsl shaders: a vertex shader which will be automatically run for each of your vertices, and a pixel shader which will determine the output color of your rasterized geometry (short story: you can alter the pixel colors). It would be too much to write down everything like how to create the shader files, compile them, etc look up a tutorial for that, for example RasterTek tutorials. In the shader you have to declare a constant buffer on a specific slot which you upload to in you c++ program and it should be have the same members as in the type of your cBuffer variable's structure, like for example:

cbuffer BUFFERNAME:register(b2) //a constant buffer bound to the second slot which you are already uploading data to
{
 float4 mMeshColor;
 float4x4 mWorld;
}

Then in your entry point function you will have to multiply your vertices' positions with your world matrix:

float4 main(float4 inPos:POSITION):SV_POSITION
{
 return mul( mul(inPos,mWorld),YourViewProjectionMatrix ); //YourViewProjectionMatrix is a float4x4 which you also upload in a constant buffer like the world matrix
}

This short example doesn't accunt for the color, just transforms a vertex by world position, then transforms and projects it by the camera matrix then returns the value which will be used by the rasterizer.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I just want two objects but during the game I want to translate them independently. \$\endgroup\$ – Hakan SONMEZ May 11 '14 at 19:30
  • \$\begingroup\$ Do you have a working shader where you can translate one "object"? \$\endgroup\$ – János Turánszki May 11 '14 at 19:35
  • \$\begingroup\$ I don't have because I don't know translating in directx. I have the object and I can render it. I don't find translate tutorial \$\endgroup\$ – Hakan SONMEZ May 11 '14 at 19:38
  • \$\begingroup\$ Oh then I will update my answer. \$\endgroup\$ – János Turánszki May 11 '14 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.