c++ - Random points generation in openGL -


i want draw 100 points on screen using opengl. means there 100 gl_points randomly located on screen each time run program. there 1 point remains on screen , position given. however, random points appeared short period of time , disappear. don't know did miss make work. below code

#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 , 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));     /* force opengl redraw change */    glutpostredisplay(); }  // draw 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);     /* show on screen randomly disappear after starnum greater 100 */    if (starnum < 100){       glpushmatrix();       stars(starx, stary, starz);       glpopmatrix();    }     /* point remain on screen. */    glpushmatrix();    stars(2.0, 2.0, 2.0);    glpopmatrix();     /* swap 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 animation function */    glutidlefunc(myidle);     initializegl();    glutmainloop(); } 

any idea miss?

to build on @reto_koradi said in comment, each time display function called, drawing 2 stars. you're drawing 1 random star (as long starnum less 100), , you're drawing star @ location (2,2,2). constant star see 1 @ (2,2,2).

what want this:

// 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);     /* show on screen randomly disappear after starnum greater 100 */    (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();    }     /* point remain on screen. */    glpushmatrix();    stars(2.0, 2.0, 2.0);    glpopmatrix();     /* swap drawing buffers */    glutswapbuffers(); } 

and remove code changes value of starnum , starx, stary , starz myidle() function.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -