[译]GLUT教程 - 重整子窗体

Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows

 

重整函数的回调需要处理两件事:修改子窗体的大小,重新计算投影每个子窗体的投影矩阵.在我们的情况中,我们不需要渲染任何几何图案到主窗体,所以我们可以跳过重新计算投影矩阵这一步.

先来介绍修改大小和重定位子窗体的函数原型.

 

void glutPositionWindow(int x, int y);
void glutReshapeWindow(int width, int height);

x, y - 窗体的左上角

width, heith - 窗体的像素维度

 

这里有两个函数可以作用到当前窗体,所以我们必须设置一个特殊的窗体来作为当前窗体.为了这个,我们要把窗体ID传入glutSetWindows.原型如下:

 

void glutSetWindow(int windowIdentifier);

windowIdentifier - 创建窗体的返回值

 

如果我们需要知道哪个窗体是当前窗体(获得焦点),我们可以用glutGetWindow函数.

 

int glutGetWindow();

该函数的返回值是当前窗体(获得焦点)的ID.

在定位和更改窗体大小之前,我们必须设置每个子窗体为当前窗体.下面代码提供了重整函数,用到changeSize函数.上一节说过,我们要定义一个回调来专门给主窗体重整窗体.这样已经足够了,因为用户默认只能更改主窗体.

在我们的示例中,投影和所有子窗体类似,我们将会定义一个函数来实现,并在每个子窗体调用它.

int w,h, border=6;
...
 

void setProjection(int w1, int h1)
{
    float ratio;
    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    ratio = 1.0f * w1 / h1;
    // Reset the coordinate system before modifying
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Set the viewport to be the entire window
        glViewport(0, 0, w1, h1);

    // Set the clipping volume
    gluPerspective(45,ratio,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
}

void changeSize(int w1,int h1) {

    if(h1 == 0)
        h1 = 1;

    // we're keeping these values cause we'll need them latter
    w = w1;
    h = h1;

    // set subwindow 1 as the active window
    glutSetWindow(subWindow1);
    // resize and reposition the sub window
    glutPositionWindow(border,border);
    glutReshapeWindow(w-2*border, h/2 - border*3/2);
    setProjection(w-2*border, h/2 - border*3/2);

    // set subwindow 2 as the active window
    glutSetWindow(subWindow2);
    // resize and reposition the sub window
    glutPositionWindow(border,(h+border)/2);
    glutReshapeWindow(w/2-border*3/2, h/2 - border*3/2);
    setProjection(w/2-border*3/2,h/2 - border*3/2);

    // set subwindow 3 as the active window
    glutSetWindow(subWindow3);
    // resize and reposition the sub window
    glutPositionWindow((w+border)/2,(h+border)/2);
    glutReshapeWindow(w/2-border*3/2,h/2 - border*3/2);
    setProjection(w/2-border*3/2,h/2 - border*3/2);
}

 

posted @ 2013-10-29 18:33  Clotho_Lee  阅读(756)  评论(0编辑  收藏  举报