Freeglut Reshape回调

时间:2014-10-06 21:06:51

标签: c++ freeglut

我没有在我的程序中使用glutMainLoop(),但我在一个简单的while循环中使用了glutMainLoopEvent()。这很好,但重塑回调永远不会被调用。我在main()中注册了reshape事件。这是我的源代码:

#include <iostream>
#include "GL/freeglut.h"


float angle = 0.0f;


void ResizeEvent(int w, int h)
{
    std::cout << "Resizing... << w << " " << h << std::Endl;

    if(h == 0)
        h = 1;

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLdouble)w / h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void InitializeGL()
{
    glClearColor(0.0, 0.0, 0.0, 1.0f);
    glViewport(0, 0, 640, 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 640.0 / 480.0, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Update()
{
    angle += 0.05f;
}

void Display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, -3.0f);
    glRotatef(angle, 0.5f, 1.0f, 0.75f);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
        glColor3f(0.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
        glColor3f(0.0f, 0.0f, 1.0f);
            glVertex3f(0.0f, 1.0f, 0.0f);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Learning freeglut");

    InitializeGL();

    glutReshapeFunc(ResizeEvent);

    while(true)
    {
        Update();

        Display();

        glutMainLoopEvent();
    }

    return 0;
}

提前致谢!

编辑:任何人?

1 个答案:

答案 0 :(得分:0)

glutReshapeFunc要求将glutDisplayFunc设置为某些内容。只要您在调整大小时不需要更新窗口,这甚至可以是空功能。

可能的解决方案

    ...

    glutReshapeFunc(ResizeEvent);

    glutDisplayFunc(Display);

    while(true)
    {
        Update();

        Display();

        glutMainLoopEvent();
    }

    ...