在 OpenGL 中,仅使用 OpenGL3 创建透视图的最简单方法是什么?方法?

发布于 2024-09-14 11:02:59 字数 214 浏览 2 评论 0原文

乍一看这可能听起来很懒,但我已经研究了两天了。

我有一个绘制图元的 SDL+GLEW 应用程序。我想以不同的视角制作一些视口。我看到了四个视口,但无法更改视角。

假设您有

draw();
swapbuffers();

什么是最简单的方法 - 在 OpenGL3+ 规范中 - 创建透视视口(或者理想情况下是几个)?

This might sound lazy at first, but I've been researching it for 2 days.

I have an SDL+GLEW app that draws a primitive. I wanted to make a few viewports in different perspectives. I see the four viewports but I can't change the the perspectives.

Assume you have

draw();
swapbuffers();

What is the simplest way - in OpenGL3+ spec - to create a perspective viewport (or ideally several ones)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手长情犹 2024-09-21 11:02:59

下面是 GLSL 1.50 代码,用于执行与 gluPerspective 相同的操作。您可以轻松地将其转换为您选择的语言,然后通过 glUniformMatrix4fv 上传。

mat4 CreatePerspectiveMatrix(in float fov, in float aspect,
    in float near, in float far)
{
    mat4 m = mat4(0.0);

    float angle = (fov / 180.0f) * PI;
    float f = 1.0f / tan( angle * 0.5f );

    /* Note, matrices are accessed like 2D arrays in C.
       They are column major, i.e m[y][x] */

    m[0][0] = f / aspect;
    m[1][1] = f;
    m[2][2] = (far + near) / (near - far);
    m[2][3] = -1.0f;
    m[3][2] = (2.0f * far*near) / (near - far);

    return m;
}

然后你就可以:

mat4 worldMatrix = CreateSomeWorldMatrix();
mat4 clipMatrix = CreatePerspectiveMatrix(90.0, 4.0/3.0, 1.0, 128.0);

gl_Position = worldMatrix * clipMatrix * myvertex;
some_varying1 = worldMatrix * myvertex; 
some_varying2 = worldMatrix * vec4(mynormal.xyz, 0.0);

Here's GLSL 1.50 code for doing the same thing as gluPerspective. You can easily convert it to your language of choice, and upload it via glUniformMatrix4fv instead.

mat4 CreatePerspectiveMatrix(in float fov, in float aspect,
    in float near, in float far)
{
    mat4 m = mat4(0.0);

    float angle = (fov / 180.0f) * PI;
    float f = 1.0f / tan( angle * 0.5f );

    /* Note, matrices are accessed like 2D arrays in C.
       They are column major, i.e m[y][x] */

    m[0][0] = f / aspect;
    m[1][1] = f;
    m[2][2] = (far + near) / (near - far);
    m[2][3] = -1.0f;
    m[3][2] = (2.0f * far*near) / (near - far);

    return m;
}

Then you do:

mat4 worldMatrix = CreateSomeWorldMatrix();
mat4 clipMatrix = CreatePerspectiveMatrix(90.0, 4.0/3.0, 1.0, 128.0);

gl_Position = worldMatrix * clipMatrix * myvertex;
some_varying1 = worldMatrix * myvertex; 
some_varying2 = worldMatrix * vec4(mynormal.xyz, 0.0);
吲‖鸣 2024-09-21 11:02:59

最简单的也是唯一的方法。自己计算(使用一些库、简单数学等)投影矩阵,并将其上传到顶点着色器中的统一,并在那里进行转换。

The simplest is the only way. Calculate yourself (with some lib, plain math, etc) a projection matrix, and upload it to a uniform in your vertex shader, and do the transformation there.

ㄟ。诗瑗 2024-09-21 11:02:59

对于透视矩阵:
gluPerspective,然后 glGetFLoatv(GL_PROJECTION_MATRIX, ...),然后 glUniformglUniformMatrix4fv

对于视口:glViewport 和 glScissor。或者使用帧缓冲区对象,渲染到纹理,然后渲染纹理。

PS 如果您不想使用已弃用的功能,请从 获取矩阵公式gluPerspective文档,自己计算矩阵,然后使用glUniformMatrix4fv。或者在兼容性配置文件中工作。

For perspective matrix:
gluPerspective, then glGetFLoatv(GL_PROJECTION_MATRIX, ...), then glUniform or glUniformMatrix4fv

For viewports: glViewport and glScissor. Or use framebuffer object, render to texture, then render texture.

P.S. If you don't want to use deprecated features, grab matrix formula from gluPerspective documentation, calculate matrix yourself, then use glUniformMatrix4fv. Or work in compatibility profile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文