[英]Freeglut glew glutIdleFunc cause lag
我剛開始使用freeglut和glew,讓它運行對我來說已經很困難。 因此,現在我開始使用敏捷的教程進行編碼,並且顯示了一個正方形,效果很好。 現在,我要用箭頭鍵使正方形移動。
從我插入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
}
我不認為這是硬件問題,因為我的計算機可以玩大型游戲。
您應該避免在閑置功能中進行任何總帳繪圖。 如果要連續重繪場景,請使用空閑功能,例如
void idle(void)
{
glutPostRedisplay();
}
看看是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.