Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clip rendering in OpenGL (C++)

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

like image 840
topright gamedev Avatar asked May 18 '10 22:05

topright gamedev


People also ask

Is OpenGL good for 2D?

OpenGL is quite appropriate for 2D games. Although it is generally used for 3D, the same functionality can be used for 2D games. That is to say, anything you can do with 3D OpenGL will be applicable with "2D" OpenGL.

What is OpenGL in computer graphics?

OpenGL (Open Graphics Library) is a cross-platform, hardware-accelerated, language-independent, industrial standard API for producing 3D (including 2D) graphics. Modern computers have dedicated GPU (Graphics Processing Unit) with its own memory to speed up graphics rendering.


2 Answers

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.

like image 197
Stringer Avatar answered Oct 17 '22 18:10

Stringer


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.

like image 40
Jerry Coffin Avatar answered Oct 17 '22 18:10

Jerry Coffin