glClearColor returning negative values? (Overflow?)

Viewed 85

I'm using GLES2 and EGL with PyOpenGL, and I am calling glClearColor(0.0, 0.0, 0.0, 1.0). After glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) I am reading the cleared color values into a buffer and noticed that the alpha value is being set to -1, instead of 255 [(2^8-1)*1]. What could be the reason for this overflow?

System: NanoPi M1 Plus with a Mali400 GPU.

Code:

glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
eglSwapBuffers(display, surface)
buffer = arrays.GLcharArray.asArray(np.empty(1000 * 2 * 4, np.ubyte))
print("\nBuffer before : ", buffer[:20])
glReadPixels(0, 0, 1000, 2, GL_RGBA, GL_UNSIGNED_BYTE, buffer)
print("Buffer after: ", buffer[0:20])

Output:

Buffer before :  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Buffer after:  [ 0  0  0 -1  0  0  0 -1  0  0  0 -1  0  0  0 -1  0  0  0 -1]

Edit:

The problem occurs for all channels: glClearColor(1.0,1.0,1.0,1.0) gives me a buffer of [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]

1 Answers

You gave 1.0 as the alpha channel value to glClearColor. When you read back the pixels as GL_UNSIGNED_BYTE you will get 255 for that channel. But 255 as an unsigned byte has the same bit pattern as -1 as a signed byte. Perhaps GLcharArray interprets the bytes as signed and you should use GLubyteArray.

Related