Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


About water refraction


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 zonozz   Members   -  Reputation: 168

Like
0Likes
Like

Posted 24 April 2013 - 09:02 AM

Hi everyone !

 

I'm doing 2D water simulation, and there is a problem in my program.

 

Figure :

Problem.jpg

 

Just as you see, I render the clip plane below to texture and over it to another texture, eventually to combine 2 texture to water plane.

 

Question :

 

1. I dont understand why my friends told me I should calculate reflection & refraction vector ? Why I need these vectors ? ( I already finish render the reflection & refraction effect ? ).

 

2. As you see the picture above, there is a obvious visual error in water plane, when I turn the camera, the goddess body teared ! How does it occurred ? How to fix it ?

 

Thanks.



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9090

Like
1Likes
Like

Posted 24 April 2013 - 09:30 AM

It looks like your refraction vector is wrong, the reflection one looks correct. How are you calculating it? The statue should appear elongated and bent at the bottom, not broken (same for the pool edge refractions in the background). Basically, post some code, because without code we cannot do much more than say "it looks wrong".


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 zonozz   Members   -  Reputation: 168

Like
0Likes
Like

Posted 24 April 2013 - 09:43 AM

It looks like your refraction vector is wrong, the reflection one looks correct. How are you calculating it? The statue should appear elongated and bent at the bottom, not broken (same for the pool edge refractions in the background). Basically, post some code, because without code we cannot do much more than say "it looks wrong".

 

OK,But I'll post HLSL's code, but I dont know Why I need reflection vector and refraction vector ? I already render the final effect (Combine reflection & refraction).

 

Here is my code in Vertex Shader & Pixel Shader :

/*
 * File name - Water.vs
 * Author - CuiXue
 * Updated date - 2013.4.21
 * Desc - 
 *
 */

 cbuffer MatrixBuffer
 {
	matrix world;
	matrix view;
	matrix proj;
 };

 cbuffer ReflectionBuffer
 {
	matrix reflectionMatrix;
 };

 cbuffer CameraBuffer
 {
	float3 cameraPosition;
	float padding;
 };

 struct VS_INPUT
 {
	float4 position : POSITION;
	float2 tex		: TEXCOORD0;
	float3 normal	: NORMAL;
	float3 tangent	: TANGENT;
	float3 binormal	: BINORMAL;
 };

 struct PS_INPUT
 {
	float4 position : SV_POSITION;
	float2 tex1		: TEXCOORD0;
	float2 tex2		: TEXCOORD4;
	float4 reflectionPos : TEXCOORD1;
	float4 refractionPos : TEXCOORD2;
	float3 viewDirection : TEXCOORD3;
	float3 normal	: NORMAL;
	float3 tangent		 : TANGENT;
	float3 binormal		 : BINORMAL;
 };

 PS_INPUT WaterVS(VS_INPUT input)
 {
	PS_INPUT output = (PS_INPUT)0;

	float4 worldPos;

	matrix reflectionProjWorld;
	matrix viewProjWorld;

	input.position.w = 1.0f;

	output.position = mul(input.position, world);
	output.position = mul(output.position, view);
	output.position = mul(output.position, proj);

	output.normal = mul(input.normal, (float3x3)world);
	output.normal = normalize(output.normal);

	output.tangent = mul(input.tangent, (float3x3)world);
	output.tangent = normalize(output.tangent);

	output.binormal = mul(input.binormal, (float3x3)world);
	output.binormal = normalize(output.binormal);

	reflectionProjWorld = mul(reflectionMatrix, proj);
	reflectionProjWorld = mul(world, reflectionProjWorld);
	output.reflectionPos = mul(input.position, reflectionProjWorld);

	viewProjWorld = mul(view, proj);
	viewProjWorld = mul(world, viewProjWorld);
	output.refractionPos = mul(input.position, viewProjWorld);

	worldPos = mul(input.position, world);
	output.viewDirection = cameraPosition.xyz - worldPos.xyz;
	output.viewDirection = normalize(output.viewDirection);

	output.tex1 = input.tex / 1.0f;
	output.tex2 = input.tex / 0.5f;

	return output;
 }

 

 

 

Pixel Shader :

/*
 * File name - Water.ps
 * Author - CuiXue
 * Updated date - 2013.4.21
 * Desc - 
 *
 */
 SamplerState samplerState;
 Texture2D reflectionTex;
 Texture2D refractionTex;
 Texture2D normalTex;

 cbuffer WaterBuffer
 {
	float reflection_refraction_scale;
	float water_translation;
	float2 padding;
 };

 cbuffer LightBuffer
 {
	float4 ambientColor;
	float4 diffuseColor;
	float3 direction;
	float specularPower;
	float4 specularColor;
 };

 struct PS_INPUT
 {
	float4 position : SV_POSITION;
	float2 tex1		: TEXCOORD0;
	float2 tex2		: TEXCOORD4;
	float4 reflectionPos : TEXCOORD1;
	float4 refractionPos : TEXCOORD2;
	float3 viewDirection : TEXCOORD3;
	float3 normal		 : NORMAL;
	float3 tangent		 : TANGENT;
	float3 binormal		 : BINORMAL;
 };

 float4 WaterPS(PS_INPUT input) : SV_TARGET
 {
	// Water params
	float fresnel;
	float specular;
	float r;
	float2 reflection_sample_coord;
	float2 refraction_sample_coord;
	float3 normal1;
	float3 normal2;
	float3 normal;
	float3 reflection;
	float3 heightView;
	float4 reflectionColor;
	float4 refractionColor;
	float4 refl_refr_lerpColor;
	float4 normalMap1;
	float4 normalMap2;
	float4 outputColor;

	float4 water_refl_color = float4( 0.7f, 0.9f, 0.7f, 1.0 );
	float4 water_refr_color = float4( 0.6f, 1.0f, 0.6f, 1.0 );

	input.tex1.y = input.tex1.y - water_translation;
	input.tex2.x = input.tex2.x + water_translation;

	heightView.x = input.viewDirection.y;
	heightView.y = input.viewDirection.y;
	heightView.z = input.viewDirection.y;

	reflection_sample_coord.x =  input.reflectionPos.x / input.reflectionPos.w / 2.0f + 0.5f;
	reflection_sample_coord.y = -input.reflectionPos.y / input.reflectionPos.w / 2.0f + 0.5f;

	refraction_sample_coord.x =  input.refractionPos.x / input.refractionPos.w / 2.0f + 0.5f;
	refraction_sample_coord.y = -input.refractionPos.y / input.refractionPos.w / 2.0f + 0.5f;
	
	// Sample normal map
	normalMap1 = normalTex.Sample(samplerState, input.tex1);
	normalMap2 = normalTex.Sample(samplerState, input.tex2);
	normal1 = (normalMap1.rgb * 2.0f) - 1.0f;
	normal2 = (normalMap2.rgb * 2.0f) - 1.0f;
	normal = normalize(normal1 + normal2);
	
	reflection_sample_coord = reflection_sample_coord + (normal.xy * reflection_refraction_scale);
	refraction_sample_coord = refraction_sample_coord + (normal.xy * reflection_refraction_scale);

	reflectionColor = reflectionTex.Sample(samplerState, reflection_sample_coord);
	refractionColor = refractionTex.Sample(samplerState, refraction_sample_coord);

	reflectionColor *= water_refl_color;
	refractionColor *= water_refr_color;

	// Calculate fresnel 
	r = pow((1.0 - 1.0 / 1.3), 2) / pow((1.0 + 1.0/1.3), 2);
	// r = pow((1.3f - 1.0f), 2) / pow((1.3f + 1.0f), 2);

	fresnel = max(0.0f, min(1.0f, r + (1.0f - r) * pow(1.0f - dot(normal, heightView), 5.0)));

	refl_refr_lerpColor = lerp(reflectionColor, refractionColor, fresnel);

	reflection = -reflect(normalize(direction), normal);

	specular = dot(normalize(reflection), normalize(input.viewDirection));

	if( specular > 0.0f )
	{
		specular = pow(specular, specularPower);

		refl_refr_lerpColor = saturate(refl_refr_lerpColor + specular * specularColor);
	}

	outputColor = refl_refr_lerpColor;

	return outputColor;
 }

 

 

I just sample the reflection texture & refraction texture. and combine these 2 pictures to water plane.



#4 zonozz   Members   -  Reputation: 168

Like
0Likes
Like

Posted 25 April 2013 - 01:38 AM

Can anyone help me please ?

 

1. Why I need to calculate refraction & reflection vector ? Use reflection & refraction render texture for water plane will contribute some visual error ?

 

2. I debugged today, and another problem here, the whole water plane rocked when I turn my camera, I always feel that the surface of the water there is a delay. ( It means the water plane will dislocated with the bath. )



#5 menohack   Members   -  Reputation: 216

Like
0Likes
Like

Posted 25 April 2013 - 12:01 PM

Why are you using the y-component of the normal? Shouldn't it be the x and z component?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS