Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
atavener's avatar
atavener
Adventurer
13 years ago

"GLSL Sandbox" shaders in Rift

I'm lazy. I needed something for "sky". I'm not going to make geometry for that, even procedurally.

So I set up for providing rays to a shader, based on view (this can be used simply, such as "y component is color gradient" -- or it can be a full ray/path-trace).

Then I realized that a lot of "demo shaders" such as on http://glsl.heroku.com and http://www.shadertoy.com really take similar inputs -- actually, they take screen-res and generate rays, sometimes rotated by mouse -- I have the rays already. So it's a fairly simple matter to adapt these for the Rift. And because I'm generating rays by inverting the view*proj matrix, the rays are generated appropriately for each eye in stereo!

boxsky.jpg

The code for this is available in the examples/gldemo.c of https://github.com/ultranbrown/libovr_nsb.

I'll describe the process though, as it's fairly simple...

This is the vertex shader in GLSL (HLSL isn't too different)...

#version 330 core

uniform mat4 toWorld;
out vec3 ray;

void main(void){
vec2 p = vec2( (gl_VertexID << 1) & 2, gl_VertexID & 2 ) - vec2(1.0);
ray = (toWorld * vec4( p, 1.0, 1.0 )).xyz;
gl_Position = vec4( p, 0.0, 1.0 )
}


All it needs is the inverse of view*proj put in the "toWorld" uniform, and it creates the vertices for a fullscreen quad, as well as "ray" vectors for each vertex -- these rays are the far plane corners in normalized-device coordinates (NDC), but premultiplying by the inverse view*proj brings them from NDC space into worldspace. Being vertex shader outputs, these are interpolated -- providing view-rays per pixel for the fragment shader to digest -- just be sure to normalize before using as a unit-vector. :)

Way better than an annoying skybox/sphere/dome! As a plus it turns out it's pretty cool to Riftify these shaders and sit "in" them rather than watching them on a screen. ;)

I don't have a Windows box (and failed at trying to get a suite capable of cross-compiling with the libs I want), so it'd be really cool if someone was able to build gldemo for Windows (and it magically worked, haha) so that non-programmers could play with this. The terrain could be stripped out too -- just leaving a "Rift-enabled shader viewer".
No RepliesBe the first to reply