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
|
//
// InterfaceControler.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2017 jp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Threading;
using System.Collections.Generic;
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 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 = Extensions.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;
}
}
public class InterfaceControler {
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 (1);
}
}
#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
}
}
|