Jump to content
  • Advertisement
Sign in to follow this  
JJtheGerman

Gerstner wave function [HLSL]

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am new to these forums here, just joined, so hi to everyone reading this.

 

I have started with HLSL around 6months ago, and use AMD's RenderMonkey to create shaders.

I already made alot of progress with learning HLSL and the occasional success keeps me coming back for more :D

 

Now one topic I tried to stear clear from are wave simulations because I dont understand how to move specific vertices.

For example. I tried moving the vertices in WorldSpace on the y axis, by moving them in an if statement by checking what the position of the vertex is in world space and then moving them accordingly.

But I cant believe that this is how you are supposed to do it.

 

I have yet to find something about this on google. Which is why I decided to sign up here, and also I believe this will be a great site for helping me with my journey into 3D :D.

 

So the question is, what would be some more or less arbitary code to move selected vertices up and down.

Some short pseudo code example would really help.

 

Thanks in advance and I appreciate any help I can get.

 

PS: If you know of some better software of creating shaders then RenderMonkey then please let me know. A software where its easier to assemble 3d scenes using custom shaders to light the scene would be nice :D

Share this post


Link to post
Share on other sites
Advertisement

Here's some code I wrote a while back. It's only a test code for a single wave. You will want to create a number of waves at different frequencies to obtain a more realistic look.

cbuffer World
{
	float3 SunDirection;
}

cbuffer Instance
{
	float4x4 WorldViewProj;
}

cbuffer PerFrame
{
	float DeltaT;
}

Texture2D DiffuseTexture;
SamplerState Sampler;

struct VS_IN
{
	float4 position : POSITION;
	float2 uv 		: TEXCOORD0;
};

struct VS_OUT
{
	float4 position	: SV_POSITION;
	float2 uv		: TEXCOORD0;
};

VS_OUT VShader(VS_IN input)
{
	VS_OUT output = (VS_OUT)0;
	
	// Gerstner Wave
	
	float A = 5;	// amplitude
	float L = 50;	// wavelength
	float w = 2*3.1416/L;
	float Q = 0.5;
	
	float3 P0 = input.position.xyz;
	float2 D = float2(0, 1);
	float dotD = dot(P0.xz, D);
	float C = cos(w*dotD + DeltaT/100);
	float S = sin(w*dotD + DeltaT/100);
	
	float3 P = float3(P0.x + Q*A*C*D.x, A * S, P0.z + Q*A*C*D.y);
	
	output.position = mul(WorldViewProj, float4(P,1));
	output.uv = input.uv;

	return output;
}

float4 PShader(VS_OUT input) : SV_Target
{
	return float4(DiffuseTexture.Sample(Sampler, input.uv).rgb, 1);
}

And some literature:

 

http://http.developer.nvidia.com/GPUGems/gpugems_ch01.html

http://hyperphysics.phy-astr.gsu.edu/hbase/waves/watwav2.html

http://www.gamedev.net/blog/715/entry-2249487-ocean-rendering/

http://graphics.ucsd.edu/courses/rendering/2005/jdewall/tessendorf.pdf

Edited by Waaayoff

Share this post


Link to post
Share on other sites

Thanks for the help!

The code helped a lot, although it doesnt work with a plane for some reason. Still I get the idea and can now write the rest myself.

I appreciate the help! biggrin.png

Share this post


Link to post
Share on other sites

Thanks for the help!

The code helped a lot, although it doesnt work with a plane for some reason. Still I get the idea and can now write the rest myself.

I appreciate the help! biggrin.png

 

The code works by transforming vertices. A plane only has 4. You need to use a grid.

 

Anytime.

Share this post


Link to post
Share on other sites

The problem was actually not your code, sorry for the confusion. It just turned out the default grid (isn't any model that is a rectangel and only has one side a grid?),
has its local z vector pointing up instead of y, so the code just needed a little bit of adjustment. As follows:

 

   float2 D = float2(0,1);

to

   float3 D = float3(0,0,1);

and

   float   dotD = dot(PO.xz, D);

to

   float   dotD = dot(PO.xy, D.z);

 

Currently working on the normals, but there are enough topics about calculating them already and your references are going to help.

Thanks, man! laugh.png

Edited by JJtheGerman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!