C++ Freeglut glew glutIdleFunc导致滞后

C++ Freeglut glew glutIdleFunc导致滞后,c++,opengl,glut,glew,freeglut,C++,Opengl,Glut,Glew,Freeglut,我刚开始使用freeglut和glew,让它运行对我来说已经很难了。所以现在我开始使用swiftless的教程进行编码,我有一个正方形显示,效果很好。现在我想用箭头键移动正方形 从我插入glutIdleFunc开始,它就开始滞后。我无法移动窗口或点击任何其他东西大约一分钟。如果我尝试拖动窗口,那么它会在大约2分钟后移动 我尝试使用以下代码执行此操作: #include <GL/glew.h> #include <GL/glut.h> bool* keyStates =

我刚开始使用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=新bool[256];
浮动位置x=0;
void renderPrimitive(void)
{
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f,0.0f);//左下角
glVertex3f(-1.0f,1.0f,0.0f);//左上角
glVertex3f(1.0f,1.0f,0.0f);//右上角
glVertex3f(1.0f,-1.0f,0.0f);//右下角
格伦德();
}
无效键专用(int键、int x、int y)
{
if(键状态[GLUT\U KEY\U RIGHT])
{   
positionX++;
}
}
作废显示(作废)
{
glClearColor(1.0f,0.0f,0.0f,1.0f);//将窗口背景清除为红色
glClear(GL_COLOR_BUFFER_BIT);//清除颜色缓冲区(以后会有更多缓冲区)
glLoadIdentity();//加载标识矩阵以重置绘图位置
glTranslatef(0.0f,0.0f,-5.0f);
renderPrimitive();
glFlush();//将OpenGL缓冲区刷新到窗口
}
空洞重塑(整型宽度、整型高度)
{
glViewport(0,0,(GLsizei)宽度,(GLsizei)高度);//将视口设置为窗口大小
glMatrixMode(GL_PROJECTION);//切换到投影矩阵,以便我们可以操纵场景的查看方式
glLoadIdentity();//将投影矩阵重置为标识矩阵,这样我们就不会得到任何瑕疵(清理)
gluPerspective(60,(GLfloat)宽度/(GLfloat)高度,1.0100);//设置视场角度(以度为单位)、窗口的纵横比以及新平面和远平面
glMatrixMode(GL_MODELVIEW);//切换回模型视图矩阵,以便我们可以正确地开始绘制形状
}
按下void键(无符号字符键,整数x,整数y)
{
keyStates[key]=true;//将当前键的状态设置为按下
}
void keyUp(无符号字符键,整数x,整数y)
{
keyStates[key]=false;//将当前键的状态设置为未按下
}
int main(int argc,字符**argv)
{
glutInit(&argc,argv);//初始化GLUT
glutInitDisplayMode(GLUT_SINGLE);//设置一个基本的显示缓冲区(目前仅单缓冲)
GLUTINITWindowsSize(500500);//设置窗口的宽度和高度
glutInitWindowPosition(100100);//设置窗口的位置
glutCreateWindow(“您的第一个OpenGL窗口”);//设置窗口的标题
glutDisplayFunc(display);//告诉GLUT使用“display”方法进行渲染
GLUTREFORMATEFUNC(整形);//告诉GLUT使用“整形”方法进行整形
glutIdleFunc(显示);
glutKeyboardFunc(keyPressed);//告诉GLUT在按键时使用“keyPressed”方法
glutKeyboardUpFunc(keyUp);//告诉GLUT使用方法“keyUp”来处理keyUp事件
glutSpecialFunc(关键专业);
glutMainLoop();//输入GLUT的主循环
}

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

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

void idle(void)
{
    glutPostRedisplay();
}

看看这是否有帮助。

bool*keyStates=newbool[256]bool keyStates[256];)?此外,不要使用单缓冲渲染如果您打算在混合GPU解决方案或合成窗口管理器(例如Windows Vista/7中的Aero、Linux中的compiz或OS X中的Quartz)上运行它,只会使您的生活更加困难。
bool*keyStates=new bool[256]来自教程。感谢您的建议,我应该使用
GLUT\u DOUBLE
而不是
GLUT\u single
并将
glFlush(…)
替换为
glutSwapBuffers(…)
。注意,GLUT规范说明:“如果启用,则在未接收到事件时会连续调用空闲回调。”根据底层平台缓冲输入/窗口事件的方式,这肯定会产生问题。如果使用双缓冲和VSYNC,问题只会变得更糟。或者,您可以将GLUTPOSTRESDISPLAY本身注册为空闲函数。void idle函数工作得很好,但不会延迟。现在唯一的问题是,在我的窗口更新我的屏幕之前,我必须点击它。如果我做了
glutIdleFunc(glutPostRedisplay),我知道如何解决这个问题吗?@datenwolf它说函数与voidOoh类型的参数不兼容,我明白你的意思了
glutIdleFunc(idle)
现在一切正常