正交投影,其原点位于屏幕左下方 [英] Orthographic projection with origin at screen bottom left

查看:281
本文介绍了正交投影,其原点位于屏幕左下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python OpenGL绑定,并尝试仅使用现代的opengl调用.我有一个带有顶点的VBO,并且试图通过传递到顶点着色器的正交投影矩阵进行渲染.

I'm using the python OpenGL bindings, and trying to only use modern opengl calls. I have a VBO with verticies, and I am trying to render with an orthographic projection matrix passed to the vertex shader.

目前,我正在使用以下值计算投影矩阵:

At present I am calculating my projection matrix with the following values:

from numpy import array

w = float(width)
h = float(height)
n = 0.5
f = 3.0

matrix = array([
                   [2/w,        0,        0,        0],
                   [  0,      2/h,        0,        0],
                   [  0,        0,  1/(f-n), -n/(f-n)],
                   [  0,        0,        0,        1],
               ], 'f')


#later
projectionUniform = glGetUniformLocation(shader, 'projectionMatrix')
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, matrix)

我从这里得到的代码:

正交投影矩阵的公式?

这似乎工作正常,但我希望原点"位于屏幕的左下角.这是我可以在矩阵上应用的函数,以便一切正常工作",还是我必须手动将每个对象转换为w/2 h/2?

This seems to work fine, but I would like my Origin to be in the bottom left corner of the screen. Is this a function I can apply over my matrix so everything "just works", or must I translate every object by w/2 h/2 manually?

旁注:坐标是否可以正常工作,以匹配像素位置?

side note: Will the coordinates match pixel positions with this working correctly?

因为我正在使用现代OpenGL技术,所以我不应该使用gluOrtho2d或GL_PROJECTION调用.

Because I'm using modern OpenGL techniques, I don't think I should be using gluOrtho2d or GL_PROJECTION calls.

推荐答案

由于我最近才刚刚开始进行OpenGL编程,所以我对投影并不完全熟悉,但是您当前的矩阵无法转换任何点.对角线将应用缩放,但最右边的列将应用平移. Dirk提供的链接为您提供了一个投影矩阵,该矩阵将使您的原点(0,0是您想要的,是吗?)在屏幕的左下角.

I'm not completely familiar with projections yet, as I've only started OpenGL programming recently, but your current matrix does not translate any points. The diagonal will apply scaling, but the right most column will apply translation. The link Dirk gave gives you a projection matrix that will make your origin (0,0 is what you want, yes?) the bottom-left corner of your screen.

我曾经用来做一个矩阵(每一行实际上是OpenGL的一列):

A matrix I've used to do this (each row is actually a column to OpenGL):

OrthoMat = mat4(
        vec4(2.0/(screenDim.s - left),  0.0,    0.0,    0.0),
        vec4(0.0,   2.0/(screenDim.t - bottom),     0.0,    0.0),
        vec4(0.0,   0.0,    -1 * (2.0/(zFar - zNear)),  0.0),
        vec4(-1.0 * (screenDim.s + left)/(screenDim.s - left), -1.0 * (screenDim.t + bottom)/(screenDim.t - bottom), -1.0 * (zFar + zNear)/(zFar - zNear), 1.0)
 );

screenDim数学实际上是宽度或高度,因为left和bottom都设置为0.zFar和zNear分别为1和-1(因为它是2D,所以不是很重要).

The screenDim math is effectively the width or height, since left and bottom are both set to 0. zFar and zNear are 1 and -1, respectively (since it's 2D, they're not extremely important).

此矩阵以像素为单位获取值,并且顶点位置也必须以像素为单位.调整屏幕大小时,点(0,32)也将始终位于同一位置.

This matrix takes values in pixels, and the vertex positions need to be in pixels as well. The point (0, 32) will always be at the same position when you resize the screen too.

希望这会有所帮助.

编辑#1:为清楚起见,我声明的left/bottom/zfar/znear值是 I 选择的值.您可以更改这些显示方式.

Edit #1: To be clear, the left/bottom/zfar/znear values I stated are the ones I chose to make them. You can change these how you see fit.

这篇关于正交投影,其原点位于屏幕左下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆