PDA

View Full Version : drawing several multipolyline



ford
02-09-2007, 02:59 AM
Hi everyone! i am newby on opengl. i want to draw many polylines, a road map. i achieve it, but very slow. I´m reading about glVertexPointer and glDrawArrays but i don´t get to work well. This is my first code (it works):



// Example of my polylines. They have different length.

float[][] af = { {x,y,x,y,x,y },{x,y,x,y},.... };
for (int i = 0; i < af.Length;i++){
GL.glBegin(GL.GL_LINE_STRIP);
// Add one by one every vertex, this is slow
for (int j = 0;j < af[i].Length;)
GL.glVertex2f(af[i][j++],af[i][j++]);
GL.glEnd();
}Thanks a lot for any help about do this with array and pointers

plasmonster
02-09-2007, 08:15 AM
I don't know what to tell you, Ford. Your code won't even compile as posted.

What do you have in mind with this? Why the nested loops?

Here's the skinny on OpenGL geometric primitives, if that's thrown you off track:
http://www.rush3d.com/reference/opengl-redbook-1.1/chapter02.html

A step in the right direction might include a single line strip; a leap might include a vertex array or VBO.

Try a search for "opengl vertex arrays" on these boards or google (this subject has come up a lot).

Hope it helps.

Edit: I see what you're doing now. Roadmap--I missed it. So that's jolly good and all that rot. You just need the vertex array bit.

On a side note, calls across .NET interop boundaries are notoriously expensive. You are well advised to make good use of arrays/VBOs in that scenario ;-)

dorbie
02-11-2007, 08:32 PM
That code looks OK (I guess you're using a C++ container for OpenGL, I won't worry about that). One proviso; game cards tend not to draw lines as effiently as poolygons but they're no slouches either.


There are several performance issues with your code:

1) you have a dobule array access with dynamic variables to read each field.

2) You are drawing in immediate mode with legacy glVertex calls, convenient but not efficient w.r.t. function call overheads or data copying.

3) You have a loop inside your begin end pair that isn't unrolled.

You could start with glDrawArrays. Instead of the inner calls replace the begin end and everything in between with this code:


GL.glVertexPointer(2, GL.GL_FLOAT, 0, af[i]);
GL.glDrawArrays(GL.GL_LINE_STRIP, 0, af[i].Length);And of course you need to call GL.glEnableClientState(GL.GL_VERTEX_ARRAY); before this and enable for other attributes if you add them.

It should be much faster (if it compiles, it is untested but I'm sure you'll figure it out).
That array code and initialization of yours is really nasty FYI.

Kinda funny seeing that in the same program as a GL C++ wrapper. I hope your C++ wrapper supports the new functions and tokens.

ford
02-12-2007, 02:26 AM
Thanks all!!
First sorry for my previous code, it was really dirty. Now i am using vertexArray, and it´s works well, but maybe isn´t enought fast. I must draw many polylines (until 1 million). I test it with 35.000 polylines aprox. and takes one second (Pentium IV 3000). In this case is acceptable but with more polylines is too slow. My current code is:

// this don´t compile. It´s a sample of my datas
// [nº of polylines][nº of Vertex,x/y]
float[][,2] af = getRoadMap();

GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
int iLen = afCoor.Length;
for (int i = 0; i < iLen;i++){
GL.glVertexPointer(2,GL.GL_FLOAT,0,af[i]);
GL.glDrawArrays(GL.GL_LINE_STRIP,0, af[i].Length>>1);
}Do you have any idea to improve this? I am using c# (my boss imposes me), maybe could be the performance problem.

Thanks again!