ShowcaseDemo
VisageRendering.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using SharpGL;
5 using VisageCSWrapper;
6 using System.Runtime.InteropServices;
7 using System.Drawing;
8 using System.Data;
9 
10 namespace ShowcaseDemo
11 {
12  public static class ListExtra
13  {
14  public static void Resize<T>(this List<T> list, int sz, T c = default(T))
15  {
16  int cur = list.Count;
17  if (sz < cur)
18  list.RemoveRange(sz, cur - sz);
19  else if (sz > cur)
20  list.AddRange(Enumerable.Repeat(c, sz - cur));
21  }
22  }
23 
24  class VisageRendering
25  {
26  public VisageRendering(OpenGL new_gl)
27  {
28  gl = new_gl;
29  currentPosition = 3;
30  }
31 
32  uint[] frame_tex_id = new uint[1];
33  uint[] model_tex_id = new uint[1];
34  static float tex_x_coord = 0;
35  static float tex_y_coord = 0;
36  static bool video_texture_inited = false;
37  static int video_texture_width = 0;
38  static int video_texture_height = 0;
39  static int numberOfVertices = 0;
40  static VSImage texture;
41 
42  static List<int> output = new List<int>();
43 
44  const float VS_PI = 3.1415926535897932384626433832795f;
45  const int EMOTIONS_NUM = 7;
46 
47  public const int DISPLAY_FEATURE_POINTS = 1;
48  public const int DISPLAY_SPLINES = 2;
49  public const int DISPLAY_GAZE = 4;
50  public const int DISPLAY_IRIS = 8;
51  public const int DISPLAY_AXES = 16;
52  public const int DISPLAY_FRAME = 32;
53  public const int DISPLAY_WIRE_FRAME = 64;
54  public const int DISPLAY_EMOTION = 128;
55  public const int DISPLAY_GENDER = 256;
56  public const int DISPLAY_AGE = 512;
57  public const int DISPLAY_TEXTURED_MODEL = 1024;
58  public const int DISPLAY_TRACKING_QUALITY = 2048;
59  public const int DISPLAY_POINT_QUALITY = 4096;
60  public const int DISPLAY_NAME = 8192;
61  public const int DISPLAY_ALL = DISPLAY_FEATURE_POINTS + DISPLAY_SPLINES + DISPLAY_GAZE + DISPLAY_IRIS + DISPLAY_AXES +
62  DISPLAY_FRAME + DISPLAY_WIRE_FRAME + DISPLAY_EMOTION + DISPLAY_GENDER + DISPLAY_AGE + DISPLAY_NAME + DISPLAY_TEXTURED_MODEL + DISPLAY_TRACKING_QUALITY + DISPLAY_POINT_QUALITY;
63 
64  public const float BCKG_LENGTH_PERC = 0.3f;
65  public static double VS_DEG2RAD(double a)
66  {
67  return (a * VS_PI / 180.0);
68  }
69  public static double VS_RAD2DEG(double a)
70  {
71  return (a * 180.0f / VS_PI);
72  }
73 
74  public OpenGL gl;
75 
76  private enum DISPLAY_POS : int
77  {
78  UP = 0,
79  RIGHT,
80  LEFT,
81  DOWN
82  }
83 
84  public int currentPosition = (int)DISPLAY_POS.DOWN;
85 
86 
87 
88  struct Vec2D
89  {
90  public Vec2D(float _x, float _y)
91  {
92  x = _x;
93  y = _y;
94  }
95  public float x, y;
96  }
97 
98  struct CubicPoly
99  {
100  public float c0, c1, c2, c3;
101  public float eval(float t)
102  {
103  float t2 = t * t;
104  float t3 = t2 * t;
105  return c0 + c1 * t + c2 * t2 + c3 * t3;
106  }
107  }
108 
109 
110  public void ClearGL()
111  {
112  gl.ClearColor(0, 0, 0, 0);
113  gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
114  }
115 
116  int NearestPow2(int n)
117  {
118  int v; // compute the next highest power of 2 of 32-bit v
119 
120  v = n;
121  v--;
122  v |= v >> 1;
123  v |= v >> 2;
124  v |= v >> 4;
125  v |= v >> 8;
126  v |= v >> 16;
127  v++;
128 
129  return v;
130  }
131 
132  void InitFrameTex(int x_size, int y_size, VSImage image)
133  {
134  //Create The Texture
135  gl.GenTextures(1, frame_tex_id);
136 
137  //Typical Texture Generation Using Data From The Bitmap
138  gl.BindTexture(OpenGL.GL_TEXTURE_2D, frame_tex_id[0]);
139  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT);
140  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT);
141  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
142  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
143 
144  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, x_size, y_size, 0, OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, null);
145 
146  tex_x_coord = (float)image.width / (float)x_size;
147  tex_y_coord = (float)image.height / (float)y_size;
148 
149  //Create The Texture
150  gl.GenTextures(1, model_tex_id);
151 
152  //Typical Texture Generation Using Data From The Bitmap
153  gl.BindTexture(OpenGL.GL_TEXTURE_2D, model_tex_id[0]);
154  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT);
155  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT);
156  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
157  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
158  gl.Enable(OpenGL.GL_BLEND);
159 
160  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, texture.width, texture.height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, texture.imageData);
161 
162  gl.Disable(OpenGL.GL_BLEND);
163  }
164 
165  public static void Swap<T>(ref T left, ref T right)
166  {
167  T temp;
168  temp = left;
169  left = right;
170  right = temp;
171  }
172 
173  public void DisplayFrame(VSImage image, int width, int height)
174  {
175  if (video_texture_inited && (video_texture_width != image.width || video_texture_height != image.height))
176  {
177  gl.DeleteTextures(1, frame_tex_id);
178  video_texture_inited = false;
179  currentPosition = 3;
180  }
181 
182  if (!video_texture_inited)
183  {
184  InitFrameTex(NearestPow2(image.width), NearestPow2(image.height), image);
185  video_texture_width = image.width;
186  video_texture_height = image.height;
187  video_texture_inited = true;
188  }
189 
190  gl.BindTexture(OpenGL.GL_TEXTURE_2D, frame_tex_id[0]);
191 
192  switch (image.nChannels)
193  {
194  case 3:
195  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, image.width, image.height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, image.imageData);
196  break;
197  case 4:
198  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, image.width, image.height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, image.imageData);
199  break;
200  case 1:
201  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, image.width, image.height, 0, OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, image.imageData);
202  break;
203  default:
204  return;
205  }
206 
207  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
208 
209  gl.PushAttrib(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_VIEWPORT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_FOG_BIT | OpenGL.GL_STENCIL_BUFFER_BIT | OpenGL.GL_TRANSFORM_BIT | OpenGL.GL_TEXTURE_BIT);
210 
211  gl.Disable(OpenGL.GL_ALPHA_TEST);
212  gl.Disable(OpenGL.GL_DEPTH_TEST);
213  gl.Disable(OpenGL.GL_BLEND);
214  gl.Disable(OpenGL.GL_DITHER);
215  gl.Disable(OpenGL.GL_FOG);
216  gl.Disable(OpenGL.GL_SCISSOR_TEST);
217  gl.Disable(OpenGL.GL_STENCIL_TEST);
218  gl.Disable(OpenGL.GL_LIGHTING);
219  gl.Disable(OpenGL.GL_LIGHT0);
220  gl.Enable(OpenGL.GL_TEXTURE_2D);
221 
222  gl.BindTexture(OpenGL.GL_TEXTURE_2D, frame_tex_id[0]);
223 
224  gl.MatrixMode(OpenGL.GL_MODELVIEW);
225  gl.PushMatrix();
226  gl.LoadIdentity();
227 
228  gl.MatrixMode(OpenGL.GL_PROJECTION);
229  gl.PushMatrix();
230  gl.LoadIdentity();
231 
232  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
233 
234  float[] vertices = {
235  0.0f,0.0f,-5.0f,
236  1.0f,0.0f,-5.0f,
237  0.0f,1.0f,-5.0f,
238  1.0f,1.0f,-5.0f,
239  };
240 
241  float[] texcoords = {
242  0.0f, 1.0f,
243  1.0f, 1.0f,
244  0.0f, 0.0f,
245  1.0f, 0.0f,
246  };
247 
248  gl.Color(1.0f, 1.0f, 1.0f, 1.0f);
249 
250  gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
251  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
252 
253  gl.VertexPointer(3, 0, vertices); // default type: GL_FLOAT
254  gl.TexCoordPointer(2, OpenGL.GL_FLOAT, 0, texcoords);
255 
256  gl.Viewport(0, 0, width, height);
257  gl.DrawArrays(OpenGL.GL_TRIANGLE_STRIP, 0, 4);
258 
259  gl.DisableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
260  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
261 
262  gl.Disable(OpenGL.GL_TEXTURE_2D);
263 
264  gl.PopMatrix();
265  gl.MatrixMode(OpenGL.GL_MODELVIEW);
266  gl.PopMatrix();
267 
268  gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
269 
270 
271  gl.PopAttrib();
272 
273  gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT);
274  }
275 
276  static void InitCubicPoly(float x0, float x1, float t0, float t1, ref CubicPoly p)
277  {
278  p.c0 = x0;
279  p.c1 = t0;
280  p.c2 = -3 * x0 + 3 * x1 - 2 * t0 - t1;
281  p.c3 = 2 * x0 - 2 * x1 + t0 + t1;
282  }
283 
284  static void InitNonuniformCatmullRom(float x0, float x1, float x2, float x3, float dt0, float dt1, float dt2, ref CubicPoly p)
285  {
286  float t1 = (x1 - x0) / dt0 - (x2 - x0) / (dt0 + dt1) + (x2 - x1) / dt1;
287  float t2 = (x2 - x1) / dt1 - (x3 - x1) / (dt1 + dt2) + (x3 - x2) / dt2;
288 
289  t1 *= dt1;
290  t2 *= dt1;
291 
292  InitCubicPoly(x1, x2, t1, t2, ref p);
293  }
294 
295  static float VecDistSquared(ref Vec2D p, ref Vec2D q)
296  {
297  float dx = q.x - p.x;
298  float dy = q.y - p.y;
299  return dx * dx + dy * dy;
300  }
301 
302  static void InitCentripetalCR(ref Vec2D p0, ref Vec2D p1, ref Vec2D p2, ref Vec2D p3, ref CubicPoly px, ref CubicPoly py)
303  {
304  float dt0 = (float)Math.Pow(VecDistSquared(ref p0, ref p1), 0.25f);
305  float dt1 = (float)Math.Pow(VecDistSquared(ref p1, ref p2), 0.25f);
306  float dt2 = (float)Math.Pow(VecDistSquared(ref p2, ref p3), 0.25f);
307 
308  if (dt1 < 1e-4f) dt1 = 1.0f;
309  if (dt0 < 1e-4f) dt0 = dt1;
310  if (dt2 < 1e-4f) dt2 = dt1;
311 
312  InitNonuniformCatmullRom(p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2, ref px);
313  InitNonuniformCatmullRom(p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2, ref py);
314  }
315 
316  void CalcSpline(ref List<float> inputPoints, int ratio, ref List<float> outputPoints)
317  {
318  int nPoints, nPointsToDraw, nLines;
319 
320  nPoints = inputPoints.Count / 2 + 2;
321  nPointsToDraw = inputPoints.Count / 2 + (inputPoints.Count() / 2 - 1) * ratio;
322  nLines = nPoints - 1 - 2;
323 
324  inputPoints.Insert(0, inputPoints[1] + (inputPoints[1] - inputPoints[3]));
325  inputPoints.Insert(0, inputPoints[1] + (inputPoints[1] - inputPoints[3]));
326  inputPoints.Insert(inputPoints.Count, inputPoints[inputPoints.Count / 2 - 2] + (inputPoints[inputPoints.Count / 2 - 2] - inputPoints[inputPoints.Count / 2 - 4]));
327  inputPoints.Insert(inputPoints.Count, inputPoints[inputPoints.Count / 2 - 1] + (inputPoints[inputPoints.Count / 2 - 1] - inputPoints[inputPoints.Count / 2 - 3]));
328 
329  Vec2D p0 = new Vec2D(0, 0);
330  Vec2D p1 = new Vec2D(0, 0);
331  Vec2D p2 = new Vec2D(0, 0);
332  Vec2D p3 = new Vec2D(0, 0);
333 
334  CubicPoly px = new CubicPoly();
335  CubicPoly py = new CubicPoly();
336 
337  ListExtra.Resize<float>(outputPoints, 2 * nPointsToDraw);
338 
339  for (int i = 0; i < nPoints - 2; i++)
340  {
341  outputPoints[i * 2 * (ratio + 1)] = inputPoints[2 * i + 2];
342  outputPoints[i * 2 * (ratio + 1) + 1] = inputPoints[2 * i + 1 + 2];
343  }
344 
345  for (int i = 0; i < 2 * nLines; i = i + 2)
346  {
347  p0.x = inputPoints[i];
348  p0.y = inputPoints[i + 1];
349  p1.x = inputPoints[i + 2];
350  p1.y = inputPoints[i + 3];
351  p2.x = inputPoints[i + 4];
352  p2.y = inputPoints[i + 5];
353  p3.x = inputPoints[i + 6];
354  p3.y = inputPoints[i + 7];
355 
356  InitCentripetalCR(ref p0, ref p1, ref p2, ref p3, ref px, ref py);
357 
358  for (int j = 1; j <= ratio; j++)
359  {
360  outputPoints[i * (ratio + 1) + 2 * j] = (px.eval(1.00f / (ratio + 1) * (j)));
361  outputPoints[i * (ratio + 1) + 2 * j + 1] = (py.eval(1.00f / (ratio + 1) * (j)));
362  }
363 
364  }
365 
366  inputPoints.RemoveRange(0, 2);
367  inputPoints.RemoveRange(inputPoints.Count - 2, 2);
368  }
369 
370  void SetupCamera(int width, int height, float f)
371  {
372  float x_offset = 1;
373  float y_offset = 1;
374  if (width > height)
375  x_offset = ((float)width) / ((float)height);
376  else if (width < height)
377  y_offset = ((float)height) / ((float)width);
378 
379  //Note:
380  // FOV in radians is: fov*0.5 = arctan ((top-bottom)*0.5 / near)
381  // In this case: FOV = 2 * arctan(frustum_y / frustum_near)
382  //set frustum specs
383  float frustum_near = 0.001f;
384  float frustum_far = 30; //hard to estimate face too far away
385  float frustum_x = x_offset * frustum_near / f;
386  float frustum_y = y_offset * frustum_near / f;
387  //set frustum
388  gl.MatrixMode(OpenGL.GL_PROJECTION);
389  gl.LoadIdentity();
390  gl.Frustum(-frustum_x, frustum_x, -frustum_y, frustum_y, frustum_near, frustum_far);
391  gl.MatrixMode(OpenGL.GL_MODELVIEW);
392  //clear matrix
393  gl.LoadIdentity();
394  //camera in (0,0,0) looking at (0,0,1) up vector (0,1,0)
395  gl.LookAt(0, 0, 0, 0, 0, 1, 0, 1, 0); //uLookAt
396  }
397 
398  public bool withinConstraints(ref FaceData trackingData)
399  {
400  float head_pitch_value_rad = trackingData.FaceRotationApparent[0];
401  float head_yaw_value_rad = trackingData.FaceRotationApparent[1];
402  float head_roll_value_rad = trackingData.FaceRotationApparent[2];
403 
404  double head_pitch_value_deg = VS_RAD2DEG(head_pitch_value_rad);
405  double head_yaw_value_deg = VS_RAD2DEG(head_yaw_value_rad);
406  double head_roll_value_deg = VS_RAD2DEG(head_roll_value_rad);
407 
408  const double CONSTRAINT_ANGLE = 40;
409 
410  if (Math.Abs(head_pitch_value_deg) > CONSTRAINT_ANGLE ||
411  Math.Abs(head_yaw_value_deg) > CONSTRAINT_ANGLE ||
412  Math.Abs(head_roll_value_deg) > CONSTRAINT_ANGLE ||
413  trackingData.FaceScale < 40)
414  return false;
415 
416  return true;
417  }
418 
419  void DrawElipse(float x, float y, float radiusX, float radiusY, bool filled = true)
420  {
421  gl.MatrixMode(OpenGL.GL_MODELVIEW);
422  gl.PushMatrix();
423  gl.LoadIdentity();
424 
425  const int circle_points = 200;
426  const float angleIncrement = 360.0f / circle_points;
427  float[] vertices = new float[circle_points * 2];
428 
429  for (int i = 0; i < circle_points; i++)
430  {
431  vertices[2 * i] = x + radiusX * (float)Math.Cos(VS_DEG2RAD((angleIncrement * i)));
432  vertices[2 * i + 1] = y + radiusY * (float)Math.Sin(VS_DEG2RAD((angleIncrement * i)));
433  }
434 
435  int size = Marshal.SizeOf(vertices[0]) * vertices.Length;
436  IntPtr pnt = Marshal.AllocHGlobal(size);
437  Marshal.Copy(vertices, 0, pnt, vertices.Length);
438 
439  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
440  gl.VertexPointer(2, OpenGL.GL_FLOAT, 0, pnt);
441 
442  gl.Enable(OpenGL.GL_BLEND);
443  gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
444  if (filled)
445  gl.DrawArrays(OpenGL.GL_TRIANGLE_FAN, 0, circle_points); //Draw fill
446  else
447  gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, circle_points); //Draw outline
448 
449  gl.Disable(OpenGL.GL_BLEND);
450  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
451 
452  Marshal.FreeHGlobal(pnt);
453 
454  gl.PopMatrix();
455  }
456 
457  void DrawSpline2D(int[] points, int num, FDP fdp, bool useAlpha = false)
458  {
459  if (num < 2)
460  return;
461 
462  List<float> pointCoords = new List<float>(); //vector<float>
463  List<float> pointCoordsQuality = new List<float>(); ;
464 
465  int n = 0;
466 
467  for (int i = 0; i < num; i++)
468  {
469  FeaturePoint fp = fdp.GetFP(points[2 * i], points[2 * i + 1]);
470  if (fp.Detected == 1 && fp.Defined == 1 && fp.Pos[0] != 0 && fp.Pos[1] != 0)
471  {
472  pointCoords.Add(fp.Pos[0]);
473  pointCoords.Add(fp.Pos[1]);
474 
475  float alphaChannel = (fp.Quality > 0 && useAlpha) ? Math.Max(fp.Quality * 0.75f, 0.2f) : 0.75f;
476  //vector of points quality
477  pointCoordsQuality.Add(alphaChannel);
478  pointCoordsQuality.Add(alphaChannel);
479 
480  n++;
481  }
482  }
483 
484  if(pointCoords.Count() == 0 || n <= 2)
485  return;
486 
487  int factor = 10;
488  List<float> pointsToDraw = new List<float>();
489  List<float> qualityToDraw = new List<float>();
490  //
491  // Interpolate between points position and points quality
492  // Poistion is used to determin the spline shape
493  // Quality is used to determin the color of the spline
494  CalcSpline(ref pointCoords, factor, ref pointsToDraw);
495  CalcSpline(ref pointCoordsQuality, factor, ref qualityToDraw);
496  int nVert = (int)pointsToDraw.Count / 2;
497  float[] vertices = new float[nVert * 3];
498  float[] colors = new float[nVert * 4];
499  float rChannel = 0.69f;
500  float gChannel = 0.77f;
501  float bChannel = 0.87f;
502  int cnt = 0;
503  for (int i = 0; i < nVert; ++i)
504  {
505  vertices[3 * i + 0] = pointsToDraw[cnt++];
506  vertices[3 * i + 1] = pointsToDraw[cnt++];
507  vertices[3 * i + 2] = 0.0f;
508  //
509  colors[4 * i + 0] = rChannel;
510  colors[4 * i + 1] = gChannel;
511  colors[4 * i + 2] = bChannel;
512  colors[4 * i + 3] = qualityToDraw[i * 2];
513  }
514 
515  int sizeColor = Marshal.SizeOf(colors[0]) * colors.Length;
516  IntPtr pColor = Marshal.AllocHGlobal(sizeColor);
517  Marshal.Copy(colors, 0, pColor, colors.Length);
518 
519 
520  gl.Enable(OpenGL.GL_BLEND);
521  gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
522  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
523  gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);
524  gl.VertexPointer(3, 0, vertices);
525  gl.ColorPointer(4, OpenGL.GL_FLOAT, 0, pColor);
526  gl.DrawArrays(OpenGL.GL_LINE_STRIP, 0, nVert);
527  gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);
528  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
529  gl.Disable(OpenGL.GL_BLEND);
530 
531  Marshal.FreeHGlobal(pColor);
532  }
533 
534  void DrawPoints2D(int[] points, int num, bool singleColor, int faceScale, int winWidth, int winHeight, int frameWidth, int frameHeight, FDP fdp, bool drawQuality = true, bool useAlpha = false)
535  {
536  float faceScaleNormalized = (frameWidth > frameHeight) ? faceScale / (float)frameWidth : faceScale / (float)frameHeight;
537  float radius = ((1 + (faceScaleNormalized * 2.0f)) * (1 + (winHeight / 250.0f)));
538 
539  float[] vertices = new float[num * 2];
540  float[] colorsQuality = new float[num * 4];
541  float[] colorsCircle = new float[num * 4];
542 
543  int n = 0;
544  for (int i = 0; i < num; i++)
545  {
546  FeaturePoint fp = fdp.GetFP(points[2 * i], points[2 * i + 1]);
547  if (fp.Detected == 1 && fp.Defined == 1 && fp.Pos[0] != 0 && fp.Pos[1] != 0)
548  {
549  vertices[2 * n + 0] = fp.Pos[0];
550  vertices[2 * n + 1] = fp.Pos[1];
551  if (drawQuality && fp.Quality >= 0)
552  {
553  colorsQuality[4 * n + 0] = 1.0f - fp.Quality;
554  colorsQuality[4 * n + 1] = fp.Quality;
555  colorsQuality[4 * n + 2] = 0;
556  colorsQuality[4 * n + 3] = useAlpha ? Math.Max(fp.Quality, 0.5f) : 1.0f;
557 
558  //
559  colorsCircle[4 * n + 0] = 0.0f;
560  colorsCircle[4 * n + 1] = 0.0f;
561  colorsCircle[4 * n + 2] = 0.0f;
562  colorsCircle[4 * n + 3] = useAlpha ? Math.Max(fp.Quality, 0.3f) : 1.0f;
563  }
564  else
565  {
566  colorsQuality[4 * n + 0] = 0;
567  colorsQuality[4 * n + 1] = 1.0f;
568  colorsQuality[4 * n + 2] = 1.0f;
569  colorsQuality[4 * n + 3] = 1.0f;
570  //
571  colorsCircle[4 * n + 0] = 0.0f;
572  colorsCircle[4 * n + 1] = 0.0f;
573  colorsCircle[4 * n + 2] = 0.0f;
574  colorsCircle[4 * n + 3] = 1.0f;
575  }
576  n++;
577  }
578  }
579 
580  int sizeVertices = Marshal.SizeOf(vertices[0]) * vertices.Length;
581  IntPtr pnt = Marshal.AllocHGlobal(sizeVertices);
582  Marshal.Copy(vertices, 0, pnt, vertices.Length);
583 
584  int sizeColorsQuality = Marshal.SizeOf(colorsQuality[0]) * colorsQuality.Length;
585  IntPtr pColorsQuality = Marshal.AllocHGlobal(sizeColorsQuality);
586  Marshal.Copy(colorsQuality, 0, pColorsQuality, colorsQuality.Length);
587 
588  int sizeColorsCircle = Marshal.SizeOf(colorsCircle[0]) * colorsCircle.Length;
589  IntPtr pColorsCircle = Marshal.AllocHGlobal(sizeColorsCircle);
590  Marshal.Copy(colorsCircle, 0, pColorsCircle, colorsCircle.Length);
591 
592  gl.Enable(OpenGL.GL_BLEND);
593  gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
594  gl.Enable(OpenGL.GL_POINT_SMOOTH);
595  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
596  gl.VertexPointer(2, OpenGL.GL_FLOAT, 0, pnt);
597 
598  gl.PointSize(radius);
599  gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);
600  gl.ColorPointer(4, OpenGL.GL_FLOAT, 0, pColorsCircle);
601  gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
602  gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);
603 
604  if (!singleColor)
605  {
606  gl.PointSize(0.65f * radius);
607  gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);
608  gl.ColorPointer(4, OpenGL.GL_FLOAT, 0, pColorsQuality);
609  gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
610  gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);
611  }
612 
613  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
614  gl.Disable(OpenGL.GL_BLEND);
615 
616  Marshal.FreeHGlobal(pnt);
617  Marshal.FreeHGlobal(pColorsQuality);
618  Marshal.FreeHGlobal(pColorsCircle);
619  }
620 
621  void DrawPoints3D(int[] points, int num, bool singleColor, int faceScale, int winWidth, int winHeight, int frameWidth, int frameHeight, FDP fdp)
622  {
623  float faceScaleNormalized = (frameWidth > frameHeight) ? faceScale / (float)frameWidth : faceScale / (float)frameHeight;
624  float radius = ((1 + (faceScaleNormalized * 2.0f)) * (1 + (winHeight / 250.0f)));
625 
626  float[] vertices = new float[num * 3];
627 
628  int n = 0;
629  for (int i = 0; i < num; i++)
630  {
631  FeaturePoint fp = fdp.GetFP(points[2 * i], points[2 * i + 1]);
632  if (fp.Defined == 1 && fp.Pos[0] != 0 && fp.Pos[1] != 0)
633  {
634  vertices[3 * n + 0] = fp.Pos[0];
635  vertices[3 * n + 1] = fp.Pos[1];
636  vertices[3 * n + 2] = fp.Pos[2];
637  n++;
638  }
639  }
640 
641  int sizeVertices = Marshal.SizeOf(vertices[0]) * vertices.Length;
642  IntPtr pnt = Marshal.AllocHGlobal(sizeVertices);
643  Marshal.Copy(vertices, 0, pnt, vertices.Length);
644 
645  gl.Enable(OpenGL.GL_POINT_SMOOTH);
646  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
647  gl.VertexPointer(3, OpenGL.GL_FLOAT, 0, pnt);
648 
649  gl.PointSize(radius);
650  gl.Color(0, 0, 0, 1.0f);
651  gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
652 
653  if (!singleColor)
654  {
655  gl.PointSize(0.65f * radius);
656  gl.Color(0, 1.0f, 1.0f, 1.0f);
657  gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
658  }
659  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
660  Marshal.FreeHGlobal(pnt);
661  }
662 
663  public void DisplayFeaturePoints(VSImage frame, FaceData trackingData, int width, int height, bool _3D, bool relative, bool drawQuality)
664  {
665  gl.Viewport(0, 0, width, height);
666 
667  gl.MatrixMode(OpenGL.GL_MODELVIEW);
668  gl.PushMatrix();
669  gl.LoadIdentity();
670 
671  gl.MatrixMode(OpenGL.GL_PROJECTION);
672  gl.PushMatrix();
673  gl.LoadIdentity();
674 
675  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
676 
677  if (_3D)
678  {
679  SetupCamera(width, height, trackingData.CameraFocus);
680 
681  if (relative)
682  {
683  float[] r = trackingData.FaceRotation;
684  float[] t = trackingData.FaceTranslation;
685 
686  gl.Translate(t[0], t[1], t[2]);
687  gl.Rotate(VS_RAD2DEG(r[1] + VS_PI), 0.0f, 1.0f, 0.0f);
688  gl.Rotate(VS_RAD2DEG(r[0]), 1.0f, 0.0f, 0.0f);
689  gl.Rotate(VS_RAD2DEG(r[2]), 0.0f, 0.0f, 1.0f);
690  }
691  }
692 
693  int faceScale = trackingData.FaceScale;
694  FDP featurePoints2D = trackingData.FeaturePoints2D;
695  FDP featurePoints3D = relative ? trackingData.featurePoints3DRelative : trackingData.FeaturePoints3D;
696  int frameWidth = frame.width;
697  int frameHeight = frame.height;
698 
699  int[] innerLipPoints = {
700  2, 2,
701  17, 18,
702  2, 6,
703  17, 14,
704  2, 4,
705  17, 16,
706  2, 8,
707  17, 20,
708  2, 3,
709  17, 19,
710  2, 9,
711  17, 15,
712  2, 5,
713  17, 13,
714  2, 7,
715  17, 17,
716  };
717  if (_3D) DrawPoints3D(innerLipPoints, 16, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
718  else DrawPoints2D(innerLipPoints, 16, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
719 
720  int[] outerLipPoints = {
721  8, 1,
722  8, 10,
723  17, 10,
724  8, 5,
725  17, 5,
726  8, 3,
727  17, 7,
728  8, 7,
729  17, 11,
730  8, 2,
731  17, 12,
732  8, 8,
733  17, 8,
734  8, 4,
735  17, 6,
736  8, 6,
737  17, 9,
738  8, 9,
739  };
740  if (_3D) DrawPoints3D(outerLipPoints, 18, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
741  else DrawPoints2D(outerLipPoints, 18, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
742 
743  int[] nosePoints = {
744  9, 5,
745  9, 4,
746  9, 3,
747  9, 15,
748  14, 22,
749  14, 23,
750  14, 24,
751  14, 25
752  };
753  if (_3D) DrawPoints3D(nosePoints, 8, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
754  else DrawPoints2D(nosePoints, 8, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
755 
756  if (trackingData.EyeClosure[1] > 0.5f)
757  {
758  //if eye is open, draw the pupil
759  int[] pupilPoints = {
760  3, 6
761  };
762  if (_3D) DrawPoints3D(pupilPoints, 1, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
763  else DrawPoints2D(pupilPoints, 1, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
764  }
765 
766  if (trackingData.EyeClosure[0] > 0.5f)
767  {
768  int[] pupilPoints = {
769  3, 5
770  };
771  if (_3D) DrawPoints3D(pupilPoints, 1, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
772  else DrawPoints2D(pupilPoints, 1, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
773  }
774 
775  int[] eyesPointsR = {
776  3, 2,
777  3, 4,
778  3, 8,
779  3, 10,
780  3, 12,
781  3, 14,
782  12, 6,
783  12, 8,
784  12, 10,
785  12, 12,
786  16, 2,
787  16, 4,
788  16, 6,
789  16, 8,
790  16, 10,
791  16, 12,
792  16, 14,
793  16, 16,
794  16, 18,
795  16, 20,
796  16, 22,
797  16, 24,
798  16, 26,
799  16, 28,
800  };
801  if (_3D) DrawPoints3D(eyesPointsR, 24, trackingData.EyeClosure[1] <= 0.5f, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
802  else DrawPoints2D(eyesPointsR, 24, trackingData.EyeClosure[1] <= 0.5f, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
803 
804  int[] eyesPointsL = {
805  3, 1,
806  3, 3,
807  3, 7,
808  3, 9,
809  3, 11,
810  3, 13,
811  12, 5,
812  12, 7,
813  12, 9,
814  12, 11,
815  16, 1,
816  16, 3,
817  16, 5,
818  16, 7,
819  16, 9,
820  16, 11,
821  16, 13,
822  16, 15,
823  16, 17,
824  16, 19,
825  16, 21,
826  16, 23,
827  16, 25,
828  16, 27,
829  };
830  if (_3D) DrawPoints3D(eyesPointsL, 24, trackingData.EyeClosure[0] <= 0.5f, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
831  else DrawPoints2D(eyesPointsL, 24, trackingData.EyeClosure[0] <= 0.5f, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
832 
833  int[] eyebrowPoints = {
834  4, 1,
835  4, 2,
836  4, 3,
837  4, 4,
838  4, 5,
839  4, 6,
840  14, 1,
841  14, 2,
842  14, 3,
843  14, 4,
844  14, 5,
845  14, 6,
846  14, 7,
847  14, 8,
848  14, 9,
849  14, 10,
850  14, 11,
851  14, 12
852  };
853  if (_3D) DrawPoints3D(eyebrowPoints, 18, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
854  else DrawPoints2D(eyebrowPoints, 18, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
855 
856  int[] leftEarPoints =
857  {
858  10, 1,
859  10, 3,
860  10, 5,
861  10, 7,
862  10, 9,
863  10, 11,
864  10, 13,
865  10, 15,
866  10, 17,
867  10, 19,
868  10, 21,
869  10, 23
870  };
871 
872  if (_3D) DrawPoints3D(leftEarPoints, 12, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
873  else DrawPoints2D(leftEarPoints, 12, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
874 
875  int[] rightEarPoints =
876  {
877  10, 2,
878  10, 4,
879  10, 6,
880  10, 8,
881  10, 10,
882  10, 12,
883  10, 14,
884  10, 16,
885  10, 18,
886  10, 20,
887  10, 22,
888  10, 24
889  };
890 
891  if (_3D) DrawPoints3D(rightEarPoints, 12, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
892  else DrawPoints2D(rightEarPoints, 12, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality);
893 
894  // physical contour
895  int[] contourPointsPhysical = {
896  15, 1,
897  15, 3,
898  15, 5,
899  15, 7,
900  15, 9,
901  15, 11,
902  15, 13,
903  15, 15,
904  15, 17,
905  15, 16,
906  15, 14,
907  15, 12,
908  15, 10,
909  15, 8,
910  15, 6,
911  15, 4,
912  15, 2
913  };
914 
915  if (_3D) DrawPoints3D(contourPointsPhysical, 17, false, faceScale, width, height, frameWidth, frameHeight, featurePoints3D);
916  else DrawPoints2D(contourPointsPhysical, 17, false, faceScale, width, height, frameWidth, frameHeight, featurePoints2D, drawQuality, true);
917 
918  gl.MatrixMode(OpenGL.GL_PROJECTION);
919  gl.PopMatrix();
920  gl.MatrixMode(OpenGL.GL_MODELVIEW);
921  gl.PopMatrix();
922  }
923 
924  public void DisplaySplines(FaceData trackingData, int width, int height)
925  {
926  gl.Viewport(0, 0, width, height);
927 
928  gl.PushAttrib(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_VIEWPORT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_FOG_BIT | OpenGL.GL_STENCIL_BUFFER_BIT | OpenGL.GL_TRANSFORM_BIT | OpenGL.GL_TEXTURE_BIT);
929 
930  gl.Disable(OpenGL.GL_ALPHA_TEST);
931  gl.Disable(OpenGL.GL_DEPTH_TEST);
932  gl.Disable(OpenGL.GL_DITHER);
933  gl.Disable(OpenGL.GL_FOG);
934  gl.Disable(OpenGL.GL_SCISSOR_TEST);
935  gl.Disable(OpenGL.GL_STENCIL_TEST);
936 
937  gl.MatrixMode(OpenGL.GL_MODELVIEW);
938  gl.PushMatrix();
939  gl.LoadIdentity();
940 
941  gl.MatrixMode(OpenGL.GL_PROJECTION);
942  gl.PushMatrix();
943  gl.LoadIdentity();
944 
945 
946  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
947 
948  gl.PointSize(3);
949  gl.LineWidth(2);
950 
951  FDP featurePoints2D = trackingData.FeaturePoints2D;
952 
953  int[] outerUpperLipPoints = {
954  8, 4,
955  17, 6,
956  8, 6,
957  17, 9,
958  8, 9,
959  8, 1,
960  8, 10,
961  17, 10,
962  8, 5,
963  17, 5,
964  8, 3,
965  };
966  DrawSpline2D(outerUpperLipPoints, 11, featurePoints2D);
967 
968  int[] outerLowerLipPoints = {
969  8, 4,
970  17, 8,
971  8, 8,
972  17, 12,
973  8, 2,
974  17, 11,
975  8, 7,
976  17, 7,
977  8, 3,
978  };
979  DrawSpline2D(outerLowerLipPoints, 9, featurePoints2D);
980 
981  int[] innerUpperLipPoints = {
982  2, 5,
983  17, 13,
984  2, 7,
985  17, 17,
986  2, 2,
987  17, 18,
988  2, 6,
989  17, 14,
990  2, 4,
991  };
992  DrawSpline2D(innerUpperLipPoints, 9, featurePoints2D);
993 
994  int[] innerLowerLipPoints = {
995  2, 5,
996  17, 15,
997  2, 9,
998  17, 19,
999  2, 3,
1000  17, 20,
1001  2, 8,
1002  17, 16,
1003  2, 4,
1004  };
1005  DrawSpline2D(innerLowerLipPoints, 9, featurePoints2D);
1006 
1007  int[] noseLinePoints = {
1008  9, 5,
1009  9, 3,
1010  9, 4
1011  };
1012  DrawSpline2D(noseLinePoints, 3, featurePoints2D);
1013 
1014  int[] noseLinePoints2 = {
1015  9, 3,
1016  14, 22,
1017  14, 23,
1018  14, 24,
1019  14, 25
1020  };
1021  DrawSpline2D(noseLinePoints2, 5, featurePoints2D);
1022 
1023  int[] outerUpperEyePointsR = {
1024  16, 28,
1025  16, 6,
1026  3, 14,
1027  16, 2,
1028  16, 26,
1029  };
1030  DrawSpline2D(outerUpperEyePointsR, 5, featurePoints2D);
1031 
1032  int[] outerLowerEyePointsR = {
1033  16, 28,
1034  16, 8,
1035  3, 10,
1036  16, 4,
1037  16, 26,
1038  };
1039  DrawSpline2D(outerLowerEyePointsR, 5, featurePoints2D);
1040 
1041  int[] innerUpperEyePointsR = {
1042  3, 12,
1043  16, 22,
1044  12, 10,
1045  16, 18,
1046  3, 2,
1047  16, 14,
1048  12, 6,
1049  16, 10,
1050  3, 8
1051  };
1052  DrawSpline2D(innerUpperEyePointsR, 9, featurePoints2D);
1053 
1054  int[] innerLowerEyePointsR = {
1055  3, 8,
1056  16, 12,
1057  12, 8,
1058  16, 16,
1059  3, 4,
1060  16, 20,
1061  12, 12,
1062  16, 24,
1063  3, 12,
1064  };
1065  DrawSpline2D(innerLowerEyePointsR, 9, featurePoints2D);
1066 
1067  int[] outerUpperEyePointsL = {
1068  16, 27,
1069  16, 5,
1070  3, 13,
1071  16, 1,
1072  16, 25,
1073  };
1074  DrawSpline2D(outerUpperEyePointsL, 5, featurePoints2D);
1075 
1076  int[] outerLowerEyePointsL = {
1077  16, 25,
1078  16, 3,
1079  3, 9,
1080  16, 7,
1081  16, 27,
1082  };
1083  DrawSpline2D(outerLowerEyePointsL, 5, featurePoints2D);
1084 
1085  int[] innerUpperEyePointsL = {
1086  3, 11,
1087  16, 21,
1088  12, 9,
1089  16, 17,
1090  3, 1,
1091  16, 13,
1092  12, 5,
1093  16, 9,
1094  3, 7
1095  };
1096  DrawSpline2D(innerUpperEyePointsL, 9, featurePoints2D);
1097 
1098  int[] innerLowerEyePointsL = {
1099  3, 7,
1100  16, 11,
1101  12, 7,
1102  16, 15,
1103  3, 3,
1104  16, 19,
1105  12, 11,
1106  16, 23,
1107  3, 11,
1108  };
1109  DrawSpline2D(innerLowerEyePointsL, 9, featurePoints2D);
1110 
1111  int[] eyebrowUpperPointsR = {
1112  14, 6,
1113  4, 2,
1114  14, 2,
1115  4, 4,
1116  14, 4,
1117  4, 6
1118  };
1119  DrawSpline2D(eyebrowUpperPointsR, 6, featurePoints2D);
1120 
1121  int[] eyebrowLowerPointsR = {
1122  4, 6,
1123  14, 12,
1124  14, 10,
1125  14, 8,
1126  14, 6
1127  };
1128  DrawSpline2D(eyebrowLowerPointsR, 5, featurePoints2D);
1129 
1130  int[] eyebrowUpperPointsL = {
1131  14, 5,
1132  4, 1,
1133  14, 1,
1134  4, 3,
1135  14, 3,
1136  4, 5
1137  };
1138  DrawSpline2D(eyebrowUpperPointsL, 6, featurePoints2D);
1139 
1140  int[] eyebrowLowerPointsL = {
1141  4, 5,
1142  14, 11,
1143  14, 9,
1144  14, 7,
1145  14, 5
1146  };
1147  DrawSpline2D(eyebrowLowerPointsL, 5, featurePoints2D);
1148 
1149  int[] leftEarPoints =
1150  {
1151  10, 11,
1152  10, 1,
1153  10, 13,
1154  10, 3,
1155  10, 15,
1156  10, 5,
1157  10, 19
1158  };
1159 
1160  DrawSpline2D(leftEarPoints, 7, featurePoints2D);
1161 
1162  int[] rightEarPoints =
1163  {
1164  10, 12,
1165  10, 2,
1166  10, 14,
1167  10, 4,
1168  10, 16,
1169  10, 6,
1170  10, 20
1171  };
1172 
1173  DrawSpline2D(rightEarPoints, 7, featurePoints2D);
1174 
1175  // physical contour
1176  int[] contourPointsPhysical = {
1177  15, 1,
1178  15, 3,
1179  15, 5,
1180  15, 7,
1181  15, 9,
1182  15, 11,
1183  15, 13,
1184  15, 15,
1185  15, 17,
1186  15, 16,
1187  15, 14,
1188  15, 12,
1189  15, 10,
1190  15, 8,
1191  15, 6,
1192  15, 4,
1193  15, 2
1194  };
1195  DrawSpline2D(contourPointsPhysical, 17, featurePoints2D, true);
1196 
1197  gl.Disable(OpenGL.GL_BLEND);
1198  gl.MatrixMode(OpenGL.GL_PROJECTION);
1199  gl.PopMatrix();
1200  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1201  gl.PopMatrix();
1202 
1203  gl.PopAttrib();
1204 
1205  }
1206 
1207  public void DisplayGaze(FaceData trackingData, int width, int height)
1208  {
1209  gl.Viewport(0, 0, width, height);
1210 
1211  gl.PushAttrib(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_VIEWPORT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_FOG_BIT | OpenGL.GL_STENCIL_BUFFER_BIT | OpenGL.GL_TRANSFORM_BIT | OpenGL.GL_TEXTURE_BIT);
1212 
1213  gl.Disable(OpenGL.GL_ALPHA_TEST);
1214  gl.Disable(OpenGL.GL_DEPTH_TEST);
1215  gl.Disable(OpenGL.GL_DITHER);
1216  gl.Disable(OpenGL.GL_FOG);
1217  gl.Disable(OpenGL.GL_SCISSOR_TEST);
1218  gl.Disable(OpenGL.GL_STENCIL_TEST);
1219 
1220  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1221  gl.PushMatrix();
1222  gl.LoadIdentity();
1223 
1224  gl.MatrixMode(OpenGL.GL_PROJECTION);
1225  gl.PushMatrix();
1226  gl.LoadIdentity();
1227 
1228 
1229  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1230 
1231  SetupCamera(width, height, trackingData.CameraFocus);
1232 
1233  gl.ShadeModel(OpenGL.GL_FLAT);
1234 
1235  float[] vertices = {
1236  0.0f, 0.0f, 0.0f,
1237  0.0f, 0.0f, 0.04f
1238  };
1239 
1240  float[] tr = { 0, 0, 0, 0, 0, 0 };
1241 
1242  FDP fdp = trackingData.FeaturePoints3D;
1243 
1244  if (fdp == null)
1245  return;
1246 
1247  FeaturePoint leye = fdp.GetFP(3, 5);
1248  FeaturePoint reye = fdp.GetFP(3, 6);
1249 
1250  if (leye.Defined == 1 && reye.Defined == 1)
1251  {
1252  tr[0] = leye.Pos[0];
1253  tr[1] = leye.Pos[1];
1254  tr[2] = leye.Pos[2];
1255  tr[3] = reye.Pos[0];
1256  tr[4] = reye.Pos[1];
1257  tr[5] = reye.Pos[2];
1258  }
1259 
1260  float h_rot = (float)VS_RAD2DEG(trackingData.GazeDirectionGlobal[1] + VS_PI);
1261  float v_rot = (float)VS_RAD2DEG(trackingData.GazeDirectionGlobal[0]);
1262  float roll = (float)VS_RAD2DEG(trackingData.GazeDirectionGlobal[2]);
1263 
1264  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1265 
1266  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1267  gl.PushMatrix();
1268 
1269  gl.Translate(tr[0], tr[1], tr[2]);
1270  gl.Rotate(h_rot, 0.0f, 1.0f, 0.0f);
1271  gl.Rotate(v_rot, 1.0f, 0.0f, 0.0f);
1272  gl.Rotate(roll, 0.0f, 0.0f, 1.0f);
1273 
1274  gl.LineWidth(2);
1275 
1276  gl.Color(0.941176471, 0.37647058823, 0, 1.0f);
1277 
1278  if (trackingData.EyeClosure[0] > 0.5f)
1279  {
1280  gl.VertexPointer(3, 0, vertices);
1281  gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
1282  }
1283 
1284  gl.PopMatrix();
1285 
1286  gl.PushMatrix();
1287 
1288  gl.Translate(tr[3], tr[4], tr[5]);
1289  gl.Rotate(h_rot, 0.0f, 1.0f, 0.0f);
1290  gl.Rotate(v_rot, 1.0f, 0.0f, 0.0f);
1291  gl.Rotate(roll, 0.0f, 0.0f, 1.0f);
1292 
1293  if (trackingData.EyeClosure[1] > 0.5f)
1294  {
1295  gl.VertexPointer(3, 0, vertices);
1296  gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
1297  }
1298 
1299  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1300 
1301  gl.PopMatrix();
1302 
1303  gl.MatrixMode(OpenGL.GL_PROJECTION);
1304  gl.PopMatrix();
1305 
1306  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1307  gl.PopMatrix();
1308 
1309  gl.PopAttrib();
1310  }
1311 
1312  public void DisplayIrises(VSImage frame, FaceData trackingData, int width, int height)
1313  {
1314  gl.Viewport(0, 0, width, height);
1315 
1316  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1317  gl.PushMatrix();
1318  gl.LoadIdentity();
1319 
1320  gl.MatrixMode(OpenGL.GL_PROJECTION);
1321  gl.PushMatrix();
1322  gl.LoadIdentity();
1323 
1324  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1325 
1326  gl.PointSize(3);
1327  gl.LineWidth(2);
1328 
1329  FDP featurePoints2D = trackingData.FeaturePoints2D;
1330  int frameWidth = frame.width;
1331  int frameHeight = frame.height;
1332 
1333  if (trackingData.IrisRadius[0] > 0)
1334  {
1335  FeaturePoint fp = featurePoints2D.GetFP(3, 5);
1336  float rx = trackingData.IrisRadius[0] / frameWidth;
1337  float ry = trackingData.IrisRadius[0] / frameHeight;
1338 
1339  gl.Color(1.0, 1.0, 1.0, 1.0);
1340  DrawElipse(fp.Pos[0], fp.Pos[1], rx, ry, false);
1341  gl.Color(1.0f, 1.0f, 1.0f, 0.15f);
1342  DrawElipse(fp.Pos[0], fp.Pos[1], rx, ry);
1343  }
1344 
1345  if (trackingData.IrisRadius[1] > 0)
1346  {
1347  FeaturePoint fp = featurePoints2D.GetFP(3, 6);
1348  float rx = trackingData.IrisRadius[1] / frameWidth;
1349  float ry = trackingData.IrisRadius[1] / frameHeight;
1350 
1351  gl.Color(1.0, 1.0, 1.0, 1.0);
1352  DrawElipse(fp.Pos[0], fp.Pos[1], rx, ry, false);
1353  gl.Color(1.0f, 1.0f, 1.0f, 0.15f);
1354  DrawElipse(fp.Pos[0], fp.Pos[1], rx, ry);
1355 
1356  }
1357 
1358  gl.MatrixMode(OpenGL.GL_PROJECTION);
1359  gl.PopMatrix();
1360  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1361  gl.PopMatrix();
1362  }
1363 
1364  public void DisplayModelAxes(FaceData trackingData, int width, int height)
1365  {
1366  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1367 
1368  gl.Viewport(0, 0, width, height);
1369 
1370  gl.PushAttrib(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_VIEWPORT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_FOG_BIT | OpenGL.GL_STENCIL_BUFFER_BIT | OpenGL.GL_TRANSFORM_BIT | OpenGL.GL_TEXTURE_BIT);
1371 
1372  gl.Disable(OpenGL.GL_ALPHA_TEST);
1373  gl.Disable(OpenGL.GL_DEPTH_TEST);
1374  gl.Disable(OpenGL.GL_DITHER);
1375  gl.Disable(OpenGL.GL_FOG);
1376  gl.Disable(OpenGL.GL_SCISSOR_TEST);
1377  gl.Disable(OpenGL.GL_STENCIL_TEST);
1378 
1379  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1380  gl.PushMatrix();
1381  gl.LoadIdentity();
1382 
1383  gl.MatrixMode(OpenGL.GL_PROJECTION);
1384  gl.PushMatrix();
1385  gl.LoadIdentity();
1386 
1387  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1388 
1389  SetupCamera(width, height, trackingData.CameraFocus);
1390 
1391  gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
1392  gl.Enable(OpenGL.GL_BLEND);
1393 
1394  gl.ShadeModel(OpenGL.GL_FLAT);
1395 
1396  //rotate and translate into the current coordinate system of the head
1397  float[] r = trackingData.FaceRotation;
1398  float[] t = trackingData.FaceTranslation;
1399  FDP fdp = trackingData.FeaturePoints3D;
1400 
1401  if (fdp == null)
1402  return;
1403 
1404  FeaturePoint fp1 = fdp.GetFP(4, 1);
1405  FeaturePoint fp2 = fdp.GetFP(4, 2);
1406 
1407  gl.Translate((fp1.Pos[0] + fp2.Pos[0]) / 2.0f, (fp1.Pos[1] + fp2.Pos[1]) / 2.0f, (fp1.Pos[2] + fp2.Pos[2]) / 2.0f);
1408  gl.Rotate(VS_RAD2DEG(r[1] + VS_PI), 0.0f, 1.0f, 0.0f);
1409  gl.Rotate(VS_RAD2DEG(r[0]), 1.0f, 0.0f, 0.0f);
1410  gl.Rotate(VS_RAD2DEG(r[2]), 0.0f, 0.0f, 1.0f);
1411 
1412  float[][] coordVertices = {
1413  new float[] {0.0f, 0.0f, 0.0f ,0.07f, 0.0f, 0.0f},
1414  new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.07f, 0.0f},
1415  new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.07f}
1416  };
1417 
1418  float[][] coordColors = {
1419  new float[]{1.0f, 0.0f, 0.0f, 0.25f},
1420  new float[]{0.0f, 0.0f, 1.0f, 0.25f},
1421  new float[]{0.0f, 1.0f, 0.0f, 0.25f}
1422  };
1423 
1424  gl.LineWidth(2);
1425 
1426  for (int i = 0; i < 3; i++)
1427  {
1428 
1429  gl.Color(coordColors[i]);
1430  gl.VertexPointer(3, 0, coordVertices[i]);
1431  gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
1432 
1433  }
1434 
1435  gl.Disable(OpenGL.GL_BLEND);
1436 
1437  gl.MatrixMode(OpenGL.GL_PROJECTION);
1438  gl.PopMatrix();
1439 
1440  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1441  gl.PopMatrix();
1442 
1443  gl.PopAttrib();
1444  }
1445 
1446  public void DisplayWireFrame(FaceData trackingData, int width, int height)
1447  {
1448  //set image specs
1449  gl.Viewport(0, 0, width, height);
1450 
1451  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1452  gl.PushMatrix();
1453  gl.LoadIdentity();
1454 
1455  gl.MatrixMode(OpenGL.GL_PROJECTION);
1456  gl.PushMatrix();
1457  gl.LoadIdentity();
1458 
1459  SetupCamera(width, height, trackingData.CameraFocus);
1460 
1461  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1462  gl.ShadeModel(OpenGL.GL_FLAT);
1463  //set the color for the wireframe
1464  gl.Color(0.0f, 1.0f, 0.0f, 1.0f);
1465 
1466  gl.LineWidth(1);
1467 
1468  float[] r = trackingData.FaceRotation;
1469  float[] t = trackingData.FaceTranslation;
1470 
1471  gl.Translate(t[0], t[1], t[2]);
1472  gl.Rotate(VS_RAD2DEG(r[1] + VS_PI), 0.0f, 1.0f, 0.0f);
1473  gl.Rotate(VS_RAD2DEG(r[0]), 1.0f, 0.0f, 0.0f);
1474  gl.Rotate(VS_RAD2DEG(r[2]), 0.0f, 0.0f, 1.0f);
1475 
1476  //draw the wireframe
1477  //initialize indexes for drawing wireframe (once per model)
1478  if (numberOfVertices != trackingData.FaceModelVertexCount)
1479  {
1480  HashSet<KeyValuePair<int, int>> indexList = new HashSet<KeyValuePair<int, int>>();
1481 
1482  for (int i = 0; i < trackingData.FaceModelTriangleCount; i++)
1483  {
1484  int[] triangle = {
1485  trackingData.FaceModelTriangles[3*i+0],
1486  trackingData.FaceModelTriangles[3*i+1],
1487  trackingData.FaceModelTriangles[3*i+2]
1488  };
1489 
1490  if (triangle[0] > triangle[1])
1491  Swap(ref triangle[0], ref triangle[1]);
1492  if (triangle[0] > triangle[2])
1493  Swap(ref triangle[0], ref triangle[2]);
1494  if (triangle[1] > triangle[2])
1495  Swap(ref triangle[1], ref triangle[2]);
1496 
1497  indexList.Add(new KeyValuePair<int, int>(triangle[0], triangle[1]));
1498  indexList.Add(new KeyValuePair<int, int>(triangle[1], triangle[2]));
1499  indexList.Add(new KeyValuePair<int, int>(triangle[0], triangle[2]));
1500  }
1501 
1502  output.Clear();
1503 
1504  foreach (KeyValuePair<int, int> pair in indexList)
1505  {
1506  output.Add(pair.Key);
1507  output.Add(pair.Value);
1508  }
1509 
1510  numberOfVertices = trackingData.FaceModelVertexCount;
1511  }
1512 
1513  //vertex list
1514  gl.VertexPointer(3, 0, trackingData.FaceModelVertices);
1515  gl.DrawElements(OpenGL.GL_LINES, output.Count, (uint[])(object)output.ToArray());
1516 
1517  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1518 
1519  gl.MatrixMode(OpenGL.GL_PROJECTION);
1520  gl.PopMatrix();
1521 
1522  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1523  gl.PopMatrix();
1524  }
1525 
1526  public void DisplayResults(VSImage frame, FaceData trackingData, int trackStat, int width, int height, int drawingOptions, float[] emotions = default(float[]), int gender = -1, float age = 0, string name = "")
1527  {
1528  if (trackStat == (int)TRACK_STAT.OK)
1529  {
1530  if ((drawingOptions & DISPLAY_SPLINES) == DISPLAY_SPLINES)
1531  {
1532  DisplaySplines(trackingData, width, height);
1533  }
1534 
1535  if ((drawingOptions & DISPLAY_FEATURE_POINTS) == DISPLAY_FEATURE_POINTS)
1536  {
1537  bool drawQuality = ((drawingOptions & DISPLAY_POINT_QUALITY) == DISPLAY_POINT_QUALITY);
1538  DisplayFeaturePoints(frame, trackingData, width, height, false, false, drawQuality); // draw 2D feature points
1539  //DisplayFeaturePoints(trackingData, width, height, true); // draw 3D feature points
1540  //DisplayFeaturePoints(trackingData, width, height, true, true); // draw relative 3D feature points
1541  }
1542 
1543  if ((drawingOptions & DISPLAY_GAZE) == DISPLAY_GAZE)
1544  {
1545  DisplayGaze(trackingData, width, height);
1546  }
1547  if ((drawingOptions & DISPLAY_IRIS) == DISPLAY_IRIS)
1548  {
1549  DisplayIrises(frame, trackingData, width, height);
1550  }
1551  if ((drawingOptions & DISPLAY_AXES) == DISPLAY_AXES)
1552  {
1553  DisplayModelAxes(trackingData, width, height);
1554  }
1555  if ((drawingOptions & DISPLAY_WIRE_FRAME) == DISPLAY_WIRE_FRAME)
1556  {
1557  DisplayWireFrame(trackingData, width, height);
1558  }
1559 
1560  bool display_background = true;
1561 
1562  if ((drawingOptions & DISPLAY_EMOTION) == DISPLAY_EMOTION && emotions != null)
1563  {
1564  DisplayEmotion(trackingData, emotions, width, height, display_background);
1565  display_background = false;
1566  }
1567  if ((drawingOptions & DISPLAY_GENDER) != DISPLAY_GENDER)
1568  {
1569  gender = -1;
1570  }
1571  if ((drawingOptions & DISPLAY_AGE) != DISPLAY_AGE)
1572  {
1573  age = -1;
1574  }
1575  if ((drawingOptions & DISPLAY_NAME) != DISPLAY_NAME)
1576  {
1577  name = "";
1578  }
1579  if ((drawingOptions & DISPLAY_GENDER) == DISPLAY_GENDER || (drawingOptions & DISPLAY_AGE) == DISPLAY_AGE || (drawingOptions & DISPLAY_NAME) == DISPLAY_NAME)
1580  {
1581  DisplayAgeGenderName(trackingData, age, gender, name, width, height, display_background);
1582  }
1583  if ((drawingOptions & DISPLAY_TEXTURED_MODEL) == DISPLAY_TEXTURED_MODEL)
1584  {
1585  DisplayTexturedModel(trackingData, width, height);
1586  }
1587  if ((drawingOptions & DISPLAY_TRACKING_QUALITY) == DISPLAY_TRACKING_QUALITY)
1588  {
1589  DisplayTrackingQualityBar(trackingData, width, height);
1590  }
1591  }
1592  }
1593 
1594  public void DisplayEmotion(FaceData trackingData, float[] emotions, int width, int height, bool display_background = false)
1595  {
1596  //SharpGL uses context size instead of viewPort size for rendering text
1597  int contextWidth = gl.RenderContextProvider.Width;
1598  int contextHeight = gl.RenderContextProvider.Height;
1599 
1600  bool _withinConstraints = withinConstraints(ref trackingData);
1601  float backgroundSize = (_withinConstraints) ? 0.18f : 0.15f;
1602 
1603  FeaturePoint position = ChooseLocation(trackingData, width, height, backgroundSize);
1604 
1605  if (display_background)
1606  DisplayBackground(position, width, height, backgroundSize);
1607 
1608  gl.Viewport(0, 0, contextWidth, contextHeight);
1609 
1610  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1611  gl.PushMatrix();
1612  gl.LoadIdentity();
1613 
1614  gl.MatrixMode(OpenGL.GL_PROJECTION);
1615  gl.PushMatrix();
1616  gl.LoadIdentity();
1617 
1618  if (_withinConstraints)
1619  {
1620  DisplayEmotionText(position, width, height);
1621  DisplayEmotionBars(position, emotions, width, height);
1622  }
1623  else
1624  {
1625  DisplayWarningText(position, width, height);
1626  }
1627 
1628  gl.MatrixMode(OpenGL.GL_PROJECTION);
1629  gl.PopMatrix();
1630 
1631  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1632  gl.PopMatrix();
1633  }
1634 
1635  public void DisplayAgeGenderName(FaceData trackingData, float age, int gender, string name, int width, int height, bool display_background = true)
1636  {
1637  string text = "";
1638  FeaturePoint fp = trackingData.FeaturePoints2D.GetFP("2.1");
1639 
1640  bool _withinConstraints = withinConstraints(ref trackingData);
1641  float backgroundSize = (_withinConstraints) ? 0.05f : 0.15f;
1642 
1643  FeaturePoint position = ChooseLocation(trackingData, width, height, backgroundSize);
1644 
1645  if (_withinConstraints)
1646  {
1647  if (name != "")
1648  {
1649  text += name;
1650  }
1651 
1652  if (gender > -1)
1653  {
1654  if (text != "")
1655  {
1656  text += ", ";
1657  }
1658  text += ((gender == 0) ? "FEMALE" : "MALE");
1659  }
1660 
1661  if (age > -1)
1662  {
1663  if (text != "")
1664  {
1665  text += ", ";
1666  }
1667  text += "AGE: " + ((age > -1) ? ((int)(age + 0.5f)).ToString() : "");
1668  }
1669  }
1670  else
1671  {
1672  if (display_background)
1673  {
1674  DisplayBackground(position, width, height, backgroundSize);
1675  }
1676  DisplayWarningText(position, width, height);
1677  }
1678 
1679  if (text != "" && display_background)
1680  {
1681  DisplayBackground(position, width, height, backgroundSize);
1682  }
1683 
1684  DisplayText(position, text, width, height);
1685  }
1686 
1687  public void DisplayTexturedModel(FaceData trackingData, int width, int height)
1688  {
1689  gl.Viewport(0, 0, width, height);
1690 
1691  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1692  gl.PushMatrix();
1693  gl.LoadIdentity();
1694 
1695  gl.MatrixMode(OpenGL.GL_PROJECTION);
1696  gl.PushMatrix();
1697  gl.LoadIdentity();
1698 
1699  SetupCamera(width, height, trackingData.CameraFocus);
1700 
1701  gl.Enable(OpenGL.GL_TEXTURE_2D);
1702  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1703  gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
1704  gl.Enable(OpenGL.GL_CULL_FACE);
1705  gl.Enable(OpenGL.GL_BLEND);
1706  gl.CullFace(OpenGL.GL_FRONT);
1707 
1708  //set texture specs
1709  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
1710  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
1711  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
1712  gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
1713 
1714  gl.ShadeModel(OpenGL.GL_FLAT);
1715  //set the color for the wireframe
1716  gl.Color(0.0f, 1.0f, 0.0f, 1.0f);
1717  //vertex list
1718  gl.VertexPointer(3, 0, trackingData.FaceModelVertices);
1719  //texture coordinate list
1720  gl.TexCoordPointer(2, OpenGL.GL_FLOAT, 0, trackingData.FaceModelTextureCoordsStatic);
1721 
1722  //clear buffer
1723  gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT);
1724  //color=white -> texture gets the right colors and lighting (no effects)
1725  gl.Color(1.0f, 1.0f, 1.0f, 1.0f);
1726 
1727  ////Typical Texture Generation Using Data From The Bitmap
1728  gl.BindTexture(OpenGL.GL_TEXTURE_2D, model_tex_id[0]);
1729 
1730  float[] r = trackingData.FaceRotation;
1731  float[] t = trackingData.FaceTranslation;
1732 
1733  gl.Translate(t[0], t[1], t[2]);
1734  gl.Rotate(VS_RAD2DEG(r[1] + VS_PI), 0.0f, 1.0f, 0.0f);
1735  gl.Rotate(VS_RAD2DEG(r[0]), 1.0f, 0.0f, 0.0f);
1736  gl.Rotate(VS_RAD2DEG(r[2]), 0.0f, 0.0f, 1.0f);
1737 
1738  //draw textured model
1739  //initialize indexes for drawing textured model(once per model)
1740  gl.DrawElements(OpenGL.GL_TRIANGLES, trackingData.FaceModelTriangleCount * 3, trackingData.FaceModelTriangles.Select(x => (uint)x).ToArray());
1741 
1742  gl.Disable(OpenGL.GL_BLEND);
1743  gl.Disable(OpenGL.GL_CULL_FACE);
1744  gl.Disable(OpenGL.GL_TEXTURE_2D);
1745  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1746  gl.DisableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
1747 
1748  gl.MatrixMode(OpenGL.GL_PROJECTION);
1749  gl.PopMatrix();
1750 
1751  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1752  gl.PopMatrix();
1753  }
1754 
1755  public void DisplayTrackingQualityBar(FaceData trackingData, int width, int height)
1756  {
1757  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1758  gl.PushMatrix();
1759  gl.LoadIdentity();
1760 
1761  gl.MatrixMode(OpenGL.GL_PROJECTION);
1762  gl.PushMatrix();
1763  gl.LoadIdentity();
1764 
1765  gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1766 
1767  int points_to_draw = 2;
1768  float[] vertices = new float[6];
1769  gl.LineWidth(10);
1770 
1771  vertices[0] = 0.1f;
1772  vertices[1] = 0.1f;
1773  vertices[2] = 0.0f;
1774  vertices[3] = 0.25f;
1775  vertices[4] = 0.1f;
1776  vertices[5] = 0.0f;
1777  gl.Color(0.5f, 0.5f, 0.5f, 1.0f);
1778  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1779  gl.VertexPointer(3, 0, vertices);
1780  gl.DrawArrays(OpenGL.GL_LINES, 0, points_to_draw);
1781  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1782 
1783  vertices[0] = 0.1f;
1784  vertices[1] = 0.1f;
1785  vertices[2] = 0.0f;
1786  vertices[3] = 0.1f + trackingData.TrackingQuality * 0.15f;
1787  vertices[4] = 0.1f;
1788  vertices[5] = 0.0f;
1789  gl.Color((1 - trackingData.TrackingQuality), trackingData.TrackingQuality, 0, 1.0f);
1790  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1791  gl.VertexPointer(3, 0, vertices);
1792  gl.DrawArrays(OpenGL.GL_LINES, 0, points_to_draw);
1793  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1794  gl.LineWidth(1);
1795 
1796  gl.MatrixMode(OpenGL.GL_PROJECTION);
1797  gl.PopMatrix();
1798 
1799  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1800  gl.PopMatrix();
1801  }
1802 
1803  public void SetTextureImage(VSImage image)
1804  {
1805  texture = image;
1806  }
1807 
1808  void DisplayBackground(FeaturePoint fp, int width, int height, float sizePerc)
1809  {
1810  const float Y_OFFSET = 0.0f;
1811  const float X_OFFSET = -0.0f;
1812  float size = (width > height) ? width * sizePerc : height * sizePerc;
1813 
1814  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1815  gl.PushMatrix();
1816  gl.LoadIdentity();
1817 
1818  gl.MatrixMode(OpenGL.GL_PROJECTION);
1819  gl.PushMatrix();
1820  gl.LoadIdentity();
1821 
1822  gl.Viewport(0, 0, width, height);
1823 
1824  gl.Ortho(0.0f, width, 0.0f, height, -10.0f, 10.0f);
1825 
1826  if (fp.Defined == 0)
1827  {
1828  gl.MatrixMode(OpenGL.GL_PROJECTION);
1829  gl.PopMatrix();
1830 
1831  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1832  gl.PopMatrix();
1833 
1834  return;
1835  }
1836 
1837  float chinX = (int)(fp.Pos[0] * width);
1838  float chinY = (int)(fp.Pos[1] * height);
1839 
1840  gl.Translate(chinX + X_OFFSET, chinY + Y_OFFSET, 0);
1841 
1842  float barHeight = size;
1843  float barLength = BCKG_LENGTH_PERC * ((width > height) ? width : height); //1% of the viewport
1844 
1845  // barWidth
1846  //x2 -------------- x3
1847  // | | barHeight
1848  //x1 -------------- x4
1849  //
1850  float[] auVis = {
1851  0.0f, 0.0f, 0.0f, //x1
1852  0.0f, -barHeight, 0.0f, //x2
1853  barLength, -barHeight, 0.0f, //x3
1854  barLength, 0.0f, 0.0f //x4
1855  };
1856 
1857  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1858  gl.VertexPointer(3, 0, auVis);
1859 
1860  gl.Color(0.8f, 0.8f, 0.8f, 0.5f);
1861  gl.DrawArrays(OpenGL.GL_POLYGON, 0, 4);
1862 
1863  gl.LineWidth(1.0f);
1864  gl.Color(0, 0, 0, 1.0f);
1865  gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, 4);
1866  gl.LineWidth(2.0f);
1867 
1868  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1869 
1870  gl.MatrixMode(OpenGL.GL_PROJECTION);
1871  gl.PopMatrix();
1872 
1873  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1874  gl.PopMatrix();
1875  }
1876 
1877  void DisplayEmotionText(FeaturePoint fp, int width, int height)
1878  {
1879  const float FONT_FACTOR = 0.10f;
1880 
1881  float size = (width > height) ? width * 0.2f : height * 0.2f;
1882 
1883  if (fp.Defined == 0)
1884  {
1885  return;
1886  }
1887 
1888  int chinX = (int)(fp.Pos[0] * width);
1889  int chinY = (int)(fp.Pos[1] * height);
1890 
1891  string[] emotions = { "Anger", "Disgust", "Fear", "Happiness", "Sadness", "Surprise", "Neutral" };
1892 
1893  // Now render some text.
1894  for (int i = 0; i < EMOTIONS_NUM; ++i)
1895  {
1896  gl.DrawText(chinX, chinY + (-i - 3) * (int)(size * FONT_FACTOR), 0.0f, 0.0f, 0.0f, "Arial", (int)(size * FONT_FACTOR), emotions[i]);
1897  }
1898  }
1899 
1900  void DisplayEmotionBars(FeaturePoint fp, float[] emotions, int width, int height)
1901  {
1902  const float FONT_FACTOR = 0.1f;
1903  float X_OFFSET = 0.13f * ((width > height) ? width : height);
1904 
1905  float size = (width > height) ? width * 0.2f : height * 0.2f;
1906 
1907  gl.Viewport(0, 0, width, height);
1908 
1909  gl.Ortho(0.0f, width, 0.0f, height, -10.0f, 10.0f);
1910 
1911  if (fp.Defined == 0)
1912  {
1913  return;
1914  }
1915 
1916  float chinX = (int)(fp.Pos[0] * width);
1917  float chinY = (int)(fp.Pos[1] * height);
1918 
1919  float barHeight = (int)(size * FONT_FACTOR); //20% of width or height adjust with FONT_FACTOR and normalized
1920  float barLength = 0.1f * width; //1% of the viewport
1921  float spacing = (int)(barHeight * 0.1f); //space between bars - before and after
1922 
1923  gl.Translate(chinX + X_OFFSET, chinY - 1 * (barHeight + 2 * spacing), 0);
1924  barHeight = barHeight - 2 * spacing;
1925 
1926  // barWidth
1927  //x2 -------------- x3
1928  // | | barHeight
1929  //x1 -------------- x4
1930  //
1931  float[] auVis = {
1932  0.0f, 0.0f, 0.0f, //x1
1933  0.0f, -barHeight, 0.0f, //x2
1934  barLength, -barHeight, 0.0f, //x3
1935  barLength, 0.0f, 0.0f //x4
1936  };
1937 
1938  gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1939  gl.VertexPointer(3, 0, auVis);
1940 
1941  for (int i = 0; i < emotions.Length; i++)
1942  {
1943  gl.Translate(0, -2 * spacing, 0);
1944  if (emotions[i] < 0.2)
1945  continue;
1946  //adjust bar width
1947  auVis[6] = auVis[9] = emotions[i] * barLength;
1948  //adjust bar y position
1949  auVis[1] = auVis[10] = -(barHeight * (i + 1));
1950  auVis[4] = auVis[7] = -(barHeight * (i + 1) + barHeight);
1951 
1952  gl.Color(0, 0.443137255f, 0.73725490196f, 1.0f);
1953  gl.DrawArrays(OpenGL.GL_POLYGON, 0, 4);
1954 
1955  gl.LineWidth(1.3f);
1956 
1957  gl.Color(0, 0, 0, 1.0f);
1958  gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, 4);
1959  }
1960 
1961  gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1962  }
1963 
1964  void DisplayText(FeaturePoint fp, string text, int width, int height)
1965  {
1966  //SharpGL uses context size instead of viewPort size for rendering text
1967  int contextWidth = gl.RenderContextProvider.Width;
1968  int contextHeight = gl.RenderContextProvider.Height;
1969 
1970  gl.Viewport(0, 0, contextWidth, contextHeight);
1971 
1972  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1973  gl.PushMatrix();
1974  gl.LoadIdentity();
1975 
1976  gl.MatrixMode(OpenGL.GL_PROJECTION);
1977  gl.PushMatrix();
1978  gl.LoadIdentity();
1979 
1980  const float FONT_FACTOR = 0.1f;
1981 
1982  float size = (width > height) ? width * 0.2f : height * 0.2f;
1983 
1984  if (fp.Defined == 0)
1985  {
1986  gl.MatrixMode(OpenGL.GL_PROJECTION);
1987  gl.PopMatrix();
1988 
1989  gl.MatrixMode(OpenGL.GL_MODELVIEW);
1990  gl.PopMatrix();
1991 
1992  return;
1993  }
1994 
1995  const float X_OFFSET = 1.0f;
1996  const float Y_OFFSET = 1.2f;
1997 
1998  int chinX = (int)(fp.Pos[0] * width);
1999  int chinY = (int)(fp.Pos[1] * height);
2000 
2001  gl.DrawText((int)(chinX * X_OFFSET), chinY - (int)(size * FONT_FACTOR * Y_OFFSET), 0.0f, 0.0f, 0.0f, "Arial", size * FONT_FACTOR, text);
2002 
2003  gl.MatrixMode(OpenGL.GL_PROJECTION);
2004  gl.PopMatrix();
2005 
2006  gl.MatrixMode(OpenGL.GL_MODELVIEW);
2007  gl.PopMatrix();
2008  }
2009 
2010  void DisplayWarningText(FeaturePoint fp, int width, int height)
2011  {
2012  //SharpGL uses context size instead of viewPort size for rendering text
2013  int contextWidth = gl.RenderContextProvider.Width;
2014  int contextHeight = gl.RenderContextProvider.Height;
2015 
2016  gl.Viewport(0, 0, contextWidth, contextHeight);
2017 
2018  gl.MatrixMode(OpenGL.GL_MODELVIEW);
2019  gl.PushMatrix();
2020  gl.LoadIdentity();
2021 
2022  gl.MatrixMode(OpenGL.GL_PROJECTION);
2023  gl.PushMatrix();
2024  gl.LoadIdentity();
2025 
2026  const float FONT_FACTOR = 0.10f;
2027 
2028  float size = (width > height) ? width * 0.2f : height * 0.2f;
2029 
2030  if (fp.Defined == 0)
2031  {
2032  return;
2033  }
2034 
2035  int chinX = (int)(fp.Pos[0] * width);
2036  int chinY = (int)(fp.Pos[1] * height);
2037 
2038  string[] message = { "Face analysis and face", "recognition are available", "only in frontal pose,", "and when face is not", "too small in the image." };
2039 
2040  // Now render some text.
2041  for (int i = 0; i < message.Count(); ++i)
2042  {
2043  gl.DrawText(chinX, chinY + (-i - 2) * (int)(size * FONT_FACTOR), 1.0f, 0.0f, 0.0f, "Arial", (int)(size * FONT_FACTOR), message[i]);
2044  }
2045 
2046 
2047  gl.MatrixMode(OpenGL.GL_PROJECTION);
2048  gl.PopMatrix();
2049 
2050  gl.MatrixMode(OpenGL.GL_MODELVIEW);
2051  gl.PopMatrix();
2052  }
2053 
2054  FeaturePoint ChooseLocation(FaceData trackingData, int width, int height, float backgroundSize)
2055  {
2056  FDP fdp = trackingData.FeaturePoints2D;
2057 
2058  FeaturePoint down = fdp.GetFP("2.1");
2059  FeaturePoint left = fdp.GetFP("4.5");
2060  FeaturePoint right = fdp.GetFP("4.6");
2061  FeaturePoint up = fdp.GetFP("11.1");
2062  FeaturePoint location = new FeaturePoint();
2063 
2064  int temporaryPosition = 0;
2065  float backgroundHeight = (width > height) ? width * backgroundSize : height * backgroundSize;
2066  float backgroundWidth = BCKG_LENGTH_PERC * ((width > height) ? width : height);
2067 
2068  if (up.Pos[1] * height - backgroundHeight < height)
2069  {
2070  if (currentPosition == (int)DISPLAY_POS.UP)
2071  return up;
2072  else
2073  {
2074  location = up;
2075  temporaryPosition = (int)DISPLAY_POS.UP;
2076  }
2077 
2078  }
2079 
2080  if (right.Pos[0] * width > backgroundWidth)
2081  {
2082  right.Pos[0] = right.Pos[0] - BCKG_LENGTH_PERC;
2083 
2084  if (currentPosition == (int)DISPLAY_POS.RIGHT)
2085  return right;
2086  else
2087  {
2088  location = right;
2089  temporaryPosition = (int)DISPLAY_POS.RIGHT;
2090  }
2091  }
2092 
2093  if ((left.Pos[0] * width + backgroundWidth) < width)
2094  {
2095  if (currentPosition == (int)DISPLAY_POS.LEFT)
2096  return left;
2097  else
2098  {
2099  location = left;
2100  temporaryPosition = (int)DISPLAY_POS.LEFT;
2101  }
2102  }
2103 
2104  if ((down.Pos[1] * height > backgroundHeight) && ((down.Pos[0] * width + backgroundWidth) < width))
2105  {
2106  currentPosition = (int)DISPLAY_POS.DOWN;
2107  return down;
2108  }
2109 
2110  currentPosition = temporaryPosition;
2111  return location;
2112 
2113  }
2114  }
2115 }