[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: Strange problem calculating refraction using arbvp1/arbfp1



Heya,



On Thu, 10 Jun 2004 09:38:06 -0500, Steve Baker <sjbaker1@airmail.net> escreveu:

> De: Steve Baker <sjbaker1@airmail.net>
> Data: Thu, 10 Jun 2004 09:38:06 -0500
> Para: linuxgames@sunsite.dk
> Assunto: Re: Strange problem calculating refraction using arbvp1/arbfp1
> 
> Miguel Osorio wrote:
> 
> > I'm not at work right now, but I can post the shader code later, if it's needed...
> 
> I think we need to see the shader.


Sure! As I said, no problem, I just couldn't send it earlier, but here it goes - the shaders are written in Cg, for the arbvp1 and arbfp1 profiles (please excuse the long post):


--- Vertex program ---

struct Input {

    float4 Pos : POSITION;
    float3 Normal : NORMAL;
    float2 TexCoord : TEXCOORD0;

    };


struct Output {

    float4 Pos : POSITION;
    float3 INormal : TEXCOORD0;
    float3 IHalf : TEXCOORD1;
    float3 ILight : TEXCOORD2;
    float2 TexCoord : TEXCOORD3;
    float3 IEye : TEXCOORD4;

    };


Output main(Input input,
                     uniform float3 eye_os,    // Eye position object-space
                     uniform float4x4 model_matrix)    // Rotation portion of the model-matrix
{
    Output output;

    output.Pos = mul(glstate.matrix.mvp, input.Pos);

    // Directional light
    float3 light_ws = float3(1.0, 1.0, 1.0);
    float3 light_os = mul(light_ws, (float3x3)model_matrix).xyz;
    float3 L = normalize(light_os);

    float3 P = input.Pos.xyz;
    float3 N = input.Normal;

    float3 V = normalize(eye_os - P);
    float3 H = (L + V);

    output.INormal = N;
    output.IHalf = H;
    output.ILight = L;
    output.TexCoord = input.TexCoord;
    output.IEye = V;

    return output;
}


--- Fragment program ---

struct Input {

    float3 INormal : TEXCOORD0;
    float3 IHalf: TEXCOORD1;
    float3 ILight : TEXCOORD2;
    float2 TexCoord : TEXCOORD3;
    float3 IEye : TEXCOORD4;

    };


float4 main(Input input,
                   uniform samplerCUBE cube_map : TEXUNIT0,    // Environment cube map
                   uniform samplerCUBE rfl_cube_map : TEXUNIT1,    // Reflection cube map
                   uniform float4 mat_s,    // Material specular color
                   uniform float mat_sh,    // Material shininess
                   uniform float4x4 model_matrix_frag) : COLOR    // Rotation portion of the model-matrix
{
    float3 N = normalize(input.INormal);
    float3 L = normalize(input.ILight);
    float3 H = normalize(input.IHalf);

    // Refraction / reflection vector
    float3 Rfr = refract(-input.IEye, N, (1.0 / 1.3));
    Rfr = mul((float3x3)model_matrix_frag, Rfr);
    float3 Rfl = reflect(-input.IEye, N);
    Rfl = mul((float3x3)model_matrix_frag, Rfl);

    // Specular term
    float s_term = pow(max(dot(N, H), 0.0), mat_sh);

    // Specular color
    float4 color = s_term * mat_s;
    float4 color_refract = texCUBE(cube_map, Rfr);
    float4 color_reflect = texCUBE(rfl_cube_map, Rfl);

    color += lerp(color_refract, color_reflect, 0.02);

    return(color);
}


Just a reminder, both cube maps are in world space.


Again, thank you for your attention,
Miguel A. Osorio.
www.somniumstudio.com