Home Technical Talk
The BRAWL² Tournament Challenge has been announced!

It starts May 12, and ends Sept 12. Let's see what you got!

https://polycount.com/discussion/237047/the-brawl²-tournament

HLSL handling cases where texture isn't loaded

polycounter lvl 20
Offline / Send Message
kodde polycounter lvl 20
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
    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.
    1. bool specularMapEnabled
    2. <
    3. //string UIType = "CheckButton";
    4. string UIName = "Specular Map Enabled";
    5. > = false;
    6.  
    7. // Then in the PixelShader
    8.  
    9. if (specularMapEnabled)
    10. {
    11. // Something like this
    12. float4 SpecularMapColor = tex2D(specularMapSampler, input.texCoord);
    13. // ...
    14. }
    15. else
    16. {
    17. // ...
    18. }
  • kodde
    Offline / Send Message
    kodde polycounter lvl 20
    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
    Offline / Send Message
    Xoliul polycounter lvl 16
    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
    Offline / Send Message
    KikiAlex polycounter lvl 18
    You can use a one instruction line :
    1. diffuseColor = tex2D(g_TopSampler,IN.UV0.xy);
    2.  
    3. diffuseColor = (diffuseColor != 0) ? diffuseColor : float4(1,1,1,1);
    the ?: translates to 1 asm instruction
  • Xoliul
    Offline / Send Message
    Xoliul polycounter lvl 16
    What if you really have pure black in your diffuse then ? That's gonna look really weird...
Sign In or Register to comment.