🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Wrong ModelView Transforms? NEED HELP!

Started by
1 comment, last by hmcen 17 years, 2 months ago
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 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 Any ideas at what is going wrong? Much thanks! [Edited by - hmcen on April 27, 2007 2:36:48 AM]
Advertisement
i think the culprit in this case is the generation of the normal coordinates.
Try outputting the normals directly to the color and see what happens.
Also if you adjust the ratio to something near 0 then the entire sphere should all but disappear unless the normals are messy.
Make little changes like that and you will come closer to an solution.
Hi, i think i've got the problem. In my vertex shader program, the "positionW" and "oNormal" used to caculate refractive direction in fragment shader are already in world space. So it's unnecessary to multiply them with "modelToWorld" Matrix.
i change my vertex shader as follows:
void main(float4 position : POSITION,		  float3 normal   : NORMAL,		  uniform float4x4 modelToWorld,		  out float4 oPosition : POSITION,		  out float3 positionW : TEXCOORD0,		  out float3 oNormal   : TEXCOORD1){	oPosition = mul(modelViewProj, position);	positionW = position.xyz;        oNormal = normalize(normal);}

and results seems alright:

Thanks!

This topic is closed to new replies.

Advertisement