簡體   English   中英

Opengl,C ++:大量積分

[英]Opengl,C++ : Large number of points

我正在研究一個程序,它將能夠可視化沿着矢量場流動的平面上的點的演變。 我已經完成了下面粘貼的第一個版本。 當運行具有大量點的程序時,似乎只有最后說30000點被吸引到窗口中。 我希望能夠畫出大約100萬點,所以我離開了。

如果迭代(Iteration變量 - 控制點數),我已經嘗試過這個數字,這里它的行為就好了。 然而,當基本上增加它時,不再繪制第一部分。

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

如果您只想在屏幕上為特定像素着色,則根本不應該使用glVertex 將它們全部放在contiguos內存塊中,從中創建紋理並渲染覆蓋整個屏幕的四邊形。 這可能比計算他們在OpenGL中的位置更快。

可能在您的實現中,基元的大小僅限於有符號的短,即32768點。 如果是這種情況,你必須為每組32768點左右做glEnd/glBegin

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

順便說一句,你可以考慮使用頂點緩沖對象(VBO)。 這種限制可能是相同的,但繪制速度要快得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM