2013-09-28 45 views
0

我剛剛開始使用freeglut和glew,讓它運行對我來說已經很難了。所以現在我開始用快速編寫的教程進行編碼,並且我有一個正方形顯示工作正常。現在我已經到了想用方向鍵移動方塊的地步。Freeglut glew glutIdleFunc cause lag

從插入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 
} 

我不希望這是一個硬件問題,因爲我的電腦能玩大型遊戲。

+0

'bool * keyStates = new bool [256];'這是一個在全局範圍內做的有效事情,考慮如何銷燬這個數組的問題?即使是這樣,你有什麼可能的原因,而不是創建一個靜態數組(例如'bool keyStates [256];')?此外,不要使用單緩衝渲染,如果您打算在混合GPU解決方案或合成窗口管理器上運行它(例如,Windows Vista/7中的Aero,Linux中的compiz或OS X中的Quartz),它只會使您的生活更加困難。 。 –

+0

'bool * keyStates = new bool [256];'來自[link](http://www.swiftless.com/tutorials/opengl/keyboard.html)教程。感謝您的建議,但 – Shishi

+0

,而不是單緩衝渲染我應該使用什麼? – Shishi

回答

2

您應該避免在空閒功能中執行任何GL繪圖。如果你想連續重畫場景,請使用空閒功能,如

void idle(void) 
{ 
    glutPostRedisplay(); 
} 

看看是否有幫助。

+2

良好的眼睛,GLUT規範。說:「如果啓用,當事件沒有被接收時,空閒的回叫被不斷地調用。」根據底層平臺緩衝輸入/窗口事件的方式,這肯定會產生問題。當使用雙緩衝和VSYNC時,問題只會變得更糟。 –

+0

或者,您可以將glutPostRedisplay本身註冊爲空閒功能。 – datenwolf

+0

void idle函數效果很好,它不會滯後。現在唯一的問題是,我必須點擊我的窗口,才能更新我的屏幕任何想法如何解決這個問題? – Shishi