2007-09-02 22:52:00 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2007-09-25 21:11:16 +00:00
|
|
|
|
using System.Drawing;
|
2007-09-02 22:52:00 +00:00
|
|
|
|
|
|
|
|
|
using OpenTK;
|
2008-04-13 16:45:56 +00:00
|
|
|
|
using OpenTK.Graphics;
|
2007-09-25 21:11:16 +00:00
|
|
|
|
using Examples.Shapes;
|
2007-09-02 22:52:00 +00:00
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
2007-09-26 11:47:30 +00:00
|
|
|
|
/// <summary>
|
2007-09-26 15:39:32 +00:00
|
|
|
|
/// Demonstrates fixed-function OpenGL lighting. Example is incomplete (documentation).
|
2007-09-26 11:47:30 +00:00
|
|
|
|
/// </summary>
|
2007-09-27 01:14:09 +00:00
|
|
|
|
class T04_Vertex_Lighting : GameWindow, IExample
|
2007-09-02 22:52:00 +00:00
|
|
|
|
{
|
2007-09-25 21:11:16 +00:00
|
|
|
|
float x_angle, zoom;
|
|
|
|
|
Shape shape = new Plane(16, 16, 4.0f, 4.0f);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
|
2008-04-13 15:51:09 +00:00
|
|
|
|
public T04_Vertex_Lighting() : base(800, 600)
|
2007-09-02 22:52:00 +00:00
|
|
|
|
{
|
2007-09-23 13:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnLoad
|
|
|
|
|
|
|
|
|
|
public override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
|
|
|
|
GL.ClearColor(Color.MidnightBlue);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
2007-09-26 11:47:30 +00:00
|
|
|
|
//GL.Enable(GL.Enums.EnableCap.CULL_FACE);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.EnableClientState(EnableCap.VertexArray);
|
|
|
|
|
GL.EnableClientState(EnableCap.NormalArray);
|
|
|
|
|
GL.VertexPointer(3, VertexPointerType.Float, 0, shape.Vertices);
|
|
|
|
|
GL.NormalPointer(NormalPointerType.Float, 0, shape.Normals);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
|
|
|
|
// Enable Light 0 and set its parameters.
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.Lightv(LightName.Light0, LightParameter.Position, new float[] { 1.0f, 1.0f, -0.5f });
|
|
|
|
|
GL.Lightv(LightName.Light0, LightParameter.Ambient, new float[] { 0.3f, 0.3f, 0.3f, 1.0f });
|
|
|
|
|
GL.Lightv(LightName.Light0, LightParameter.Diffuse, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
|
GL.Lightv(LightName.Light0, LightParameter.Specular, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
|
GL.Lightv(LightName.Light0, LightParameter.SpotExponent, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
|
GL.LightModelv(LightModelParameter.LightModelAmbient, new float[] { 0.2f, 0.2f, 0.2f, 1.0f });
|
|
|
|
|
GL.LightModel(LightModelParameter.LightModelTwoSide, 1);
|
|
|
|
|
GL.LightModel(LightModelParameter.LightModelLocalViewer, 1);
|
|
|
|
|
GL.Enable(EnableCap.Lighting);
|
|
|
|
|
GL.Enable(EnableCap.Light0);
|
|
|
|
|
|
|
|
|
|
// Use GL.Material to set your object's material parameters.
|
|
|
|
|
GL.Materialv(MaterialFace.Front, MaterialParameter.Ambient, new float[] { 0.3f, 0.3f, 0.3f, 1.0f });
|
|
|
|
|
GL.Materialv(MaterialFace.Front, MaterialParameter.Diffuse, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
|
GL.Materialv(MaterialFace.Front, MaterialParameter.Specular, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
|
GL.Materialv(MaterialFace.Front, MaterialParameter.Emission, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
|
2007-09-02 22:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
#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>
|
2007-09-02 22:52:00 +00:00
|
|
|
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
|
|
|
|
|
|
double ratio = e.Width / (double)e.Height;
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
2007-09-02 22:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
#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)
|
|
|
|
|
{
|
2007-09-26 11:47:30 +00:00
|
|
|
|
if (Keyboard[OpenTK.Input.Key.Escape])
|
2007-09-23 13:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
this.Exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 11:47:30 +00:00
|
|
|
|
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
|
|
|
|
|
Keyboard[OpenTK.Input.Key.Enter])
|
2008-04-20 17:59:05 +00:00
|
|
|
|
if (WindowState != WindowState.Fullscreen)
|
|
|
|
|
WindowState = WindowState.Fullscreen;
|
|
|
|
|
else
|
|
|
|
|
WindowState = WindowState.Normal;
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-09-26 11:47:30 +00:00
|
|
|
|
if (Mouse[OpenTK.Input.MouseButton.Left])
|
2007-09-26 15:39:32 +00:00
|
|
|
|
x_angle += Mouse.XDelta;
|
2007-09-25 21:11:16 +00:00
|
|
|
|
else
|
|
|
|
|
x_angle += 0.5f;
|
|
|
|
|
|
2007-09-26 16:12:59 +00:00
|
|
|
|
// zoom = Mouse.Wheel * 0.5f; // Mouse.Wheel is broken on both Linux and Windows.
|
|
|
|
|
if (Mouse[OpenTK.Input.MouseButton.Right])
|
|
|
|
|
zoom += Mouse.YDelta;
|
2007-09-25 21:11:16 +00:00
|
|
|
|
|
2007-09-26 15:39:32 +00:00
|
|
|
|
// Do not leave x_angle drift too far away, as this will cause inaccuracies.
|
|
|
|
|
if (x_angle > 360.0f)
|
|
|
|
|
x_angle -= 360.0f;
|
|
|
|
|
else if (x_angle < -360.0f)
|
|
|
|
|
x_angle += 360.0f;
|
2007-09-23 13:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnRenderFrame
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Place your rendering code here.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
|
|
|
|
{
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.LoadIdentity();
|
2007-11-04 15:32:24 +00:00
|
|
|
|
Glu.LookAt(0.0, 0.0, -7.5 + zoom,
|
|
|
|
|
0.0, 0.0, 0.0,
|
|
|
|
|
0.0, 1.0, 0.0);
|
2007-09-25 21:11:16 +00:00
|
|
|
|
GL.Rotate(x_angle, 0.0f, 1.0f, 0.0f);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
//GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, shape.Indices.Length,
|
|
|
|
|
// GL.Enums.All.UNSIGNED_INT, shape.Indices);
|
|
|
|
|
GL.DrawArrays(BeginMode.Points, 0, shape.Vertices.Length);
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-09-26 11:47:30 +00:00
|
|
|
|
SwapBuffers();
|
2007-09-23 13:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void Launch()
|
2007-09-02 22:52:00 +00:00
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
2007-09-02 22:52:00 +00:00
|
|
|
|
public void Launch()
|
|
|
|
|
{
|
2007-09-23 13:38:03 +00:00
|
|
|
|
// Lock UpdateFrame and RenderFrame at 60Hz.
|
|
|
|
|
Run(60.0, 60.0);
|
2007-09-02 22:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-10-20 10:54:40 +00:00
|
|
|
|
|
|
|
|
|
public static readonly int order = 4;
|
2007-09-02 22:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|