Search code examples
c++openglvertex

Opengl,C++ : Large number of points


I'm currently working on a program, that will be able to visualize the evolution of point on the plane that are flowing along a vectorfield. I've finished the first version which I've pasted below. When running the program with a large number of points it seems that only the last say 30000 points gets drawn into the window. I'd like to be able to draw about 1000000 points, so I'm way off.

I've tried lovering the number if iterations (the Iteration variable - controlling the number of points), and here it acts just fine. However when increasing it substantially the first part is no longer drawn.

#include <iostream>
#include <stdio.h>
#include <math.h>
//#include <gsl/gsl_math.h>
//#include <gsl/gsl_sf.h>
#include <GL/freeglut.h>
#include <GL/gl.h>

using namespace std;

//Initial iterations to make the system settle:
int initIter=0;
//Iterations once system has settled:
int Iterations = 100000;
/**Starting point in time-phase-space (t,x,y,vx,vy).
For mathematical reasons the last two components should
always be 0**/
float TPS[5]={0,0.00,0.100,0.00,0.000};
//Timestep:
float dt=0.001;




/**The Step function make one Picard
iteration **/
float * Step(float * Arr){
static float NewTPS[5];
NewTPS[0] = Arr[0]+dt;
NewTPS[1] = Arr[1]+Arr[3]*dt;
NewTPS[2] = Arr[2]+Arr[4]*dt;
//This is the dynamical functions:
NewTPS[3] = -Arr[2];
NewTPS[4] = Arr[1];
return NewTPS;
}




/** This function sets up GLUT plotting
window: **/
void myInit(){
 // set the background color
 glClearColor(0.0f, 0.0f, 0.0f, 1.00f);

 // set the foreground (pen) color
  glColor4f(1.0f, 1.0f, 1.0f, 0.04f);

 // set up the viewport
  glViewport(0, 0, 800, 800);

 // set up the projection matrix (the camera)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);

 // set up the modelview matrix (the objects)
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

 //Computing initial iterations:
  for (int i=0;i<initIter;i++){
   //cout << TPS[1]<<" " << TPS[2] << endl;
   float * newTPS2;
   newTPS2 = Step(TPS);
   //Assigning the values of newTPS2 to TPS:
    for (int j=0; j<5;j++){
     TPS[j]=*(newTPS2+j);
 }
  }

 // enable blending
  //glEnable(GL_BLEND);
  //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

 // enable point smoothing
  //glEnable(GL_POINT_SMOOTH);  
  //glPointSize(1.0f);
 }





/** This function draws a the point that
is passed to it: **/
 void Draw(){
 // clear the screen
  glClear(GL_COLOR_BUFFER_BIT);

 // draw some points
  glBegin(GL_POINTS);
  for (int i = 0; i <Iterations ; i++) {
   float * newTPS2;
   //cout << TPS[0]<< " " << TPS[1] << " " << TPS[2]<< endl;
    newTPS2 = Step(TPS);
    //Assigning the values of newTPS to TPS:
    for (int j=0; j<5;j++){
     TPS[j]=*(newTPS2+j);
}   
  // draw the new point
   glVertex2f(TPS[1], TPS[2]);
   }    
  glEnd();

  // swap the buffers
  glutSwapBuffers();
  //glFlush();
  }



  int main(int argc, char** argv){
  // initialize GLUT
  glutInit(&argc, argv);

  // set up our display mode for color with alpha and double buffering
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);


  glutInitWindowSize(800, 800);
  glutCreateWindow("Trace of 2D-dynamics");
  myInit();
  // register our callback functions
  glutDisplayFunc(Draw);
  //  glutKeyboardFunc(mykey);

  // start the program
  glutMainLoop();
  return 0;
  }

Solution

  • If you want to just color specific pixels on the screen, you should't be using glVertex at all. Put them all in a contiguos memory block, create a texture from it and render a quad covering the whole screen. That might be faster than calculating their positions inside OpenGL.