-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
executable file
·150 lines (123 loc) · 3.67 KB
/
Program.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Threading;
using ColladaParser.Collada;
using ColladaParser.Collada.Model;
using ColladaParser.Shaders;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace ColladaParser
{
public class Program : GameWindow
{
private DefaultShader defaultShader;
private ColladaModel model;
private float cameraDistance = 20.0f;
private float cameraRotation = 0.0f;
private int FPS;
private double lastFPSUpdate;
private string modelName;
private bool useBlend;
private Multisampling multisampling;
public Program(string modelName) : base(
1280, 720,
new GraphicsMode(32, 24, 0, 0), "ColladaParser", 0,
DisplayDevice.Default, 3, 3,
GraphicsContextFlags.ForwardCompatible | GraphicsContextFlags.Debug)
{
this.modelName = modelName;
this.lastFPSUpdate = 0;
Keyboard.KeyRepeat = false;
}
protected override void OnLoad (System.EventArgs e)
{
VSync = VSyncMode.On;
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.FrontFace(FrontFaceDirection.Cw);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.ClearColor(Color.FromArgb(255, 24, 24, 24));
multisampling = new Multisampling(Width, Height, 8);
defaultShader = new DefaultShader();
model = ColladaLoader.Load(modelName);
model.CreateVBOs();
model.LoadTextures();
model.Bind(defaultShader.ShaderProgram,
defaultShader.Texture,
defaultShader.HaveTexture,
defaultShader.Ambient,
defaultShader.Diffuse,
defaultShader.Specular,
defaultShader.Shininess);
}
protected override void OnKeyDown(KeyboardKeyEventArgs e)
{
if (e.IsRepeat)
return;
if (Keyboard[OpenTK.Input.Key.Escape])
Exit();
if (Keyboard[OpenTK.Input.Key.F])
WindowState = WindowState == WindowState.Fullscreen ? WindowState.Normal : WindowState.Fullscreen;
if (Keyboard[OpenTK.Input.Key.B]) {
useBlend = !useBlend;
if (useBlend) {
GL.Disable(EnableCap.DepthTest);
GL.Enable(EnableCap.Blend);
} else {
GL.Enable(EnableCap.DepthTest);
GL.Disable(EnableCap.Blend);
}
}
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
if (Keyboard[OpenTK.Input.Key.W]) {
cameraDistance -= 0.5f;
}
if (Keyboard[OpenTK.Input.Key.S]) {
cameraDistance += 0.5f;
}
lastFPSUpdate += e.Time;
if (lastFPSUpdate > 1) {
Title = $"Collada Parser (Vsync: {VSync}) - FPS: {FPS}";
FPS = 0;
lastFPSUpdate %= 1;
}
cameraRotation += (float)e.Time;
var camX = (float)Math.Sin(cameraRotation) * cameraDistance;
var camZ = (float)Math.Cos(cameraRotation) * cameraDistance;
Matrix.SetViewMatrix(defaultShader.ViewMatrix, new Vector3(camX, cameraDistance * 0.5f, camZ), new Vector3(0, 0, 0));
}
protected override void OnResize(EventArgs e)
{
var aspectRatio = (float)Width / (float)Height;
Matrix.SetProjectionMatrix(defaultShader.ProjectionMatrix, (float)Math.PI / 4, aspectRatio);
GL.Viewport(0, 0, Width, Height);
multisampling.RefreshBuffers(Width, Height);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
FPS ++;
multisampling.Bind();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
model.Render();
multisampling.Draw();
SwapBuffers();
}
public static void Main(string[] args)
{
if (args.Length < 1) {
Console.WriteLine("Model not specified!");
Environment.Exit(-1);
}
using (var program = new Program(args[0]))
{
program.Run(30);
}
}
}
}