0

我目前正在开发一个程序,该程序将能够可视化平面上沿矢量场流动的点的演变。我已经完成了我在下面粘贴的第一个版本。当运行具有大量点的程序时,似乎只有最后说的 30000 个点被绘制到窗口中。我希望能够绘制大约 1000000 点,所以我很遥远。

我已经尝试过喜欢迭代次数(迭代变量 - 控制点数),在这里它表现得很好。然而,当大幅增加它时,第一部分不再被绘制。

#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;
  }
4

2 回答 2

5

如果您只想为屏幕上的特定像素着色,则根本不应该使用glVertex。将它们全部放在一个连续的内存块中,从中创建一个纹理并渲染一个覆盖整个屏幕的四边形。这可能比在 OpenGL 中计算它们的位置更快。

于 2013-02-24T12:35:02.143 回答
-2

可能在您的实现中,原语的大小限制为带符号的短,即 32768 点。如果是这种情况,您必须glEnd/glBegin为每组 32768 点左右做:

for (int i = 0, x = 0; i <Iterations ; i++, x++) {
    //...
    if (x >= 32768)
    {
        x = 0;
        glEnd();
        glBegin(GL_POINTS);
    }
    //...
}

顺便说一句,您可以考虑使用顶点缓冲区对象 (VBO)。此限制可能相同,但绘制速度要快得多。

于 2013-02-24T12:25:29.577 回答