简体   繁体   English

OpenGL 和 X-Window 系统坐标映射

[英]OpenGL and X-Window system coordinate mapping

OpenGL's "screen space coordinate system" has the origin in the lower left (which is also 0th index for the pixel in opengl memory). OpenGL 的“屏幕空间坐标系”的原点在左下角(这也是 opengl 内存中像素的第 0 个索引)。 When rendering to default framebuffer in linux, according to https://tronche.com/gui/x/xlib/introduction/overview.html , the origin is at upper left.在 linux 中渲染到默认帧缓冲区时,根据https://tronche.com/gui/x/xlib/introduction/overview.html ,原点在左上角。 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)? opengl 屏幕空间的 0,0 是映射到 X 窗口系统的 0,0(不需要翻转?)还是 0,opengl 屏幕空间的高度映射到 x 窗口系统的 0,0(这意味着某处 y-flip 需要发生)? where along this whole pipline a flip happens(if it actually happens)沿着整个管道发生翻转的地方(如果确实发生了)

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.大多数图形硬件,从左上角像素行中的地址,快速运行的索引(通常表示为 x)向右,而慢速运行的索引(通常表示为 y)向下。

So the address for a pixel at locations (x,y) is y*row_stride + x .因此,位置 (x,y) 处像素的地址是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. OpenGL 将原点放在左下角,所有发生的事情就是翻转y坐标的符号,并将窗口高度添加为偏移量。 So the calculation becomes (height - y)*row_stride + x .所以计算变成(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.或者更确切地说,对于现代 GPU,这些设置是视口和剪刀矩形配置命令的一部分,它们是命令缓冲区中命令流的一部分。 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;您在 OpenGL 中没有明确看到这一点,但是如果您对 Vulkan(或 Metal 或 DX12)有些熟悉,它使这些东西变得明确,当您使用vkCmdBeginRenderPass启动渲染过程时,该命令是添加到命令的一部分缓冲区正在设置那些寻址参数; 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.偏移量和符号的正确值,以便 Vulkan 的约定和正在使用的窗口系统保持一致,由驱动程序在幕后填写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM