Note that there are some explanatory texts on larger screens.

plurals
  1. PODirect3D11(C++): Updating Texture coordinates in constant buffer?
    primarykey
    data
    text
    <p>I'm trying to make a rather basic 2D Engine with Direct3D.</p> <p>I made a LoadImage() function which stores all the rather static behaviour of the image in an object. (Shaders, Vertexbuffers, Samplers etc)</p> <p>I am planning to do the positioning of the vertices with matrices in constant buffers. </p> <p>However, I would also like to have a DrawImage() function, which would have a parameter to tell what part of the texture should be drawn (clipped), so I would have to update the texture coordinates. Since the vertexbuffer is already pre-defined, I wondered if there is a way to update texture coordinates via a constantbuffer that would be sent to the vertexshader?</p> <p>I hope my question is clear enough, if you have any doubts look at the code below.</p> <pre><code>bool GameManager::GMLoadImage(Image* pImage, const char* pkcFilePath, ImageDesc* pImDesc) { pImage = new Image(); ID3D11ShaderResourceView* pColorMap = (pImage)-&gt;GetpColorMap(); /// CREATE SHADER RESOURCE VIEW (from file) /// HRESULT result = D3DX11CreateShaderResourceViewFromFileA(m_pDevice, pkcFilePath, 0, 0, &amp;pColorMap, 0); if (FAILED(result)) { MessageBoxA(NULL,"Error loading ShaderResourceView from file","Error",MB_OK); return false; } /// RECEIVE TEXTURE DESC /// ID3D11Resource* pColorTex; pColorMap-&gt;GetResource(&amp;pColorTex); ((ID3D11Texture2D*)pColorTex)-&gt;GetDesc(&amp;((pImage)-&gt;GetColorTexDesc())); pColorTex-&gt;Release(); /// CREATE VERTEX BUFFER /// D3D11_TEXTURE2D_DESC colorTexDesc = pImage-&gt;GetColorTexDesc(); float halfWidth = static_cast&lt;float&gt;(colorTexDesc.Width)/2.0f; float halfHeight = static_cast&lt;float&gt;(colorTexDesc.Height)/2.0f; Vertex.PosTex vertices[]= { {XMFLOAT3( halfWidth, halfHeight, 1.0f ), XMFLOAT2( 1.0f, 0.0f )}, {XMFLOAT3( halfWidth, -halfHeight, 1.0f ), XMFLOAT2( 1.0f, 1.0f )}, {XMFLOAT3( -halfWidth, -halfHeight, 1.0f ), XMFLOAT2( 0.0f, 1.0f )}, {XMFLOAT3( -halfWidth, -halfHeight, 1.0f ), XMFLOAT2( 0.0f, 1.0f )}, {XMFLOAT3( -halfWidth, halfHeight, 1.0f ), XMFLOAT2( 0.0f, 0.0f )}, {XMFLOAT3( halfWidth, halfHeight, 1.0f ), XMFLOAT2( 1.0f, 0.0f )} }; D3D11_BUFFER_DESC vertexDesc; ZeroMemory(&amp;vertexDesc,sizeof(vertexDesc)); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexDesc.ByteWidth = sizeof(Vertex.PosTex)*6; D3D11_SUBRESOURCE_DATA resourceData; resourceData.pSysMem = vertices; ID3D11Buffer* pVBuffer = pImage-&gt;GetpVertexBuffer(); result = m_pDevice-&gt;CreateBuffer(&amp;vertexDesc,&amp;resourceData,&amp;pVBuffer); if (FAILED(result)) { MessageBoxA(NULL,"Error Creating VBuffer","Error",MB_OK); return false; } /// SET POINTER TO IMAGEDESC ImageDesc* pThisImDesc = pImage-&gt;GetpImageDesc(); pThisImDesc = pImDesc; return true; } bool GameManager::GMDrawImage(Image* pImage, const CLIPRECT&amp; rkClip) { ImageDesc* thisImDesc = pImage-&gt;GetpImageDesc(); if ( (thisImDesc != m_pImDesc) ) { m_pImDesc = thisImDesc; m_pContext-&gt;IASetInputLayout(m_pImDesc-&gt;pInputLayout); m_pContext-&gt;IASetPrimitiveTopology(m_pImDesc-&gt;Topology); m_pContext-&gt;VSSetShader(m_pImDesc-&gt;pSolidColorVS,0,0); m_pContext-&gt;PSSetShader(m_pImDesc-&gt;pSolidColorPS,0,0); m_pContext-&gt;PSSetSamplers(0,1,&amp;m_pImDesc-&gt;pSampler); m_pContext-&gt;OMSetBlendState(m_pImDesc-&gt;pBlendState,NULL,0xFFFFFFFF); } UINT stride = m_pImDesc-&gt;VertexSize; UINT offset = 0; ID3D11Buffer* pVBuffer = pImage-&gt;GetpVertexBuffer(); ID3D11ShaderResourceView* pColorMap = pImage-&gt;GetpColorMap(); m_pContext-&gt;IASetVertexBuffers(0,1,&amp;pVBuffer,&amp;stride,&amp;offset); m_pContext-&gt;PSSetShaderResources(0,1,&amp;pColorMap); //set constant buffers? m_pContext-&gt;Draw(6,0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload