2016-11-06 442 views
0

我想用openGL在屏幕上繪製100個點。這意味着每次運行程序時,屏幕上都會隨機顯示100個GL_POINTS。目前屏幕上只剩下一個點,並且它的位置是以前給出的。但是,我的隨機點只出現一段時間,然後消失。我不知道我錯過了什麼才能使它工作。以下是我的代碼openGL中的隨機點生成

#include <stdlib.h> 
#include <GL/freeglut.h> 
#include <math.h> 

GLfloat cameraPosition[] = { 0.0, 0.2, 1.0 }; 

/* Random Star position */ 
GLfloat starX, starY, starZ; 
GLint starNum = 0; 

void myIdle(void){ 
    starNum += 1; 

    /* Generate random number between 1 and 4. */ 
    starX = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 
    starY = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 
    starZ = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 

    /* Now force OpenGL to redraw the change */ 
    glutPostRedisplay(); 
} 

// Draw a single point 
void stars(GLfloat x, GLfloat y, GLfloat z){ 
    glBegin(GL_POINTS); 
    glColor3f(1.0, 0.0, 0.0); 
    glVertex3f(x, y, z); 
    glEnd(); 
} 

// Draw random points. 
void myDisplay(void){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_LINE_SMOOTH); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glLoadIdentity(); 
    gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

    /* They show up on the screen randomly but they disappear after starNum greater than 100 */ 
    if (starNum < 100){ 
     glPushMatrix(); 
     stars(starX, starY, starZ); 
     glPopMatrix(); 
    } 

    /* This point will remain on the screen. */ 
    glPushMatrix(); 
    stars(2.0, 2.0, 2.0); 
    glPopMatrix(); 

    /* swap the drawing buffers */ 
    glutSwapBuffers(); 
} 

void initializeGL(void){ 
    glEnable(GL_DEPTH_TEST); 
    glClearColor(0, 0, 0, 1.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glPointSize(2.0); 
    glOrtho(-4.0, 4.0, -4.0, 4.0, 0.1, 10.0); 
    glMatrixMode(GL_MODELVIEW); 
} 

void main(int argc, char** argv){ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 
    glutInitWindowSize(1800, 1000); 
    glutInitWindowPosition(100, 150); 
    glutCreateWindow("Random points"); 

    /* Register display function */ 
    glutDisplayFunc(myDisplay); 

    /* Register the animation function */ 
    glutIdleFunc(myIdle); 

    initializeGL(); 
    glutMainLoop(); 
} 

任何想法我錯過了什麼?

+3

你期望什麼?如果'starNum <100',你正在畫星星,並想知道爲什麼當這種情況不再被滿足時他們消失了?我不明白你的意外。 –

+0

我想在屏幕上畫出100點,並且它們的位置是隨機的。基本上,我希望所有的星星都留在屏幕上。 – zihaow

+4

OpenGL不是場景圖形API。每次調用顯示功能時,都需要繪製所有星星。 –

回答

1

建立在@Reto_Koradi在他的評論中說過的話,每次你的顯示函數被調用時,你正在繪製2顆星。你正在畫一個隨機星(只要starnum小於100),然後你在位置(2,2,2)畫一顆星。你看到的恆星是(2,2,2)處的恆星。

什麼你可能想要做的是這樣的:

// Draw random points. 
void myDisplay(void){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_LINE_SMOOTH); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glLoadIdentity(); 
    gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

    /* They show up on the screen randomly but they disappear after starNum greater than 100 */ 
    for (starnum = 0; starnum < 100; starnum++) { 
     glPushMatrix(); 

     starX = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 
     starY = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 
     starZ = 1.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/3.0)); 

     stars(starX, starY, starZ); 
     glPopMatrix(); 
    } 

    /* This point will remain on the screen. */ 
    glPushMatrix(); 
    stars(2.0, 2.0, 2.0); 
    glPopMatrix(); 

    /* swap the drawing buffers */ 
    glutSwapBuffers(); 
} 

,然後刪除該改變的starnumstarXstarYstarZmyIdle()函數值的代碼。

+0

1.我不會使用星號調用來取代'glBegin'' glEnd'中的整個循環... 2.您在哪裏設置種子,以便隨機星星是相同的所有幀(或者需要閃爍)? ? 3.爲什麼推動彈出矩陣,如果它不使用? – Spektre

+0

我的觀點並不是寫出最正確,最有效的答案。這是爲了幫助新來的人瞭解他們寫的代碼出錯了。因此,我的回覆儘可能多地使用了現有的代碼。一旦海報知道它的正確,那麼他們可能會擔心使其效率。 – user1118321

+0

我不是故意把它當作批評,而是作爲前進的附加提示......對不起,如果那不明確 – Spektre

0

正如@Reet Koradi所說,我需要在每次調用顯示函數時畫出所有100顆星。爲此,我需要一個GLfloat數據結構,它在開始時存儲x,y,z隨機值,然後訪問數據數組以繪製星星,這將使它們全部保留在屏幕上。這裏是沒有'myIdle'功能的解決方案,一切都在'myDisplay'函數中完成。

/* Data structure for generating stars at random location */ 
typedef GLfloat star2[100]; 
star2 randomX = {}; 
star2 randomY = {}; 
star2 randomZ = {}; 

// Draw random points. 
void myDisplay(void){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_LINE_SMOOTH); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glLoadIdentity(); 
    gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

    /* Generate random number of stars */ 
    if (starNum < 100){ 
     /* This will store 100 stars' location to a data array. */ 
     for (starNum = 0; starNum < 100; starNum++) { 


     starX = -4.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/8.0)); 
     starY = -4.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/8.0)); 
     starZ = -4.0 + static_cast <float> (rand())/(static_cast <float> (RAND_MAX/8.0)); 

     randomX[starNum] = { starX }; 
     randomY[starNum] = { starY }; 
     randomZ[starNum] = { starZ }; 
     } 
    } 
    else{ 
     /* This will draw 100 stars on the screen */ 
     for (int i = 0; i < starNum; i++){ 
      glPushMatrix(); 
      stars(randomX[i],randomY[i], randomZ[i]); 
      glPopMatrix(); 
     } 
    } 

    /* swap the drawing buffers */ 
    glutSwapBuffers(); 
}