Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


DrawIndexedPrimitive and texcoords


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 neeker   Members   -  Reputation: 224

Like
0Likes
Like

Posted 09 August 2013 - 11:53 AM

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?

 

 



Sponsor:

#2 Dave Hunt   Crossbones+   -  Reputation: 2479

Like
1Likes
Like

Posted 09 August 2013 - 12:23 PM

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?



#3 neeker   Members   -  Reputation: 224

Like
1Likes
Like

Posted 09 August 2013 - 02:01 PM

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;
}


#4 Muzzy A   Members   -  Reputation: 693

Like
0Likes
Like

Posted 09 August 2013 - 03:10 PM

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

Edited by Muzzy A, 09 August 2013 - 03:15 PM.


#5 neeker   Members   -  Reputation: 224

Like
0Likes
Like

Posted 09 August 2013 - 03:59 PM

 

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.



#6 belfegor   Crossbones+   -  Reputation: 2722

Like
0Likes
Like

Posted 09 August 2013 - 04:24 PM

Did you set index buffer before draw call?

Any warning/error messages from debug runtimes?



#7 neeker   Members   -  Reputation: 224

Like
0Likes
Like

Posted 09 August 2013 - 04:29 PM

Did you set index buffer before draw call?

Any warning/error messages from debug runtimes?

Yes and no, in that order.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS