OpenGLを使用して画面上に100点を描きたい。これは、プログラムを実行するたびに画面上にランダムに100個のGL_POINTSがあることを意味します。現在のところ、画面に残っているポイントは1つだけで、その位置は以前に与えられています。しかし、私のランダムな点は短時間しか現れず、消えてしまいます。私はそれを動作させるために何が欠けていたのか分かりません。以下は私のコードです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();
}
私は何が分かりますか?
あなたは何を期待していますか? 'starNum <100'なら星を描き、この条件がもはや満たされないときになぜ消えてしまうのだろうか?私はあなたの驚きを理解していません。 –
画面上に100点を描きたいのですが、その位置はランダムです。基本的に、私はすべての星が画面上に残るようにします。 – zihaow
OpenGLはシーングラフAPIではありません。あなたの表示機能が呼び出されるたびに、すべての星を描画する必要があります。 –