Advertisement

Wrap around clip space points created in the vexter shader

Started by October 23, 2018 05:25 PM
1 comment, last by Andr 6 years, 3 months ago

Hello,

I am currently drawing an FFT ocean into a texture including Dx, Dy and Dz. As i need the real height at a point for another algorithm, i am passing the points through a vertex shader as following:


#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoords;

// Displacement and normal map
uniform sampler2D displacementMap;

uniform mat4 MVPMatrix;
uniform int N;
uniform vec2 gridLowerLeftCorner;

out float displacedHeight;

void main()
{	
	// Displace the original position by the amount in the texture
	vec3 displacedVertex = texture(displacementMap, texCoords).xyz + position;
	
	// Scale vertex to the 0 -> 1 range
	vec2 waterCellIndices = vec2((displacedVertex.x - gridLowerLeftCorner.x)/N, (displacedVertex.z - gridLowerLeftCorner.y)/N);

	// scale it to -1 -> 1
	waterCellIndices = (waterCellIndices * 2.0) - 1.0;
	
	displacedHeight = displacedVertex.y;
	
    gl_Position = vec4(waterCellIndices, 0, 1);
}

This works correctly (it writes the correct height at a given point). The issue is that some points due to the Dx and Dz displacement will get outside the clip space. This points should instead wrap around as the ocean is a collection of tiles.

As you can see in the attached file the edges fit together perfectly inside white square if they would wrap around (this is the clip space dimensions from RenderDoc).

Is there any way i could wrap around this texture (in reality wrap around the clip space positions) so it stays all inside the viewport correctly?

I tried to wrap around in the vertex shader by checking the boundaries and wrapping around but it doesnt work when a triangle has a least one vertice inside of the viewport and others outside.

 

Many thanks,

André

 

tempsnip.png

So as you can probably guess, if i draw 9 instances of this points each one with a N offset (to cover all regions around the center) the final result is a perfect match. However this is really not efficient as we throw away a lot of the points (and GPU time) that arent in the center position as they fall outside the viewport. This will also scale horribly with more FFT points.

Anyone has a more efficient solution to this problem?

 

Thanks

Capture.PNG

This topic is closed to new replies.

Advertisement