Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with CsGL

Started by nullsmind Apr 13, 2006 at 11:17 AM 1 replies 800+ views
Original Post
nullsmind
nullsmind
I have OpenGL almost working fine with C#, but I'm noticing terrible performance problems. Every frame being rendered is chopping almost constantly besides a few seconds in the beginning. All that's showing is a rotating white triangle. Any idea how this code can be optimized?

#region Using Clauses
using System; 
using System.Drawing; 
using System.Windows.Forms;
using System.Threading;
using CsGL.OpenGL;
#endregion

namespace CSGL_Application
{
    public partial class Form1 : Form
    {
        public CSGL_Application.OurView view; 

        public Form1()
        {
            InitializeComponent();

            this.view = new CSGL_Application.OurView();
            this.view.Parent = this;
            this.view.Dock = Dockstyle.Fill; 
        } 
    }

    public class OurView : OpenGLControl
    {
        float angle = 0.0f;

        public OurView() : base()
        {
            this.KeyDown += new KeyEventHandler(OurView_OnKeyDown);
        }

        public override void glDraw()
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);      
            GL.glLoadIdentity();  
            GL.gluLookAt(0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

            GL.glRotatef(angle, 0.0f, 1.0f, 0.0f);

            GL.glBegin(GL.GL_TRIANGLES);
            GL.glVertex3f(0.0f, 1.0f, 0.0f);
            GL.glVertex3f(-1.0f, -1.0f, 0.0f);
            GL.glVertex3f(1.0f, -1.0f, 0.0f);
            GL.glEnd();

            angle += 0.3f;

            this.SwapBuffer();
            this.Refresh();
        }

        protected override void InitGLContext()
        {
            GL.glShadeModel(GL.GL_SMOOTH);              // Set Smooth Shading                 
            GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // BackGround Color         
            GL.glClearDepth(1.0f);                      // Depth buffer setup             
            GL.glEnable(GL.GL_DEPTH_TEST);              // Enables Depth Testing             
            GL.glDepthFunc(GL.GL_LEQUAL);               // The Type Of Depth Test To Do     
            GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); 
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            Size s = Size;
            double aspect_ratio = (double)s.Width / (double)s.Height;
            GL.glMatrixMode(GL.GL_PROJECTION);  
            GL.glLoadIdentity();  

            // Calculate The Aspect Ratio Of The Window
            GL.gluPerspective(45.0f, aspect_ratio, 0.1f, 100.0f);

            GL.glMatrixMode(GL.GL_MODELVIEW);  
            GL.glLoadIdentity(); 
        }

        protected void OurView_OnKeyDown(object Sender, KeyEventArgs kea)
        {
            if (kea.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }
    }
}


nullsmind
nullsmind
Nice, I got it. I just had to rearrange Main() around to the following:

        /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            using (Form1 frm = new Form1())            {                frm.Show();                while (frm.Created)                {                    frm.view.glDraw();                    frm.view.Refresh();                    Application.DoEvents();                }            }        }
Expandable
Expandable
Application.DoEvents() is slow, if I remember correctly. Use the approach described here: http://blogs.msdn.com/tmiller/archive/2005/05/05/415008.aspx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.