1

如何在 Open GL 中创建 4 个视图(左/上/透视/前)?

我用过这个:

int main(int c,char ** argv)
{
    glutInit(&c,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH |GLUT_RGB );
    glutInitWindowSize(w,h);
    MainWin =glutCreateWindow("Teapot Window");
    glutDisplayFunc(display);

    LeftWin = glutCreateSubWindow(MainWin,0,0,s_window_w,s_window_h);
    glutDisplayFunc(displayLeft);
    glutReshapeFunc(reshapeLeft);

    TopWin = glutCreateSubWindow(MainWin,s_window_w+3,0,s_window_w,s_window_h);
    glutDisplayFunc(displayTop);
    glutReshapeFunc(reshapeTop);

    PerspectiveWin = glutCreateSubWindow(MainWin,0,s_window_h+3,s_window_w,s_window_h);
    glutDisplayFunc(displayPerspective);
    glutReshapeFunc(reshapePerspective);

    FrontWin = glutCreateSubWindow(MainWin,s_window_w+3,s_window_h+3,s_window_w,s_window_h);
    glutDisplayFunc(displayFront);
    glutReshapeFunc(reshapeFront);



    glutMainLoop();
    return 0;
}

然后我做了顶部和左侧的投影,如下所示:

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5f,0.5f,-0.35f,0.35f,1,500);

当我也使用前面的顶线时,输出是这样的: 在此处输入图像描述

我的错在哪里?提前致谢

为什么前视图是空的,而透视图和左视图是一样的??对于透视投影,我使用了 glFrustum .... 那么我错了吗?请帮助创建多个视图,如 3ds max 或 maya ...下面是显示功能的代码:

void displayLeft()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,-1,0,0,0,0,1,0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.2f,0.3f,0.4f,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glTranslatef(teapot_x,teapot_y,teapot_z);
    glRotatef(teapot_angle,0,1,0);
    glColor3f(0,1,0);
    glutSolidTeapot(0.2f);

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}
void displayTop()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,-1,0,0,0,0,0,0,-1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.2f,0.3f,0.4f,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glTranslatef(teapot_x,teapot_y,teapot_z);
    glRotatef(teapot_angle,0,1,0);
    glColor3f(0,1,0);
    glutSolidTeapot(0.2f);

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}
void displayFront()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,0,0,0,0,1,0,0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.2f,0.3f,0.4f,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glTranslatef(teapot_x,teapot_y,teapot_z);
    glRotatef(teapot_angle,0,1,0);
    glColor3f(0,1,0);
    glutSolidTeapot(0.2f);

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}
void displayPerspective()
{

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,-1,0,0,0,0,1,0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.2f,0.3f,0.4f,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glTranslatef(teapot_x,teapot_y,teapot_z);
    glRotatef(0,0,1,0);
    glColor3f(0,1,0);
    glutSolidTeapot(0.2f);

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}
4

1 回答 1

2

使用glViewportglScissor剪切窗口的窗格。然后像往常一样为每个窗格执行渲染。设置投影矩阵其实是一个绘图状态操作,所以属于显示功能,不是reshape。

您的显示功能应该变成类似

void ViewportScissor(int x, int y, int width, int height)
{
    glViewport(x, y, width, height);
    glScissor(x, y, width, height);
}

void display(void)
{

    int const win_width  = glutGet(GLUT_WINDOW_WIDTH);
    int const win_height = glutGet(GLUT_WINDOW_HEIGHT);

    glDisable(GL_SCISSOR);
    glClear(…);

    glEnable(GL_SCISSOR);

    ViewportScissor(0, 0, win_width/2, win_height/2);
    glMatrixMode(GL_PROJECTION);
    setup_frontview_projection();
    glMatrixMode(GL_MODELVIEW);
    setup_frontview();
    draw_scene();

    ViewportScissor(win_width/2, 0, win_width/2, win_height/2);
    glMatrixMode(GL_PROJECTION);
    setup_rightview_projection();
    glMatrixMode(GL_MODELVIEW);
    setup_rightview();
    draw_scene();

    ViewportScissor(0, win_height/2, win_width/2, win_height/2);
    glMatrixMode(GL_PROJECTION);
    setup_topview_projection();
    glMatrixMode(GL_MODELVIEW);
    setup_topview();
    draw_scene();

    ViewportScissor(win_width/2, win_height/2, win_width/2, win_height/2);
    glMatrixMode(GL_PROJECTION);
    setup_freecamview_projection();
    glMatrixMode(GL_MODELVIEW);
    setup_freecamview();
    draw_scene();

    glutSwapBuffers();

}

添加分割线/框架留给读者作为练习。

于 2012-12-21T17:17:47.983 回答