Original Post
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();
}
}
}
}