我用tao openGl 在c#里绘制动画,一切都很顺利,但是在最后主函数里加上while循环后,运行程序直接卡死,大家帮我想想是什么原因?我的代码:namespace tom.tao.opengl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); this.CreateParams.ClassStyle = this.CreateParams.ClassStyle | // Redraw On Size, And Own DC For Window.
User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background
this.SetStyle(ControlStyles.DoubleBuffer, true); // Buffer Control
this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background
this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves
}
public float rtri; // 用于三角形的角度
public float rquad; // 用于四边形的角度 public bool InitGL() // 此处开始对OpenGL进行所有设置
{
Gl.glShadeModel(Gl.GL_SMOOTH); // 启用阴影平滑
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 黑色背景
Gl.glClearDepth(1.0f); // 设置深度缓存
Gl.glEnable(Gl.GL_DEPTH_TEST); // 启用深度测试
Gl.glDepthFunc(Gl.GL_LEQUAL); // 所作深度测试的类型
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // 告诉系统对透视进行修正 Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height); // Reset The Current Viewport
Gl.glMatrixMode(Gl.GL_PROJECTION); // Select The Projection Matrix
Gl.glLoadIdentity(); // Reset The Projection Matrix
Glu.gluPerspective(45, simpleOpenGlControl1.Width / (double)simpleOpenGlControl1.Height, 0.1, 100); // Calculate The Aspect Ratio Of The Window
Gl.glMatrixMode(Gl.GL_MODELVIEW); // Select The Modelview Matrix
Gl.glLoadIdentity();
return true; // 初始化 OK
} private bool DrawGLScene()
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
Gl.glLoadIdentity(); // Reset The Current Modelview Matrix
Gl.glTranslatef(-1.5f, 0, -6); // Move Left 1.5 Units And Into The Screen 6.0
Gl.glRotatef(rtri, 0, 1, 0); // Rotate The Triangle On The Y axis ( NEW )
Gl.glBegin(Gl.GL_TRIANGLES); // Drawing Using Triangles
Gl.glColor3f(1, 0, 0); // Set The Color To Red
Gl.glVertex3f(0, 1, 0); // Top
Gl.glColor3f(0, 1, 0); // Set The Color To Green
Gl.glVertex3f(-1, -1, 0); // Bottom Left
Gl.glColor3f(0, 0, 1); // Set The Color To Blue
Gl.glVertex3f(1, -1, 0); // Bottom Right
Gl.glEnd(); // Finished Drawing The Triangle
Gl.glLoadIdentity(); // Reset The Current Modelview Matrix
Gl.glTranslatef(1.5f, 0, -6); // Move Right 1.5 Units And Into The Screen 6.0
Gl.glRotatef(rquad, 1, 0, 0); // Rotate The Quad On The X axis ( NEW )
Gl.glColor3f(0.5f, 0.5f, 1); // Set The Color To Blue One Time Only
Gl.glBegin(Gl.GL_QUADS); // Draw A Quad
Gl.glVertex3f(-1, 1, 0); // Top Left
Gl.glVertex3f(1, 1, 0); // Top Right
Gl.glVertex3f(1, -1, 0); // Bottom Right
Gl.glVertex3f(-1, -1, 0); // Bottom Left
Gl.glEnd(); // Done Drawing The Quad
rtri += 0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad -= 0.15f; Gl.glFlush();
// Decrease The Rotation Variable For The Quad ( NEW )
return true;
}
/// <summary>
/// 加载时开始动画
/// </summary>
/// <param name="se"></param>
/// <param name="e"></param>
public void simpleOpenGlControl1_Load(object se, EventArgs e)
{
InitGL();
} private void button1_Click_1(object sender, EventArgs e)
{
bool s = true;
while (s)//加上这个后就卡死了,但是openGl的程序都是这样写的啊
{
if (DrawGLScene() != true)
s = false;
simpleOpenGlControl1.Draw();
}
//simpleOpenGlControl1.Refresh();
} }
}
谢谢!
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); this.CreateParams.ClassStyle = this.CreateParams.ClassStyle | // Redraw On Size, And Own DC For Window.
User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background
this.SetStyle(ControlStyles.DoubleBuffer, true); // Buffer Control
this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background
this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves
}
public float rtri; // 用于三角形的角度
public float rquad; // 用于四边形的角度 public bool InitGL() // 此处开始对OpenGL进行所有设置
{
Gl.glShadeModel(Gl.GL_SMOOTH); // 启用阴影平滑
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 黑色背景
Gl.glClearDepth(1.0f); // 设置深度缓存
Gl.glEnable(Gl.GL_DEPTH_TEST); // 启用深度测试
Gl.glDepthFunc(Gl.GL_LEQUAL); // 所作深度测试的类型
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // 告诉系统对透视进行修正 Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height); // Reset The Current Viewport
Gl.glMatrixMode(Gl.GL_PROJECTION); // Select The Projection Matrix
Gl.glLoadIdentity(); // Reset The Projection Matrix
Glu.gluPerspective(45, simpleOpenGlControl1.Width / (double)simpleOpenGlControl1.Height, 0.1, 100); // Calculate The Aspect Ratio Of The Window
Gl.glMatrixMode(Gl.GL_MODELVIEW); // Select The Modelview Matrix
Gl.glLoadIdentity();
return true; // 初始化 OK
} private bool DrawGLScene()
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
Gl.glLoadIdentity(); // Reset The Current Modelview Matrix
Gl.glTranslatef(-1.5f, 0, -6); // Move Left 1.5 Units And Into The Screen 6.0
Gl.glRotatef(rtri, 0, 1, 0); // Rotate The Triangle On The Y axis ( NEW )
Gl.glBegin(Gl.GL_TRIANGLES); // Drawing Using Triangles
Gl.glColor3f(1, 0, 0); // Set The Color To Red
Gl.glVertex3f(0, 1, 0); // Top
Gl.glColor3f(0, 1, 0); // Set The Color To Green
Gl.glVertex3f(-1, -1, 0); // Bottom Left
Gl.glColor3f(0, 0, 1); // Set The Color To Blue
Gl.glVertex3f(1, -1, 0); // Bottom Right
Gl.glEnd(); // Finished Drawing The Triangle
Gl.glLoadIdentity(); // Reset The Current Modelview Matrix
Gl.glTranslatef(1.5f, 0, -6); // Move Right 1.5 Units And Into The Screen 6.0
Gl.glRotatef(rquad, 1, 0, 0); // Rotate The Quad On The X axis ( NEW )
Gl.glColor3f(0.5f, 0.5f, 1); // Set The Color To Blue One Time Only
Gl.glBegin(Gl.GL_QUADS); // Draw A Quad
Gl.glVertex3f(-1, 1, 0); // Top Left
Gl.glVertex3f(1, 1, 0); // Top Right
Gl.glVertex3f(1, -1, 0); // Bottom Right
Gl.glVertex3f(-1, -1, 0); // Bottom Left
Gl.glEnd(); // Done Drawing The Quad
rtri += 0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad -= 0.15f; Gl.glFlush();
// Decrease The Rotation Variable For The Quad ( NEW )
return true;
}
/// <summary>
/// 加载时开始动画
/// </summary>
/// <param name="se"></param>
/// <param name="e"></param>
public void simpleOpenGlControl1_Load(object se, EventArgs e)
{
InitGL();
} private void button1_Click_1(object sender, EventArgs e)
{
bool s = true;
while (s)//加上这个后就卡死了,但是openGl的程序都是这样写的啊
{
if (DrawGLScene() != true)
s = false;
simpleOpenGlControl1.Draw();
}
//simpleOpenGlControl1.Refresh();
} }
}
谢谢!
while (s)
{
...这是在当前线程中执行的,当然死掉。就是说程序时时都在处理这里的东西,无暇其它诸如显示之类的东西了。
应该新建一个线程来执行这个死循环。主线程依然维护自己的消息循环