We will introduce the usage of discard.

The key word discard in fragment shader make the GPU throw current pixel away, and the clear color will be applied for current pixel.

 

 

Source code

VertexShader

// simple vertex shader

void main()

{

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

gl_FrontColor = gl_Color;

gl_TexCoord[0] = gl_MultiTexCoord0;

}

Fragment Shader

/* bigHole.fs, discard usage example */

//

// This fragment shader demos the usage of discard keyword.

//

// It will cut a big hole in the screen, and fill it with clear color.

//

uniform sampler2D textureImage;

uniform vec2 holeCenter; // the hole of center. this is computered according to screen coordinates, not opengl coordinates.

uniform float radius;// the radius of the hole. this is computered according to screen coordinates, not opengl coordinates.

void main( void )

{

gl_FragColor = texture2D( textureImage, gl_TexCoord[0].st );

vec2 fragPoint=vec2(gl_FragCoord.x,gl_FragCoord.y);

if(distance(holeCenter,fragPoint) < radius)

discard;// the fragments in the hole will be discarded.

else if (distance(holeCenter,fragPoint) < radius+10)

gl_FragColor=vec4(1.0,0.0,0.0,0.5); // draw a circle of 10 pixels in width.

}

 

 

Screen shot

this picture is shooted with hole center (200,207), radius=72. Remember that hole center and radius apply the coordinates of screen, not OpenGL coordinate.

bigHole

Last edited Mar 19, 2013 at 2:48 AM by younthu, version 2

Comments

No comments yet.