/////////////////////////////////////////////////////////////////////////////// // // ## ###### // ###### ### // ## ############### Shark 3D Engine (www.shark3d.com) // ########## # # # // ######## Copyright (c) 1996-2006 Spinor GmbH. // ######### # # # All rights reserved. // ## ########## // ## // /////////////////////////////////////////////////////////////////////////////// #ifndef STDDEF_D3D9_HLSL_SC #define STDDEF_D3D9_HLSL_SC /////////////////////////////////////////////////////////////////////////////// #include \ /////////////////////////////////////////////////////////////////////////////// // Compression /////////////////////////////////////////////////////////////////////////////// float3 s3d_uncomprNormal( float3 comprNormal) { #ifdef S3D_COMPR_NORMAL return comprNormal / 127.5 - 1.0; #else return comprNormal; #endif } float4 s3d_uncomprBoneWgh( float4 comprBoneWgh) { #ifdef S3D_COMPR_BONEWGH return comprBoneWgh / 255.0f; #else return comprBoneWgh; #endif } /////////////////////////////////////////////////////////////////////////////// // Bones /////////////////////////////////////////////////////////////////////////////// // default to 4 for bone transformation w/o bone subscriptions #ifndef S3D_MATBONE_CNT #define S3D_MATBONE_CNT 4 #endif // works w/ compressed and uncompressed bone weights. void s3d_calcBoneTransfSubscr( out float4x4 matBoneFinal, const float4x4 matBone[S3D_MATBONE_CNT], const float4 boneWgh, const int4 boneSubscr) { float4 uncomprBoneWgh = s3d_uncomprBoneWgh(boneWgh); matBoneFinal = uncomprBoneWgh.x * matBone[boneSubscr.x]; matBoneFinal += uncomprBoneWgh.y * matBone[boneSubscr.y]; matBoneFinal += uncomprBoneWgh.z * matBone[boneSubscr.z]; matBoneFinal += uncomprBoneWgh.w * matBone[boneSubscr.w]; } /////////////////////////////////////////////////////////////////////////////// #ifdef S3D_USE_BONESUBSCR #define S3D_BONE_DECL_STD(boneWgh, boneSubscr) \ float4 boneWgh: BLENDWEIGHT; \ int4 boneSubscr: BLENDINDICES; #define S3D_MATBONE_DECL_STD(matBone) \ const float4x4 matBone[S3D_MATBONE_CNT]; #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \ s3d_calcBoneTransfSubscr(matBoneFinal, matBone, boneWgh, boneSubscr); #else #ifdef S3D_USE_BONEWGH #define S3D_BONE_DECL_STD(boneWgh, boneSubscr) \ float4 boneWgh: BLENDWEIGHT; #define S3D_MATBONE_DECL_STD(matBone) \ const float4x4 matBone[S3D_MATBONE_CNT];\ static const int4 dummyBoneSubscr = int4(0, 1, 2, 3); #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \ s3d_calcBoneTransfSubscr( \ matBoneFinal, matBone, boneWgh, dummyBoneSubscr); #else #define S3D_BONE_DECL_STD(boneWgh, boneSubscr) #define S3D_MATBONE_DECL_STD(matBone) #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \ matBoneFinal = matView; #endif #endif /////////////////////////////////////////////////////////////////////////////// // Lighting /////////////////////////////////////////////////////////////////////////////// float s3d_calcLightRangeFac(float dist, float range) { float lightRsqRange = 1.0 / (range * range); float rangeFac = max(1.0 - dist * dist * lightRsqRange, 0.0); return rangeFac; } float s3d_calcLightRangeFac(float dist, float4 posRange) { return s3d_calcLightRangeFac(dist, posRange.w); } /////////////////////////////////////////////////////////////////////////////// s3d_ColPair s3d_calcPointLightTerm( float3 camToVertView, float3 normalView, float mtrlPower, float4 lightPos, float4 lightAmbient, float4 lightDiffuse, float4 lightSpecular, float4 lightAtten) { float3 vertToLightDirView = normalize(lightPos - camToVertView); float normalDotVertToLightDir = dot(normalView, vertToLightDirView); float3 camToVertDirView = normalize(camToVertView); float3 HalfDirView = normalize(vertToLightDirView - camToVertDirView); float normalDotHalfDir = dot(normalView, HalfDirView); float dist = length(lightPos - camToVertView); float rangeFac = s3d_calcLightRangeFac(dist, lightAtten); float4 litVec = lit(normalDotVertToLightDir, normalDotHalfDir, mtrlPower) * rangeFac; float ambient = litVec.x; float diffuse = litVec.y; float specular = litVec.z; s3d_ColPair result; result.diffuse = lightAmbient * ambient; result.diffuse += lightDiffuse * diffuse; result.specular = lightSpecular * specular; return result; } s3d_ColPair s3d_calcPointLightTerm( float3 camToVertView, float3 normalView, float mtrlPower, s3d_StdProgLight light) { return s3d_calcPointLightTerm( camToVertView, normalView, mtrlPower, light.pos, light.ambient, light.diffuse, light.specular, light.atten); } /////////////////////////////////////////////////////////////////////////////// void s3d_calcBump( out float3 diffuseDirSurf, out float3 specularDirSurf, float3 normalView, float3 vertToLightDirView, float3 halfDirView, float3 tangentUView, float3 tangentVView) { diffuseDirSurf.x = dot(tangentUView, vertToLightDirView); diffuseDirSurf.y = dot(tangentVView, vertToLightDirView); diffuseDirSurf.z = dot(normalView, vertToLightDirView); specularDirSurf.x = dot(tangentUView, halfDirView); specularDirSurf.y = dot(tangentVView, halfDirView); specularDirSurf.z = dot(normalView, halfDirView); } /////////////////////////////////////////////////////////////////////////////// // Fog /////////////////////////////////////////////////////////////////////////////// float3 s3d_calcFogExp( float3 color, float3 fogColor, float fogDensity, float fogCoord) { float fogFactor = exp(-clamp(fogDensity, 0, 1) * fogCoord); return lerp(fogColor, color, fogFactor); } float3 s3d_calcFogExp2( float3 color, float3 fogColor, float fogDensity, float fogCoord) { float fogFactor = exp(-pow(clamp(fogDensity, 0, 1) * fogCoord, 2)); return lerp(fogColor, color, fogFactor); } /////////////////////////////////////////////////////////////////////////////// // Tex /////////////////////////////////////////////////////////////////////////////// #endif // STDDEF_D3D9_HLSL_SC