How to clip rendering in OpenGL (simple rectangle area)? Please post a C++ example.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With