[译]GLUT教程 - 改变窗体大小

Lighthouse3d.com >> GLUT Tutorial >> Basics >> Resizing the Window

 

上一章的例子创建了两个窗体,命令行窗体和OpenGL窗体.当改变窗体大小的时候,高宽比例改变,三角形就会扭曲.得到这样的效果的原因是我们没设置正确的视觉.视觉默认是声明宽高比为1并依此来绘图.所以当比例改变的时候,视觉会发生扭曲.因此,每次比例改变的时候视觉必须要重新计算.

GLUT提供了一个回调接口给窗体大小改变事件.此外,该函数在窗体初始化创建的时候也会被调用,所以即便你初始化的窗体不是正方形看上去也不会有问题.原型如下:

void glutReshapeFunc(void (*func)(int width, int height));

func - 指向的函数名,就是该接口要绑定哪个函数作为窗体大小改变事件的响应函数.

 

我们要做的是在main函数中添加这个的实现代码.加入后main函数如下:

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

        // register callbacks
    glutDisplayFunc(renderScene);

    // Here is our new entry in the main function
    glutReshapeFunc(changeSize);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

 

下一步要做的是定义函数来自适应视觉.glutReshapeFunchan函数有两个参数,这两个参数就是新的宽和高,视觉上的,是窗体的客户区域,不包括窗体边框.

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if(h == 0)
        h = 1;
    float ratio = 1.0* w / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

        // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45,ratio,1,1000);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

 

第一步是先计算宽高比,注意窗体的高为0要做容错处理.

我们把当前矩阵转换成投影矩阵.这样做是为了确定可视的体积.然后我们读取单位矩阵来初始化它.再往后,我们利用glViewport函数把视口设置成全窗体.你可以试一下不同的值,看出来什么效果.前两个参数是左下角的坐标,后两个参数是视口的宽高.注意坐标是和客户区域有关,而不是屏幕.如果你用不同值来试,记得要用新的宽高来计算比例.

gluPerspective函数是OpenGL的另一个库,OpenGL工具库或者GLU. GLU是一个实现OpenGL的标准组件.

gluPerspective函数包含创建视觉的参数.第一个参数定义了可视角度域的yz平面,ratio参数定义了视口的宽高比关系.后面两个参数定义远近的切面.介乎于最近值和最远值之间的所有东西都会从场景中减掉.设置了以上参数后,你就会什么都看不见了.

最后,我们告知OpenGL所有矩阵操作都会用到模型视觉矩阵.这会用在安全边上.例如设置镜头和透明对象会用到该矩阵.最佳方案是保持模型视觉矩阵在默认值.

 

 

完整代码如下:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45,ratio,1,100);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glVertex3f(-2,-2,-5.0);
        glVertex3f(2,0.0,-5.0);
        glVertex3f(0.0,2,-5.0);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D - GLUT Tutorial");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);

    // enter GLUT event processing loop
    glutMainLoop();

    return 1;
}

 

posted @ 2013-10-21 18:12  Clotho_Lee  阅读(1020)  评论(0编辑  收藏  举报