speed problem when drawing lines and points

Hello to everyone. Today I came across a small problem in my opengl app. My program needs to draw a trace line of the mouse (record the actions and replay later, in real-time). Because the program is going to be used with a touch screen I needed a different way to represent the trace line when the left mouse button was down and when it was up. So I thought I’d represent the mouse move with a line_strip when the button is down and with points when the button is up.
After a lot of thinking I finally came up with the algorithm to do this (the trace line is drawn in real time):

for (i = 0; i < g_cTraceLine.size(); i+=2) {
  // if ButtonTrace[i / 2] is true the mouse button is down.
  if (ButtonTrace[i / 2]) {
    //draw line strip
    glColor3f(1.0, 0.0, 0.0);			  
    glBegin(GL_LINE_STRIP);
    // if the ButtonTrace changes then end line_strip mode
    for (int j = i; ButtonTrace[i / 2] == ButtonTrace[j / 2]; j+=2) {
    glVertex2i(g_cTraceLine[j], g_cTraceLine[j + 1]);
    }
    glEnd();
  }
  else {
    //draw points
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    for (int j = i; ButtonTrace[i / 2] == ButtonTrace[j / 2]; j+=2) {
    glVertex2i(g_cTraceLine[j], g_cTraceLine[j + 1]);
    }
    glEnd();
  }
}

g_cTraceLine is a c++ vector, as is ButtonTrace.
What I do is have two vectors, g_cTraceLine which holds the mouse coordinates and ButtonTrace which is half the size of g_cTraceLine and for each mouse move holds the button’s state at the moment. But when I use the code, after a few seconds it starts to get really slow (a 15 second trace file, takes over a minute to complete). If I decide to draw only in points or only in line_strip then the speed is ok. Does anyone have any idea what might be causing this? I should mention that the g_cTraceLine and ButtonTrace vectors increase with each frame (I don’t read the whole trace before hand, but again if I switch to only one type of drawing there’s no speed problem).

Also a smaller problem, why when painting the GL_POINTS they seem to be black? I tried changing the color before but they’re always black. Thank you for your patience and your time.

You’re resending lots of geometry, because you don’t increment the outer loop’s i for each vertex you submit. You should :slight_smile:

Suppose the first five vertices are of the line type. You end up submitting five line strips:
1)0 1 2 3 4
2)1 2 3 4
3)2 3 4
4)3 4
5)4 (degenerate line strip; no error, but won’t draw anything)
Same for the points. That means that performance goes down quadratically with the length of “primitive runs”. IMO this would explain things.

You should also watch out for how many mouse movements you store. If you sample the full rate an average Windows system can deliver, you’ll get up to 200 new vertices per second …

As for the black points, I don’t know. What graphics hardware/driver version do you use?

Originally posted by zeckensack:
You’re resending lots of geometry, because you don’t increment the outer loop’s i for each vertex you submit. You should[…]
Exactly, as far as I can see you don’t need two loop variables at all.
You’re touching every point once and either add it to a line strip or to a point list, sounds pretty linear to me.

I just assume you mean by ‘black points’ that you don’t see the points at all. Try to increase point size to an amount where you can be sure that you should see something on screen.
But if you meant that the points are indeed black forget what I’ve written.

Yes, the point that I resent vertices along the way was exactly it. Making this fix did the trick:

glBegin(GL_LINE_STRIP);
for (int j = i; ButtonTrace[i / 2] == ButtonTrace[j / 2]; j+=2) {
    glVertex2i(g_cTraceLine[j], g_cTraceLine[j + 1]);
}
i = j;  // increment i, we've already read the past values
glEnd();

Thanks guys!

PS My problem with my points being “colored” black still remains. If you take a look in my initial post’s code you’ll see that before calling glBegin(GL_POINTS) I specifically ask glColor3f(0.0, 1.0, 0.0); But even though I do see my points (I have a white background) they are black! For the record my equipment is a Matrox Millenium Dualhead G450 with the 5.92.6.0 driver installed.

Matrox OpenGL drivers = major headache.

As soon as I find “Matrox” in the vendor string I terminate the program with a nice little messagebox saying “Unsupported 3d card…”, because I dont waste anymore of my time on their crappy drivers.