oswa86's picture

call glcontrol1_paint many times spontaneous

I am using the OpenTK and c#. I have developed a graphics application that works fine but is slow. I need help to improve the performance of the application. My application generates a model that is made up of several elements. This is what I do for each element :

1- test if element has a result.
2- open file and read the result of each element.
3- choice the color of the element switch of the value of the result.
4- draw the element.

I'm new in opentk, when i follow the execution of my program, i note that my program return to glcontrol1_paint many times ( and in my program i call once time).
i think that 's why my program is slow.

my function glControl1_Paint,

private void glControl1_Paint(object sender, PaintEventArgs e)
{
glControl1.MakeCurrent();
GL.End();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

GL.PushMatrix();
GL.Translate(x, 0, 0);
GL.Translate(0, y, 0);
GL.Translate(0, 0, z);
GL.Translate(xp, yp, zp);
GL.Rotate(rotationZ, Vector3.UnitZ);
GL.Rotate(rotationY, Vector3.UnitY);
GL.Rotate(rotationX, Vector3.UnitX);
GL.Translate(-xp, -yp, -zp);
GL.Scale(scale, scale, scale);

Draw();

GL.PopMatrix();
glControl1.SwapBuffers();

}

i like to know how do eliminate the repetition not wanted.

Thanks.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mOfl's picture

You did not show much of your code, but it appears that you are using the GLControl and its PaintEvent. The purpose of this event is to redraw the GL content - which is therefore called as often as possible (except you limit the frame rate using VSync or some other delay function) to have a high refresh rate.

oswa86 wrote:

i note that my program return to glcontrol1_paint many times ( and in my program i call once time)

This is wrong, you should not call the event handler function yourself. If you want to use the event, you have to redesign your code. Usually, you do not draw something once in OpenGL, but continuously - even if the contents do not change. However, for what I see from the code you posted (leaving out Draw()), these commands should not slow the system down noticeably.

Apart from the fact that the immediate mode (the whole push/popmatrix, translate, scale, vertex4f etc. thing) is deprecated because there are much faster methods to draw primitives nowadays:

glControl1.MakeCurrent();
GL.End();

Is there a reason why you call GL.End() here? You did not even start your commands. The purpose of GL.Begin() and GL.End() is to collect a batch of drawing commands and send them to the video card at once to reduce the communication overhead between processor and video card. So you would do something like

GL.Begin(BeginMode.Triangles);
    GL.Vertex3f(0.5f, 0.5f, 0.0f);
    GL.Vertex3f(1.5f, 0.5f, 0.0f);
    GL.Vertex3f(0.5f, 1.0f, 0.0f);
GL.End();

in your Draw() function, if needed. I'm not quite familiar with the immediate mode because I never used it, but I think there is no other use for GL.End().

If this post isn't helpful for you, maybe try to specify what exactly is slow and how you want it to be. Try using a Stopwatch (look it up) object to see where exactly in the code the time consumption is so high. Maybe you can also fix the whole performance issue if you just activate vertical synchronisation (VSync, if it's like in the GameWindow class, there should be a property that you just set in the constructor) which limits the refresh rate of the GL output to whatever your screen refresh rate is (usually 60 Hz, so 60 frames per second).