Opentk/Source/Examples/OpenGL/Basic/Lesson01/Basic01.cs

164 lines
4.8 KiB
C#
Raw Normal View History

2006-11-05 11:50:08 +00:00
#region --- License ---
/* This source file is released under the MIT license. See License.txt for more information.
* Coded by Erik Ylvisaker and Stephen Apostolopoulos.
*/
#endregion
2006-11-02 21:40:36 +00:00
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OpenTK.OpenGL;
namespace Lesson01
{
2006-11-05 11:50:08 +00:00
public class Cube : OpenTK.Framework
2006-11-02 21:40:36 +00:00
{
static float angle;
2006-11-05 11:50:08 +00:00
#region Entry point
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Cube().Run();
}
#endregion
2006-11-02 21:40:36 +00:00
#region Load event handler
protected override void OnLoad(object sender, EventArgs e)
{
base.OnLoad(sender, e);
Text =
GL.GetString(Enums.StringName.VENDOR) + " " +
GL.GetString(Enums.StringName.RENDERER) + " " +
GL.GetString(Enums.StringName.VERSION);
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
GL.Enable(Enums.EnableCap.DEPTH_TEST);
OnResize(this, e);
}
#endregion
#region Resize event handler
protected override void OnResize(object sender, EventArgs e)
{
base.OnResize(sender, e);
// if (this.Context == null)
// return;
if (ClientSize.Height == 0)
ClientSize = new System.Drawing.Size(ClientSize.Width, 1);
GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
double ratio = 0.0;
ratio = ClientSize.Width / (double)ClientSize.Height;
//if (ClientSize.Width > ClientSize.Height)
// ratio = ClientSize.Width / (double)ClientSize.Height;
//else
// ratio = ClientSize.Height / (double)ClientSize.Width;
GL.MatrixMode(Enums.MatrixMode.PROJECTION);
GL.LoadIdentity();
Glu.Perspective(45.0, ratio, 1.0, 64.0);
}
#endregion
#region Paint event handler
protected override void OnPaint()
{
GL.MatrixMode(Enums.MatrixMode.MODELVIEW);
GL.LoadIdentity();
Glu.LookAt(
0.0, 5.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GL.Rotatef(angle, 0.0f, 1.0f, 0.0f);
angle += 0.5f;
GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
DrawCube();
ActiveContext.SwapBuffers();
}
#endregion
#region KeyDown event handler
protected override void OnKeyDown(object sender, KeyEventArgs e)
{
base.OnKeyDown(sender, e);
switch (e.KeyData)
{
case Keys.Escape:
Application.Exit();
break;
case Keys.F1:
this.SetResolution(this.Width, this.Height, this.ColorDepth, !this.IsFullscreen);
break;
}
}
#endregion
#region DrawCube
public void DrawCube()
{
GL.Begin(Enums.BeginMode.QUADS);
GL.Color3f(1, 0, 0);
GL.Vertex3f(-1.0f, -1.0f, -1.0f);
GL.Vertex3f(-1.0f, 1.0f, -1.0f);
GL.Vertex3f(1.0f, 1.0f, -1.0f);
GL.Vertex3f(1.0f, -1.0f, -1.0f);
GL.Color3f(1, 1, 0);
GL.Vertex3f(-1.0f, -1.0f, -1.0f);
GL.Vertex3f(1.0f, -1.0f, -1.0f);
GL.Vertex3f(1.0f, -1.0f, 1.0f);
GL.Vertex3f(-1.0f, -1.0f, 1.0f);
GL.Color3f(1, 0, 1);
GL.Vertex3f(-1.0f, -1.0f, -1.0f);
GL.Vertex3f(-1.0f, -1.0f, 1.0f);
GL.Vertex3f(-1.0f, 1.0f, 1.0f);
GL.Vertex3f(-1.0f, 1.0f, -1.0f);
GL.Color3f(0, 1, 0);
GL.Vertex3f(-1.0f, -1.0f, 1.0f);
GL.Vertex3f(1.0f, -1.0f, 1.0f);
GL.Vertex3f(1.0f, 1.0f, 1.0f);
GL.Vertex3f(-1.0f, 1.0f, 1.0f);
GL.Color3f(0, 0, 1);
GL.Vertex3f(-1.0f, 1.0f, -1.0f);
GL.Vertex3f(-1.0f, 1.0f, 1.0f);
GL.Vertex3f(1.0f, 1.0f, 1.0f);
GL.Vertex3f(1.0f, 1.0f, -1.0f);
GL.Color3f(0, 1, 1);
GL.Vertex3f(1.0f, -1.0f, -1.0f);
GL.Vertex3f(1.0f, 1.0f, -1.0f);
2006-11-05 11:50:08 +00:00
GL.Vertex3f(1.0f, 1.0f, 1.0f);
2006-11-02 21:40:36 +00:00
GL.Vertex3f(1.0f, -1.0f, 1.0f);
GL.End();
}
#endregion
}
}