0001: // ------------------------------------------------------------
0002: // Tetrahedron env map
0003: // 
0004: // Copyright (c) 2004 IMAGIRE Takashi. All rights reserved.
0005: // ------------------------------------------------------------
0006: #include "paraboloid.fx"
0007: 
0008: // ------------------------------------------------------------
0009: // Global variables(グローバル変数)
0010: // ------------------------------------------------------------
0011: float4x4 mWVP;      // Local-Projection matrix
0012: float4x4 mWV;       // Local-View matrix
0013: float4x4 mW;        // Local-World matrix
0014: float4 vEye;        // Eye position(Local coordinate)
0015: 
0016: float MAP_WIDTH;    // texture width
0017: float MAP_HEIGHT;   // texture height
0018: 
0019: // ------------------------------------------------------------
0020: // Textures(テクスチャ)
0021: // ------------------------------------------------------------
0022: texture SrcTex;
0023: sampler SrcSamp = sampler_state
0024: {
0025:     Texture = <SrcTex>;
0026:     MinFilter = LINEAR;
0027:     MagFilter = LINEAR;
0028:     MipFilter = NONE;
0029: 
0030:     AddressU = Clamp;
0031:     AddressV = Clamp;
0032: };
0033: // ------------------------------------------------------------
0034: texture DecaleTex;
0035: sampler DecaleSamp = sampler_state
0036: {
0037:     Texture = <DecaleTex>;
0038:     MinFilter = LINEAR;
0039:     MagFilter = LINEAR;
0040:     MipFilter = NONE;
0041: 
0042:     AddressU = Clamp;
0043:     AddressV = Clamp;
0044: };
0045: // ------------------------------------------------------------
0046: // Dual paraboloid environment maps(双放物環境マップ)
0047: // ------------------------------------------------------------
0048: texture ParaboloidFrontTex;
0049: sampler ParaboloidFrontSamp = sampler_state
0050: {
0051:     Texture = <ParaboloidFrontTex>;
0052:     MinFilter = LINEAR;
0053:     MagFilter = LINEAR;
0054:     MipFilter = NONE;
0055: 
0056:     AddressU = Clamp;
0057:     AddressV = Clamp;
0058: };
0059: // ------------------------------------------------------------
0060: texture ParaboloidBackTex;
0061: sampler ParaboloidBackSamp = sampler_state
0062: {
0063:     Texture = <ParaboloidBackTex>;
0064:     MinFilter = LINEAR;
0065:     MagFilter = LINEAR;
0066:     MipFilter = NONE;
0067: 
0068:     AddressU = Clamp;
0069:     AddressV = Clamp;
0070: };
0071: 
0072: 
0073: // ------------------------------------------------------------
0074: // Vertex shader output(頂点シェーダからピクセルシェーダに渡すデータ)
0075: // ------------------------------------------------------------
0076: struct VS_OUTPUT
0077: {
0078:     float4 Pos          : POSITION;
0079:     float4 Color        : COLOR0;
0080:     float2 Tex0         : TEXCOORD0;
0081:     float2 Tex1         : TEXCOORD1;
0082:     float3 Normal       : TEXCOORD2;
0083:     float2 DecaleTex    : TEXCOORD3;
0084: };
0085: 
0086: // ------------------------------------------------------------
0087: // Simple vertex shader program(背景描画頂点シェーダプログラム)
0088: // ------------------------------------------------------------
0089: VS_OUTPUT VS (
0090:       float4 Pos    : POSITION           // (モデルの頂点)
0091:      ,float4 Normal : NORMAL             // (法線ベクトル)
0092:      ,float4 Tex0   : TEXCOORD0          // (テクスチャ座標)
0093: ){
0094:     VS_OUTPUT Out = (VS_OUTPUT)0;
0095:     
0096:     // Position(位置座標)
0097:     Out.Pos = mul( Pos, mWVP );
0098:     
0099:     // Texture coordinate(テクスチャ座標)
0100:     Out.Tex0 = Tex0;
0101:     
0102:     return Out;
0103: }
0104: 
0105: 
0106: 
0107: 
0108: // ------------------------------------------------------------
0109: // Simple pixel shader program(背景描画ピクセルシェーダプログラム)
0110: // ------------------------------------------------------------
0111: float4 PS (VS_OUTPUT In) : COLOR
0112: {
0113:     float4 Out;
0114:     
0115:     // Color(色)
0116:     Out = tex2D( SrcSamp, In.Tex0 );
0117:     
0118:     return Out;
0119: }
0120: 
0121: 
0122: 
0123: 
0124: 
0125: // ------------------------------------------------------------
0126: // Parabploid vertex shader program(放物変換頂点シェーダプログラム)
0127: // ------------------------------------------------------------
0128: VS_OUTPUT VS_Parabploid (
0129:       float4 Pos    : POSITION           // (モデルの頂点)
0130: ){
0131:     VS_OUTPUT Out = (VS_OUTPUT)0;
0132:     
0133:     // Position(位置座標)
0134:     Out.Pos = paraboloid( Pos, mWV );
0135:     
0136:     // Texture coordinate(テクスチャ座標)
0137:     Out.Tex0.x = + 0.5 * Pos.x + 0.5;
0138:     Out.Tex0.y = - 0.5 * Pos.y + 0.5;
0139:     
0140:     return Out;
0141: }
0142: 
0143: 
0144: // ------------------------------------------------------------
0145: // Dual-Parabploid environment mapping vertex shader program(放物環境マップ頂点シェーダプログラム)
0146: // ------------------------------------------------------------
0147: VS_OUTPUT VS_Envmap (
0148:       float4 Pos    : POSITION           // (モデルの頂点)
0149:     , float4 Normal : NORMAL             // (法線ベクトル)
0150:     , float2 Tex    : TEXCOORD0          // (テクスチャ座標)
0151:       )
0152: {
0153:     VS_OUTPUT Out = (VS_OUTPUT)0;
0154:     float3 ray;
0155:     float3 ref;
0156:     
0157:     // Position(位置座標)
0158:     Out.Pos = mul( Pos, mWVP );
0159:     
0160:     // Color(色)
0161:     Out.Color = 0.0f;
0162:     
0163:     // Texture Coordinate(テクスチャ座標)
0164:     ray = normalize( Pos - vEye ).xyz;
0165:     ref = reflect( ray, Normal.xyz );       // Reflection vector(反射ベクトル)
0166:     ref = mul( ref, mW );
0167: 
0168:     Out.Tex0.x = 0.5 * ( 1 + ref.x/(1+ref.z));
0169:     Out.Tex0.y = 0.5 * ( 1 - ref.y/(1+ref.z));
0170:     Out.Tex1.x = 0.5 * ( 1 - ref.x/(1-ref.z));
0171:     Out.Tex1.y = 0.5 * ( 1 - ref.y/(1-ref.z));
0172:     
0173:     Out.Color.w = ref.z + 0.5f;     // The judgement of the signature of the z-component of the reflection vector(反射ベクトルの正負の判定)
0174:     
0175:     // Normal vector in the view coordinate system(ビュー座標系での法線ベクトル)
0176:     Out.Normal = mul( Normal.xyz, mWV );
0177:     
0178:     Out.DecaleTex = Tex;
0179:     
0180:     return Out;
0181: }
0182: 
0183: 
0184: // ------------------------------------------------------------
0185: // Sampling environment map(環境マップの読み込み)
0186: // ------------------------------------------------------------
0187: float4 GetEnvMap( float2 tex_front, float2 tex_back, float NE, bool front_or_back )
0188: {
0189:     float4 env;
0190:     
0191:     if( front_or_back ){
0192:         env = tex2D( ParaboloidFrontSamp, tex_front );
0193:     }else{
0194:         env = tex2D( ParaboloidBackSamp,  tex_back  );
0195:     }
0196:     
0197:     // Fresnel term (フレネル項)
0198:     float F = 0.1 + 1.0 * pow(1+NE, 4);
0199:     
0200:     return F * env;
0201: }
0202: 
0203: // ------------------------------------------------------------
0204: // Full Global Illumination(全部込みピクセルシェーダプログラム)
0205: // ------------------------------------------------------------
0206: float4 PS_Envmap (VS_OUTPUT In) : COLOR
0207: {
0208:     float4 Env = GetEnvMap( In.Tex0, In.Tex1, In.Normal.z, 0.5 < In.Color.w );// Environment map(環境マップ)
0209:     float4 decale = tex2D( DecaleSamp, In.DecaleTex );
0210:     
0211:     return 0.6 * decale + Env;
0212: }
0213: 
0214: 
0215: 
0216: 
0217: 
0218: 
0219: // ------------------------------------------------------------
0220: // テクニック
0221: // ------------------------------------------------------------
0222: technique TShader
0223: {
0224:     pass P0 // Simple transform(地形描画)
0225:     {
0226:         VertexShader = compile vs_1_1 VS();
0227:         PixelShader  = compile ps_1_1 PS();
0228:     }
0229:     
0230:     pass P1 // Paraboloid transform(放物変換地形描画)
0231:     {
0232:         VertexShader = compile vs_1_1 VS_Parabploid();
0233:         PixelShader  = compile ps_1_1 PS();
0234:     }
0235:     
0236:     pass P2 // 環境マップを張る
0237:     {
0238:         VertexShader = compile vs_1_1 VS_Envmap();
0239:         PixelShader  = compile ps_2_0 PS_Envmap();
0240:     }
0241: }
0242: