Game Development Community

Specular for pixel shader 1.1 by Ashu Rege (NVIDIA)

by Cisor · in Torque Game Engine Advanced · 06/14/2004 (11:36 pm) · 4 replies

I found the below ps1.1 shader code for doing specular highlighting in a presentation by Ashu Rege of NVidia. I'm not too sure how to integrate this to TSE, (maybe another pass on pixel shader?) so any thoughts would be appreciated. (I'm a complete shader noob):)

//Per-Pixel Specular Lighting in HLSL (PS 1.1 Model)

sampler normalMap: register(s0);
sampler specularCubeMap: register(s3);
float4 vAmbient;
float4 vSpecular;

float4 main( float4 diffuse : COLOR,
      float2 TexCoord: TEXCOORD0,
      float4 EnvXform[3] : TEXCOORD1 ) : COLOR
{
   float3 N = tex2D(normalMap, TexCoord);
   float3 Nworld;
   Nworld.x= dot(N*2-1, EnvXform[0]);
   Nworld.y= dot(N*2-1, EnvXform[1]);
   Nworld.z= dot(N*2-1, EnvXform[2]);

   float3 Eye;
   Eye.x = EnvXform[0].w;
   Eye.y = EnvXform[1].w;
   Eye.z = EnvXform[2].w;
   float3 R = 2*dot(Nworld,Eye)*NworldEye*dot(Nworld,Nworld);
   float4 specular = texCUBE(specularCubeMap, R);
   return(specular * vSpecular+ diffuse + vAmbient);
}

EDIT : The full presentation is available as a pdf at www.atitech.ca/developer/gdc/D3DTutorial2_FX_HLSL.pdf.

#1
06/15/2004 (9:40 am)
I've seen this method before. I think EnvXform contains the pre-computed camera/incident transform for reflections, basically you pre-calculate and give this data to the pixel shader via a texture. I can't say how you'd go about calculating this or how fast the resulting shader would be, but it should be fairly simple to implement.

Dunno, I've only dabbled in shaders. And even less with TSE.
#2
06/15/2004 (12:42 pm)
Just looking at the code, EnvXform is going to be used to translate the normals (which are in object space) into world space.
#3
06/16/2004 (12:25 pm)
There are a few techniques out there for doing this and similar effects. This one is just essentially doing bump-environment-cubemapping. It's probably the best technique because it works even on large flat polys.

The problem is, it won't work in conjunction with how we've set up the interior lighting. We're using the light-normal maps for specular on the interiors and it's nice because it can do specular on any number of lights in an interior in one pass. It doesn't work for ps 1.x though.

Just the same, it would be good to support this technique as an extra pass for tsShapes on ps 1.x.
#4
06/16/2004 (11:29 pm)
Thanks for the replies, guys, and especially Brian.
Once I get a few more examples under my belt I might give this a bash. Looking at the code, it seems pixspecular.cpp will have to change to cater for this.