2016-11-06 5 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 개를 그립니다. 1 개의 무작위 별을 그리는 중입니다 (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(); 
} 

을 다음 myIdle() 기능에서 starnumstarX, starYstarZ의 값을 변경하는 코드를 제거합니다.

+0

1. 나는 별 호출을 쓰지 않고'glBegin''glEnd' ... 안에 전체 루프를 얻습니다. 2. 임의의 별이 모든 프레임과 동일하도록 (또는 깜박임이 필요합니까?) 어디에서 시드를 설정합니까? ? 3. 팝 매트릭스를 사용하지 않는 이유는 무엇입니까? – Spektre

+0

저의 요점은 가장 정확하고 가장 효율적인 대답을 쓰는 것이 아니 었습니다. 그것은 새로운 사람이 코드를 작성한 곳을 잘못 이해하도록 돕는 것이 었습니다. 이와 같이 내 대답은 기존 코드를 가능한 많이 사용했습니다. 포스터가 올바른 것을 알게되면 효율적인 포스터를 만들 수 있습니다. – user1118321

+0

나는 비판으로 그것을 의미하지는 않았지만 앞으로 나아갈 추가적 힌트로 ... 미안 미안하지만 분명하지 않다면 – Spektre

0

@Reto Koradi가 언급했듯이 디스플레이 기능이 호출 될 때마다 100 개의 별을 모두 그려야합니다. 그렇게하기 위해, 나는 x, y, z 무작위 값을 처음에 저장하고 별을 그리기 위해 데이터 배열에 액세스하는 GLfloat 데이터 구조가 필요하며, 이것으로 모든 것들이 화면에 남아있게됩니다. 여기에 '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(); 
}