我试图使用"u_transformation“在OpenGL中裁剪一个着色器。具体来说,我只想保留上半部分。视图不应更改。我尝试了以下代码,它做了一些事情,但结果是奇怪的,而不是我所期望的那样:
gfloat left = -1.0f;
gfloat right = 1.0f;
gfloat bottom = 0.0f;//-1.0f gives the identity matrix and I see everything
gfloat top = 1.0f;
gfloat far = 1.0f;
gfloat near = -1.0f;
gfloat r_l = right - left;
gfloat t_b = top - bottom;
gfloat f_n = far - near;
gfloat tx = - (right + left) / (right - left);
gfloat ty = - (top + bottom) / (top - bottom);
gfloat tz = - (far + near) / (far - near);
gfloat orthographicMatrix[16];
orthographicMatrix[0] = 2.0f / r_l;
orthographicMatrix[1] = 0.0f;
orthographicMatrix[2] = 0.0f;
orthographicMatrix[3] = tx;
orthographicMatrix[4] = 0.0f;
orthographicMatrix[5] = 2.0f / t_b;
orthographicMatrix[6] = 0.0f;
orthographicMatrix[7] = ty;
orthographicMatrix[8] = 0.0f;
orthographicMatrix[9] = 0.0f;
orthographicMatrix[10] = 2.0f / f_n;
orthographicMatrix[11] = tz;
orthographicMatrix[12] = 0.0f;
orthographicMatrix[13] = 0.0f;
orthographicMatrix[14] = 0.0f;
orthographicMatrix[15] = 1.0f;
glUniformMatrix4fv(glGetUniformLocation(program_handle, "u_transformation"), 1, FALSE, orthographicMatrix);我该如何设置我的正字法矩阵?
编辑1这里是一张图片,我拥有什么,我得到了什么,我想要什么。

发布于 2016-02-11 04:56:57
OpenGL期望矩阵按列的主要顺序存储。因此,平移向量进入矩阵元素12、13和14:
orthographicMatrix[0] = 2.0f / r_l;
orthographicMatrix[1] = 0.0f;
orthographicMatrix[2] = 0.0f;
orthographicMatrix[3] = 0.0f;
orthographicMatrix[4] = 0.0f;
orthographicMatrix[5] = 2.0f / t_b;
orthographicMatrix[6] = 0.0f;
orthographicMatrix[7] = 0.0f;
orthographicMatrix[8] = 0.0f;
orthographicMatrix[9] = 0.0f;
orthographicMatrix[10] = 2.0f / f_n;
orthographicMatrix[11] = 0.0f;
orthographicMatrix[12] = tx;
orthographicMatrix[13] = ty;
orthographicMatrix[14] = tz;
orthographicMatrix[15] = 1.0f;这是假设在GLSL代码中将向量与左边的矩阵相乘,例如:
gl_Position = u_transformation * inPosition;另一个问题是如何设置bottom值:
gfloat bottom = 0.0f;//-1.0f gives the identity matrix and I see everything
gfloat top = 1.0f;您似乎假定这些值指定了将信函映射到的窗口的范围。矩阵的计算方法,不是这样的。这些值指定映射到窗口大小的输入坐标范围。
在您的示例中,您似乎使用了一个坐标范围- 1.0,1.0来绘制您的字母。使用标识矩阵,这将直接映射到OpenGL规范化设备坐标,该坐标的范围也为-1.0,1.0,这使得字母映射到整个窗口。
现在,如果对bottom使用0.0,这意味着要将输入坐标范围0.0,1.0映射到窗口大小,这是字母的上半部分。
要使字母只填充窗口的一半,您需要将更大的坐标范围映射到窗口大小。按照你的草图,范围应该是-3.0,1.0。这样,范围的中间是-1.0,这意味着字母的底部(y坐标为-1.0)映射到窗口的中间。
在此基础上,应该将值设置为:
gfloat bottom = -3.0f;
gfloat top = 1.0f;发布于 2016-02-10 21:51:19
设orthographicMatrix10 = -2.0f / f_n;
您可以获得有关计算投影矩阵的详细信息。projectionmatrix.html#ortho
https://stackoverflow.com/questions/35325078
复制相似问题