【问题标题】:Freeglut Reshape callbackFreeglut Reshape 回调
【发布时间】:2014-10-06 21:06:51
【问题描述】:

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

#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;
}

提前致谢!

编辑:有人吗?

【问题讨论】:

  • 如果你使用 glutMainLoop(),reshape 函数会被调用吗?
  • 是的。我尝试使用其他事件,例如键盘事件,它适用于 glutMainLoop 和 glutMainLoopEvent。

标签: c++ freeglut


【解决方案1】:

glutReshapeFunc 要求将glutDisplayFunc 设置为something。这甚至可以是一个空函数,只要您在调整大小时不需要更新窗口

可能的解决方案

    ...

    glutReshapeFunc(ResizeEvent);

    glutDisplayFunc(Display);

    while(true)
    {
        Update();

        Display();

        glutMainLoopEvent();
    }

    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多