我在OpenGL中有一个用于投影的类,用户可以如下使用:

//inside the draw method
customCam1.begin();
    //draw various things here
customCam1.end();

我的类(class)中的beginend方法现在是简单的方法,如下所示:
void CustomCam::begin(){
    saveGlobalMatrices();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-hParam,hParam,-tParam,tParam,near,far);//hParam and tParam are supplied by the user of the class
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void CustomCam::end(){
    loadGlobalMatrices();
};

我希望用户能够创建上述类的多个实例(每种类的lParamtParam提供不同的参数),然后在屏幕上绘制所有这三个类。从本质上讲,这就像场景中的三个不同的摄像头,两个摄像头绘制在屏幕上。 (例如,考虑将屏幕分为三列在屏幕上绘制的俯 View ,右 View ,底 View )。

现在,由于只有一个投影矩阵,如何同时实现三个不同的自定义凸轮 View ?

最佳答案

您仅需每次使用不同的投影矩阵(在您的情况下为相机对象)绘制场景三遍。在这三遍中的每遍中,您都设置了不同的视口(viewport),以使渲染显示在整个帧缓冲区的不同部分中:

glViewport(0, 0, width/3, height);   //first column
customCam1.begin();
//draw scene
customCam1.end();

glViewport(width/3, 0, width/3, height);   //second column
customCam2.begin();
//draw scene
customCam2.end();

glViewport(2*width/3, 0, width/3, height);   //third column
customCam3.begin();
//draw scene
customCam3.end();

但是您不能一次使用三个不同的投影矩阵和三个不同的视口(viewport)绘制整个场景。

编辑:为了完整起见,您确实可以通过一次使用几何着色器和GL_ARB_viewport_array extension(4.1以来的核心)来完成此操作。在这种情况下,顶点着色器将只进行模型 View 转换,您将所有三个投影矩阵统一化,然后在几何着色器中为每个输入三角形生成三个不同的三角形(由相应的矩阵投影),每个三角形具有不同的gl_ViewportIndex:
layout(triangles) in;
layout(triangle_strip, max_vertices=9) out;

uniform mat4 projection[3];

void main()
{
    for(int i=0; i<projection.length(); ++i)
    {
        gl_ViewportIndex = i;
        for(int j=0; j<gl_in.length(); ++j)
        {
            gl_Position = projection[i] * gl_in[j].gl_Position;
            EmitVertex();
        }
        EndPrimitive();
    }
}

但是考虑到您使用的是已废弃的旧功能,我想说几何着色器和OpenGL 4.1功能还不是您的选择(或者至少不是当前框架中要改变的第一件事)。

关于c++ - 在单个屏幕上绘制场景的多个 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17107499/

10-11 22:38
查看更多