Open GL创建4个视图,如3ds max或maya

时间:2012-12-21 16:29:23

标签: c++ opengl

如何在Open GL中创建4个视图(LEFT / TOP / PERSPECTIVE / FRONT)?

我用过这个:

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

当我也使用前面的顶行时,输出是这样的: enter image description here

我的错在哪里? 提前谢谢

为什么前视图为空,透视图和左视图相同? 对于透视投影我使用了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();
}

1 个答案:

答案 0 :(得分:2)

使用glViewportglScissor剪切窗口的窗格。然后为每个窗格照常执行渲染。设置投影矩阵实际上是一个绘图状态操作,因此它属于显示功能,而不是重新整形。

你的显示功能应该像

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

}

添加分割线/帧是留给读者的练习。