Opentk/Source/Examples/WinForms/W02_Immediate_Mode_Cube.cs

220 lines
5.6 KiB
C#
Raw Normal View History

#region --- License ---
/* This source file is released under the MIT license. See License.txt for more information.
* Coded by Erik Ylvisaker and Stefanos Apostolopoulos.
*/
#endregion
#region --- Using directives ---
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using OpenTK.OpenGL;
//using Enums = OpenTK.OpenGL.GL.Enums;
using OpenTK.Platform;
using System.Threading;
#endregion
namespace Examples.WinForms
{
public partial class W02_Immediate_Mode_Cube : Form, IExample
{
static float angle = 0.0f;
#region --- Constructor ---
public W02_Immediate_Mode_Cube()
{
InitializeComponent();
}
#endregion
#region OnLoad
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
glControl.KeyDown += new KeyEventHandler(glControl_KeyDown);
//glControl.Resize += new EventHandler(glControl_Resize);
glControl.Paint += new PaintEventHandler(glControl_Paint);
Text =
GL.GetString(GL.Enums.StringName.VENDOR) + " " +
GL.GetString(GL.Enums.StringName.RENDERER) + " " +
GL.GetString(GL.Enums.StringName.VERSION);
GL.ClearColor(Color.MidnightBlue);
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
Application.Idle += Application_Idle;
//glControl_Resize(glControl, EventArgs.Empty);
}
#endregion
#region OnClosing
protected override void OnClosing(CancelEventArgs e)
{
Application.Idle -= Application_Idle;
base.OnClosing(e);
}
#endregion
#region Application_Idle event
void Application_Idle(object sender, EventArgs e)
{
while (glControl.IsIdle)
{
Render();
}
}
#endregion
#region private void Render()
private void Render()
2007-08-20 12:25:48 +00:00
{
GL.MatrixMode(GL.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
);
2007-09-02 00:53:24 +00:00
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
angle += 0.5f;
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
DrawCube();
glControl.SwapBuffers();
}
#endregion
#region GLControl.Resize event handler
void glControl_Resize(object sender, EventArgs e)
{
OpenTK.GLControl c = sender as OpenTK.GLControl;
if (c.ClientSize.Height == 0)
c.ClientSize = new System.Drawing.Size(c.ClientSize.Width, 1);
GL.Viewport(0, 0, c.ClientSize.Width, c.ClientSize.Height);
double ratio = 0.0;
ratio = c.ClientSize.Width / (double)c.ClientSize.Height;
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
GL.LoadIdentity();
Glu.Perspective(45.0, ratio, 1.0, 64.0);
}
#endregion
#region GLControl.KeyDown event handler
void glControl_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.Escape:
this.Close();
break;
}
}
#endregion
#region GLControl.Paint event handler
void glControl_Paint(object sender, PaintEventArgs e)
{
Render();
}
#endregion
2007-08-10 20:16:05 +00:00
#region private void DrawCube()
private void DrawCube()
{
GL.Begin(GL.Enums.BeginMode.QUADS);
GL.Color3(Color.Silver);
2007-08-01 21:14:39 +00:00
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Color3(Color.Honeydew);
2007-08-01 21:14:39 +00:00
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Color3(Color.Moccasin);
2007-08-20 12:25:48 +00:00
2007-08-01 21:14:39 +00:00
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Color3(Color.IndianRed);
2007-08-01 21:14:39 +00:00
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Color3(Color.PaleVioletRed);
2007-08-01 21:14:39 +00:00
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Color3(Color.ForestGreen);
2007-08-01 21:14:39 +00:00
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.End();
}
2007-08-10 20:16:05 +00:00
#endregion
#region IExample Members
public void Launch()
{
}
2007-10-20 10:54:40 +00:00
public static readonly int order = 2;
#endregion
private void glControl_Layout(object sender, LayoutEventArgs e)
{
glControl_Resize(sender, EventArgs.Empty);
}
}
}