2012-11-23 72 views
0

我需要一些C++/OpenGL編碼的幫助。我正在做的是一個多邊形近似算法。OpenGL顯示兩個數組

我的代碼首先從.txt文件中提取一組點,將它們全部存儲在一個數組中,然後顯示該數組。然後它接受這些點並對它們執行算法,並創建一個新的點數組。我不知道該怎麼做,是如何讓第二組點顯示在與第一個窗口相同的窗口上。我必須創建一個新的顯示功能並調用那個功能嗎?或者,也許修改我現在接受數組的基本顯示功能?下面是我的顯示功能代碼:

void display(void){ 
    glClearColor(0,0,0,0); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1,1,1); 

    glBegin(GL_POINTS); 
    for(int i=0; i<2000; i++) 
     glVertex2i(pixel[i].x,pixel[i].y); 
    glEnd(); 

    glFlush(); 
} 

回答

1

您只需繪製已處理的數組。考慮到你只是想使所得的點數,比如你的代碼示例中,你可以使用:

void display(void){ 
    glClearColor(0,0,0,0); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1,1,1); 

    glBegin(GL_POINTS); 
    for(int i=0; i<2000; i++) 
    glVertex2i(pixel[i].x,pixel[i].y); 
    // here goes the rendering of the new set of points. 
    glColor3f(1,0,0); // change the color so we can see better the new points. 
    for(int i=0; i<2000; i++) 
    glVertex2i(result[i].x,result[i].y); 
    glEnd(); 

    glFlush(); 
} 

變量result與處理結果的陣列。

你不能修改display函數,因爲它是由OpenGL調用的,它不知道你的數組。但是沒有任何東西反對你將你的代碼分解成你的display函數調用的許多函數。

希望它有幫助。

+0

這是有效的。所以通過使用上述,我不得不同時渲染它們。所以我會在算法完成之後而不是之前調用glutMainLoop()? – Seldom

+0

如果我正確理解你的意思,glutMainLoop()總是在顯示函數之前調用。當你調用glutMainLoop時,渲染循環開始(每次迭代只渲染一幀)。顯示函數在每次迭代中被調用並負責渲染幀。這就是爲什麼在調用glutMainLoop之前必須在代碼中調用glutDisplayFunc的原因:所以你需要向GLUT指定哪個函數將執行渲染。 –