2013-02-24 63 views
0

我目前正在研究一個程序,它將能夠可視化沿着矢量場流動的平面上的點的演變。我已經完成了我在下面粘貼的第一個版本。當運行帶有大量點的程序時,似乎只有最後一點說30000點被吸入窗口。我希望能夠畫出大約1000000點,所以我走了。Opengl,C++:大量點

我已經試過如果迭代(迭代變量 - 控制點數)lovinging數量,並在這裏行爲就好了。然而,當增加它時,第一部分不再被繪製。

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

'glVertex2f'已棄用。您可能想要學習現代OpenGL,而不是20世紀90年代的OpenGL:http://www.arcsynthesis.org/gltut/。特別是如果你想讓事情順利進行。 – Flexo 2013-02-24 12:20:56

+0

謝謝你的鏈接 - 將看看它。 – Martin 2013-02-24 12:28:16

+0

所有的OpenGL都是合理的;我懷疑問題在於其他地方。特別是,您對'gluOrtho2D'的調用將所有粒子限制在x和y範圍內的[-2,2]範圍內。你確定你所有的數字整合(以及特定的位置)都在這個範圍內嗎? – radical7 2013-02-24 14:27:45

回答

4

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

+0

好戲 - 然後我會看看紋理。 – Martin 2013-02-24 12:54:49

+0

其實,在這個例子中我沒有看到使用'glVertex'的問題(除了在使用舊式OpenGL的時候可能不太喜歡)。 OP想要做的僅僅是繪製一系列2D點(忽略速度特徵,其中有些方法(例如,VBOs)),這是要走的路。它只是解決了問題,而程序員不需要有效編寫光柵化器。這是一種「風格」(使用現代OpenGL,因爲它是很酷的方式),而不是「生產力」(讓你的工作完成)。 – radical7 2013-02-24 14:32:31

-2

可能在您的實現中,原語的大小限於簽名short,即32768點。如果是這樣的話,你必須爲每個組的32768點左右做glEnd/glBegin

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

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

+0

似乎您對32768像素的限制是正確的。謝謝 - 我會看看VBO。 – Martin 2013-02-24 12:53:21

+0

@rodrigo不,這是不正確的。在OpenGL中,對於可以在'glBegin' /'glEnd'對之間發送的頂點數量,特別是對於'GL_POINTS',沒有這種限制。根據基本類型(例如GL_POINTS),當OpenGL接收到許多頂點時,它將柵格化該幾何基元。看看(挑動頂點)[https://www.opengl.org/wiki/Primitive#Provoking_vertex]。順便說一句,無論你使用的是開始/結束語義,還是VBOs和'glDrawArrays'或'glDrawElements',都是如此。 – radical7 2013-02-24 14:24:18