0
votes

I'm pretty new to DirectX and still learning and I think my lack of understanding is not that advanced to find a good direction to solve my problem.
I'm using here Direct3D-9 and trying to render a texture in certain uv coordinates of a vertex buffer.
Here is the vertex buffer and the uv coordinates:

VertexXYZColorT1 * m_pVB = new  VertexXYZColorT1[10];
m_pVB[0].Set( 0.0f,  0.1f,  0.0f, m_dwCurrColor, 0.5f, 0.5f);
m_pVB[9].Set(-0.5f,  0.1f, -0.5f, m_dwCurrColor, 0.0f, 1.0f);
m_pVB[8].Set( 0.0f,  0.1f, -0.5f, m_dwCurrColor, 0.5f, 1.0f);
m_pVB[7].Set( 0.5f,  0.1f, -0.5f, m_dwCurrColor, 1.0f, 1.0f);
m_pVB[6].Set( 0.5f,  0.1f,  0.0f, m_dwCurrColor, 1.0f, 0.5f);
m_pVB[5].Set( 0.5f,  0.1f,  0.5f, m_dwCurrColor, 1.0f, 0.0f);
m_pVB[4].Set( 0.0f,  0.1f,  0.5f, m_dwCurrColor, 0.5f, 0.0f);
m_pVB[3].Set(-0.5f,  0.1f,  0.5f, m_dwCurrColor, 0.0f, 0.0f);
m_pVB[2].Set(-0.5f,  0.1f,  0.0f, m_dwCurrColor, 0.0f, 0.5f);
m_pVB[1].Set(-0.5f,  0.1f, -0.5f, m_dwCurrColor, 0.0f, 1.0f);

And here is the texture:
enter image description here

Here is a picture to visualize the coordinates:
enter image description here

Now I wonder if there is any way of positioning the texture in certain location of the vertex buffer, i.e top left while the vertex buffer still in the center?

Here is a picture of what I mean:
enter image description here

Now instead of moving the whole vertex buffer to top left as I do here:
enter image description here

I want to draw the texture on top left side of the vertex buffer while keeping the vertex buffer centered so I can draw something like this:
enter image description here

I was reading many articles and also in the book of Introduction to 3D game programming with DirectX 9.0 where he talks about the texturing and that we can set the d3d-device to:

s_lpD3DDev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
s_lpD3DDev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);

And also checking the documentation of Microsoft here:
https://docs.microsoft.com/en-us/windows/desktop/direct3d9/wrap-texture-address-mode

I can't seem to find a good direction to solve my problem and I would really appreciate your help or explanation of how to achieve this goal. Basically my goal is using this texture and draw it 4 times to make a full circle.
I guess it has also to do with rotation the uv coordinates or something like that? or would I need to draw 4 vertex buffers for this and 1 texture for each vertex buffer?

Or am I making things too complicated and it has something to do with telling directx to render 4 textures using this flag?:

const DWORD FVF_XYZCOLORT4 = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX4;

Then how would I rotate this texture to draw a full circle?

Thanks in advance for your help and time!

1
I'm not sure where exactly your problem lies. Do you really have to draw the same triangles again just with different texture coordinates? Can you not just add more vertices to make additional triangles that have appropriate texture coordinates? Or are you searching for an automated approach to generate such triangles?Michael Kenzel
Hey Michael! I'm basically trying to create a circle out of that 1 single texture. So I'm not sure exactly how would I place that same texture 4 times in different angles to create a circle on the same vertex buffer if that makes sense. So basically taking 1 texture and making it 4/4 on that same vertex buffer. I think it might be a simple solution, just that I'm not sure how it should be done.user7867434
For example the last picture in the post, as you can see I do 2/4, but I created multiple vertex buffer which I'm sure it's not the way to go and it was just a test.user7867434
You may want to look into the 'mirror' D3D11_TEXTURE_ADDRESS_MODE (see also this outdated but useful explanation). You can draw your desired image in a single VB submission with clever use of negative UV coordinates. BTW, if you are new to DirectX, take a look at DirectX Tool Kit.Chuck Walbourn

1 Answers

0
votes

You have 2 alternatives:

  1. Create all the vertices you need. (full circle of 1/4 texture would require 4 times vertices of what you have). Just manually compute u,v so you get what you want.

  2. Write your own pixel shader, where you can manipulate u,v as much as you'd like. For example (just a fragment of the code you'll need for using a pixel shader):

    sampler2D Sampler1 = sampler_state 
    {
      MinFilter = linear;
      MagFilter = linear;
      MaxAnisotropy = 16;
      Texture = <g_Tex0>;
    };
    
    void PS1 (float2 TexCoord : TEXCOORD1, out float4 oColor : COLOR0)
    {
      if (g_InvX)
        TexCoord.x = 1-TexCoord.x;
      if (g_InvY)
        TexCoord.y = 1-TexCoord.y;
      oColor = tex2D(Sampler1 , TexCoord),
    };
    

g_Tex0, g_InvX and g_InvY are set by SetTexture and SetBool methods to an ID3DXEffect * object. (That's only a hook. There's a lot to learn for writing and using shaders).