Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:58:36+00:00 2026-05-25T12:58:36+00:00

I’m trying to make a rather basic 2D Engine with Direct3D. I made a

  • 0

I’m trying to make a rather basic 2D Engine with Direct3D.

I made a LoadImage() function which stores all the rather static behaviour of the image in an object. (Shaders, Vertexbuffers, Samplers etc)

I am planning to do the positioning of the vertices with matrices in constant buffers.

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?

I hope my question is clear enough, if you have any doubts look at the code below.

bool GameManager::GMLoadImage(Image* pImage, const char* pkcFilePath, ImageDesc* pImDesc)
{
    pImage = new Image();

    ID3D11ShaderResourceView* pColorMap = (pImage)->GetpColorMap();


/// CREATE SHADER RESOURCE VIEW (from file) ///
    HRESULT result = D3DX11CreateShaderResourceViewFromFileA(m_pDevice,
                                                             pkcFilePath,
                                                             0,
                                                             0,
                                                             &pColorMap,
                                                             0);
    if (FAILED(result)) {
        MessageBoxA(NULL,"Error loading ShaderResourceView from file","Error",MB_OK);
        return false;
    }


/// RECEIVE TEXTURE DESC ///
    ID3D11Resource* pColorTex;
    pColorMap->GetResource(&pColorTex);
    ((ID3D11Texture2D*)pColorTex)->GetDesc(&((pImage)->GetColorTexDesc()));
    pColorTex->Release();

/// CREATE VERTEX BUFFER ///
    D3D11_TEXTURE2D_DESC colorTexDesc = pImage->GetColorTexDesc();
    float halfWidth = static_cast<float>(colorTexDesc.Width)/2.0f;
    float halfHeight = static_cast<float>(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(&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->GetpVertexBuffer();
    result = m_pDevice->CreateBuffer(&vertexDesc,&resourceData,&pVBuffer);

    if (FAILED(result))
    {
        MessageBoxA(NULL,"Error Creating VBuffer","Error",MB_OK);
        return false;
    }



/// SET POINTER TO IMAGEDESC
    ImageDesc* pThisImDesc = pImage->GetpImageDesc();
    pThisImDesc = pImDesc;

    return true;
}

bool GameManager::GMDrawImage(Image* pImage, const CLIPRECT& rkClip)
{
    ImageDesc* thisImDesc = pImage->GetpImageDesc();

    if ( (thisImDesc != m_pImDesc) ) {
        m_pImDesc = thisImDesc;
        m_pContext->IASetInputLayout(m_pImDesc->pInputLayout);
        m_pContext->IASetPrimitiveTopology(m_pImDesc->Topology);

        m_pContext->VSSetShader(m_pImDesc->pSolidColorVS,0,0);
        m_pContext->PSSetShader(m_pImDesc->pSolidColorPS,0,0);
        m_pContext->PSSetSamplers(0,1,&m_pImDesc->pSampler);
        m_pContext->OMSetBlendState(m_pImDesc->pBlendState,NULL,0xFFFFFFFF);
    }

    UINT stride = m_pImDesc->VertexSize;
    UINT offset = 0;
    ID3D11Buffer* pVBuffer = pImage->GetpVertexBuffer();
    ID3D11ShaderResourceView* pColorMap = pImage->GetpColorMap();

    m_pContext->IASetVertexBuffers(0,1,&pVBuffer,&stride,&offset);
    m_pContext->PSSetShaderResources(0,1,&pColorMap);

    //set constant buffers?

    m_pContext->Draw(6,0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:58:36+00:00Added an answer on May 25, 2026 at 12:58 pm

    Yes, as long as your texture coordinates are hardcoded to 0.0 through 1.0 in your vertex buffer, you can use a texture transformation matrix. It’s a 3×3 matrix that will transform your 2D texture coordinates.

    For instance, if you want to use the bottom-right quadrant of your texture (assuming top-left is origin), you could use the following matrix:

    0.5 0.0 0.0
    0.0 0.5 0.0
    0.5 0.5 1.0
    

    Then, in the vertex shader, you multiply the texture coordinates by that matrix like so:

    float3 coord = float3(In.texCoord, 1.0);
    coord *= textureTransform;
    Out.texCoord = coord.xy / coord.z;
    

    In.texCoord and Out.texCoord being float2 input and output texture coordinates respectively.

    The division by Z is optional if you are only doing affine transformations (translations, scaling, rotations and skews) so feel free to remove it if not needed.

    To generalize the matrix:

    Sx  0.0 0.0
    0.0 Sy  0.0
    Tx  Ty  1.0
    

    Where Txy is the position of the clip area and Sxy the size of the clip area, in texture coordinates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.