Search code examples
openglglsl

What's the best way to draw a fullscreen quad in OpenGL 3.2?


I'm doing ray casting in the fragment shader. I can think of a couple ways to draw a fullscreen quad for this purpose. Either draw a quad in clip space with the projection matrix set to the identity matrix, or use the geometry shader to turn a point into a triangle strip. The former uses immediate mode, deprecated in OpenGL 3.2. The latter I use out of novelty, but it still uses immediate mode to draw a point.


Solution

  • You can send two triangles creating a quad, with their vertex attributes set to -1/1 respectively.

    You do not need to multiply them with any matrix in the vertex/fragment shader.

    Here are some code samples, simple as it is :)

    Vertex Shader:

    const vec2 madd=vec2(0.5,0.5);
    attribute vec2 vertexIn;
    varying vec2 textureCoord;
    void main() {
       textureCoord = vertexIn.xy*madd+madd; // scale vertex attribute to [0-1] range
       gl_Position = vec4(vertexIn.xy,0.0,1.0);
    }
    

    Fragment Shader :

    varying vec2 textureCoord;
    void main() {
       vec4 color1 = texture2D(t,textureCoord);
       gl_FragColor = color1;
    }