[英]C++ shader, HLSL , how to send to the shader , the texture? “sampler2d”?
我开始学习一些使用C ++和HLSL的着色器操作,同时在学习时使用D3DXCompileShaderFromFile进行一些测试
我能够编译并运行一些VertexShader和PixelShader,发送一些属性,例如float,float2,float3,float4,float4x4,以设置worldMatrix,颜色信息等,这是一些示例:
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);
}
与SetMatrix和SetFloat几乎相同,例如:
myShader->SetVector4("Diffuse",Vect4f(1.0f,0.0f,0.0f,1.0f) );
但是我在这样做时遇到了一个问题
如何将纹理发送到“ pixelShader”? 在我的ps中被声明为“ sampler2d”类型,因此我不确定使用什么来发送此信息
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;
}
您是否只需要照常绑定纹理? 我通常不使用Ogre,因此对DirectX api并不熟悉,但是快速查找表明SetTexture(sampler,texture)是可行的方法,其中sampler为采样器寄存器。 您可以根据需要明确指出纹理在着色器中使用的寄存器。
谢谢Pete !,我像您说的那样快速浏览了SetTexture(sampler,texture)文档,并且我能够弄清楚该怎么做,我制作了这个新的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);
}
例如:
myShader->SetTexture("baseMap");
并以某种方式,它的工作原理,我不太清楚为什么,因为我发送到一个空的“纹理”到“ SetTexture”属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.