本人想用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);
             }
        }       
    }

解决方案 »

  1.   

    你添加了glut.dll到你的系统目录了吗?
      

  2.   

    系统目录C:\WINDOWS\system32下既有glut32.dll,还有glut.dll,还有Tao.FreeGlut.dll啊!!
      

  3.   


    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); 
                } 
            }       
        } 
    这样看的清些!
      

  4.   


    PS: 
    我的目标是 ----> ^_^
      

  5.   

    谢谢3楼的supper168!!!
    不明白4楼什么意思???
      

  6.   

    不知道你用的lib是啥东西?共享下
      

  7.   

    c++中采用lib文件吧??
    我用的是c#,且OpenGL是Tao框架的OpenGL,没有lib文件,只有dll和xml文件。
      

  8.   

    没用过这个库,难道库有问题??
    找作者报个bug或是咨询下。
      

  9.   

    因为C#不象C++那样可以直接用OpenGL,所以只能借助一些辅助库。我在Google上搜了,国外也在用这个Tao框架。
      

  10.   

    楼主也是高手啊。我是菜菜鸟。学习学习。要是楼主弄一个C SHARP的游戏或是图形或是动画特效的教程就好了。
      

  11.   

    谢谢各位兄弟的帮忙!!还请各位高手指点!!
    另外,13楼的你所说的“C SHARP的游戏或是图形或是动画特效的教程”,我没有,我只是有一些相关的源程序。
      

  12.   

    我也用C#做opengl,但用的是CsGL,这个连glut都没有,跟郁闷,现在感觉还是换Tao吧~~~
      

  13.   

    我也遇到你这个问题 用Tao.Opengl 和Tao.Platform.Windows 都没有问题 就是glut库有问题 郁闷死了 搞了几天了 
      

  14.   

    我用C++写的,同样是碰到死机的问题,是ChoosePixelFormat这个函数搞的,一跑到它就死了,我也不知道怎么解决,可能是驱动程序有问题,我跑到其他电脑上运行就没问题,一样的src,我再查查有没有解决办法。
      

  15.   

    “无法加载 DLL“freeglut.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”为何?有人知道吗?
      

  16.   

    重新在下载一个freeglut.dll文件,加载上
    http://www.windows7recovery.com/post/2009/07/27/TaoFreeGlutdll-Download-File-TaoFreeGlutdll.aspx
      

  17.   

    把系统目录C:\WINDOWS\system32下的Tao.FreeGlut.dll换成 freeGlut.dll  我是这样解决的,呵呵