如何使用 u_transformation 在 OpenGL 中裁剪/剪辑 [英] How to crop/clip in OpenGL using u_transformation

查看:26
本文介绍了如何使用 u_transformation 在 OpenGL 中裁剪/剪辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenGL 中的u_transformation"裁剪/剪辑着色器.具体来说,我只想保留顶部的一半.不应更改视口.我尝试了以下代码,它做了一些事情,但结果很奇怪,而不是我所期望的:

I'm trying to crop / clip a shader using "u_transformation" in OpenGL. Specifically, I want to keep only half of the top part. Viewport should not be changed. I have tried the following code, it does something but the result is strange and not what I'm expecting:

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

我应该如何设置正交矩阵?

How should I setup my orthographic matrix?

编辑 1这是我拥有的,得到的和想要的图片.

EDIT 1 Here is a picture of what I have, what I get and what I want.

推荐答案

OpenGL 期望矩阵以列主要顺序存储.所以平移向量进入矩阵元素 12、13 和 14:

OpenGL expects matrices to be stored in column major order. So the translation vector goes into matrix elements 12, 13, and 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 代码中左侧的矩阵相乘,例如:

This is under the assumption that you're multiplying vectors with matrices from the left in your GLSL code, for example:

gl_Position = u_transformation * inPosition;

另一个问题是你如何设置 bottom 值:

Another problem is in how you set the bottom value:

gfloat bottom = 0.0f;//-1.0f gives the identity matrix and I see everything
gfloat top = 1.0f;

您似乎假定这些值指定了您的字母映射到的窗口范围.矩阵的计算方式并非如此.这些值指定映射到窗口大小的输入坐标范围.

You seem to assume that these values specify the range of the window that your letter is mapped to. The way the matrix is calculated, that's not the case. These values specify the range of input coordinates that is mapped to the size of the window.

在您的示例中,您似乎使用 [-1.0, 1.0] 的坐标范围来​​绘制字母.使用单位矩阵,这直接映射到 OpenGL 规范化设备坐标,其范围也为 [-1.0, 1.0],这使得字母映射到整个窗口.

In your example, it looks like you're using a coordinate range of [-1.0, 1.0] for drawing your letters. With the identity matrix, this maps directly to OpenGL normalized device coordinates, which also have a range of [-1.0, 1.0], which makes the letter map to the whole window.

现在,如果您使用 0.0 作为 bottom,这意味着您将输入坐标范围 [0.0, 1.0] 映射到窗口大小,即字母的上半部分.

Now if you use 0.0 for bottom, this means that you're mapping the input coordinate range of [0.0, 1.0] to the window size, which is the top half of your letter.

要使字母仅填充窗口的一半,您需要将更大的坐标范围映射到窗口大小.根据您的草图,范围应为 [-3.0, 1.0].这样,范围的中间是 -1.0,这意味着字母的底部(y 坐标为 -1.0)映射到窗口的中间.

To make the letter fill only half the window, you need to map a larger range of coordinates to the window size. Following your sketch, the range should be [-3.0, 1.0]. With this, the middle of the range is -1.0, meaning that the bottom of the letter, which has a y-coordinate of -1.0, maps to the middle of the window.

基于此,您应该将值设置为:

Base on this, you should set the values as:

gfloat bottom = -3.0f;
gfloat top = 1.0f;

这篇关于如何使用 u_transformation 在 OpenGL 中裁剪/剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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