Todd
Todd

Reputation: 23

Translate and rotate image outside of glControl1_Paint using OpenTK?

I am making CAD type software in VS2010 Pro using a C# Windows Form Application and OpenTK. Nothing fancy; I just want to be able to read in some basic shapes and draw them. I'm not sure if this makes a difference to the answer, but I am drawing in 2D space using GL.Ortho();

To get familiar with graphics I've done a few OpenTK examples straight from the OpenTK documentation and have a basic understanding of it. From what I've learned so far I cannot move/rotate my primitives unless they were created within this event:

    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
    }

My program launches and waits for the user to select the CAD file to read in. After I read the file and break it down into primitives I draw it to the glControl1 form. So far it works as expected. However, I do not draw it in the "glControl1_Paint" event. Thus I have no control to translate/rotate it by using keyboard/mouse inputs.

I have read answers to other questions where the asker was directed to draw in the "glControl1_Paint" event. I would love to because it would solve my problem, but I am not sure how to do that since I don't have the primitives upon launch of the application, I wait for the user to provide the data.

I suppose I have a few questions that I would like to know the answers to:

1) When does the "glControl1_Paint" event happen in the program? I assumed it was part of initializing the glControl1 window and fired upon startup. Can I control when this happens so that I can draw my primitives here? If so, how do I control when this happens and how do I pass my geometry into this?

2) Is there a way to translate/rotate the my primitives outside of the "glControl1_Paint" event?

Upvotes: 0

Views: 421

Answers (1)

Gorkem
Gorkem

Reputation: 1757

No you can not know when paint event will trigger. But you can manually trigger it via Invalidate() function.

The flow should be like this.

  1. You should do the all the drawing in your paint event.
  2. If something happened that effects the drawing, you should call Invalidate() Keyboard events that moves objects or mouse events that rotates camera etc. all of them should call Invalidate()
  3. If you like maximum frame rate. you should override application main loop and make it call Invalidate() if there are no other windows messages to process.

here is my programming loop

static void Main()
{
...

MainForm mainFrom = new MainForm();
mainFrom.FormClosed += QuitLoop;
mainFrom.Show();
do
{
     Application.DoEvents();
     mainFrom.glControl1.Invalidate(true); //actually may program is a lot more complex than this
     if (mainFrom.IsRunning)
          System.Threading.Thread.Sleep(0);
     else
          System.Threading.Thread.Sleep(1);
} while (!mQuit);

...

Upvotes: 0

Related Questions