首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Freeglut glew glutIdleFunc引起滞后

Freeglut glew glutIdleFunc引起滞后
EN

Stack Overflow用户
提问于 2013-09-28 16:59:02
回答 1查看 633关注 0票数 1

我刚开始的时候是自由人和glew,对我来说已经很难了。因此,现在我开始用来自“迅捷”的教程编写代码,并且我让一个方块显示得很好。现在,我想用箭头键使正方形移动。

从插入glutIdleFunc开始,它就开始滞后了。我不能移动窗口或点击其他任何东西大约一分钟。如果我试着拉窗,大约2分钟后它就会移动。

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

代码语言:javascript
复制
#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
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-28 17:18:20

您应该避免在空闲函数中执行GL绘图。如果您想要不断地重新绘制场景,请使用一个空闲函数,如

代码语言:javascript
复制
void idle(void)
{
    glutPostRedisplay();
}

看看这是否有帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19069426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档