本人想用C#.net调用OpenGL进行绘图,已经添加了using Tao.Platform.Windows;
using Tao.OpenGl;using Tao.FreeGlut;等引用,一般绘图没问题,但不能调用Glut函数,一旦调用,生成可以通过,但执行时总是执行不完,类似死机状态。请高手帮忙解决,非常感谢!!程序源代码如下:
public partial class Form1 : Form
{
public CustomControl1 glContainer = new CustomControl1(); public Form1()
{
InitializeComponent();
this.Controls.Add(glContainer);
glContainer.Location = new Point(0, 0);
glContainer.Dock = DockStyle.Fill;
glContainer.Visible = true;
} private void Form1_SizeChanged(object sender, EventArgs e)
{
glContainer.CustomControl1_SizeChanged(sender, e);
}
}public partial class CustomControl1 : Control
{
private IntPtr hRC = IntPtr.Zero;
private IntPtr hDC = IntPtr.Zero; public CustomControl1()
{
InitializeComponent();
InitializeStyles();
InitializeContexts();
}
private void InitializeStyles()
{
this.CreateParams.ClassStyle = this.CreateParams.ClassStyle |
User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, false);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
} public void InitializeContexts()
{
hDC = User.GetDC(this.Handle);
if (hDC == IntPtr.Zero)
{
MessageBox.Show("No window handle.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
pfd.nSize = (short)Marshal.SizeOf(pfd);
pfd.nVersion = 1;
pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |
Gdi.PFD_SUPPORT_OPENGL |
Gdi.PFD_DOUBLEBUFFER |
Gdi.PFD_GENERIC_ACCELERATED;
pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
pfd.cColorBits = (byte)32;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0; int pixelFormat = 0;
pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);
if (pixelFormat == 0) {
MessageBox.Show("Can't Set PixelFormat", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd))
{
MessageBox.Show("Can't Set The Chosen PixelFormat.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} hRC = Wgl.wglCreateContext(hDC);
if (hRC == IntPtr.Zero) {
MessageBox.Show("Can't Create GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
} MakeCurrent(); Gl.glClearColor(0, 0, 0, 0);
Gl.glClearDepth(100.0f); //清除深度缓存
Gl.glDepthFunc(Gl.GL_LESS); //指定用于深度缓存比较的值
Gl.glEnable(Gl.GL_DEPTH_TEST);
} public void MakeCurrent()
{
if (!Wgl.wglMakeCurrent(hDC, hRC))
{
MessageBox.Show("Can't Active GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} protected override void OnPaint(PaintEventArgs pe)
{
draw();
base.OnPaint(pe);
} private void draw()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glPushMatrix(); //旋转、平移一定的程度
Gl.glTranslatef(0.0f, 0.0f, -80.0f);
Gl.glRotatef(45.0f, 1.0f, 1.0f, 1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f);
//绘制一个球体
Glut.glutWireSphere(20.0, 2, 2);
Gl.glPopMatrix();
Gl.glFinish();
Gdi.SwapBuffers(Wgl.wglGetCurrentDC());
} public void CustomControl1_SizeChanged(object sender, EventArgs e)
{
if (this.Height > 0)
{
Gl.glViewport(0, 0, this.Width, this.Height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(70.0, (double)this.Width / (double)this.Height, 1.0, 200.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
}
}
using Tao.OpenGl;using Tao.FreeGlut;等引用,一般绘图没问题,但不能调用Glut函数,一旦调用,生成可以通过,但执行时总是执行不完,类似死机状态。请高手帮忙解决,非常感谢!!程序源代码如下:
public partial class Form1 : Form
{
public CustomControl1 glContainer = new CustomControl1(); public Form1()
{
InitializeComponent();
this.Controls.Add(glContainer);
glContainer.Location = new Point(0, 0);
glContainer.Dock = DockStyle.Fill;
glContainer.Visible = true;
} private void Form1_SizeChanged(object sender, EventArgs e)
{
glContainer.CustomControl1_SizeChanged(sender, e);
}
}public partial class CustomControl1 : Control
{
private IntPtr hRC = IntPtr.Zero;
private IntPtr hDC = IntPtr.Zero; public CustomControl1()
{
InitializeComponent();
InitializeStyles();
InitializeContexts();
}
private void InitializeStyles()
{
this.CreateParams.ClassStyle = this.CreateParams.ClassStyle |
User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, false);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
} public void InitializeContexts()
{
hDC = User.GetDC(this.Handle);
if (hDC == IntPtr.Zero)
{
MessageBox.Show("No window handle.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
pfd.nSize = (short)Marshal.SizeOf(pfd);
pfd.nVersion = 1;
pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |
Gdi.PFD_SUPPORT_OPENGL |
Gdi.PFD_DOUBLEBUFFER |
Gdi.PFD_GENERIC_ACCELERATED;
pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
pfd.cColorBits = (byte)32;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0; int pixelFormat = 0;
pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);
if (pixelFormat == 0) {
MessageBox.Show("Can't Set PixelFormat", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd))
{
MessageBox.Show("Can't Set The Chosen PixelFormat.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} hRC = Wgl.wglCreateContext(hDC);
if (hRC == IntPtr.Zero) {
MessageBox.Show("Can't Create GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
} MakeCurrent(); Gl.glClearColor(0, 0, 0, 0);
Gl.glClearDepth(100.0f); //清除深度缓存
Gl.glDepthFunc(Gl.GL_LESS); //指定用于深度缓存比较的值
Gl.glEnable(Gl.GL_DEPTH_TEST);
} public void MakeCurrent()
{
if (!Wgl.wglMakeCurrent(hDC, hRC))
{
MessageBox.Show("Can't Active GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} protected override void OnPaint(PaintEventArgs pe)
{
draw();
base.OnPaint(pe);
} private void draw()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glPushMatrix(); //旋转、平移一定的程度
Gl.glTranslatef(0.0f, 0.0f, -80.0f);
Gl.glRotatef(45.0f, 1.0f, 1.0f, 1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f);
//绘制一个球体
Glut.glutWireSphere(20.0, 2, 2);
Gl.glPopMatrix();
Gl.glFinish();
Gdi.SwapBuffers(Wgl.wglGetCurrentDC());
} public void CustomControl1_SizeChanged(object sender, EventArgs e)
{
if (this.Height > 0)
{
Gl.glViewport(0, 0, this.Width, this.Height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(70.0, (double)this.Width / (double)this.Height, 1.0, 200.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
}
}
public partial class Form1 : Form
{
public CustomControl1 glContainer = new CustomControl1(); public Form1()
{
InitializeComponent();
this.Controls.Add(glContainer);
glContainer.Location = new Point(0, 0);
glContainer.Dock = DockStyle.Fill;
glContainer.Visible = true;
} private void Form1_SizeChanged(object sender, EventArgs e)
{
glContainer.CustomControl1_SizeChanged(sender, e);
}
}
public partial class CustomControl1 : Control
{
private IntPtr hRC = IntPtr.Zero;
private IntPtr hDC = IntPtr.Zero; public CustomControl1()
{
InitializeComponent();
InitializeStyles();
InitializeContexts();
}
private void InitializeStyles()
{
this.CreateParams.ClassStyle = this.CreateParams.ClassStyle |
User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, false);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
} public void InitializeContexts()
{
hDC = User.GetDC(this.Handle);
if (hDC == IntPtr.Zero)
{
MessageBox.Show("No window handle.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
pfd.nSize = (short)Marshal.SizeOf(pfd);
pfd.nVersion = 1;
pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |
Gdi.PFD_SUPPORT_OPENGL |
Gdi.PFD_DOUBLEBUFFER |
Gdi.PFD_GENERIC_ACCELERATED;
pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
pfd.cColorBits = (byte)32;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0; int pixelFormat = 0;
pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);
if (pixelFormat == 0) {
MessageBox.Show("Can't Set PixelFormat", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd))
{
MessageBox.Show("Can't Set The Chosen PixelFormat.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} hRC = Wgl.wglCreateContext(hDC);
if (hRC == IntPtr.Zero) {
MessageBox.Show("Can't Create GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
} MakeCurrent(); Gl.glClearColor(0, 0, 0, 0);
Gl.glClearDepth(100.0f); //清除深度缓存
Gl.glDepthFunc(Gl.GL_LESS); //指定用于深度缓存比较的值
Gl.glEnable(Gl.GL_DEPTH_TEST);
} public void MakeCurrent()
{
if (!Wgl.wglMakeCurrent(hDC, hRC))
{
MessageBox.Show("Can't Active GL Rendering Context", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} protected override void OnPaint(PaintEventArgs pe)
{
draw();
base.OnPaint(pe);
} private void draw()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glPushMatrix(); //旋转、平移一定的程度
Gl.glTranslatef(0.0f, 0.0f, -80.0f);
Gl.glRotatef(45.0f, 1.0f, 1.0f, 1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f);
//绘制一个球体
Glut.glutWireSphere(20.0, 2, 2);
Gl.glPopMatrix();
Gl.glFinish();
Gdi.SwapBuffers(Wgl.wglGetCurrentDC());
} public void CustomControl1_SizeChanged(object sender, EventArgs e)
{
if (this.Height > 0)
{
Gl.glViewport(0, 0, this.Width, this.Height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(70.0, (double)this.Width / (double)this.Height, 1.0, 200.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
}
}
这样看的清些!
PS:
我的目标是 ----> ^_^
不明白4楼什么意思???
我用的是c#,且OpenGL是Tao框架的OpenGL,没有lib文件,只有dll和xml文件。
找作者报个bug或是咨询下。
另外,13楼的你所说的“C SHARP的游戏或是图形或是动画特效的教程”,我没有,我只是有一些相关的源程序。
http://www.windows7recovery.com/post/2009/07/27/TaoFreeGlutdll-Download-File-TaoFreeGlutdll.aspx