#version 120 // choose whichever version you like
attribute vec4 Position;
attribute vec4 Color;
varying vec4 vPosition;
varying vec4 vColor;
uniform mat4 view; // for scrolling and zooming or rotating
uniform mat4 proj;
uniform vec4 ClipRect; // xy = upper left, zw = lower right
void main(){
vColor = Color;
vPosition = view * Position;
gl_Position = proj * vPosition; //This could be done with one matrix in one operation
//but we are going to need the value of vPosition later on
}
vPosition.xy = max( min( vPosition.xy, ClipRect.zw ), ClipRect.xy );
This kind of works for filled rectangles but If we have a line rectangle or any other shape it will still render the clipped lines at the edges instead of removing them. If the lines are angled it gets even worse. Because the endpoints of the lines are clipped the line direction might change (Unfortunately I did not make a screen shot to illustrate that).#version 120
uniform mat4 proj;
varying vec4 vPosition;
varying vec4 vColor;
uniform vec4 ClipRect; // xy = upper left, zw = lower right
void main(){
// actually the clip variable does the opposite as expected
// if it is 1 no clipping occurs and if it is zero the pixel is clipped, therefore the "1-..." part
float clip = 1 - clamp( 0, 1, dot( vec2( 1 ), step( ClipRect.zw, vPosition.xy ))); //check if clipping of the lower right occurs
clip *= 1 - clamp( 0, 1, dot( vec2( 1 ), 1-step( ClipRect.xy, vPosition.xy )));
gl_FragColor = clip * vColor; // do the final clipping by changing the alpha (and also the color, which is optional)
}
We use a float clip variable which we later multiply by the color value, so it's alpha will be zero if clipping should be done.