0

im starting learning some Shader manipulation witg C++ and HLSL, im using D3DXCompileShaderFromFile to do some tests while am learning

i was able to compile and run some VertexShader and PixelShader, send some Attributes, like float, float2, float3, float4 , float4x4 , to set worldMatrix ,Color information ,etc, here is some example:

void CShader::SetVector4(char name[], Vect4f vect)
{
D3DXVECTOR4 v(vect.x,vect.y,vect.z,vect.w);
D3DXHANDLE h = m_pConstantTable->GetConstantByName(NULL, name);
m_pConstantTable->SetVector(m_pDevice, h, &v);
}

almost the same thing with SetMatrix and SetFloat, ex:

myShader->SetVector4("Diffuse",Vect4f(1.0f,0.0f,0.0f,1.0f) );

but i ran to a problem doing so Basic Light shader

how can i send a texture to my "pixelShader"? in my ps is declared as "sampler2d" type, so im not sure what to use to send this information

float4  Diffuse;
float4  Ambient;
sampler2D baseMap;


float4 Main(float3 L: TEXCOORD0, float3 N: TEXCOORD1,float2 texCoord: TEXCOORD2 ) : COLOR
{
  float facingRatio =  dot(L, N);
  float4 tex = tex2D( baseMap, texCoord );

  return tex * Diffuse * facingRatio + Ambient * tex;

}

2 Answers 2

3

Don't you just need to bind the texture as normal? Not familiar with the directx api as I normally use Ogre, but quick look up suggests SetTexture(sampler, texture) is the way to go, with sampler being the sampler register. You can explicitly say which register the texture uses in the shader if you want.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Pete!, i took a quick look on SetTexture(sampler, texture) documentation like you said, and i was able to figure out what to do, i made this new setter

void CShader::SetTexture(char name[])
{
    IDirect3DBaseTexture9* texture = NULL;
    D3DXHANDLE h = m_pConstantTable->GetConstantByName(NULL, name);

    int tex_idx = m_pConstantTable->GetSamplerIndex(h);

    m_pDevice->SetTexture(tex_idx, texture);    
    m_pDevice->SetSamplerState(tex_idx, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    m_pDevice->SetSamplerState(tex_idx, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    m_pDevice->SetSamplerState(tex_idx, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}

ex:

myShader->SetTexture("baseMap");

and somehow, it works, im not quite sure why , because i send to the "SetTexture" property an empty "texture"

1 Comment

Looks reasonable apart from the NULL texture. If it is actually getting the texture you want then you must be sending it to the GPU elsewhere and it is some sort of fluke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.