-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterfaceControler.cs
268 lines (236 loc) · 8.16 KB
/
InterfaceControler.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
//
// InterfaceControler.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Threading;
using System.Collections.Generic;
using Crow.Extensions;
namespace Crow
{
public class ProjectiveIFaceControler : InterfaceControler {
Matrix4 modelview;
int[] viewport = new int[4];
Vector3 vEyePosition;
public Matrix4 ifaceModelMat;
Point localMousePos;
public ProjectiveIFaceControler(Rectangle ifaceBounds, Matrix4 _ifaceModelMat)
: base(ifaceBounds){
ifaceModelMat = _ifaceModelMat;
}
public override Matrix4 InterfaceMVP {
get { return ifaceModelMat * modelview * projection; }
}
public override void initGL(){
quad = new Crow.vaoMesh (0, 0, 0, 1, 1, 1, -1);
//ifaceModelMat = Matrix4.CreateRotationX(MathHelper.PiOver2) * Matrix4.CreateTranslation(Vector3.UnitY);
CrowInterface.ProcessResize(iRect);
createContext ();
//CrowInterface.ProcessResize (iRect);
}
public override void ProcessResize (Rectangle newSize)
{
}
public override void OpenGLDraw ()
{
GL.Enable (EnableCap.DepthTest);
base.OpenGLDraw ();
GL.Disable (EnableCap.DepthTest);
}
public void UpdateView (Matrix4 _projection, Matrix4 _modelview, int[] _viewport, Vector3 _vEyePosition)
{
projection = _projection;
modelview = _modelview;
viewport = _viewport;
vEyePosition = _vEyePosition;
}
public override bool ProcessMouseMove (int x, int y)
{
Matrix4 mv = ifaceModelMat * modelview;
Vector3 vMouse = UnProject(ref projection, ref mv, viewport, new Vector2 (x, y)).Xyz;
Vector3 vE = vEyePosition.Transform (ifaceModelMat.Inverted());
Vector3 vMouseRay = Vector3.Normalize(vMouse - vE);
float a = vE.Z / vMouseRay.Z;
vMouse = vE - vMouseRay * a;
//vMouse = vMouse.Transform (interfaceModelView.Inverted());
localMousePos = new Point ((int)Math.Truncate ((vMouse.X + 0.5f) * iRect.Width),
iRect.Height - (int)Math.Truncate ((vMouse.Y + 0.5f) * iRect.Height));
mouseIsInInterface = localMousePos.X.IsInBetween (0, iRect.Width) & localMousePos.Y.IsInBetween (0, iRect.Height);
return mouseIsInInterface ? CrowInterface.ProcessMouseMove (localMousePos.X, localMousePos.Y) : false;
}
Vector4 UnProject(ref Matrix4 projection, ref Matrix4 view, int[] viewport, Vector2 mouse)
{
Vector4 vec;
vec.X = 2.0f * mouse.X / (float)viewport[2] - 1;
vec.Y = -(2.0f * mouse.Y / (float)viewport[3] - 1);
vec.Z = 0f;
vec.W = 1.0f;
Matrix4 viewInv = Matrix4.Invert(view);
Matrix4 projInv = Matrix4.Invert(projection);
Vector4.Transform(ref vec, ref projInv, out vec);
Vector4.Transform(ref vec, ref viewInv, out vec);
if (vec.W > float.Epsilon || vec.W < float.Epsilon)
{
vec.X /= vec.W;
vec.Y /= vec.W;
vec.Z /= vec.W;
}
return vec;
}
}
public class InterfaceControler : IDisposable {
public Interface CrowInterface;
public int texID;
public vaoMesh quad;
public Rectangle iRect = new Rectangle(0,0,2048,2048);
public bool mouseIsInInterface = false;
protected Matrix4 projection;
public virtual Matrix4 InterfaceMVP {
get { return projection; }
}
#if MEASURE_TIME
public List<PerformanceMeasure> PerfMeasures;
public PerformanceMeasure glDrawMeasure = new PerformanceMeasure("OpenGL Draw", 10);
#endif
#region CTOR
public InterfaceControler(Rectangle ifaceBounds){
iRect = ifaceBounds;
CrowInterface = new Interface ();
#if MEASURE_TIME
PerfMeasures = new List<PerformanceMeasure> (
new PerformanceMeasure[] {
this.CrowInterface.updateMeasure,
this.CrowInterface.layoutingMeasure,
this.CrowInterface.clippingMeasure,
this.CrowInterface.drawingMeasure,
this.glDrawMeasure
}
);
#endif
Thread t = new Thread (interfaceThread);
t.IsBackground = true;
t.Start ();
initGL ();
}
#endregion
void interfaceThread()
{
while (CrowInterface.ClientRectangle.Size.Width == 0)
Thread.Sleep (5);
while (true) {
CrowInterface.Update ();
Thread.Sleep (2);
}
}
#region Mouse And Keyboard handling
public virtual void ProcessResize(Rectangle newSize){
iRect = newSize;
CrowInterface.ProcessResize(newSize);
createContext ();
GL.Viewport (0, 0, newSize.Width, newSize.Height);//TODO:find a better place for this
}
public virtual bool ProcessMouseMove(int x, int y){
return CrowInterface.ProcessMouseMove (x, y);
}
public virtual bool ProcessMouseButtonUp(int button)
{
return CrowInterface.ProcessMouseButtonUp (button);
}
public virtual bool ProcessMouseButtonDown(int button)
{
return CrowInterface.ProcessMouseButtonDown (button);
}
public virtual bool ProcessMouseWheelChanged(float delta)
{
return CrowInterface.ProcessMouseWheelChanged (delta);
}
public virtual bool ProcessKeyDown(int Key){
return CrowInterface.ProcessKeyDown(Key);
}
public virtual bool ProcessKeyUp(int Key){
return CrowInterface.ProcessKeyUp(Key);
}
public virtual bool ProcessKeyPress(char Key){
return CrowInterface.ProcessKeyPress(Key);
}
#endregion
#region graphic context
public virtual void initGL(){
projection = OpenTK.Matrix4.CreateOrthographicOffCenter (-0.5f, 0.5f, -0.5f, 0.5f, 1, -1);
quad = new Crow.vaoMesh (0, 0, 0, 1, 1, 1, -1);
createContext ();
}
/// <summary>Create the texture for the interface redering</summary>
public virtual void createContext()
{
if (GL.IsTexture(texID))
GL.DeleteTexture (texID);
GL.GenTextures(1, out texID);
GL.ActiveTexture (TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, texID);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
iRect.Width, iRect.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.bmp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
/// <summary>Rendering of the interface</summary>
public virtual void OpenGLDraw()
{
#if MEASURE_TIME
glDrawMeasure.StartCycle();
#endif
GL.ActiveTexture (TextureUnit.Texture0);
GL.BindTexture (TextureTarget.Texture2D, texID);
if (Monitor.TryEnter(CrowInterface.RenderMutex)) {
if (CrowInterface.IsDirty) {
GL.TexSubImage2D (TextureTarget.Texture2D, 0,
CrowInterface.DirtyRect.Left, CrowInterface.DirtyRect.Top,
CrowInterface.DirtyRect.Width, CrowInterface.DirtyRect.Height,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.dirtyBmp);
CrowInterface.IsDirty = false;
}
Monitor.Exit (CrowInterface.RenderMutex);
}
quad.Render (BeginMode.TriangleStrip);
GL.BindTexture(TextureTarget.Texture2D, 0);
#if MEASURE_TIME
glDrawMeasure.StopCycle();
#endif
}
#endregion
#region IDisposable implementation
public void Dispose ()
{
if (GL.IsTexture(texID))
GL.DeleteTexture (texID);
if (quad != null)
quad.Dispose ();
}
#endregion
}
}