2 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
12 public static class ListExtra
14 public static void Resize<T>(
this List<T> list,
int sz, T c =
default(T))
18 list.RemoveRange(sz, cur - sz);
20 list.AddRange(Enumerable.Repeat(c, sz - cur));
26 public VisageRendering(OpenGL new_gl)
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;
42 static List<int> output =
new List<int>();
44 const float VS_PI = 3.1415926535897932384626433832795f;
45 const int EMOTIONS_NUM = 7;
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;
64 public const float BCKG_LENGTH_PERC = 0.3f;
65 public static double VS_DEG2RAD(
double a)
67 return (a * VS_PI / 180.0);
69 public static double VS_RAD2DEG(
double a)
71 return (a * 180.0f / VS_PI);
76 private enum DISPLAY_POS :
int
84 public int currentPosition = (int)DISPLAY_POS.DOWN;
90 public Vec2D(
float _x,
float _y)
100 public float c0, c1, c2, c3;
101 public float eval(
float t)
105 return c0 + c1 * t + c2 * t2 + c3 * t3;
110 public void ClearGL()
112 gl.ClearColor(0, 0, 0, 0);
113 gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
116 int NearestPow2(
int n)
132 void InitFrameTex(
int x_size,
int y_size,
VSImage image)
135 gl.GenTextures(1, frame_tex_id);
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);
144 gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, x_size, y_size, 0, OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, null);
146 tex_x_coord = (float)image.
width / (
float)x_size;
147 tex_y_coord = (float)image.
height / (
float)y_size;
150 gl.GenTextures(1, model_tex_id);
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);
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);
162 gl.Disable(OpenGL.GL_BLEND);
165 public static void Swap<T>(ref T left, ref T right)
173 public void DisplayFrame(
VSImage image,
int width,
int height)
175 if (video_texture_inited && (video_texture_width != image.
width || video_texture_height != image.
height))
177 gl.DeleteTextures(1, frame_tex_id);
178 video_texture_inited =
false;
182 if (!video_texture_inited)
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;
190 gl.BindTexture(OpenGL.GL_TEXTURE_2D, frame_tex_id[0]);
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);
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);
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);
207 gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
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);
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);
222 gl.BindTexture(OpenGL.GL_TEXTURE_2D, frame_tex_id[0]);
224 gl.MatrixMode(OpenGL.GL_MODELVIEW);
228 gl.MatrixMode(OpenGL.GL_PROJECTION);
232 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
241 float[] texcoords = {
248 gl.Color(1.0f, 1.0f, 1.0f, 1.0f);
250 gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
251 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
253 gl.VertexPointer(3, 0, vertices);
254 gl.TexCoordPointer(2, OpenGL.GL_FLOAT, 0, texcoords);
256 gl.Viewport(0, 0, width, height);
257 gl.DrawArrays(OpenGL.GL_TRIANGLE_STRIP, 0, 4);
259 gl.DisableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
260 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
262 gl.Disable(OpenGL.GL_TEXTURE_2D);
265 gl.MatrixMode(OpenGL.GL_MODELVIEW);
268 gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
273 gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT);
276 static void InitCubicPoly(
float x0,
float x1,
float t0,
float t1, ref CubicPoly p)
280 p.c2 = -3 * x0 + 3 * x1 - 2 * t0 - t1;
281 p.c3 = 2 * x0 - 2 * x1 + t0 + t1;
284 static void InitNonuniformCatmullRom(
float x0,
float x1,
float x2,
float x3,
float dt0,
float dt1,
float dt2, ref CubicPoly p)
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;
292 InitCubicPoly(x1, x2, t1, t2, ref p);
295 static float VecDistSquared(ref Vec2D p, ref Vec2D q)
297 float dx = q.x - p.x;
298 float dy = q.y - p.y;
299 return dx * dx + dy * dy;
302 static void InitCentripetalCR(ref Vec2D p0, ref Vec2D p1, ref Vec2D p2, ref Vec2D p3, ref CubicPoly px, ref CubicPoly py)
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);
308 if (dt1 < 1e-4f) dt1 = 1.0f;
309 if (dt0 < 1e-4f) dt0 = dt1;
310 if (dt2 < 1e-4f) dt2 = dt1;
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);
316 void CalcSpline(ref List<float> inputPoints,
int ratio, ref List<float> outputPoints)
318 int nPoints, nPointsToDraw, nLines;
320 nPoints = inputPoints.Count / 2 + 2;
321 nPointsToDraw = inputPoints.Count / 2 + (inputPoints.Count() / 2 - 1) * ratio;
322 nLines = nPoints - 1 - 2;
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]));
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);
334 CubicPoly px =
new CubicPoly();
335 CubicPoly py =
new CubicPoly();
337 ListExtra.Resize<
float>(outputPoints, 2 * nPointsToDraw);
339 for (
int i = 0; i < nPoints - 2; i++)
341 outputPoints[i * 2 * (ratio + 1)] = inputPoints[2 * i + 2];
342 outputPoints[i * 2 * (ratio + 1) + 1] = inputPoints[2 * i + 1 + 2];
345 for (
int i = 0; i < 2 * nLines; i = i + 2)
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];
356 InitCentripetalCR(ref p0, ref p1, ref p2, ref p3, ref px, ref py);
358 for (
int j = 1; j <= ratio; j++)
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)));
366 inputPoints.RemoveRange(0, 2);
367 inputPoints.RemoveRange(inputPoints.Count - 2, 2);
370 void SetupCamera(
int width,
int height,
float f)
375 x_offset = ((float)width) / ((float)height);
376 else if (width < height)
377 y_offset = ((float)height) / ((float)width);
383 float frustum_near = 0.001f;
384 float frustum_far = 30;
385 float frustum_x = x_offset * frustum_near / f;
386 float frustum_y = y_offset * frustum_near / f;
388 gl.MatrixMode(OpenGL.GL_PROJECTION);
390 gl.Frustum(-frustum_x, frustum_x, -frustum_y, frustum_y, frustum_near, frustum_far);
391 gl.MatrixMode(OpenGL.GL_MODELVIEW);
395 gl.LookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);
398 public bool withinConstraints(ref
FaceData trackingData)
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];
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);
408 const double CONSTRAINT_ANGLE = 40;
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)
419 void DrawElipse(
float x,
float y,
float radiusX,
float radiusY,
bool filled =
true)
421 gl.MatrixMode(OpenGL.GL_MODELVIEW);
425 const int circle_points = 200;
426 const float angleIncrement = 360.0f / circle_points;
427 float[] vertices =
new float[circle_points * 2];
429 for (
int i = 0; i < circle_points; i++)
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)));
435 int size = Marshal.SizeOf(vertices[0]) * vertices.Length;
436 IntPtr pnt = Marshal.AllocHGlobal(size);
437 Marshal.Copy(vertices, 0, pnt, vertices.Length);
439 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
440 gl.VertexPointer(2, OpenGL.GL_FLOAT, 0, pnt);
442 gl.Enable(OpenGL.GL_BLEND);
443 gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
445 gl.DrawArrays(OpenGL.GL_TRIANGLE_FAN, 0, circle_points);
447 gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, circle_points);
449 gl.Disable(OpenGL.GL_BLEND);
450 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
452 Marshal.FreeHGlobal(pnt);
457 void DrawSpline2D(
int[] points,
int num,
FDP fdp,
bool useAlpha =
false)
462 List<float> pointCoords =
new List<float>();
463 List<float> pointCoordsQuality =
new List<float>(); ;
467 for (
int i = 0; i < num; i++)
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)
472 pointCoords.Add(fp.Pos[0]);
473 pointCoords.Add(fp.Pos[1]);
475 float alphaChannel = (fp.Quality > 0 && useAlpha) ? Math.Max(fp.Quality * 0.75f, 0.2f) : 0.75f;
477 pointCoordsQuality.Add(alphaChannel);
478 pointCoordsQuality.Add(alphaChannel);
484 if(pointCoords.Count() == 0 || n <= 2)
488 List<float> pointsToDraw =
new List<float>();
489 List<float> qualityToDraw =
new List<float>();
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;
503 for (
int i = 0; i < nVert; ++i)
505 vertices[3 * i + 0] = pointsToDraw[cnt++];
506 vertices[3 * i + 1] = pointsToDraw[cnt++];
507 vertices[3 * i + 2] = 0.0f;
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];
515 int sizeColor = Marshal.SizeOf(colors[0]) * colors.Length;
516 IntPtr pColor = Marshal.AllocHGlobal(sizeColor);
517 Marshal.Copy(colors, 0, pColor, colors.Length);
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);
531 Marshal.FreeHGlobal(pColor);
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)
536 float faceScaleNormalized = (frameWidth > frameHeight) ? faceScale / (
float)frameWidth : faceScale / (float)frameHeight;
537 float radius = ((1 + (faceScaleNormalized * 2.0f)) * (1 + (winHeight / 250.0f)));
539 float[] vertices =
new float[num * 2];
540 float[] colorsQuality =
new float[num * 4];
541 float[] colorsCircle =
new float[num * 4];
544 for (
int i = 0; i < num; i++)
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)
549 vertices[2 * n + 0] = fp.Pos[0];
550 vertices[2 * n + 1] = fp.Pos[1];
551 if (drawQuality && fp.Quality >= 0)
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;
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;
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;
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;
580 int sizeVertices = Marshal.SizeOf(vertices[0]) * vertices.Length;
581 IntPtr pnt = Marshal.AllocHGlobal(sizeVertices);
582 Marshal.Copy(vertices, 0, pnt, vertices.Length);
584 int sizeColorsQuality = Marshal.SizeOf(colorsQuality[0]) * colorsQuality.Length;
585 IntPtr pColorsQuality = Marshal.AllocHGlobal(sizeColorsQuality);
586 Marshal.Copy(colorsQuality, 0, pColorsQuality, colorsQuality.Length);
588 int sizeColorsCircle = Marshal.SizeOf(colorsCircle[0]) * colorsCircle.Length;
589 IntPtr pColorsCircle = Marshal.AllocHGlobal(sizeColorsCircle);
590 Marshal.Copy(colorsCircle, 0, pColorsCircle, colorsCircle.Length);
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);
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);
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);
613 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
614 gl.Disable(OpenGL.GL_BLEND);
616 Marshal.FreeHGlobal(pnt);
617 Marshal.FreeHGlobal(pColorsQuality);
618 Marshal.FreeHGlobal(pColorsCircle);
621 void DrawPoints3D(
int[] points,
int num,
bool singleColor,
int faceScale,
int winWidth,
int winHeight,
int frameWidth,
int frameHeight,
FDP fdp)
623 float faceScaleNormalized = (frameWidth > frameHeight) ? faceScale / (
float)frameWidth : faceScale / (float)frameHeight;
624 float radius = ((1 + (faceScaleNormalized * 2.0f)) * (1 + (winHeight / 250.0f)));
626 float[] vertices =
new float[num * 3];
629 for (
int i = 0; i < num; i++)
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)
634 vertices[3 * n + 0] = fp.Pos[0];
635 vertices[3 * n + 1] = fp.Pos[1];
636 vertices[3 * n + 2] = fp.Pos[2];
641 int sizeVertices = Marshal.SizeOf(vertices[0]) * vertices.Length;
642 IntPtr pnt = Marshal.AllocHGlobal(sizeVertices);
643 Marshal.Copy(vertices, 0, pnt, vertices.Length);
645 gl.Enable(OpenGL.GL_POINT_SMOOTH);
646 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
647 gl.VertexPointer(3, OpenGL.GL_FLOAT, 0, pnt);
649 gl.PointSize(radius);
650 gl.Color(0, 0, 0, 1.0f);
651 gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
655 gl.PointSize(0.65f * radius);
656 gl.Color(0, 1.0f, 1.0f, 1.0f);
657 gl.DrawArrays(OpenGL.GL_POINTS, 0, n);
659 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
660 Marshal.FreeHGlobal(pnt);
663 public void DisplayFeaturePoints(
VSImage frame,
FaceData trackingData,
int width,
int height,
bool _3D,
bool relative,
bool drawQuality)
665 gl.Viewport(0, 0, width, height);
667 gl.MatrixMode(OpenGL.GL_MODELVIEW);
671 gl.MatrixMode(OpenGL.GL_PROJECTION);
675 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
679 SetupCamera(width, height, trackingData.
CameraFocus);
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);
695 FDP featurePoints3D = relative ? trackingData.featurePoints3DRelative : trackingData.
FeaturePoints3D;
696 int frameWidth = frame.
width;
697 int frameHeight = frame.
height;
699 int[] innerLipPoints = {
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);
720 int[] outerLipPoints = {
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);
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);
759 int[] pupilPoints = {
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);
768 int[] pupilPoints = {
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);
775 int[] eyesPointsR = {
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);
804 int[] eyesPointsL = {
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);
833 int[] eyebrowPoints = {
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);
856 int[] leftEarPoints =
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);
875 int[] rightEarPoints =
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);
895 int[] contourPointsPhysical = {
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);
918 gl.MatrixMode(OpenGL.GL_PROJECTION);
920 gl.MatrixMode(OpenGL.GL_MODELVIEW);
924 public void DisplaySplines(
FaceData trackingData,
int width,
int height)
926 gl.Viewport(0, 0, width, height);
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);
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);
937 gl.MatrixMode(OpenGL.GL_MODELVIEW);
941 gl.MatrixMode(OpenGL.GL_PROJECTION);
946 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
953 int[] outerUpperLipPoints = {
966 DrawSpline2D(outerUpperLipPoints, 11, featurePoints2D);
968 int[] outerLowerLipPoints = {
979 DrawSpline2D(outerLowerLipPoints, 9, featurePoints2D);
981 int[] innerUpperLipPoints = {
992 DrawSpline2D(innerUpperLipPoints, 9, featurePoints2D);
994 int[] innerLowerLipPoints = {
1005 DrawSpline2D(innerLowerLipPoints, 9, featurePoints2D);
1007 int[] noseLinePoints = {
1012 DrawSpline2D(noseLinePoints, 3, featurePoints2D);
1014 int[] noseLinePoints2 = {
1021 DrawSpline2D(noseLinePoints2, 5, featurePoints2D);
1023 int[] outerUpperEyePointsR = {
1030 DrawSpline2D(outerUpperEyePointsR, 5, featurePoints2D);
1032 int[] outerLowerEyePointsR = {
1039 DrawSpline2D(outerLowerEyePointsR, 5, featurePoints2D);
1041 int[] innerUpperEyePointsR = {
1052 DrawSpline2D(innerUpperEyePointsR, 9, featurePoints2D);
1054 int[] innerLowerEyePointsR = {
1065 DrawSpline2D(innerLowerEyePointsR, 9, featurePoints2D);
1067 int[] outerUpperEyePointsL = {
1074 DrawSpline2D(outerUpperEyePointsL, 5, featurePoints2D);
1076 int[] outerLowerEyePointsL = {
1083 DrawSpline2D(outerLowerEyePointsL, 5, featurePoints2D);
1085 int[] innerUpperEyePointsL = {
1096 DrawSpline2D(innerUpperEyePointsL, 9, featurePoints2D);
1098 int[] innerLowerEyePointsL = {
1109 DrawSpline2D(innerLowerEyePointsL, 9, featurePoints2D);
1111 int[] eyebrowUpperPointsR = {
1119 DrawSpline2D(eyebrowUpperPointsR, 6, featurePoints2D);
1121 int[] eyebrowLowerPointsR = {
1128 DrawSpline2D(eyebrowLowerPointsR, 5, featurePoints2D);
1130 int[] eyebrowUpperPointsL = {
1138 DrawSpline2D(eyebrowUpperPointsL, 6, featurePoints2D);
1140 int[] eyebrowLowerPointsL = {
1147 DrawSpline2D(eyebrowLowerPointsL, 5, featurePoints2D);
1149 int[] leftEarPoints =
1160 DrawSpline2D(leftEarPoints, 7, featurePoints2D);
1162 int[] rightEarPoints =
1173 DrawSpline2D(rightEarPoints, 7, featurePoints2D);
1176 int[] contourPointsPhysical = {
1195 DrawSpline2D(contourPointsPhysical, 17, featurePoints2D,
true);
1197 gl.Disable(OpenGL.GL_BLEND);
1198 gl.MatrixMode(OpenGL.GL_PROJECTION);
1200 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1207 public void DisplayGaze(
FaceData trackingData,
int width,
int height)
1209 gl.Viewport(0, 0, width, height);
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);
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);
1220 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1224 gl.MatrixMode(OpenGL.GL_PROJECTION);
1229 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1231 SetupCamera(width, height, trackingData.
CameraFocus);
1233 gl.ShadeModel(OpenGL.GL_FLAT);
1235 float[] vertices = {
1240 float[] tr = { 0, 0, 0, 0, 0, 0 };
1250 if (leye.Defined == 1 && reye.Defined == 1)
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];
1264 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1266 gl.MatrixMode(OpenGL.GL_MODELVIEW);
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);
1276 gl.Color(0.941176471, 0.37647058823, 0, 1.0f);
1280 gl.VertexPointer(3, 0, vertices);
1281 gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
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);
1295 gl.VertexPointer(3, 0, vertices);
1296 gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
1299 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1303 gl.MatrixMode(OpenGL.GL_PROJECTION);
1306 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1312 public void DisplayIrises(
VSImage frame,
FaceData trackingData,
int width,
int height)
1314 gl.Viewport(0, 0, width, height);
1316 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1320 gl.MatrixMode(OpenGL.GL_PROJECTION);
1324 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1330 int frameWidth = frame.
width;
1331 int frameHeight = frame.
height;
1336 float rx = trackingData.
IrisRadius[0] / frameWidth;
1337 float ry = trackingData.
IrisRadius[0] / frameHeight;
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);
1348 float rx = trackingData.
IrisRadius[1] / frameWidth;
1349 float ry = trackingData.
IrisRadius[1] / frameHeight;
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);
1358 gl.MatrixMode(OpenGL.GL_PROJECTION);
1360 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1364 public void DisplayModelAxes(
FaceData trackingData,
int width,
int height)
1366 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1368 gl.Viewport(0, 0, width, height);
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);
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);
1379 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1383 gl.MatrixMode(OpenGL.GL_PROJECTION);
1387 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1389 SetupCamera(width, height, trackingData.
CameraFocus);
1391 gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
1392 gl.Enable(OpenGL.GL_BLEND);
1394 gl.ShadeModel(OpenGL.GL_FLAT);
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);
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}
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}
1426 for (
int i = 0; i < 3; i++)
1429 gl.Color(coordColors[i]);
1430 gl.VertexPointer(3, 0, coordVertices[i]);
1431 gl.DrawArrays(OpenGL.GL_LINES, 0, 2);
1435 gl.Disable(OpenGL.GL_BLEND);
1437 gl.MatrixMode(OpenGL.GL_PROJECTION);
1440 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1446 public void DisplayWireFrame(
FaceData trackingData,
int width,
int height)
1449 gl.Viewport(0, 0, width, height);
1451 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1455 gl.MatrixMode(OpenGL.GL_PROJECTION);
1459 SetupCamera(width, height, trackingData.
CameraFocus);
1461 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1462 gl.ShadeModel(OpenGL.GL_FLAT);
1464 gl.Color(0.0f, 1.0f, 0.0f, 1.0f);
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);
1480 HashSet<KeyValuePair<int, int>> indexList =
new HashSet<KeyValuePair<int, int>>();
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]);
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]));
1504 foreach (KeyValuePair<int, int> pair
in indexList)
1506 output.Add(pair.Key);
1507 output.Add(pair.Value);
1515 gl.DrawElements(OpenGL.GL_LINES, output.Count, (uint[])(
object)output.ToArray());
1517 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1519 gl.MatrixMode(OpenGL.GL_PROJECTION);
1522 gl.MatrixMode(OpenGL.GL_MODELVIEW);
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 =
"")
1530 if ((drawingOptions & DISPLAY_SPLINES) == DISPLAY_SPLINES)
1532 DisplaySplines(trackingData, width, height);
1535 if ((drawingOptions & DISPLAY_FEATURE_POINTS) == DISPLAY_FEATURE_POINTS)
1537 bool drawQuality = ((drawingOptions & DISPLAY_POINT_QUALITY) == DISPLAY_POINT_QUALITY);
1538 DisplayFeaturePoints(frame, trackingData, width, height,
false,
false, drawQuality);
1543 if ((drawingOptions & DISPLAY_GAZE) == DISPLAY_GAZE)
1545 DisplayGaze(trackingData, width, height);
1547 if ((drawingOptions & DISPLAY_IRIS) == DISPLAY_IRIS)
1549 DisplayIrises(frame, trackingData, width, height);
1551 if ((drawingOptions & DISPLAY_AXES) == DISPLAY_AXES)
1553 DisplayModelAxes(trackingData, width, height);
1555 if ((drawingOptions & DISPLAY_WIRE_FRAME) == DISPLAY_WIRE_FRAME)
1557 DisplayWireFrame(trackingData, width, height);
1560 bool display_background =
true;
1562 if ((drawingOptions & DISPLAY_EMOTION) == DISPLAY_EMOTION && emotions != null)
1564 DisplayEmotion(trackingData, emotions, width, height, display_background);
1565 display_background =
false;
1567 if ((drawingOptions & DISPLAY_GENDER) != DISPLAY_GENDER)
1571 if ((drawingOptions & DISPLAY_AGE) != DISPLAY_AGE)
1575 if ((drawingOptions & DISPLAY_NAME) != DISPLAY_NAME)
1579 if ((drawingOptions & DISPLAY_GENDER) == DISPLAY_GENDER || (drawingOptions & DISPLAY_AGE) == DISPLAY_AGE || (drawingOptions & DISPLAY_NAME) == DISPLAY_NAME)
1581 DisplayAgeGenderName(trackingData, age, gender, name, width, height, display_background);
1583 if ((drawingOptions & DISPLAY_TEXTURED_MODEL) == DISPLAY_TEXTURED_MODEL)
1585 DisplayTexturedModel(trackingData, width, height);
1587 if ((drawingOptions & DISPLAY_TRACKING_QUALITY) == DISPLAY_TRACKING_QUALITY)
1589 DisplayTrackingQualityBar(trackingData, width, height);
1594 public void DisplayEmotion(
FaceData trackingData,
float[] emotions,
int width,
int height,
bool display_background =
false)
1597 int contextWidth = gl.RenderContextProvider.Width;
1598 int contextHeight = gl.RenderContextProvider.Height;
1600 bool _withinConstraints = withinConstraints(ref trackingData);
1601 float backgroundSize = (_withinConstraints) ? 0.18f : 0.15f;
1603 FeaturePoint position = ChooseLocation(trackingData, width, height, backgroundSize);
1605 if (display_background)
1606 DisplayBackground(position, width, height, backgroundSize);
1608 gl.Viewport(0, 0, contextWidth, contextHeight);
1610 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1614 gl.MatrixMode(OpenGL.GL_PROJECTION);
1618 if (_withinConstraints)
1620 DisplayEmotionText(position, width, height);
1621 DisplayEmotionBars(position, emotions, width, height);
1625 DisplayWarningText(position, width, height);
1628 gl.MatrixMode(OpenGL.GL_PROJECTION);
1631 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1635 public void DisplayAgeGenderName(
FaceData trackingData,
float age,
int gender,
string name,
int width,
int height,
bool display_background =
true)
1640 bool _withinConstraints = withinConstraints(ref trackingData);
1641 float backgroundSize = (_withinConstraints) ? 0.05f : 0.15f;
1643 FeaturePoint position = ChooseLocation(trackingData, width, height, backgroundSize);
1645 if (_withinConstraints)
1658 text += ((gender == 0) ?
"FEMALE" :
"MALE");
1667 text +=
"AGE: " + ((age > -1) ? ((
int)(age + 0.5f)).ToString() :
"");
1672 if (display_background)
1674 DisplayBackground(position, width, height, backgroundSize);
1676 DisplayWarningText(position, width, height);
1679 if (text !=
"" && display_background)
1681 DisplayBackground(position, width, height, backgroundSize);
1684 DisplayText(position, text, width, height);
1687 public void DisplayTexturedModel(
FaceData trackingData,
int width,
int height)
1689 gl.Viewport(0, 0, width, height);
1691 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1695 gl.MatrixMode(OpenGL.GL_PROJECTION);
1699 SetupCamera(width, height, trackingData.
CameraFocus);
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);
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);
1714 gl.ShadeModel(OpenGL.GL_FLAT);
1716 gl.Color(0.0f, 1.0f, 0.0f, 1.0f);
1723 gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT);
1725 gl.Color(1.0f, 1.0f, 1.0f, 1.0f);
1728 gl.BindTexture(OpenGL.GL_TEXTURE_2D, model_tex_id[0]);
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);
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);
1748 gl.MatrixMode(OpenGL.GL_PROJECTION);
1751 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1755 public void DisplayTrackingQualityBar(
FaceData trackingData,
int width,
int height)
1757 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1761 gl.MatrixMode(OpenGL.GL_PROJECTION);
1765 gl.Ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);
1767 int points_to_draw = 2;
1768 float[] vertices =
new float[6];
1774 vertices[3] = 0.25f;
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);
1786 vertices[3] = 0.1f + trackingData.TrackingQuality * 0.15f;
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);
1796 gl.MatrixMode(OpenGL.GL_PROJECTION);
1799 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1803 public void SetTextureImage(
VSImage image)
1808 void DisplayBackground(
FeaturePoint fp,
int width,
int height,
float sizePerc)
1810 const float Y_OFFSET = 0.0f;
1811 const float X_OFFSET = -0.0f;
1812 float size = (width > height) ? width * sizePerc : height * sizePerc;
1814 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1818 gl.MatrixMode(OpenGL.GL_PROJECTION);
1822 gl.Viewport(0, 0, width, height);
1824 gl.Ortho(0.0f, width, 0.0f, height, -10.0f, 10.0f);
1826 if (fp.Defined == 0)
1828 gl.MatrixMode(OpenGL.GL_PROJECTION);
1831 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1837 float chinX = (int)(fp.Pos[0] * width);
1838 float chinY = (int)(fp.Pos[1] * height);
1840 gl.Translate(chinX + X_OFFSET, chinY + Y_OFFSET, 0);
1842 float barHeight = size;
1843 float barLength = BCKG_LENGTH_PERC * ((width > height) ? width : height);
1852 0.0f, -barHeight, 0.0f,
1853 barLength, -barHeight, 0.0f,
1854 barLength, 0.0f, 0.0f
1857 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1858 gl.VertexPointer(3, 0, auVis);
1860 gl.Color(0.8f, 0.8f, 0.8f, 0.5f);
1861 gl.DrawArrays(OpenGL.GL_POLYGON, 0, 4);
1864 gl.Color(0, 0, 0, 1.0f);
1865 gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, 4);
1868 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1870 gl.MatrixMode(OpenGL.GL_PROJECTION);
1873 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1877 void DisplayEmotionText(
FeaturePoint fp,
int width,
int height)
1879 const float FONT_FACTOR = 0.10f;
1881 float size = (width > height) ? width * 0.2f : height * 0.2f;
1883 if (fp.Defined == 0)
1888 int chinX = (int)(fp.Pos[0] * width);
1889 int chinY = (int)(fp.Pos[1] * height);
1891 string[] emotions = {
"Anger",
"Disgust",
"Fear",
"Happiness",
"Sadness",
"Surprise",
"Neutral" };
1894 for (
int i = 0; i < EMOTIONS_NUM; ++i)
1896 gl.DrawText(chinX, chinY + (-i - 3) * (
int)(size * FONT_FACTOR), 0.0f, 0.0f, 0.0f,
"Arial", (
int)(size * FONT_FACTOR), emotions[i]);
1900 void DisplayEmotionBars(
FeaturePoint fp,
float[] emotions,
int width,
int height)
1902 const float FONT_FACTOR = 0.1f;
1903 float X_OFFSET = 0.13f * ((width > height) ? width : height);
1905 float size = (width > height) ? width * 0.2f : height * 0.2f;
1907 gl.Viewport(0, 0, width, height);
1909 gl.Ortho(0.0f, width, 0.0f, height, -10.0f, 10.0f);
1911 if (fp.Defined == 0)
1916 float chinX = (int)(fp.Pos[0] * width);
1917 float chinY = (int)(fp.Pos[1] * height);
1919 float barHeight = (int)(size * FONT_FACTOR);
1920 float barLength = 0.1f * width;
1921 float spacing = (int)(barHeight * 0.1f);
1923 gl.Translate(chinX + X_OFFSET, chinY - 1 * (barHeight + 2 * spacing), 0);
1924 barHeight = barHeight - 2 * spacing;
1933 0.0f, -barHeight, 0.0f,
1934 barLength, -barHeight, 0.0f,
1935 barLength, 0.0f, 0.0f
1938 gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
1939 gl.VertexPointer(3, 0, auVis);
1941 for (
int i = 0; i < emotions.Length; i++)
1943 gl.Translate(0, -2 * spacing, 0);
1944 if (emotions[i] < 0.2)
1947 auVis[6] = auVis[9] = emotions[i] * barLength;
1949 auVis[1] = auVis[10] = -(barHeight * (i + 1));
1950 auVis[4] = auVis[7] = -(barHeight * (i + 1) + barHeight);
1952 gl.Color(0, 0.443137255f, 0.73725490196f, 1.0f);
1953 gl.DrawArrays(OpenGL.GL_POLYGON, 0, 4);
1957 gl.Color(0, 0, 0, 1.0f);
1958 gl.DrawArrays(OpenGL.GL_LINE_LOOP, 0, 4);
1961 gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
1964 void DisplayText(
FeaturePoint fp,
string text,
int width,
int height)
1967 int contextWidth = gl.RenderContextProvider.Width;
1968 int contextHeight = gl.RenderContextProvider.Height;
1970 gl.Viewport(0, 0, contextWidth, contextHeight);
1972 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1976 gl.MatrixMode(OpenGL.GL_PROJECTION);
1980 const float FONT_FACTOR = 0.1f;
1982 float size = (width > height) ? width * 0.2f : height * 0.2f;
1984 if (fp.Defined == 0)
1986 gl.MatrixMode(OpenGL.GL_PROJECTION);
1989 gl.MatrixMode(OpenGL.GL_MODELVIEW);
1995 const float X_OFFSET = 1.0f;
1996 const float Y_OFFSET = 1.2f;
1998 int chinX = (int)(fp.Pos[0] * width);
1999 int chinY = (int)(fp.Pos[1] * height);
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);
2003 gl.MatrixMode(OpenGL.GL_PROJECTION);
2006 gl.MatrixMode(OpenGL.GL_MODELVIEW);
2010 void DisplayWarningText(
FeaturePoint fp,
int width,
int height)
2013 int contextWidth = gl.RenderContextProvider.Width;
2014 int contextHeight = gl.RenderContextProvider.Height;
2016 gl.Viewport(0, 0, contextWidth, contextHeight);
2018 gl.MatrixMode(OpenGL.GL_MODELVIEW);
2022 gl.MatrixMode(OpenGL.GL_PROJECTION);
2026 const float FONT_FACTOR = 0.10f;
2028 float size = (width > height) ? width * 0.2f : height * 0.2f;
2030 if (fp.Defined == 0)
2035 int chinX = (int)(fp.Pos[0] * width);
2036 int chinY = (int)(fp.Pos[1] * height);
2038 string[] message = {
"Face analysis and face",
"recognition are available",
"only in frontal pose,",
"and when face is not",
"too small in the image." };
2041 for (
int i = 0; i < message.Count(); ++i)
2043 gl.DrawText(chinX, chinY + (-i - 2) * (
int)(size * FONT_FACTOR), 1.0f, 0.0f, 0.0f,
"Arial", (
int)(size * FONT_FACTOR), message[i]);
2047 gl.MatrixMode(OpenGL.GL_PROJECTION);
2050 gl.MatrixMode(OpenGL.GL_MODELVIEW);
2054 FeaturePoint ChooseLocation(
FaceData trackingData,
int width,
int height,
float backgroundSize)
2064 int temporaryPosition = 0;
2065 float backgroundHeight = (width > height) ? width * backgroundSize : height * backgroundSize;
2066 float backgroundWidth = BCKG_LENGTH_PERC * ((width > height) ? width : height);
2068 if (up.Pos[1] * height - backgroundHeight < height)
2070 if (currentPosition == (
int)DISPLAY_POS.UP)
2075 temporaryPosition = (int)DISPLAY_POS.UP;
2080 if (right.Pos[0] * width > backgroundWidth)
2082 right.Pos[0] = right.Pos[0] - BCKG_LENGTH_PERC;
2084 if (currentPosition == (
int)DISPLAY_POS.RIGHT)
2089 temporaryPosition = (int)DISPLAY_POS.RIGHT;
2093 if ((left.Pos[0] * width + backgroundWidth) < width)
2095 if (currentPosition == (
int)DISPLAY_POS.LEFT)
2100 temporaryPosition = (int)DISPLAY_POS.LEFT;
2104 if ((down.Pos[1] * height > backgroundHeight) && ((down.Pos[0] * width + backgroundWidth) < width))
2106 currentPosition = (int)DISPLAY_POS.DOWN;
2110 currentPosition = temporaryPosition;
float[] FaceModelVertices
int FaceModelTriangleCount
float[] FaceModelTextureCoordsStatic
float[] GazeDirectionGlobal