我想使用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();
}
知道我错过了什么?
答案 0 :(得分: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();
}
然后从starnum
函数中删除更改starX
和starY
,starZ
和myIdle()
值的代码。
答案 1 :(得分:0)
正如@Reto Koradi所提到的,每次调用我的显示功能时,我都需要绘制所有100颗星。为此,我需要一个GLfloat数据结构,它在开头存储x,y,z随机值,然后访问数据数组以绘制星星,这将使它们全部保留在屏幕上。这是实现它的解决方案,没有&#39; myIdle&#39;功能,一切都在&#39; myDisplay&#39;功能
/* 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();
}