Best way of clipping pixel

Started by
5 comments, last by Z01 19 years, 6 months ago
Whats the best way of clipping pixels to ractanges? Stencil, or glVeiwport, or something else I don't know of?
-- I waz here --
Advertisement
Don't forget you can also use the scissors test or do the clipping manually on the CPU. glViewport is good if you're rendering a whole scene to a subportion of a window, but not if want to clip individual items to different rectangles. There's also the hardware clip planes.

Maybe if you tell us why you're trying to do this, instead of asking a generic question whose answer depends on the context, someone could help you?
If you're trying to clip 2D objects, an easy way is:

void TCanvas::ClipDrawing(const TRect& Rect)
{
glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);
glEnable(GL_CLIP_PLANE2);
glEnable(GL_CLIP_PLANE3);

double eqn0[4] = { -1.0, 0.0, 0.0, Rect.right };
double eqn1[4] = { 1.0, 0.0, 0.0, -Rect.left };
double eqn2[4] = { 0.0,-1.0, 0.0, Rect.bottom };
double eqn3[4] = { 0.0, 1.0, 0.0, -Rect.top };

glClipPlane(GL_CLIP_PLANE0, eqn0);
glClipPlane(GL_CLIP_PLANE1, eqn1);
glClipPlane(GL_CLIP_PLANE2, eqn2);
glClipPlane(GL_CLIP_PLANE3, eqn3);
}

You should be careful when setting the coordinates (the example above are for : Top-Left Screen (0, 0) Bottom-Right Screen (W, H).
I'm using this method to clip the controls of my OpenGL gui you can find here.
rzv - <a href="http://guide.jejik.com>guide.jejik.com
Hey thanks rzv! Thats just what I wanted, and it's also for my GUI! =P
-- I waz here --
Firstup, dont use glviewport() to clip, it can lead to strange results.

Also, I'm not convinced clipplanes are the best solution to the problem, apprently Nvidia dont play ball all the time (clicky) and I seem to recall something about clipplanes requireing an extra texture unit to be used....

In short, I'd have gone with the scissors test as thats what its designed for.
the scissor test is the best way to clip anything to a rectangle is, is much faster than stencil or clipplanes etc
Scissors is good, but setting the scissors rectangle and enabling/disabling the test can be expensive. My gui ran a lot faster when I did all my clipping in software instead of using scissors. Scissors can also potentially disrupt batching, depending how you are rendering your text/gui objects.

This topic is closed to new replies.

Advertisement