Jump to content
  • Advertisement
Sign in to follow this  

Using Textures w/ Shaders

This topic is 5179 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

1) I have a very simple vertex and pixel shader that I've been playing around with. In my main app, I set a single texture during each effect pass. In my shader, I have a variable "sample TextureSampler" declared, and I use it in my pixel shader. How does this sample variable work? I have seen examples where the sampler declaration contains a texture and mipfilter initializers, but my shader doesn't specify those. Is it simply specifying the default texture (that is set with my call to .SetTexture(0, _texture)) and the default filter? 2) All that is ever used to render a geometry "chunk" is one or more textures and one or more shader effects, right? Therefore, when I sort my geometry for rendering, I should sort by the attributes of a shader (transparency and parameters), and the textures being used, right? 3) texture tex0 < string name = "tiger\\tiger.bmp"; >; Is a line in the default shader in the Effect Editor. This is just something that the EffectEdit app reads to load textures, right? It is not an actual shader command? 3) I have posted C# and HLSL source snippets below, for reference for the above questions, and for one final question: Am I doing this right? Specifically, should I be using SetTexture() to maintain texture state? - Mike C# Code:
_worldMatrix = Matrix.RotationYawPitchRoll(_angle, 0.0f, 0.0f);
_angle += 0.1f;
Matrix worldViewProj = _worldMatrix * _viewMatrix * _projMatrix;
_time += 0.01f;

_device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, _display.BackColor, 1.0f, 0);
int numPasses = _effect.Begin(0);
for(int pass = 0; pass < numPasses; pass++)
    _effect.SetValue("WorldViewProj", worldViewProj);
    _effect.SetValue("Time", _time);
    _device.SetTexture(0, _texture);



Shader.fx (HLSL):
float Time;
float4x4 WorldViewProj;
sampler TextureSampler;

// Transform our coordinates into world space
void Transform(
    inout float4 Pos  : POSITION, 
    in float3 Normal : NORMAL,
    inout float2 TexCoords : TEXCOORD0,
    out float4 Diffuse : COLOR0 )
    Diffuse.rgba = 1.0f;
    Pos = mul(Pos, WorldViewProj);
    TexCoords = TexCoords;

void PS(in float2 TexCoords : TEXCOORD0,
        inout float4 Diffuse : COLOR0)
	Diffuse *= tex2D(TextureSampler, TexCoords);

technique TransformDiffuse
    pass P0
        // shaders
        VertexShader = compile vs_2_0 Transform();
        PixelShader  = compile ps_2_0 PS();

Share this post

Link to post
Share on other sites
1) I'll explain in number.. uhh, 3-2.

2) Correct, and that's probably a good way to go about it. Then just apply the attributes when they change from object to object.

3-1) Correct, if you want the texture to load, you'll have to get the annotation and load it yourself in your app.

3-2) Okay, this is one way to do it, although you're not quite doing it right ;)

If you want to do what you're doing by using SetTexture, you'll have to declare your sampler in your effect like so:

sampler TextureSampler : register(s0);

This will force the sampler to use the texture in texture stage 0. If I remember correctly, this will make it possible to set the texture with SetTexture(0,texture) and it'll show up in the shader.

There is another way, like so:

texture Texture1;
sampler TextureSampler=sampler_state

This way, you can set the "Texture1" parameter with ID3DXEffect::SetTexture and it'll show up, instead of binding the texture to sampler 0.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!