• Advertisement
Sign in to follow this  

UV Texture Coordinate problem

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having some problems getting texture coordinates to work. I've got a simple square quad that I'm texturing. My texture loads fine, however, changing the UV texture coordinates for the quad has no effect on the texture. Regardless of what I change the texture coordinates to, the texture is always tiled repeatedly over the surface of the quad, rotated 90o clockwise and smeary on the bottom right half of the triangle. I've included a picture to show what I mean: textured quad I'm not sure what could be causing the problem so not sure what code samples to provide but I'll start with the vertex declerations and an example of how I'm creating vertex data: Vertex Decl:
struct VertexTex 
{ 
 VertexTex():pos(0.0f, 0.0f, 0.0f), col(0x00000000), tex0(0.0f, 0.0f){} 
 VertexTex(float x, float y, float z, D3DCOLOR c, float u, float v):pos(x,y,z), col(c), tex0(u,v){} 
 VertexTex(const D3DXVECTOR3& v, D3DCOLOR c, const D3DXVECTOR2& uv):pos(v),col(c),tex0(uv){} 
 
 D3DXVECTOR3 pos; 
 D3DCOLOR col; 
 D3DXVECTOR2 tex0; 
 static IDirect3DVertexDeclaration9* Decl; 
}; 
 D3DVERTEXELEMENT9 VertexTexElements[] = 
 { 
  {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, 
  {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, 
  {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, 
  D3DDECL_END() 
 }; 
 Device->CreateVertexDeclaration(VertexTexElements, &d3d::VertexTex::Decl); 
and here's how I'm making vertex data (the vector2 at the end being the UV coords):
  vbLeavesLocal[(iLeaf * 4)] = d3d::VertexTex(D3DXVECTOR3(vStartPos.x, vStartPos.y, vStartPos.z), cTint , D3DXVECTOR2(0.0f, 0.0f)); 
  vbLeavesLocal[(iLeaf * 4) + 1] = d3d::VertexTex(D3DXVECTOR3(vStartPos.x, vStartPos.y, vStartPos.z), cTint, D3DXVECTOR2(0.0f, 1.0f)); 
  vbLeavesLocal[(iLeaf * 4) + 2] = d3d::VertexTex(D3DXVECTOR3(vStartPos.x, vStartPos.y, vStartPos.z), cTint, D3DXVECTOR2(1.0f, 0.0f)); 
  vbLeavesLocal[(iLeaf * 4) + 3] = d3d::VertexTex(D3DXVECTOR3(vStartPos.x, vStartPos.y, vStartPos.z), cTint, D3DXVECTOR2(1.0f, 1.0f)); 
If anyone has any idea what might be going wrong here or needs any other specific code to look at, please let me know. I've been wracking my brain but can't see what's going wrong here. Cheers for any help.

Share this post


Link to post
Share on other sites
Advertisement
Are those your actual vertex positions? They're all the same point... It looks to me like one of your texture coordinates are wrong, but without the correct vertex coordinates, we can't tell which one [smile]

Share this post


Link to post
Share on other sites
I agree with the observation that this can't be the code to setup the vertex positions - but I'm assuming that's just a cut and paste error.

As far as your UVs, one problem seems to be the offset you're telling DirectX to use when pulling out the UV data. You've told it the UV data starts 24 bytes into the vertex structure - but given a D3DCOLOR is only a DWORD (4 bytes), it should be 12 + 4 = 16, like this I think:

D3DVERTEXELEMENT9 VertexTexElements[] =

{

{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},

{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},

{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},

D3DDECL_END()

};

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Are those your actual vertex positions? They're all the same point... It looks to me like one of your texture coordinates are wrong, but without the correct vertex coordinates, we can't tell which one [smile]


Haha, yeah, the vertex positions all start off as the same point because I make them expand over time until they're fully 'stretched' out. They end up like they do in the screen shot as a fully spaced out quad. As far the texture coordinates, I've tried every possible combination of coordinates and none of them make any difference, the result is always the same. It's as if they're just being ignored by the program for some reason.

Share this post


Link to post
Share on other sites
Quote:
Original post by Milkshake
I agree with the observation that this can't be the code to setup the vertex positions - but I'm assuming that's just a cut and paste error.

As far as your UVs, one problem seems to be the offset you're telling DirectX to use when pulling out the UV data. You've told it the UV data starts 24 bytes into the vertex structure - but given a D3DCOLOR is only a DWORD (4 bytes), it should be 12 + 4 = 16, like this I think:

D3DVERTEXELEMENT9 VertexTexElements[] =

{

{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},

{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},

{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},

D3DDECL_END()

};


Ohhhh! Such a silly mistake! Correcting this completly solved it! Thanks a lot :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement