glViewport(x(), y(), width(), height());
glDisable(GL_DEPTH_TEST);
glClearColor(0, 0.3, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
//... drawing commands
The drawing is correctly happening in a small area of the window, set by the glViewport(x(), y(), width(), height())
However, the background color set by glClearColor() is affecting the entire window, even though this is set after the call to glViewport(). Why?
On glClear
side:
The pixel ownership test, the scissor test, dithering, and the buffer writemasks affect the operation of
glClear
.
However, glViewport
states that
glViewport
specifies the affine transformation of x and y from normalized device coordinates to window coordinates.
If I read the references correctly, this is by design; glViewport
merely offsets the viewport, which affects the drawcalls which rasterize primitives, while glClear
simply works on the whole framebuffer area.
The full spec (4.3) seems to agree with that:
§17.4.3 The GL provides a means for setting portions of every pixel in a particular buffer to the same value.
If you only want to clean up a small area, you need to enable GL_SCISSOR_TEST. Try the following code, hope this can help you.
glEnable(GL_SCISSOR_TEST);
glScissor(100, 100, 100, 100);
glClearColor(1, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
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