【问题标题】:Freeglut glew glutIdleFunc cause lagFreeglut glew glutIdleFunc 导致滞后
【发布时间】:2013-09-28 16:59:02
【问题描述】:

我刚开始使用 freeglut 和 glew,让它运行对我来说已经很困难了。因此,现在我开始使用 swiftless 的教程进行编码,并且我的正方形显示效果很好。现在我想用箭头键移动方块。

从我插入 glutIdleFunc 开始,它就开始滞后了。我无法移动窗口或单击其他任何东西大约一分钟。如果我尝试拖动窗口,它会在大约 2 分钟后移动。

我尝试使用以下代码进行此操作:

#include <GL/glew.h> 
#include <GL/glut.h>

bool* keyStates = new bool[256]; 
float positionX = 0;
void renderPrimitive (void) 
{
    glBegin(GL_QUADS);
    glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
    glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner
    glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
    glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
    glEnd();
}
void keySpecial (int key, int x, int y) 
{
    if (keyStates[GLUT_KEY_RIGHT]) 
    {   
        positionX++;
    }
}

void display (void) 
{

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
    glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
    glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
    glTranslatef(0.0f, 0.0f, -5.0f);
    renderPrimitive();
    glFlush(); // Flush the OpenGL buffers to the window
}

void reshape (int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
    glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
    glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
    glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

void keyPressed (unsigned char key, int x, int y) 
{
    keyStates[key] = true; // Set the state of the current key to pressed
}

void keyUp (unsigned char key, int x, int y)
{
    keyStates[key] = false; // Set the state of the current key to not pressed
}

int main (int argc, char **argv) 
{
    glutInit(&argc, argv); // Initialize GLUT
    glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
    glutInitWindowSize (500, 500); // Set the width and height of the window
    glutInitWindowPosition (100, 100); // Set the position of the window
    glutCreateWindow ("Your first OpenGL Window"); // Set the title for the window

    glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering

    glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping
    glutIdleFunc(display);
    glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
    glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
    glutSpecialFunc(keySpecial);
    glutMainLoop(); // Enter GLUT's main loop
}

我不认为这是硬件问题,因为我的电脑可以玩大型游戏。

【问题讨论】:

  • bool* keyStates = new bool[256]; 考虑到如何销毁这个数组的问题,这在全局范围内是否有效?即使是这样,你有什么可能的理由这样做而不是创建一个静态数组(例如bool keyStates [256];)?此外,不要使用单缓冲渲染,如果您打算在混合 GPU 解决方案或合成窗口管理器(例如 Windows Vista/7 中的 Aero、Linux 中的 compiz 或 OS X 中的 Quartz)上运行它,只会让您的生活更加困难.
  • The bool* keyStates = new bool[256]; 来自link 教程。不过感谢您的建议
  • 我应该使用什么来代替单缓冲渲染?
  • 使用GLUT_DOUBLE 代替GLUT_SINGLE 并将glFlush (...) 替换为glutSwapBuffers (...)

标签: c++ opengl glut glew freeglut


【解决方案1】:

您应该避免在空闲函数中进行任何 GL 绘制。如果要连续重绘场景,请使用空闲函数,如

void idle(void)
{
    glutPostRedisplay();
}

看看有没有帮助。

【讨论】:

  • 好眼光,GLUT 规范。说:“如果启用,则在未收到事件时连续调用空闲回调。”根据底层平台如何缓冲输入/窗口事件,这肯定会产生问题。当使用双缓冲和 VSYNC 时,问题只会变得更糟。
  • 或者,您可以将 glutPostRedisplay 本身注册为空闲函数。
  • void idle 功能很好,不会滞后。现在唯一的问题是我必须在我的窗口中单击它才能更新我的屏幕知道如何解决这个问题吗?
  • @datenwolf 如果我这样做 glutIdleFunc(glutPostRedisplay); 它说该函数与 void 类型的参数不兼容
  • 哦,我明白你的意思了glutIdleFunc(idle) 现在一切正常
猜你喜欢
  • 1970-01-01
  • 2013-03-14
  • 2015-12-03
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
相关资源
最近更新 更多