topright gamedev
topright gamedev

Reputation: 2665

How to clip rendering in OpenGL (C++)

How to clip rendering in OpenGL (simple rectangle area)? Please post a C++ example.

Upvotes: 5

Views: 11564

Answers (2)

elmattic
elmattic

Reputation: 12174

What you probably need is OpenGL's scissor mechanism.

It clips rendering of pixels that do not fall into a rectangle defined by x, y, width and height parameters.

Note also that this OpenGL state when enabled, affects glClear command by restricting the area cleared.

Upvotes: 7

Jerry Coffin
Jerry Coffin

Reputation: 490048

If you only want to display a specific rectangle, you need a combination of something like glFrustrum or glOrtho along with glViewPort. It's actually glViewPort that sets the clipping rectangle. glFrustrum, glOrtho (gluPerspective, etc.) then map some set of real coordinates to that rectangle. Typically you hardly notice the glViewPort, because it's normally set to the entire area of whatever window you're using, and what you change is the mapping to get different views in the window.

If you just adjust glFrustum (for example) by itself, the display area on the screen will stay the same, and you'll just change the mapping so you'll still fill the entire window area, and basically just move the virtual camera around, so you zoom in or out (etc.) on the "world" being displayed. Conversely, if you just adjust glViewPort, you'll display exactly the same data, but into a smaller rectangle.

To "clip" the data to the smaller rectangle, you need to adjust both at once, in more or less the "opposite" directions so as your view-port rectangle gets smaller, you zoom in your view frustum to compensate.

Upvotes: 2

Related Questions