Orthographic projection with origin at screen bottom left

旧巷老猫 提交于 2019-12-01 14:39:29

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.

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)
 );

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).

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.

Hope this helps.

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.

You can use a more general projection matrix which additionally uses left,right positions.

See Wikipedia for the definition.

glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, matrix)

Your matrix is stored in row-major ordering. So you should pass GL_TRUE, or you should change your matrix to column-major.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!