Opentk/Source/Examples/Tutorial/T03_Immediate_Mode_Cube.cs

203 lines
5.4 KiB
C#
Raw Normal View History

#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
#region --- Using Directives ---
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using OpenTK;
using OpenTK.OpenGL;
using OpenTK.OpenGL.Enums;
using OpenTK.Platform;
#endregion
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates immediate mode rendering. Example is incomplete, and will probably go away in the future.
/// </summary>
public class T03_Immediate_Mode_Cube : OpenTK.GameWindow, IExample
{
#region --- Fields ---
/// <summary>
/// Denotes the cube rotation.
/// </summary>
float angle = 0.0f;
#endregion
#region --- Constructors ---
public T03_Immediate_Mode_Cube() : base(new DisplayMode(800, 600), "OpenTK Tutorial: Immediate mode rendering")
{ }
#endregion
#region OnLoad
public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(Color.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
}
#endregion
#region OnResize
/// <summary>
/// Called when the user resizes the window.
/// </summary>
/// <param name="e">Contains the new width/height of the window.</param>
/// <remarks>
/// You want the OpenGL viewport to match the window. This is the place to do it!
/// </remarks>
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Width, Height);
double ratio = e.Width / (double)e.Height;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Glu.Perspective(45.0, ratio, 1.0, 64.0);
}
#endregion
#region OnUpdateFrame
/// <summary>
/// Prepares the next frame for rendering.
/// </summary>
/// <remarks>
/// Place your control logic here. This is the place to respond to user input,
/// update object positions etc.
/// </remarks>
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
/*
if (Keyboard[OpenTK.Input.Key.Escape])
{
this.Exit();
return;
}
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
Keyboard[OpenTK.Input.Key.Enter])
{
Fullscreen = !Fullscreen;
}
*/
angle += 3.0f;
if (angle > 720.0f)
angle -= 720.0f;
}
#endregion
#region OnRenderFrame
/// <summary>
/// Place your rendering code here.
/// </summary>
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(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.Rotate(angle, 0.0f, 1.0f, 0.0f);
DrawCube();
Context.SwapBuffers();
Thread.Sleep(0);
}
#endregion
#region private void DrawCube()
private void DrawCube()
{
GL.Begin(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-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);
2007-08-20 10:46:37 +00:00
GL.Vertex3(1.0f, 1.0f, -1.0f);
2007-08-01 21:14:39 +00:00
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();
}
#endregion
#region public void Launch()
/// <summary>
/// Launches this example.
/// </summary>
/// <remarks>
/// Provides a simple way for the example launcher to launch the examples.
/// </remarks>
public void Launch()
{
2007-10-20 10:54:40 +00:00
Run(85.0, 0.0);
}
#endregion
2007-10-20 10:54:40 +00:00
public static readonly int order = 3;
}
}