【问题标题】:Random points generation in openGLopenGL中的随机点生成
【发布时间】:2016-11-06 02:26:18
【问题描述】:

我想使用 openGL 在屏幕上绘制 100 个点。这意味着每次我运行程序时,屏幕上都会随机出现 100 个 GL_POINTS。目前屏幕上只剩下一个点,并且它的位置是先前给出的。然而,我的随机点只出现了很短的时间,然后就消失了。我不知道我错过了什么让它工作。下面是我的代码

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

知道我错过了什么吗?

【问题讨论】:

  • 你期待什么?如果starNum &lt; 100,您正在绘制星星,并且想知道为什么不再满足此条件时它们会消失?我不明白你的惊讶。
  • 我想在屏幕上画100个点,它们的位置是随机的。基本上,我希望所有的星星都留在屏幕上。
  • OpenGL 不是场景图 API。每次调用显示函数时都需要绘制所有星星。

标签: c++ opengl graphics glut freeglut


【解决方案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() 函数中删除更改starnumstarXstarYstarZ 值的代码。

【讨论】:

  • 1.我不会使用星号调用,而是在glBeginglEnd 内获取整个循环... 2. 你在哪里设置种子,所以随机星在所有帧中都是相同的(或者需要闪烁?)? 3. pop矩阵不用为什么要push?
  • 我的意思不是写出最正确、最有效的答案。这是为了帮助新手了解他们编写的代码哪里出错了。因此,我的回复尽可能多地使用现有代码。一旦发布者知道它的正确性,他们就可以担心它的效率。
  • 我的意思不是批评,而是进一步的提示……如果不清楚,请见谅
  • 对不起,我是被狼养大的。我倾向于反应过度。无论如何,这一切都很好。感谢您的澄清。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多