-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple3DModel.cs
126 lines (105 loc) · 3.93 KB
/
Simple3DModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Graphics.OpenGL4;
namespace Simple3DModel
{
class Program
{
static void Main(string[] args)
{
var nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "Simple 3D Model - Rotating Cube"
};
using (var window = new ModelWindow(GameWindowSettings.Default, nativeWindowSettings))
{
window.Run();
}
}
}
class ModelWindow : GameWindow
{
private float _rotationAngle;
private DateTime _lastUpdateTime;
public ModelWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
}
protected override void OnLoad()
{
base.OnLoad();
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
_lastUpdateTime = DateTime.Now;
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.Translate(0.0f, 0.0f, -6.0f);
GL.Rotate(_rotationAngle, Vector3.UnitX);
GL.Rotate(_rotationAngle, Vector3.UnitY);
GL.Rotate(_rotationAngle, Vector3.UnitZ);
DrawCube();
SwapBuffers();
DateTime currentTime = DateTime.Now;
TimeSpan elapsedTime = currentTime - _lastUpdateTime;
_rotationAngle += (float)elapsedTime.TotalMilliseconds / 100.0f;
_lastUpdateTime = currentTime;
}
private void DrawCube()
{
GL.Begin(PrimitiveType.Quads);
// Front face
GL.Color3(1.0f, 0.0f, 0.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.Vertex3(-1.0f, 1.0f, 1.0f);
// Back face
GL.Color3(0.0f, 1.0f, 0.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.Vertex3(-1.0f, 1.0f, -1.0f);
// Left face
GL.Color3(0.0f, 0.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.Vertex3(-1.0f, 1.0f, -1.0f);
// Right face
GL.Color3(1.0f, 1.0f, 0.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.Vertex3(1.0f, 1.0f, -1.0f);
// Top face
GL.Color3(0.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.Vertex3(1.0f, 1.0f, -1.0f);
// Bottom face
GL.Color3(1.0f, 0.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.Vertex3(1.0f, -1.0f, -1.0f);
GL.End();
}
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Size.X, Size.Y);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Matrix4 perspectiveMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Size.X / Size.Y, 1.0f, 100.0f);
GL.LoadMatrix(ref perspectiveMatrix);
}
}
}