2017-03-06 54 views
1

我嘗試編寫一個簡單的過量程序,但在調整窗口大小時發現顯示錯誤(如圖所示)。我用kde使用Arch Linux。 我該如何解決它?在Linux中使用過量格式調整窗口大小時顯示錯誤

調整大小前:

enter image description here

調整大小後:

enter image description here

#include <GL/glut.h> 
#include <stdio.h> 
#include <stdlib.h> 
void myinit(void) 
{ 
    glClearColor(1.0, 1.0, 1.0, 1.0); 
    glColor3f(1.0, 0.0, 0.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, 500.0, 0.0, 500.0); 
    glMatrixMode(GL_MODELVIEW); 
} 
void display(void) 
{ 
    typedef GLfloat point2[2]; 
    point2 vertices[3]={{0.0,0.0},{250.0,500.0},{500.0,0.0}}; 
    int i, j, k; 
    int rand(); 
    point2 p ={75.0,50.0}; 
    glClear(GL_COLOR_BUFFER_BIT); 
    for(k=0; k<5000; k++) 
    { 
    j=rand()%3; 
    p[0] = (p[0]+vertices[j][0])/2.0; 
    p[1] = (p[1]+vertices[j][1])/2.0; 
    glBegin(GL_POINTS); 
      glVertex2fv(p); 
    glEnd(); 
    } 
    glFlush(); 
    printf ("display invokved....\n"); 
} 
void main(int argc, char** argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(500,500); 
    glutInitWindowPosition(0,0); 
    glutCreateWindow("Sierpinski Gasket"); 
    glutDisplayFunc(display); 
    myinit(); 
    glutMainLoop(); 
} 
+0

試在繪製每一幀之前調用帶有當前窗口寬度和高度的'glViewport()'作爲參數。 – Bartvbl

回答

0

切換到雙緩衝(GLUT_DOUBLE & glutSwapBuffers())修復它在我的系統:

#include <GL/glut.h> 
#include <stdio.h> 
#include <stdlib.h> 
void display(void) 
{ 
    typedef GLfloat point2[2]; 
    point2 vertices[3]={{0.0,0.0},{250.0,500.0},{500.0,0.0}}; 
    int i, j, k; 
    int rand(); 
    point2 p ={75.0,50.0}; 

    glClearColor(1.0, 1.0, 1.0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, 500.0, 0.0, 500.0); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glBegin(GL_POINTS); 
    glColor3f(1.0, 0.0, 0.0); 
    for(k=0; k<5000; k++) 
    { 
    j=rand()%3; 
    p[0] = (p[0]+vertices[j][0])/2.0; 
    p[1] = (p[1]+vertices[j][1])/2.0; 
    glVertex2fv(p); 
    } 
    glEnd(); 
    glutSwapBuffers(); 
} 
int main(int argc, char** argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize(500,500); 
    glutInitWindowPosition(0,0); 
    glutCreateWindow("Sierpinski Gasket"); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 
+0

謝謝,它的作品! 我可以問爲什麼它會使用GLUT_SINGLE有問題嗎? –

+0

@haokunli:可悲的是我沒有很好的解釋爲什麼'GLUT_SINGLE'不能正常工作:(我* *能夠在我的運行MATE的Linux機器上重現時髦的不調整行爲,無論是否使用桌面組合已啓用 – genpfault

+0

我明白了,非常感謝! –

0

您需要捕獲resize事件。在供應過剩中,您必須使用功能glutReshapeFunc註冊回叫。在此功能中,您可以更改投影參數和視口。

下面是程序中的一個小的變化,展示瞭如何做到這一點:

(...) 
void reshape(int width, int height) 
{ 
    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, width, 0.0, height); 
    glMatrixMode(GL_MODELVIEW); 
} 
(...) 
int main(int argc, char** argv) 
{ 
(...) 
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); /* <--- */ 
    myinit(); 
(...) 
} 
+0

對不起,它不起作用。無法正確顯示的區域仍然無法顯示。但是,由於您展開了屏幕座標,所以在調整窗口大小時點不會改變位置。還是謝謝。 –