openGL中的随机点生成

Random points generation in openGL

本文关键字:随机 openGL      更新时间:2023-10-16

我想用openGL在屏幕上画100个点。这意味着每次运行程序时,屏幕上会随机出现100个gl_point。目前屏幕上只剩下一个点,它的位置是事先给定的。然而,我的随机点只出现了很短的一段时间,然后就消失了。我不知道我错过了什么才成功。下面是我的代码

#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();
}

你知道我错过了什么吗?

以@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();
}

然后从您的myIdle()函数中删除改变starnumstarX, starYstarZ值的代码。

正如@Reto 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();
}