forked from MapStudioProject/GLFrameworkEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLContext.cs
435 lines (357 loc) · 14.6 KB
/
GLContext.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
using OpenTK;
namespace GLFrameworkEngine
{
public class GLContext
{
//Todo maybe relocate. This is to quickly access the current context (which is only one atm)
public static GLContext ActiveContext = null;
/// <summary>
/// Default rendering arguments when a rendered frame is being drawn.
/// </summary>
public RenderFrameArgs FrameArgs = new RenderFrameArgs();
/// <summary>
/// The screen buffer storing the current color/depth render texture of the drawn scene objects.
/// </summary>
public Framebuffer ScreenBuffer { get; set; }
/// <summary>
/// Selection tools used for selecting scene objects in different ways.
/// </summary>
public SelectionToolEngine SelectionTools = new SelectionToolEngine();
/// <summary>
/// Transform tools for transforming scene objects.
/// </summary>
public TransformEngine TransformTools = new TransformEngine();
/// <summary>
/// Picking toolsets for picking objects.
/// </summary>
public PickingTool PickingTools = new PickingTool();
/// <summary>
/// A toolset for linking IObjectLink objects to a transformable object.
/// </summary>
public LinkingTool LinkingTools = new LinkingTool();
/// <summary>
///
/// </summary>
public BoxCreationTool BoxCreationTool = new BoxCreationTool();
/// <summary>
/// Color picking for picking scene objects using a color ID pass.
/// </summary>
public ColorPicker ColorPicker = new ColorPicker();
/// <summary>
/// Ray picking for picking scene objects using bounding radius/boxes.
/// </summary>
public RayPicking RayPicker = new RayPicking();
/// <summary>
/// Collision ray picking for dropping scene objects to collision.
/// </summary>
public CollisionRayCaster CollisionCaster = new CollisionRayCaster();
/// <summary>
/// The camera instance to use in the scene.
/// </summary>
public Camera Camera { get; set; }
/// <summary>
/// The scene information containing the list of drawables along with selection handling.
/// </summary>
public GLScene Scene = new GLScene();
/// <summary>
/// The width of the current context. Should be given the viewport width.
/// </summary>
public int Width { get; set; }
/// <summary>
/// The height of the current context. Should be given the viewport height.
/// </summary>
public int Height { get; set; }
/// <summary>
/// If the current context is in focus or not.
/// </summary>
public bool Focused { get; set; } = true;
/// <summary>
/// Gets or sets the mouse position after a mouse down event.
/// </summary>
public Vector2 MouseOrigin { get; set; }
/// <summary>
/// Gets or sets the current mouse postion.
/// </summary>
public Vector2 CurrentMousePoint = Vector2.Zero;
/// <summary>
/// Gets or sets the offset from the mouse origin.
/// </summary>
public Vector2 MouseOffset => CurrentMousePoint - MouseOrigin;
/// <summary>
/// Determines to enable SRGB or not for the current context.
/// </summary>
public bool UseSRBFrameBuffer;
/// <summary>
/// Toggles bloom usage.
/// </summary>
public bool EnableBloom;
/// <summary>
/// Toggles fog usage.
/// </summary>
public bool EnableFog = true;
/// <summary>
/// Toggles dropping objects to collision of the current CollisionCaster or not
/// during a translation transform.
/// </summary>
public bool EnableDropToCollision = true;
/// <summary>
/// The preview scale to scale up model displaying.
/// </summary>
public static float PreviewScale
{
get { return ActiveContext._previewScale; }
set { ActiveContext._previewScale = value; }
}
private float _previewScale = 1.0f;
/// <summary>
/// Updates the viewport to draw a frame.
/// </summary>
public bool UpdateViewport { get; set; }
/// <summary>
/// Disables camera movement in the context.
/// </summary>
public bool DisableCameraMovement = false;
/// <summary>
/// Drawer to draw 2D elements onto the screen.
/// </summary>
public UIDrawer UIDrawer = new UIDrawer();
public GLContext() { Init(); }
/// <summary>
/// Inits the resources used by the context.
/// </summary>
private void Init() {
TransformTools.Init();
}
/// <summary>
/// Sets the active context used in the viewer.
/// </summary>
public void SetActive() {
GLContext.ActiveContext = this;
}
/// <summary>
/// Enables the second color pass alpha to be used as a selection mask
/// </summary>
public void EnableSelectionMask() {
GL.ColorMask(1, true, true, true, true);
}
/// <summary>
/// Disables the second color pass alpha to be used as a selection mask
/// </summary>
public void DisableSelectionMask() {
GL.ColorMask(1, false, false, false, false);
}
/// <summary>
/// Gets or sets the 3D Cursor position used for placing objects and custom origin points.
/// </summary>
public Vector3 Cursor3DPosition
{
get
{
if (Scene.Cursor3D == null)
return Vector3.Zero;
return Scene.Cursor3D.Transform.Position;
}
set
{
if (Scene.Cursor3D != null) {
Scene.Cursor3D.Transform.Position = value;
Scene.Cursor3D.Transform.UpdateMatrix(true);
}
}
}
/// <summary>
/// Gets the camera ray of the current mouse point on screen/
/// <returns></returns>
public CameraRay PointScreenRay() => CameraRay.PointScreenRay((int)CurrentMousePoint.X, (int)CurrentMousePoint.Y, Camera);
/// <summary>
/// Gets the camera ray of the given x y screen scoordinates.
/// <returns></returns>
public CameraRay PointScreenRay(int x, int y) => CameraRay.PointScreenRay(x, y, Camera);
/// <summary>
/// Gets the x y screen coordinates from a 3D position.
/// </summary>
public Vector2 WorldToScreen(Vector3 coord)
{
Vector3 vec = Vector3.Project(coord, 0, 0, Width, Height, -1, 1, Camera.ViewMatrix * Camera.ProjectionMatrix);
return new Vector2((int)vec.X, Height - (int)(vec.Y));
}
/// <summary>
/// Gets a 3D position of the current mouse coordinates given the depth.
/// </summary>
public Vector3 GetPointUnderMouse(float depth, bool useMouseDepth = false) {
return ScreenToWorld(this.CurrentMousePoint.X, this.CurrentMousePoint.Y, depth, useMouseDepth);
}
/// <summary>
/// Gets a 3D position given the screen x y coordinates.
/// The depth is fixed to 100 unless the mouse depth is used in given arguments.
/// </summary>
public Vector3 ScreenToWorld(float x, float y, bool useMouseDepth = false){
return ScreenToWorld(x, y, 100, useMouseDepth);
}
/// <summary>
/// Gets a 3D position given the screen x y coordinates and depth.
/// </summary>
public Vector3 ScreenToWorld(float x, float y, float depth, bool useMouseDepth = false)
{
//The depth pixel from mouse cursor
//This will place the selected point near rendered models.
if (useMouseDepth && ColorPicker.NormalizedPickingDepth != Camera.ZFar)
depth = ColorPicker.NormalizedPickingDepth;
var ray = PointScreenRay((int)x, (int)y);
return ray.Origin.Xyz + (ray.Direction * depth);
}
/// <summary>
/// Converts the given mouse coordinates to normalized coordinate space to use in the GL coordinate system -1 1.
/// </summary>
public Vector2 NormalizeMouseCoords(Vector2 mousePos)
{
return new Vector2(
2.0f * mousePos.X / Width - 1,
-(2.0f * mousePos.Y / Height - 1));
}
/// <summary>
/// Checks if the given shader is active in the context.
/// </summary>
public bool IsShaderActive(ShaderProgram shader) {
return shader != null && shader.program == CurrentShader.program;
}
private ShaderProgram shader;
/// <summary>
/// The current shader in the context to be drawn.
/// If null, will disable drawing the shader.
/// </summary>
public ShaderProgram CurrentShader
{
get { return shader; }
set
{
//Disable active shader when given a null value
if(value == null) {
GL.UseProgram(0);
return;
}
shader = value;
//Force enable the shader.
//Could check to only do this only when the shader is changed but some shaders may still use higher level GL.UseProgram() atm
shader.Enable();
//Update standard camera matrices
var mtxMdl = Camera.ModelMatrix;
var mtxCam = Camera.ViewProjectionMatrix;
shader.SetMatrix4x4("mtxMdl", ref mtxMdl);
shader.SetMatrix4x4("mtxCam", ref mtxCam);
}
}
private Vector2 _previousPosition;
public void OnMouseMove(MouseEventInfo e, KeyEventInfo k, bool mouseDown)
{
UpdateViewport = true;
//Set a saved mouse point to use in the application
CurrentMousePoint = new Vector2(e.X, e.Y);
SelectionTools.OnMouseMove(this, e);
Scene.OnMouseMove(this, e);
LinkingTools.OnMouseMove(this, e);
BoxCreationTool.OnMouseMove(this, e);
int transformState = 0;
//Transforming can occur on shortcut keys rather than just mouse down
if (TransformTools.ActiveActions.Count > 0)
transformState = TransformTools.OnMouseMove(this, e);
if (mouseDown)
{
if (!e.HasValue)
e.Position = new System.Drawing.Point((int)_previousPosition.X, (int)_previousPosition.Y);
if (transformState != 0 || SelectionTools.IsActive || LinkingTools.IsActive || DisableCameraMovement)
return;
if (e.RightButton == OpenTK.Input.ButtonState.Pressed)
MouseEventInfo.CursorHiddenMode = true;
Camera.Controller.MouseMove(e, k, _previousPosition);
_previousPosition = new OpenTK.Vector2(e.X, e.Y);
}
PickingTools.OnMouseMove(this, e);
}
private float previousMouseWheel;
public void ResetPrevious() {
previousMouseWheel = 0;
}
public void OnMouseWheel(MouseEventInfo e, KeyEventInfo k)
{
UpdateViewport = true;
if (previousMouseWheel == 0)
previousMouseWheel = e.WheelPrecise;
e.Delta = e.WheelPrecise - previousMouseWheel;
if (SelectionTools.IsActive) {
SelectionTools.OnMouseWheel(this, e);
}
else
{
Camera.Controller.MouseWheel(e, k);
}
previousMouseWheel = e.WheelPrecise;
}
public void OnMouseUp(MouseEventInfo e)
{
UpdateViewport = true;
MouseEventInfo.CursorHiddenMode = false;
TransformTools.OnMouseUp(this, e);
_previousPosition = new OpenTK.Vector2(e.X, e.Y);
SelectionTools.OnMouseUp(this, e);
BoxCreationTool.OnMouseUp(this, e);
Scene.OnMouseUp(this, e);
PickingTools.OnMouseUp(this, e);
LinkingTools.OnMouseUp(this, e);
}
public void OnKeyDown(KeyEventInfo keyInfo, bool isRepeat, bool viewportFoucsed)
{
if (viewportFoucsed)
{
SelectionTools.OnKeyDown(this, keyInfo);
TransformTools.OnKeyDown(this, keyInfo);
if (!isRepeat)
Camera.KeyPress(keyInfo);
}
Scene.OnKeyDown(this, keyInfo, isRepeat);
}
public void OnMouseDown(MouseEventInfo e, KeyEventInfo k)
{
UpdateViewport = true;
MouseOrigin = new Vector2(e.X, e.Y);
Scene.OnMouseDown(this, e);
_previousPosition = new OpenTK.Vector2(e.X, e.Y);
SelectionTools.OnMouseDown(this, e);
LinkingTools.OnMouseDown(this, e);
BoxCreationTool.OnMouseDown(this, e);
if (!TransformTools.Enabled || SelectionTools.IsActive ||
LinkingTools.IsActive || BoxCreationTool.IsActive) //Skip picking, transforming and camera events for selection tools
return;
if (TransformTools.ActiveActions.Count > 0 && Scene.GetSelected().Count > 0)
{
var state = TransformTools.OnMouseDown(this, e);
if (state != 0) //Skip picking and camera events for transforming objects
return;
}
//Transform is in a moving state through shortcut keys
//Don't apply deselection from picking during the mouse down when it applies
if (TransformTools.ReleaseTransform) {
TransformTools.ReleaseTransform = false;
return;
}
PickingTools.OnMouseDown(this, e);
Camera.Controller.MouseClick(e, k);
}
public int ViewportX = 0;
public int ViewportY = 0;
public int ViewportWidth;
public int ViewportHeight;
public void SetViewportSize()
{
ViewportWidth = this.Width;
ViewportHeight = this.Height;
ViewportX = 0;
ViewportY = 0;
GL.Viewport(ViewportX, ViewportY, ViewportWidth, ViewportHeight);
}
}
}