Inifite plane using shaders

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Rusty Rooster
Posts: 11
Joined: Thu May 24, 2018 6:43 pm
Location: USA

Inifite plane using shaders

Post by Rusty Rooster »

Let's create an infinite plane that extends to the horizon. We can do it using shaders. It's a good example to study if you're trying to understand shaders and the whole Model->View->Projection->Divide by w thing. Tags: Homogenous Coordinates, Unproject

IMPORTANT: To follow along with what's happening, read this tutorial:
https://github.com/martin-pr/possumwood ... SL-shaders

The only node needed is a quad with vertices (-1,-1,0) , (-1,1,0), (1,1,0), (1,-1,0). See the tutorial on creating a custom scene node. Don't forget to disable automatic culling.

Here is the shader callback stuff:

Code: Select all

 
        //Projection and WorldView matrices
 
        core::matrix4 Proj = driver->getTransform(video::ETS_PROJECTION);
 
        services->setPixelShaderConstant("mProj", Proj.pointer(), 16);
 
        core::matrix4 MV = driver->getTransform(video::ETS_VIEW);
        core::matrix4 World = driver->getTransform(video::ETS_WORLD);
        MV*= World;
 
        services->setPixelShaderConstant("mWorldView", MV.pointer(), 16);
 
        core::matrix4 iMV = MV;
        iMV.makeInverse();
 
        //Inverse ModelView and Inverse Projection matrices
 
        core::matrix4 iproj = driver->getTransform(video::ETS_PROJECTION);
        iproj.makeInverse();
 
        services->setVertexShaderConstant("mInvProj", iproj.pointer(), 16);
 
        services->setVertexShaderConstant("iMV", iMV.pointer(), 16);
 
 
Vertex Shader:

Code: Select all

 
//Vertex Shader
uniform mat4 mInvProj;
uniform mat4 iMV;
 
varying vec3 vNear;
varying vec3 vFar;
 
void main(void)
{
 
    gl_Position = gl_Vertex;
 
    vec4 _vNear = mInvProj * vec4(gl_Vertex.xy,-1,1);
    vec4 _vFar = mInvProj * vec4(gl_Vertex.xy,1.0,1);
 
    _vNear /= _vNear.w;
    _vFar  /= _vFar.w;
 
    _vNear = iMV * _vNear;
    _vFar = iMV * _vFar;
 
    vNear = _vNear.xyz;
    vFar = _vFar.xyz;
 
    gl_TexCoord[0] = gl_MultiTexCoord0;
}
 
Pixel Shader:

Code: Select all

 
uniform sampler2D myTexture;
uniform mat4 mProj;
uniform mat4 mWorldView;
 
varying vec3 vNear;
varying vec3 vFar;
 
 
float computeDepth(vec3 pos) {
    vec4 clip_space_pos = mProj * mWorldView * vec4(pos.xyz, 1.0);
    float clip_space_depth = clip_space_pos.z / clip_space_pos.w;
 
    float far = gl_DepthRange.far;
    float near = gl_DepthRange.near;
 
    float depth = (((far-near) * clip_space_depth) + near + far) / 2.0;
 
    return depth;
}
 
void main (void)
{
   // vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0]));
 
    float t = -vNear.y  / ( vFar.y - vNear.y );
 
    vec3 R = vNear + t * (vFar-vNear);
 
    float c = (
        int(floor((R.x/1000) ))+
        int(floor((R.z/1000) ))
    ) % 2;
 
    vec4 col = vec4(vec3(c/2.0 + 0.3), 0);
    col *= float(t>0);
 
    gl_FragDepth = computeDepth(R);
        gl_FragColor = col;
}
 
Post Reply