如何在opengl中绘制模拟生成的10万个离散点,并给出性能消耗?

解决方案 »

  1.   

    #include <GL/glut.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include <iostream>
    using namespace std;
     const int MAX=1000;
     const int count=1000000;
    class Point
      {
        public:
             GLfloat x;
             GLfloat y;
             Point(const GLfloat &x1=0.0,const GLfloat & y1=0.0){x=x1;y=y1;}
     };
    Point point[count];
    void init (void) 
    { glClearColor (1.0, 1.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, MAX, 0.0, MAX, -1.0, 1.0);
    }
    void display(void)
    {   glClear (GL_COLOR_BUFFER_BIT);
       glColor3f (0.0, 0.0, 0.0);
      glPointSize(1);  clock_t start, finish; 
      double duration; 
      int m_count=100;
    cout<<"Time to draw "<<count<<" nodes is: ";
    start = clock(); 
      for (int m=0;m<m_count;m++)
      {   glBegin(GL_POINTS);
      for (int j=0;j<count;j++)
      {
      glVertex2f(point[j].x,point[j].y);
      }
      glEnd();
      }
     
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC/m_count; 
    cout<<duration<<" seconds"<<endl;    
       glFlush ();
    }int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (1000, 500); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("opengl");
    init (); srand( (unsigned)time( NULL ) );
    for (int i=0;i<count;i++)
    {
    point[i].x=(GLfloat)(rand()%MAX);
    point[i].y=(GLfloat)(rand()%MAX);
    }
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;  
    }