0

I have a program that will draw a line as shown below.

private void glControl1_Paint(object sender, PaintEventArgs e)
    {
        GL.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT);

        GL.glMatrixMode(GL.GL_MODELVIEW);
        GL.glLoadIdentity();
        GL.glColor(Color.Yellow);

        GL.glBegin(GL.GL_LINES);
        GL.glVertex3f(100.0f, 100.0f, 0.0f); // origin of the line
        GL.glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the line
        GL.glEnd();

        glControl1.SwapBuffers();
    }

The method above is called during Paint event.

But I have another method as shown below:

    private void glControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
            GL.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT);
            GL.glMatrixMode(GL.GL_MODELVIEW);
            GL.glLoadIdentity();
            GL.glColor(Color.Yellow);

            GL.glBegin(GL.GL_LINES);
            GL.glVertex3f(100.0f, 100.0f, 0.0f); // origin of the FIRST line
            GL.glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the FIRST line
            GL.glVertex3f(120.0f, 170.0f, 10.0f); // origin of the SECOND line
            GL.glVertex3f(240.0f, 120.0f, 5.0f); // ending point of the SECOND line
            GL.glEnd();
    }

I wish to draw something in this method but it didn't work.

What's wrong.

Thanks

RJ Uy
  • 377
  • 3
  • 10
  • 20

1 Answers1

1

You should call glControl1.SwapBuffers(); after all your drawing at the end of your Paint event.

SwapBuffers will present the current buffer to the screen. usually you have two of those buffers switching all the time in render loops. You can clear it calling

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

What your second method does, is painting outside the Paint loop. You either need to use SwapBuffers in that event or queue your drawing and work the queue in your paint event.

Depending on how complex your drawing code gets, it may be appropriate to introduce a concept of a "scene" holding all objects that are to be drawn on each paint invocation.

Vengarioth
  • 684
  • 5
  • 13
  • 1
    Best practive would be to just update some variables in the `mousemove` event, and paint the line in the `paint` event, (where it should be done) using those variables – Timothy Groote Oct 27 '14 at 15:08