本文介绍了如何在OpenGL ES 2.0中的场景中设置可视区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在OpenGL中完成编程,并且我知道如何使用gluOrtho()在其中设置可见区域,但是OpenGL ES 2.0中不存在类似的功能.

I have done programming in OpenGL and I know how to set the viewable area in it with gluOrtho(), but a function like this does not exist in OpenGL ES 2.0.

我将如何在OpenGL ES 2.0中做到这一点?

How would I do this in OpenGL ES 2.0?

P.S:我正在使用PowerVR SDK模拟器在Ubuntu 10.10中进行OpenGL ES 2.0开发.

P.S : I am doing my OpenGL ES 2.0 development in Ubuntu 10.10 with the PowerVR SDK emulator.

推荐答案

正如Nicol所建议的,您将要建立一个正交投影矩阵.例如,我用来执行此操作的Objective-C方法如下:

As Nicol suggests, you'll want to set up an orthographic projection matrix. For example, an Objective-C method I use to do this is as follows:

- (void)loadOrthoMatrix:(GLfloat *)matrix left:(GLfloat)left right:(GLfloat)right bottom:(GLfloat)bottom top:(GLfloat)top near:(GLfloat)near far:(GLfloat)far;
{
    GLfloat r_l = right - left;
    GLfloat t_b = top - bottom;
    GLfloat f_n = far - near;
    GLfloat tx = - (right + left) / (right - left);
    GLfloat ty = - (top + bottom) / (top - bottom);
    GLfloat tz = - (far + near) / (far - near);

    matrix[0] = 2.0f / r_l;
    matrix[1] = 0.0f;
    matrix[2] = 0.0f;
    matrix[3] = tx;

    matrix[4] = 0.0f;
    matrix[5] = 2.0f / t_b;
    matrix[6] = 0.0f;
    matrix[7] = ty;

    matrix[8] = 0.0f;
    matrix[9] = 0.0f;
    matrix[10] = 2.0f / f_n;
    matrix[11] = tz;

    matrix[12] = 0.0f;
    matrix[13] = 0.0f;
    matrix[14] = 0.0f;
    matrix[15] = 1.0f;
}

即使您不熟悉Objective-C方法的语法,该代码的C主体也应易于遵循.矩阵定义为

Even if you're not familiar with Objective-C method syntax, the C body of this code should be easy to follow. The matrix is defined as

GLfloat orthographicMatrix[16];

然后,您可以使用以下代码将其应用于顶点着色器中,以调整顶点的位置:

You would then apply this within your vertex shader to adjust the locations of your vertices, using code like the following:

gl_Position = modelViewProjMatrix * position * orthographicMatrix;

基于此,您应该能够设置显示空间的各种限制以适应您的几何形状.

Based on this, you should be able to set the various limits of your display space to accommodate your geometry.

这篇关于如何在OpenGL ES 2.0中的场景中设置可视区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:56