openGL中的随机点生成
Random points generation in openGL
我想用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()
函数中删除改变starnum
和starX
, starY
和starZ
值的代码。
正如@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();
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 了解 GLM- openGL 中的相机转换
- 如何在全屏模式下(在OpenGL中)使背景透明
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGl glm rotate
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL在启用深度测试时不会丢弃我的碎片
- C++/OpenGL 随机分段错误
- OpenGL C 如何获得随机值
- OpenGL For loop(随机月亮生成)
- SDL_ttf和OpenGL正在输出看似随机的垃圾和崩溃
- 如何使用 OpenGL 在圆内绘制随机点
- OpenGL-在随机方向上移动的对象
- openGL中的随机点生成