DrawIndexedPrimitive and texcoords

Started by
5 comments, last by neeker 10 years, 8 months ago

Hi,

I'm fairly new to DX, but I'll try not to be confusing. I'm having a problem with applying a texture with a shader when I use DrawIndexedPrimitive and I suspect I'm not setting up my texture coordinates correctly. Currently, the shader is coloring the entire polygon with pixel (0,0).

The shader works fine with DrawPrimitive calls and the texture gets applied correctly if I used the fixed pipeline and call DrawIndexedPrimitive, so I'm a little lost on what might be going wrong.

My vertex is pretty basic:

struct MyVert {

float[3] vert;

float[3] norm;

float[2] tc;

};

Anyone have any ideas where to start?

Advertisement

What does your vertex declaration look like? What values are going into your vertex buffer? What values are going into your index buffer? What do your vertex and pixel shaders look like?

The vertex decl is:


   D3DVERTEXELEMENT9 decl[MAX_VERTEXDECL];
   size_t uDeclCount = 1;

   // Position declaration
   decl[0].Stream = 0;
   decl[0].Offset = 0;
   decl[0].Type = D3DDECLTYPE_FLOAT3;
   decl[0].Method = D3DDECLMETHOD_DEFAULT;
   decl[0].Usage = D3DDECLUSAGE_POSITION;
   decl[0].UsageIndex = 0;

   // Normal declaration
   if ((FVF & D3DFVF_NORMAL) != 0) {

      decl[uDeclCount].Stream = 0;
      decl[uDeclCount].Offset = sizeof(float) * 3;
      decl[uDeclCount].Type = D3DDECLTYPE_FLOAT3;
      decl[uDeclCount].Method = D3DDECLMETHOD_DEFAULT;
      decl[uDeclCount].Usage = D3DDECLUSAGE_NORMAL;
      decl[uDeclCount].UsageIndex = 0;
      uDeclCount++;
   }
   
   // Texture declaration
   if ((FVF & D3DFVF_TEX1) != 0) {
      decl[uDeclCount].Stream = 0;
      decl[uDeclCount].Offset = sizeof(float) * 3 * uDeclCount;
      decl[uDeclCount].Type = D3DDECLTYPE_FLOAT2;
      decl[uDeclCount].Method = D3DDECLMETHOD_DEFAULT;
      decl[uDeclCount].Usage = D3DDECLUSAGE_TEXCOORD;
      decl[uDeclCount].UsageIndex = 0;
      uDeclCount++;
   }

   // End of declarations
   decl[uDeclCount].Stream = 0xFF;
   decl[uDeclCount].Offset = 0;
   decl[uDeclCount].Type = D3DDECLTYPE_UNUSED;
   decl[uDeclCount].Method = 0;
   decl[uDeclCount].Usage = 0;
   decl[uDeclCount].UsageIndex = 0;

The values for the vertex buffer are procedurally generated, so posting those exactly would just be noisy. But, you can imagine a cuboid:


v4 v5
+---+
/| /|
/ | / |
/ |/ |
/ / |
/ /| |
v2 +---+v3 |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | +---+v7
| |/v6 /
| | /
| /| /
| / | /
|/ |/
+---+
v0 v1

With indices:

4, 2, 0
0, 6, 4
1, 3, 7
5, 7, 3
1, 2, 0
1, 3, 2
5, 6, 4
5, 7, 6
3, 2, 4
3, 4, 5

The shaders are extremely simple:


struct VS_INPUT {
    float4 position : POSITION;
    float3 normal : NORMAL;
    float2 Texture : TEXCOORD0;
};

struct VS_OUTPUT {
	float4 position : POSITION;
	float2 Texture : TEXCOORD0;
	float3 normal : TEXCOORD1;
	float4 color : COLOR;
};

float4x4 matWVP;
float4x4 matWorld;
bool bIsSelected;

VS_OUTPUT vs_main(in VS_INPUT input)
{
	VS_OUTPUT output;
	
	output.position = mul(float4(input.position.xyz, 1.0f), matWVP);
	output.Texture = input.Texture;
	output.normal = normalize(mul(input.normal, matWorld));
	output.color = float4(1.0f, 1.0f, 1.0f, 1.0f);
	
	return output;
}


struct PS_INPUT {
	float4 position : VPOS;
	float2 Texture : TEXCOORD0;
	float3 normal : TEXCOORD1;
	float4 color : COLOR0;
};

struct PS_OUTPUT {
	float4 color : COLOR0;
};

sampler2D Tex0;

PS_OUTPUT ps_main(in PS_INPUT input)
{
	PS_OUTPUT output;

	output.color = tex2D(Tex0, input.Texture);

	return output;
}

I haven't messed with shaders in a good while, but don't you need a 'sampler_state' struct setup?


Texture2D texture;
sampler2D sampler = 
sampler_state
{
    Texture = texture;
     mipfilter = LINEAR;
}

something like that? I could be wrong.

You're doing the Declaration the hard way


 
D3DVERTEXELEMENT9 decl[] =
{
{ 0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0 },
{ 0,12,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0 },
{ 0,24,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0 },
D3DDECL_END()
};
 
// That's much neater =)
// And if you have different types of vertex declarations, use flags to distinguish each one.

and a little minor note, on calculating your normal in the vertex shader


    output.normal = normalize(mul(input.normal, matWorld));
 
// You might want to do this instead
output.normal = normalize( mul(float4( input.normal,0 ), matWorld) );
 
// So that you don't get all of the position information from the matrix, you just want the rotation stuff
// Unless it automatically puts a '0' there for you and I didn't know that, in which case you're fine :P

I haven't messed with shaders in a good while, but don't you need a 'sampler_state' struct setup?


Texture2D texture;
sampler2D sampler = 
sampler_state
{
    Texture = texture;
     mipfilter = LINEAR;
}

something like that? I could be wrong.

I was hoping that was it, but no luck. Again, the shader works fine if I use it with a DrawPrimitive call.

Did you set index buffer before draw call?

Any warning/error messages from debug runtimes?

Did you set index buffer before draw call?

Any warning/error messages from debug runtimes?

Yes and no, in that order.

This topic is closed to new replies.

Advertisement