Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't glClearColor() obey the drawing area set by glViewport()?

Tags:

opengl

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?

like image 482
johnbakers Avatar asked Sep 16 '13 14:09

johnbakers


2 Answers

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.

like image 124
Bartek Banachewicz Avatar answered Nov 15 '22 09:11

Bartek Banachewicz


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);
like image 35
MemetGhini Avatar answered Nov 15 '22 09:11

MemetGhini