Reputation: 23
OpenGL's "screen space coordinate system" has the origin in the lower left (which is also 0th index for the pixel in opengl memory). When rendering to default framebuffer in linux, according to https://tronche.com/gui/x/xlib/introduction/overview.html, the origin is at upper left. How does the mapping work? is 0,0 of opengl screen space mapped to 0,0 of X window system (no flip needed?) or is 0,height of opengl screen space mapped to 0,0 of x-window system (which means somewhere y-flip needs to take place)? where along this whole pipline a flip happens(if it actually happens)
Upvotes: 1
Views: 238
Reputation: 162174
It's just all about conventions. There's no "fancy" mapping going on, it just boils down to sign conventions used for generating addresses into the framebuffer. Most graphics hardware out there, addresses in pixel rows from the upper left, with the fast running index (usually denoted x) going rightward, and the slow running index (typically denoted y) going downward.
So the address for a pixel at locations (x,y) is y*row_stride + x
.
With OpenGL placing the origin in the lower left, all that happens is the sign for the y
coordinate being flipped and the window height being added as a offset. So the calculation becomes (height - y)*row_stride + x
.
In general the address generation circuitry generalizes this to
address = (off_y + sign_y*y)*row_stride + off_x + sign_x*x
And it's then up to the graphics driver to set the configuration registers to the right values, before the drawing operation commences.
Or rather, with modern GPUs, these settings are part of the viewport and scissor rectangle configuration commands that are part of the command stream in a command buffer. You don't see this explicitly in OpenGL, but if you're somewhat familiar with Vulkan (or Metal or DX12) which make this stuff explicit, when you start a render pass with vkCmdBeginRenderPass, part of the command that is added to the command buffer is setting those addressing parameters; the right values for the offsets and the sign so that the conventions of Vulkan and the Windowing System being used are consistent, are filled in behind the curtains, by the driver.
Upvotes: 1