mirror of
https://github.com/Ryujinx/Opentk.git
synced 2024-12-24 02:25:28 +00:00
Start of T04_Lighting example. Slight updates to T10_GLSL_Cube. Updates to ExampleLauncher exception handling.
This commit is contained in:
parent
dfe1d7c335
commit
8cfee01131
|
@ -88,8 +88,8 @@ namespace Examples
|
|||
}
|
||||
catch (Exception expt)
|
||||
{
|
||||
MessageBox.Show(String.Format("Stacktrace:{0}{1}{0}{0}Inner exception:{0}{2}",
|
||||
System.Environment.NewLine, expt.StackTrace, expt.InnerException), expt.Message);
|
||||
MessageBox.Show(String.Format("{3}{0}Stacktrace:{0}{1}{0}{0}Inner exception:{0}{2}",
|
||||
System.Environment.NewLine, expt.StackTrace, expt.InnerException, expt.Message), expt.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,12 +39,14 @@ namespace Examples.Shapes
|
|||
|
||||
public static readonly Vector3[] Normals =
|
||||
{
|
||||
new Vector3( 1.0f, 0.0f, 0.0f),
|
||||
new Vector3( 0.0f, 1.0f, 0.0f),
|
||||
new Vector3( 0.0f, 0.0f, 1.0f),
|
||||
new Vector3(-1.0f, 0.0f, 0.0f),
|
||||
new Vector3( 0.0f, -1.0f, 0.0f),
|
||||
new Vector3( 0.0f, 0.0f, -1.0f),
|
||||
new Vector3(-1.0f, -1.0f, 1.0f),
|
||||
new Vector3( 1.0f, -1.0f, 1.0f),
|
||||
new Vector3( 1.0f, 1.0f, 1.0f),
|
||||
new Vector3(-1.0f, 1.0f, 1.0f),
|
||||
new Vector3(-1.0f, -1.0f, -1.0f),
|
||||
new Vector3( 1.0f, -1.0f, -1.0f),
|
||||
new Vector3( 1.0f, 1.0f, -1.0f),
|
||||
new Vector3(-1.0f, 1.0f, -1.0f),
|
||||
};
|
||||
|
||||
public static readonly int[] Colors =
|
||||
|
|
|
@ -10,28 +10,149 @@ using System.Text;
|
|||
|
||||
using OpenTK;
|
||||
using OpenTK.OpenGL;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Examples.Tutorial
|
||||
{
|
||||
class T04_Lit_Cube : GameWindow//, IExample
|
||||
class T04_Lit_Cube : GameWindow, IExample
|
||||
{
|
||||
float angle;
|
||||
|
||||
#region Constructor
|
||||
|
||||
public T04_Lit_Cube()
|
||||
{
|
||||
CreateWindow(new DisplayMode(800, 600));
|
||||
this.CreateWindow(new DisplayMode(800, 600));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region OnLoad
|
||||
|
||||
public override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
|
||||
GL.ClearColor(Color.MidnightBlue);
|
||||
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
|
||||
|
||||
GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY);
|
||||
GL.EnableClientState(GL.Enums.EnableCap.NORMAL_ARRAY);
|
||||
GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, Shapes.Cube.Vertices);
|
||||
GL.NormalPointer(GL.Enums.NormalPointerType.FLOAT, 0, Shapes.Cube.Normals);
|
||||
//GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors);
|
||||
|
||||
// Enable Light 0 and set its parameters.
|
||||
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.POSITION, new float[] { 1.0f, 1.0f, 0.0f });
|
||||
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.AMBIENT, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
|
||||
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.DIFFUSE, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
||||
GL.Lightv(GL.Enums.LightName.LIGHT0, GL.Enums.LightParameter.SPECULAR, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
||||
GL.LightModelv(GL.Enums.LightModelParameter.LIGHT_MODEL_AMBIENT, new float[] { 0.2f, 0.2f, 0.2f, 1.0f });
|
||||
GL.Enable(GL.Enums.EnableCap.LIGHTING);
|
||||
GL.Enable(GL.Enums.EnableCap.LIGHT0);
|
||||
|
||||
// Use GL.Material to set your object's material parameters..
|
||||
GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.AMBIENT, new float[] { 0.3f, 0.3f, 0.9f, 1.0f });
|
||||
GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.DIFFUSE, new float[] { 0.3f, 0.3f, 0.9f, 1.0f });
|
||||
GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.SPECULAR, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
|
||||
GL.Materialv(GL.Enums.MaterialFace.FRONT, GL.Enums.MaterialParameter.EMISSION, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
|
||||
}
|
||||
|
||||
#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.ARB.
|
||||
GL.Viewport(0, 0, Width, Height);
|
||||
|
||||
double ratio = e.Width / (double)e.Height;
|
||||
|
||||
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
|
||||
GL.LoadIdentity();
|
||||
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
||||
}
|
||||
|
||||
#region IExample Members
|
||||
#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[0][OpenTK.Input.Key.Escape])
|
||||
{
|
||||
this.Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
|
||||
Keyboard[0][OpenTK.Input.Key.Enter])
|
||||
{
|
||||
Fullscreen = !Fullscreen;
|
||||
}
|
||||
|
||||
//angle += 180.0f * (float)e.Time;
|
||||
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(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
||||
|
||||
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
|
||||
);
|
||||
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, Shapes.Cube.Indices.Length,
|
||||
GL.Enums.All.UNSIGNED_SHORT, Shapes.Cube.Indices);
|
||||
|
||||
Context.SwapBuffers();
|
||||
}
|
||||
|
||||
#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()
|
||||
{
|
||||
this.Run(60.0, 60.0);
|
||||
// Lock UpdateFrame and RenderFrame at 60Hz.
|
||||
Run(60.0, 60.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -28,17 +28,21 @@ namespace Examples.Tutorial
|
|||
#region Shaders
|
||||
|
||||
string[] vertex_shader_source =
|
||||
{
|
||||
"void main() {",
|
||||
"gl_FrontColor = gl_Color;",
|
||||
"gl_Position = ftransform();",
|
||||
"}",
|
||||
};
|
||||
{@"
|
||||
void main()
|
||||
{
|
||||
gl_FrontColor = gl_Color;
|
||||
gl_Position = ftransform();
|
||||
}
|
||||
"};
|
||||
|
||||
string[] fragment_shader_source =
|
||||
{
|
||||
"void main() { gl_FragColor = gl_Color; }\0"
|
||||
};
|
||||
{@"
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = gl_Color;
|
||||
}
|
||||
"};
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -76,9 +80,14 @@ namespace Examples.Tutorial
|
|||
return;
|
||||
}
|
||||
|
||||
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
|
||||
GL.ClearColor(Color.MidnightBlue);
|
||||
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
|
||||
|
||||
GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY);
|
||||
GL.EnableClientState(GL.Enums.EnableCap.COLOR_ARRAY);
|
||||
GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, Shapes.Cube.Vertices);
|
||||
GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors);
|
||||
|
||||
uint vertex_shader_object, fragment_shader_object;
|
||||
int status;
|
||||
uint shader_program;
|
||||
|
@ -126,14 +135,20 @@ namespace Examples.Tutorial
|
|||
|
||||
#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 = 0.0;
|
||||
ratio = this.Width / (double)this.Height;
|
||||
double ratio = e.Width / (double)e.Height;
|
||||
|
||||
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
|
||||
GL.LoadIdentity();
|
||||
|
@ -145,18 +160,27 @@ namespace Examples.Tutorial
|
|||
#region OnUpdateFrame
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when it is time to update the next frame.
|
||||
/// Prepares the next frame for rendering.
|
||||
/// </summary>
|
||||
/// <param name="e">Not used yet.</param>
|
||||
/// <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)
|
||||
{
|
||||
base.OnUpdateFrame(e);
|
||||
|
||||
if (Keyboard[0][OpenTK.Input.Key.Escape])
|
||||
{
|
||||
this.Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
|
||||
Keyboard[0][OpenTK.Input.Key.Enter])
|
||||
{
|
||||
Fullscreen = !Fullscreen;
|
||||
}
|
||||
|
||||
//angle += 180.0f * (float)e.Time;
|
||||
angle += 3.0f;
|
||||
if (angle > 720.0f)
|
||||
angle -= 720.0f;
|
||||
|
@ -166,10 +190,11 @@ namespace Examples.Tutorial
|
|||
|
||||
#region OnRenderFrame
|
||||
|
||||
/// <summary>
|
||||
/// Place your rendering code here.
|
||||
/// </summary>
|
||||
public override void OnRenderFrame(RenderFrameEventArgs e)
|
||||
{
|
||||
base.OnRenderFrame(e);
|
||||
|
||||
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
||||
|
||||
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
|
||||
|
@ -181,61 +206,14 @@ namespace Examples.Tutorial
|
|||
);
|
||||
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
DrawCube();
|
||||
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, Shapes.Cube.Indices.Length,
|
||||
GL.Enums.All.UNSIGNED_SHORT, Shapes.Cube.Indices);
|
||||
|
||||
Context.SwapBuffers();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region private void DrawCube()
|
||||
|
||||
private void DrawCube()
|
||||
{
|
||||
GL.Begin(GL.Enums.BeginMode.QUADS);
|
||||
|
||||
GL.Color3(Color.Silver);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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>
|
||||
|
@ -246,6 +224,7 @@ namespace Examples.Tutorial
|
|||
/// </remarks>
|
||||
public void Launch()
|
||||
{
|
||||
// Lock UpdateFrame and RenderFrame at 60Hz.
|
||||
Run(60.0, 60.0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue