
Update glcontrol_paint when clicking a button on a form
Posted Saturday, 28 July, 2012 - 00:52 by aa12 inI am making a form application. I want to update my image whenever I click a button. I was told that GL.MatrixMode and Viewport() should be in glControl1_Paint. The reason for that is because I have tabs and want to keep the same image when moving tabs back and forth (otherwise my loaded image disappers after I move to tab2 and back to tab1 where there is my image).
I do not know how to call glControl1_Paint in button1. I wrote the code below but it does not work. Could someone help me?
private void glControl1_Paint(object sender, PaintEventArgs e)
{
SetupViewport();
GL.MatrixMode(MatrixMode.Modelview);
}
private void button1_Click(object sender, EventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.LoadIdentity();
glControl1.MakeCurrent();
(my graphic functions.......)
glControl1.SwapBuffers();
}


Comments
Re: Update glcontrol_paint when clicking a button on a form
Call the void in button1_click ?
Re: Update glcontrol_paint when clicking a button on a form
What I want to do is always keep a black point defined in glControl1_paint, say as a reference point. Then when I click a button 1, the other point is displayed. When I click button 2, the point displayed by button 1 disappears and shows another point defined in button 2. I have two problems in this code.
1) After clicking a button 1 and the point is displayed, it does not stay there once I minimize a window and go back to orignal widow size. I believe it's because I did not call paint event correctly, but do not know how to do it.
2) When I click the second button, 1st point does not disaapear. How can I clear only the point defined in button_click1 (while keeping the reference point)?
private void glControl1_Paint(object sender, EventArgs e)
{
SetupViewport();
GL.ClearColor(Color.White);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.Color3(1, 0, 0);
GL.PointSize(5);
GL.Begin(BeginMode.Points);
GL.Vertex3(0, 0, 10);
GL.End();
glControl1.SwapBuffers();
}
private void button1_Click(object sender, EventArgs e)
{
glControl1_Paint(null, null);
GL.PointSize(20);
GL.Color3(1, .5, 0);
GL.Begin(BeginMode.Points);
GL.Vertex3(0, 0, -30);
GL.End();
glControl1.SwapBuffers();
}
private void button2_Click(object sender, EventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
glControl1_Paint(null, null);
GL.Color3(.5, 0, 1);
GL.PointSize(30);
GL.Begin(BeginMode.Points);
GL.Vertex3(0, -50, 30);
GL.End();
glControl1.SwapBuffers();
}
Re: Update glcontrol_paint when clicking a button on a form
First, do all your drawing in the paint event. Your button event handlers should simply set a variable which the paint event checks to decide whether to draw the other points; you can force a repaint from the button handler by calling glControl1.Invalidate().
Second, you should reset your viewport in the glcontrol1_resize event (I believe that's the correct name--going by memory here); there's no need to mess with it during every paint event.
Re: Update glcontrol_paint when clicking a button on a form
Thank you for your reply. Now I can do what I want. There is still one thing I need to fix. How can I disable/ignore GL.Begin and End() command to draw a point when I just start debugging? Since the program loads paint event initially, they draw a point with my inital values of x, y z.
I do not want anything displayed on the screen before clicking buttons. Right now, what I am donig is just set x,y,z very large number so that its coordinate is out of display limit. Is there any smarter way to do this?