OpenTK Hello World

using System;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace OpenTKTest {
 public class GLForm : Form {
  protected static GLControl cglGLView;
  protected static Label clblLog;
  protected static bool cbGLReady;

  public static void Main() {
   GLForm frmGL;

   //Create the form and give it control
   frmGL = new GLForm();
   Application.Run(frmGL);

   return;
  }

  public GLForm() {
   //Initialize class variables
   cbGLReady = false;
   cglGLView = new GLControl();
   clblLog = new Label();

   //Position and configure the controls
   cglGLView.Top = 0;
   cglGLView.Left = 0;
   cglGLView.Width = 640;
   cglGLView.Height = 480;

   clblLog.Top = 485;
   clblLog.Left = 0;
   clblLog.Width = 800;
   clblLog.Height = 125;
   clblLog.Text = "";

   //Define callbacks for the controls
   cglGLView.Load += new EventHandler(GLViewLoading);  
   cglGLView.Paint += new PaintEventHandler(GLViewPainting); 

   //Position and configure the form
   this.Width = 775;
   this.Height = 670;
   this.FormBorderStyle = FormBorderStyle.FixedSingle;
   Controls.Add(cglGLView);
   Controls.Add(clblLog);

   return;
  }

  public int WriteLog(string strText) {
   string strCurrText, strNewText;
   string[] astrList;
   int iCtr;

   strCurrText = clblLog.Text;
   astrList = strCurrText.Split('\n');

   if (astrList.Length > 9) { //log shows too man lines, truncate
    strCurrText = "";
    for (iCtr = 0; iCtr < 10; iCtr++) {
     strCurrText = astrList[astrList.Length - iCtr - 1] + "\n" + strCurrText;
    }
   } else { //Array has space, just add line
    strCurrText = clblLog.Text;
   }

   strNewText = strCurrText + "\n" + strText;

   //Remove extraneous new lines
   strNewText = strNewText.Replace("\n\n", "\n");
   while (strNewText.Substring(0, 1) == "\n") {
    strNewText = strNewText.Substring(1, strNewText.Length - 2);
   }

   //Put the log in the label
   clblLog.Text = strNewText;
   return 0;
  }

  private void GLViewLoading(object oSender, EventArgs eaArgs) {
   //The GLControl is now loading
   Matrix4 pmatMatrix;
   float fAspect;
   WriteLog("GLControl: Loading...");

   //Setup GL
   GL.ClearColor(0.0f, 0.0f, 0.2f, 0.0f);
   GL.Enable(EnableCap.DepthTest);

   //Setup the viewport
   GL.Viewport(0, 0, cglGLView.Width, cglGLView.Height);
   fAspect = cglGLView.Width / cglGLView.Height;
   pmatMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, fAspect, 0.1f, 100.0f);
   GL.MatrixMode(MatrixMode.Projection);
   GL.LoadMatrix(ref pmatMatrix);
   GL.MatrixMode(MatrixMode.Modelview);

   //Determine GL Version
   WriteLog("OpenGL Version: " + GL.GetString(StringName.Version));
   cbGLReady = true;

   return;
  }

  private void GLViewPainting(object oSender, EventArgs eaArgs) {
   Vector3[] v3Triangle;
   Vector4[] v4Colors;

   if (cbGLReady == false)  {//Control is not loaded, do nothing
    return;
   }
   WriteLog("Painting...");

   //Clear the GL View
   GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
   GL.MatrixMode(MatrixMode.Modelview);

   //Draw triangles
   GL.Begin(BeginMode.Triangles);
   GL.Color4(1.0f, 0.0f, 0.0f, 1.0f);
   GL.Vertex3(-0.5f, -1.0f, -8.0f);
   GL.Color4(0.0f, 1.0f, 0.0f, 1.0f);
   GL.Vertex3(-1.5f, 1.0f, -8.0f);
   GL.Color4(0.0f, 0.0f, 1.0f, 1.0f);
   GL.Vertex3(-2.5f, -1.0f, -8.0f);
   GL.End();

   v3Triangle = new Vector3[3];
   v4Colors = new Vector4[3];

   v3Triangle[0].X = 2.5f;
   v3Triangle[0].Y = -1.0f;
   v3Triangle[0].Z = -8.0f;
   v3Triangle[1].X = 1.5f;
   v3Triangle[1].Y = 1.0f;
   v3Triangle[1].Z = -8.0f;
   v3Triangle[2].X = 0.5f;
   v3Triangle[2].Y = -1.0f;
   v3Triangle[2].Z = -8.0f;

   v4Colors[0].X = 1.0f;
   v4Colors[0].Y = 0.0f;
   v4Colors[0].Z = -0.0f;
   v4Colors[0].W = 1.0f;
   v4Colors[1].X = 0.0f;
   v4Colors[1].Y = 1.0f;
   v4Colors[1].Z = 0.0f;
   v4Colors[1].W = 1.0f;
   v4Colors[2].X = 0.0f;
   v4Colors[2].Y = 0.0f;
   v4Colors[2].Z = 1.0f;
   v4Colors[2].W = 1.0f;

   GL.EnableClientState(ArrayCap.VertexArray);
   GL.EnableClientState(ArrayCap.ColorArray);

   GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(v3Triangle), v3Triangle);
   GL.ColorPointer(4, ColorPointerType.Float, BlittableValueType.StrideOf(v4Colors), v4Colors);
   GL.DrawArrays(BeginMode.Triangles, 0, v3Triangle.Length);

   GL.DisableClientState(ArrayCap.VertexArray);
   GL.DisableClientState(ArrayCap.ColorArray);

   //Drawing complete
   cglGLView.SwapBuffers();

   return;
  }
 }
}

No comments:

Post a Comment