cancel
Showing results for 
Search instead for 
Did you mean: 

Shader Warping not quite correct yet seems to work

onidaito
Honored Guest
Hello all. I was wondering if anyone can see what is wrong with my shader and the warping. As you can see it almost works but it's not quite as curved as it needs to be (or rather, appears to be on the official demos)



The view lines up with the left hand edge, as per the SDK documents but the top of the view is cropped sadly.

Im using GLSL with a geometry shader

Geometry Shader

#version 150

// https://github.com/dghost/glslRiftDistort

layout(points) in;
layout(triangle_strip, max_vertices = 😎 out;

out vec2 sTexCoord;
invariant out vec2 sScreenCenter;
invariant out vec2 sLensCenter;

uniform float uDistortionOffset;
uniform float uDistortionScale = 0.8f;

void emitQuad(vec4 screen, vec4 coords) {
/*
screen is a rect describing the screen space coordinates
of the rectangle to be emitted. screen.xy is the bottom left
corner, and screen.zw is the upper right corner.

coords is a rect describing the texture coordinates to be emitted
with coords.xy describing the bottom left corner and coords.zw
describing the upper right corner

*/
gl_Position = vec4(screen.z, screen.w, 0.0, 1.0 );
sTexCoord = vec2( coords.z, coords.w);
EmitVertex();

gl_Position = vec4(screen.x, screen.w, 0.0, 1.0 );
sTexCoord = vec2( coords.x, coords.w );
EmitVertex();

gl_Position = vec4(screen.z,screen.y, 0.0, 1.0 );
sTexCoord = vec2( coords.z, coords.y );
EmitVertex();

gl_Position = vec4(screen.x,screen.y, 0.0, 1.0 );
sTexCoord = vec2( coords.x, coords.y );
EmitVertex();

EndPrimitive();
}

void main() {
sScreenCenter = vec2(0.25,0.5);
sLensCenter = vec2(0.25 + uDistortionOffset * 0.25, 0.5);

emitQuad(vec4(-1.0,-1.0,0.0,1.0),vec4(0.0,1.0,0.5,0.0));

sScreenCenter = vec2(0.75,0.5);
sLensCenter = vec2(0.75 - uDistortionOffset * 0.25, 0.5);

emitQuad(vec4(0.0,-1.0,1.0,1.0),vec4(0.5,1.0,1.0,0.0));

}


Vertex Shader



#version 150

in vec4 sPosition;

void main() {
gl_Position = sPosition;
}


Fragment Shader


#version 150

// https://github.com/dghost/glslRiftDistort

uniform sampler2DRect uTexSampler0;

invariant in vec2 sLensCenter;
invariant in vec2 sScreenCenter;

// Oculus specific uniforms
uniform vec2 uScale = vec2(0.25,0.5);
uniform vec2 uScaleIn = vec2(4.0,2.0);
uniform vec4 uHmdWarpParam = vec4(1.0,0.22,0.24,0.0);
uniform vec4 uChromAbParam;

uniform float uDistortionScale = 0.8;

in vec2 sTexCoord;

//layout(location = 0) out vec4 outColor; // GLSL 3.30 or higher only

out vec4 sOutColour; // GLSL 1.50 or higher

// Performs Barrel distortion and Chromatic Abberation correction

void main(void)
{
vec2 tex_size = textureSize(uTexSampler0);
vec2 tc = sTexCoord;
tc.y = 1.0 - tc.y;

vec2 theta = (tc - sLensCenter) * uScaleIn; // uScales to [-1, 1]
float rSq = theta.x * theta.x + theta.y * theta.y;
vec2 rvector= theta * ( uHmdWarpParam.x + uHmdWarpParam.y * rSq +
uHmdWarpParam.z * rSq * rSq
+ uHmdWarpParam.w * rSq * rSq * rSq
);

vec2 theta_blue = rvector * (uChromAbParam.z + uChromAbParam.w * rSq);
vec2 tc_blue = sLensCenter + uScale * uDistortionScale * theta_blue;
if (!all(equal(clamp(tc_blue, sScreenCenter-vec2(0.25,0.5), sScreenCenter+vec2(0.25,0.5)), tc_blue))) {
sOutColour = vec4(0);
return;
}


float blue = texture(uTexSampler0, tc_blue * tex_size).b;

vec2 tc_green = sLensCenter + uScale * uDistortionScale * rvector;
vec4 center = texture(uTexSampler0, tc_green * tex_size);

vec2 theta_red = rvector * (uChromAbParam.x + uChromAbParam.y * rSq);
vec2 tc_red = sLensCenter + uScale * uDistortionScale * theta_red;
float red = texture(uTexSampler0, tc_red * tex_size).r;


sOutColour = vec4(red, center.g, blue, center.a);

//sOutColour = texture(uTexSampler0, tc * tex_size);
}




I've tried to match it as closely as I can to the shader found inside the demos but its still just a little bit out.
0 REPLIES 0