Home Technical Talk

HLSL handling cases where texture isn't loaded

polycounter lvl 19
Offline / Send Message
kodde polycounter lvl 19
Hey guys,

I'm following Ben Clowards HLSL DVDs and so far I'm at DVD2 at the Specular Lighting chapter.

Here's a question I've been thinking about for a while. In my experience so far when working with shaders (both CGFX in Mental Mill and HLSL with FX Composer) when you have a texture sampler and you don't load a texture it's value defaults to 0 (right?).

As it is now in the DVD I'm implementing Specularity with both a texture, a specular color value and a gloss value. These are all later on multiplied by each other in the end. If I do not load any texture at all this breaks the whole feature since it's value will be 0. So what if I want to give the user the possibility not to load a specular texture at all to have the same uniform amount and color all over the model?

Is there a way to check if a texture has been loaded? Couldn't I then with a simple if statement set this color to 1 if no texture is loaded?

Thanks.

Replies

  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi kodde, as far as I know, you cannot make the code check if a texture has been loaded. The workaround I adopted, and I guess is pretty standard, is to use a checkbutton widget in the UI. If it is checked the texture is taken into account, otherwise do the standard calculations without considering it.
    bool specularMapEnabled
    <
        //string UIType = "CheckButton";
        string UIName = "Specular Map Enabled";
    > = false;
    
    // Then in the PixelShader
    
        if (specularMapEnabled)
        {
            // Something like this
            float4 SpecularMapColor = tex2D(specularMapSampler, input.texCoord);
            // ...
        }
        else
        {
            // ...
        }
    
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Thanks SyncViewS.

    Yeah I used that very same workaround when I was working with Mental Mill. Just thought maybe it could be handled more nicely :)
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    You'd need to have your application (the c++ code) handle this. It's quite doable if you have access to the shader loading code, I remember I did something similar back when I still programmed c++. Unfortunately that's not really a possibility with most applications.
  • KikiAlex
    Options
    Offline / Send Message
    You can use a one instruction line :
     diffuseColor = tex2D(g_TopSampler,IN.UV0.xy);
    
    diffuseColor = (diffuseColor != 0) ? diffuseColor : float4(1,1,1,1);
    
    the ?: translates to 1 asm instruction
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    What if you really have pure black in your diffuse then ? That's gonna look really weird...
Sign In or Register to comment.