I added the ability to enable and specify scissoring via RenderState. So for example you would use:
someMaterial.getAdditionalRenderState().setScissorTest(true);
someMaterial.getAdditionalRenderState().setScissorRect(x, y, width, height);
which would be handled by the following code in a Renderer’s applyRenderState() method:
[java]
-
if (state.isScissorTest()) {
-
if (!context.clipRectEnabled) {
-
glEnable(GL_SCISSOR_TEST);
-
glScissor(state.getScissorX(), state.getScissorY(), state.getScissorW(), state.getScissorH());
-
} else {
-
int scisX = state.getScissorX();
-
int scisY = state.getScissorY();
-
int scisW = state.getScissorW();
-
int scisH = state.getScissorH();
-
ScissorRectangle i = ScissorRectangle.intersect(clipX, clipY, clipW, clipH, scisX, scisY, scisW, scisH);
-
if (i == null) {
-
glScissor(0, 0, 0, 0);
-
} else {
-
glScissor(i.getX(), i.getY(), i.getW(), i.getH());
-
}
-
}
-
} else {
-
if (context.clipRectEnabled) {
-
glScissor(clipX, clipY, clipW, clipH);
-
} else {
-
glDisable(GL_SCISSOR_TEST);
-
}
-
}
[/java]
If the scissor test is turned on and GL_SCISSOR_TEST isn’t already enabled, it turns it on and applies the rectangle. If GL_SCISSOR_TEST is already enabled, it sets the rectangle to be the intersection of the desired area and the existing one specified by setClipRect() and clipx/clipy/clipw/cliph. If the scissor test is turned off but GL_SCISSOR_TEST is enabled, it makes sure the area is clipx/clipy/clipw/cliph as specified by setClipRect(). If both are off, it makes sure GL_SCISSOR_TEST is disabled.
ScissorRectangle is just a simple rectangle class that allows a quick intersection test and a way to get the result. This bit does the heavy lifting:
[java]
-
/**
-
* Intersects two rectangles represented by the values that define them.
-
* If there is no intersection between them, the result is null.
-
* Otherwise it is a new <code>ScissorRectangle</code> representing
-
* the intersection.
-
*
-
* @param x0 the x coordinate of the lower left of the first rectangle
-
* @param y0 the y coordinate of the lower left of the first rectangle
-
* @param w0 the width of the first rectangle
-
* @param h0 the height of the first rectangle
-
* @param x1 the x coordinate of the lower left of the second rectangle
-
* @param y1 the y coordinate of the lower left of the second rectangle
-
* @param w1 the width of the second rectangle
-
* @param h1 the height of the second rectangle
-
* @return null if there is no intersection otherwise the resulting
-
* <code>ScissorRectangle</code>
-
*/
-
public static ScissorRectangle intersect(int x0, int y0, int w0, int h0,
-
int x1, int y1, int w1, int h1)
-
{
-
int left = Math.max(x0, x1);
-
int bottom = Math.max(y0, y1);
-
int right = Math.min(x0 + w0, x1 + w1);
-
int top = Math.min(y0 + h0, y1 + h1);
-
int width = right - left;
-
int height = top - bottom;
-
// If width or height are zero or less, there was no intersection.
-
if ((width <= 0) || (height <= 0)) {
-
return null;
-
} else {
-
return new ScissorRectangle(left, bottom, width, height);
-
}
-
}
[/java]
Is this of any use/merit?