Hello! I'm trying to write a shader in Cg to implement refraction effect. First i construct a cube enviroment map from HDR lightprobe, then a sphere is drawn, with refraction effects using Cg shaders. but the result seems quite problematic, see this screenshots:
![1](http://img340.imageshack.us/img340/580/38312794md9.jpg)
it seems left hemisphere gives plausible result, but quite erroneous on the right side.
here is my shader code:
vertex shader:
void main(float4 position : POSITION,
float3 normal : NORMAL,
uniform float4x4 modelToWorld,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float3 positionW : TEXCOORD0,
out float3 oNormal : TEXCOORD1)
{
oPosition = mul(modelViewProj, position);
positionW = mul((float3x3)modelToWorld, position.xyz);
oNormal = mul((float3x3)modelToWorld, normal);
oNormal = normalize(oNormal);
}
fragment shader:
void main(float3 position : TEXCOORD0,
float3 normal : TEXCOORD1,
out float4 color : COLOR,
uniform float3 eyePositionW,
uniform samplerCUBE cubemap,
uniform float etaRatio)
{
float3 positionW = position.xyz;
float3 N = normal;
float3 I = positionW - eyePositionW;
float3 T = refract(I, N, etaRatio);
color = texCUBE(cubemap, T);
}
rendering routine
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Rotate the camera. The angles are updated when mouse moves
cameraPos[0]=10*cos(angle_x);
cameraPos[1]=10*cos(angle_y);
cameraPos[2]=10*sin(angle_x);
gluLookAt( cameraPos[0], cameraPos[1], cameraPos[2],
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
//draw cube environment map
drawCubeMap();
//render a refractive sphere
cgGLSetStateMatrixParameter(modelViewProj_param,CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter(modelToWorld_param,CG_GL_MODELVIEW_MATRIX,CG_GL_MATRIX_IDENTITY );
cgGLSetParameter1f(etaRatio_param, etaRatio);
cgGLSetParameter3fv(eyePos_param, cameraPos);
cgGLBindProgram(ss_refraction_vp);
cgGLEnableProfile(CG_PROFILE_VP30);
cgGLBindProgram(ss_refraction_fp);
cgGLEnableProfile(CG_PROFILE_FP30);
glutSolidSphere(2,64,64);
cgGLDisableProfile(CG_PROFILE_VP30);
cgGLDisableProfile(CG_PROFILE_FP30);
glutSwapBuffers();
}
I guess there are something wrong in vertex shader and parameter passing between OpenGL and Cg.
when i modify vertex shader as follows, the result becomes much better, but cannot guarantee correct.
revised vertex shader
void main(float4 position : POSITION,
float3 normal : NORMAL,
uniform float4x4 modelToWorld,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float3 positionW : TEXCOORD0,
out float3 oNormal : TEXCOORD1)
{
oPosition = mul(modelViewProj, position);
positionW = mul((float3x3)modelToWorld, position.xyz);
oNormal = normalize(normal);
}
here is the screenshots:
![1](http://img253.imageshack.us/img253/5947/99421186ue4.jpg)
Any ideas at what is going wrong? Much thanks!
[Edited by - hmcen on April 27, 2007 2:36:48 AM]