FaceTracker2
OpenGLWndFaceTracker.cpp
1 // OpenGLWnd.cpp : implementation file
2 //
3 
4 #include "stdafx.h"
5 
6 #include "OpenGLWndFaceTracker.h"
7 #include "MainFrm.h"
8 #include <process.h>
9 
10 #include <assert.h>
11 #include <math.h>
12 #include <stdlib.h>
13 
14 #include <sys/timeb.h>
15 #include <sys/types.h>
16 
17 #include <iostream>
18 #include <fstream>
19 
20 
21 #ifdef _DEBUG
22 #define new DEBUG_NEW
23 #undef THIS_FILE
24 static char THIS_FILE[] = __FILE__;
25 #endif
26 
27 #define ANIM_VIEW_SIZE 0.40f
28 #define SLIDER_RESOLUTION 200
29 
30 
31 /////////////////////////////////////////////////////////////////////////////
32 // COpenGLWnd
33 
34 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
35 PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
36 
37 /**
38 * This function is used to render texture on screen.
39 */
41 {
42  static CMainFrame* pMainWnd = 0;
43  struct _timeb timebuffer;
44 
45  _ftime( &timebuffer );
46 
47  long current_time = 1000 * (long)timebuffer.time + timebuffer.millitm; //CHANGED BY I.K. added explicit cast
48  static long last_times[50];
49  static int cnt = -1;
50 
51 
52  if(rendererBusy) // a simple mechanism to make sure that we do not draw again before last rendering is done
53  {
54  return;
55  }
56  rendererBusy = true;
57 
58  SetContext();
59 
60 
61 
62  if(cnt == -1)
63  {
64  cnt = 0;
65  for(int i=0;i<10;i++)
66  last_times[i]=0;
67  }
68  cnt++;
69  if(cnt == 50) cnt = 0;
70 
71  renderingFrameRate = 50000.0f / (float)(current_time - last_times[cnt]);
72 
73  last_times[cnt] = current_time;
74 
75  glMatrixMode(GL_MODELVIEW);
76  if(modelViewMatrix)
77  {
78  glLoadMatrixf(modelViewMatrix);
79  }
80  else
81  {
82  glLoadIdentity();
83 
84  //glTranslatef(mXTranslate, mYTranslate, mZTranslate);
85  glRotatef(mXRotAngle, 1.0f, 0.0f, 0.0f);
86  glRotatef(mYRotAngle, 0.0f, 1.0f, 0.0f);
87  }
88 
89  SwapGLBuffers();
90 
91  rendererBusy = false;
92 
93 /* // display the frame rate
94  if(!pMainWnd) pMainWnd = (CMainFrame*)AfxGetMainWnd();
95  char tmpmsg[100];
96  sprintf(tmpmsg,"%2.2f FPS",renderingFrameRate);
97  pMainWnd->m_wndStatusBar.SetPaneText( 0, tmpmsg, TRUE );*/
98 }
99 
100 /**
101 * Close down the renderer.
102 * Tidy up and close the rendering system.
103 */
105 {
106  // Kill any pending timers.
107  KillTimer(ID_ROTATE);
108 
109  SetContext();
110 
111  mXAccel = mYAccel = mZAccel = 0;
112 
113  mXTranslate = 0;
114  mYTranslate = 0;
115  mZTranslate = 0;
116 
117  mXRotAngle = 0;
118  mYRotAngle = 0;
119 
120  SwapGLBuffers();
121 }
122 
123 IMPLEMENT_DYNCREATE(COpenGLWnd, CView)
124 
126 
127 : otvoren_dialog(false)
128 
129 {
130  m_bTimerSet = false;
131  m_pDC = NULL;
132  m_bSwap = true;
133  bgtexture[0] = 0;
134  insttexture[0] = 0;
135 
136  mXTranslate = 0;
137  mYTranslate = 0;
138  mZTranslate = 0;
139 
140  mXRotAngle = 0;
141  mYRotAngle = 0;
142  // mZRotAngle = 0;
143 
144  mVGLR_PERSPECT_FOVY = 45.0f;
145  mVGLR_PERSPECT_TAN_FOVY = tan(mVGLR_PERSPECT_FOVY / 2.0);
146  mVGLR_PERSPECT_NEAR_PLANE = 0.1f;
147  mFarPlane = 100.0f;
148 
149  mBackfaceCull = true;
150 
151  modelViewMatrix = 0;
152 
153  trackerActive=false;
154 
155  InitializeCriticalSection(&RenderingSection);
156 }
157 
158 COpenGLWnd::~COpenGLWnd()
159 {
160  DeleteCriticalSection(&RenderingSection);
161 
162  wglMakeCurrent(0,0);
163  wglDeleteContext(m_hRC);
164  wglDeleteContext(m_hRC1);
165  if( m_pDC )
166  {
167  delete m_pDC;
168  }
169 }
170 
171 // Standard OpenGL Init Stuff
172 BOOL COpenGLWnd::SetupPixelFormat()
173 {
174 
175  PIXELFORMATDESCRIPTOR pfdMain;
176 
177  memset( &pfdMain , 0 , sizeof( PIXELFORMATDESCRIPTOR ) );
178  pfdMain.nSize = sizeof( PIXELFORMATDESCRIPTOR );
179  pfdMain.nVersion = 1;
180  pfdMain.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
181  pfdMain.iPixelType = PFD_TYPE_RGBA;
182 
183  // Set to 16 for performance reasons.
184  pfdMain.cColorBits = 16;
185  pfdMain.cRedBits = 8;
186  pfdMain.cRedShift = 16;
187  pfdMain.cGreenBits = 8;
188  pfdMain.cGreenShift = 8;
189  pfdMain.cBlueBits = 8;
190  pfdMain.cBlueShift = 0;
191  pfdMain.cAlphaBits = 0;
192  pfdMain.cAlphaShift = 0;
193  pfdMain.cAccumBits = 64;
194  pfdMain.cAccumRedBits = 16;
195  pfdMain.cAccumGreenBits = 16;
196  pfdMain.cAccumBlueBits = 16;
197  pfdMain.cAccumAlphaBits = 0;
198  pfdMain.cDepthBits = 32;
199  pfdMain.cStencilBits = 8;
200  pfdMain.cAuxBuffers = 0;
201  pfdMain.iLayerType = PFD_MAIN_PLANE;
202  pfdMain.bReserved = 0;
203  pfdMain.dwLayerMask = 0;
204  pfdMain.dwVisibleMask = 0;
205  pfdMain.dwDamageMask = 0;
206 
207  // int m_nPixelFormat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), &pfdMain );
208  int m_nPixelFormat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), &pfdMain );
209 
210  if ( m_nPixelFormat == 0 )
211  return FALSE;
212 
213  // return ::SetPixelFormat( m_pDC->GetSafeHdc(), m_nPixelFormat, &pfdMain );
214  return ::SetPixelFormat( m_pDC->GetSafeHdc(), m_nPixelFormat, &pfdMain );
215 }
216 
217 /**
218 * Initialize OpenGL.
219 * Perform necessary OpenGL initializations.
220 */
222 {
223  static int init = 0; //CHANGED BY I.K. added type specifier
224 
225  if(init) return true;
226  init = 1;
227 
228  //Get a DC for the Client Area
229  if (m_pDC != NULL) delete m_pDC;
230  m_pDC = new CClientDC(this);
231 
232  //Failure to Get DC
233  if( m_pDC == NULL )
234  return FALSE;
235 
236  if( !SetupPixelFormat() )
237  return FALSE;
238 
239  m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() );
240  m_hRC1 = ::wglCreateContext( m_pDC->GetSafeHdc() );
241 
242  //Failure to Create Rendering Context
243  if(m_hRC == 0 )
244  return FALSE;
245  if(m_hRC1 == 0 )
246  return FALSE;
247 
248  //Make the RC Current
249  if( ::wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC) == FALSE )
250  return FALSE;
251 
252  // REMOVE
253  if( ::wglMakeCurrent( NULL, NULL ) == FALSE )
254  return FALSE;
255 
256  rendererBusy = false;
257 
258 // StartRenderingTimer();
259 
260 
261  return TRUE;
262 }
263 
264 BEGIN_MESSAGE_MAP(COpenGLWnd, CView)
265  //{{AFX_MSG_MAP(COpenGLWnd)
266  ON_WM_CREATE()
267  ON_WM_ERASEBKGND()
268  ON_WM_SIZE()
269  ON_WM_LBUTTONDOWN()
270  ON_WM_RBUTTONDOWN()
271  ON_WM_KEYDOWN()
272  ON_WM_LBUTTONUP()
273  ON_WM_MOUSEMOVE()
274  ON_WM_RBUTTONUP()
275  ON_WM_TIMER()
276 
277  //}}AFX_MSG_MAP
278 END_MESSAGE_MAP()
279 
280 /////////////////////////////////////////////////////////////////////////////
281 // COpenGLWnd drawing
282 
283 void COpenGLWnd::OnDraw(CDC* pDC)
284 {
285  if(!trackerActive) //disable OpenGL calls during tracking so they do not disturb tracking
286  RenderToScreen();
287 }
288 
289 /////////////////////////////////////////////////////////////////////////////
290 // COpenGLWnd diagnostics
291 
292 #ifdef _DEBUG
293 void COpenGLWnd::AssertValid() const
294 {
295  CView::AssertValid();
296 }
297 
298 void COpenGLWnd::Dump(CDumpContext& dc) const
299 {
300  CView::Dump(dc);
301 }
302 #endif //_DEBUG
303 
304 /////////////////////////////////////////////////////////////////////////////
305 // COpenGLWnd message handlers
306 
307 /**
308 * Initialize OpenGL when window is created.
309 *
310 */
311 int COpenGLWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
312 {
313  if (CView::OnCreate(lpCreateStruct) == -1)
314  return -1;
315 
316  if ( !InitOpenGL() )
317  {
318  MessageBox( "Error setting up OpenGL!", "Init Error!",
319  MB_OK | MB_ICONERROR );
320  return -1;
321  }
322 
323  return 0;
324 }
325 
326 // Override the errase background function to
327 // do nothing to prevent flashing.
328 BOOL COpenGLWnd::OnEraseBkgnd(CDC* pDC)
329 {
330  return true;
331 }
332 
333 // Set a few flags to make sure OpenGL only renders
334 // in its viewport.
335 BOOL COpenGLWnd::PreCreateWindow(CREATESTRUCT& cs)
336 {
337  cs.style |= (WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC);
338 
339  cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS,
340  ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
341 
342  return CView::PreCreateWindow(cs);
343 }
344 
345 void COpenGLWnd::OnSize(UINT nType, int cx, int cy)
346 {
347  CView::OnSize(nType, cx, cy);
348 
349  if ( 0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED )
350  return;
351 
352  EnterCriticalSection(&RenderingSection);
353 
354  SetContext();
355 
356  ReSizeGLScene(cx, cy);
357 
358  LeaveCriticalSection(&RenderingSection);
359 }
360 /** Pre-render setup function.
361 * This function is called before the rendering in each rendering cycle. It sets up the GL context and clears the screen
362 * to prepare for the next frame to be rendered.
363 *
364 * This is a good place to insert the rendering of other objects in the scene,
365 * background image or similar.
366 */
368 {
369  // set the context
370  wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
371 
372 
373  // clear the frame buffer before next drawing
374  if(!trackerActive) // if tracker is active, do not clear screen
375  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
376 
377  // INSERT CODE HERE TO RENDER ANY ADDITIONAL OBJECTS, BACKGROUND IMAGE OR SIMILAR
379 }
380 
381 void COpenGLWnd::SwapGLBuffers()
382 {
383  if(trackerActive) // if tracker is active, do not swap buffers
384  return;
385  SwapBuffers(m_pDC->GetSafeHdc());
386  wglMakeCurrent(NULL, NULL);
387 }
388 
389 int COpenGLWnd::LoadTexture(char *fullPath)
390 {
391  FILE *File=NULL;
392  File=fopen(fullPath,"r");
393 
394  if(!File)
395  {
396  return -1;
397  }
398  fclose(File);
399 
400  // Texture varible, return
401  GLuint texture[1];
402 
403  IplImage* img = cvLoadImage(fullPath);
404 
405  cvFlip(img);
406 
407  glGenTextures(1, &texture[0]);
408 
409  texture[0] += 20; //leave some texture names for the tracker because it uses names starting from 1
410 
411  // Typical Texture Generation Using Data From The Bitmap
412  glBindTexture(GL_TEXTURE_2D, texture[0]);
413 
414  // Load texture into GL.
415  int maxSize = 0;
416  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
417 
418  // Create a texture of nearest power 2
419  int xPowSize = 1;
420  int yPowSize = 1;
421  while((img->width / xPowSize) > 0) xPowSize <<= 1;
422  while((img->height / yPowSize) > 0) yPowSize <<= 1;
423 
424  // So we're not overriding gfx-cards capability
425  if(xPowSize > maxSize) xPowSize = maxSize;
426  if(yPowSize > maxSize) yPowSize = maxSize;
427 
428  // Allocate memory to the scaled image.
429  GLubyte* tempScaleImage = new GLubyte[yPowSize * xPowSize * 3];
430 
431  // Scale original image to nearest lower power of 2.
432  gluScaleImage(GL_RGB, img->width, img->height, GL_UNSIGNED_BYTE, img->imageData, xPowSize, yPowSize, GL_UNSIGNED_BYTE, tempScaleImage);
433 
434  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xPowSize, yPowSize, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, tempScaleImage);
435 
436  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
437  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
438 
439  delete [] tempScaleImage;
440  cvReleaseImage(&img);
441 
442  wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
443  wglSwapIntervalEXT(0);
444 
445  return texture[0];
446 }
447 
448 /**
449 * Draw the background image on the screen.
450 * Load the background image from the file background.jpg the first time this function is called.
451 * If loading is succesful, the image is drawn on the screen every time the function is called.
452 * This function is called by SetContext(), so it is executed at the beginning of every frame.
453 *
454 * @return true if image is succesfully drawn, 0 otherwise
455 * @see SetContext()
456 */
458 {
459  // draw the background image, if any
460  static int noImage = 0;
461 
462  if(noImage)
463  return false;
464 
465  if(!bgtexture[0])
466  {
467  bgtexture[0] = LoadTexture("background.jpg");
468  if(bgtexture[0] == -1)
469  {
470  bgtexture[0] = LoadTexture("../OpenGL/data/background.jpg");
471  }
472  if(bgtexture[0] == -1)
473  {
474  noImage=1;
475  return false;
476  }
477  }
478 
479  int animViewWidth,animViewHeight;
480 
481  animViewWidth = (int)(ANIM_VIEW_SIZE*mWindowWidth);
482  animViewHeight = (animViewWidth * 3) / 4; // 3/4 aspect ratio
483  if(animViewHeight > mWindowHeight)
484  animViewHeight = mWindowHeight;
485 
486  animViewWidth = mWindowWidth;
487  animViewHeight = mWindowHeight;
488 
489 
490  glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_VIEWPORT_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_STENCIL_BUFFER_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT );
491  glDisable(GL_ALPHA_TEST);
492  glDisable(GL_DEPTH_TEST);
493  glDisable(GL_BLEND );
494  glDisable(GL_DITHER);
495  glDisable(GL_FOG);
496  glDisable(GL_SCISSOR_TEST);
497  glDisable(GL_STENCIL_TEST);
498  glDisable(GL_LIGHTING);
499  glDisable(GL_LIGHT0);
500  glEnable(GL_TEXTURE_2D);
501  glBindTexture(GL_TEXTURE_2D, bgtexture[0]); // Select Our Texture
502 
503  glMatrixMode(GL_MODELVIEW);
504  glPushMatrix();
505  glLoadIdentity(); // Reset The Projection Matrix
506 
507 
508  glMatrixMode(GL_PROJECTION);
509  glPushMatrix();
510  glLoadIdentity(); // Reset The Projection Matrix
511  glOrtho(0.0f,1.0f,0.0f,animViewHeight/(float)animViewWidth,-10.0f,10.0f);
512 
513  const float W = 1.0f;
514  const float H = 0.25f;
515  const float X = 0.0f;
516  float Y = (animViewHeight/(float)animViewWidth - H) / 2.0f;
517  const float Z = -5.0f;
518 
519  GLfloat vertices[] = {
520  X, Y, Z,
521  X + W, Y, Z,
522  X + W, Y + H, Z,
523  X, Y + H, Z
524  };
525 
526  GLfloat vertices2[] = {
527  0.0f, 0.0f, Z,
528  1.0f, 0.0f, Z,
529  1.0f, 1.0f, Z,
530  0.0f, 1.0f, Z
531  };
532 
533  static GLfloat tex_coords[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
534 
535  glEnableClientState(GL_VERTEX_ARRAY);
536 
537  glVertexPointer(3, GL_FLOAT, 0, vertices2);
538  glDrawArrays(GL_QUADS, 0, 4);
539 
540 
541  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
542 
543  glVertexPointer(3, GL_FLOAT, 0, vertices);
544  glTexCoordPointer(2, GL_FLOAT, 0, tex_coords);
545 
546  glDrawArrays(GL_QUADS, 0, 4);
547 
548  glDisableClientState(GL_VERTEX_ARRAY);
549  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
550 
551  glPopMatrix();
552  glMatrixMode(GL_MODELVIEW);
553  glPopMatrix();
554 
555  glBindTexture(GL_TEXTURE_2D, 0);
556  glPopAttrib();
557  glClear(GL_DEPTH_BUFFER_BIT);
558 
559 
560  return TRUE;
561 
562 
563 }
564 
565 bool COpenGLWnd::DrawInstructionsAndInitStatus(bool face_detected,float yaw)
566 {
567  // draw the instructions image, if any
568  static int noImage = 0;
569 
570  if(noImage)
571  return false;
572 
573  if(!insttexture[0])
574  {
575  insttexture[0] = LoadTexture("InitializationInstructions.jpg");
576  if(insttexture[0] == -1)
577  {
578  noImage=1;
579  return false;
580  }
581  }
582 
583 
584  glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_VIEWPORT_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_STENCIL_BUFFER_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT );
585  glDisable(GL_ALPHA_TEST);
586  glDisable(GL_DEPTH_TEST);
587  glDisable(GL_BLEND );
588  glDisable(GL_DITHER);
589  glDisable(GL_FOG);
590  glDisable(GL_SCISSOR_TEST);
591  glDisable(GL_STENCIL_TEST);
592  glDisable(GL_LIGHTING);
593  glDisable(GL_LIGHT0);
594  glEnable(GL_TEXTURE_2D);
595  glBindTexture(GL_TEXTURE_2D, insttexture[0]); // Select Our Texture
596 
597  glMatrixMode(GL_MODELVIEW);
598  glPushMatrix();
599  glLoadIdentity(); // Reset The Projection Matrix
600 
601 
602  glMatrixMode(GL_PROJECTION);
603  glPushMatrix();
604  glLoadIdentity(); // Reset The Projection Matrix
605  glOrtho(0.0f,1.0f,0.0f,1.0f,-10.0f,10.0f);
606 
607  glColor3ub(255,255,255);
608 
609  glBegin(GL_QUADS);
610  glTexCoord2f(0.0f,0.0f);
611  glVertex3f(0.0f,0.0f,-5.0f);
612 
613  glTexCoord2f(1.0f,0.0f);
614  glVertex3f(1.0f,0.0f,-5.0f);
615 
616  glTexCoord2f(1.0f,1.0f);
617  glVertex3f(1.0f,0.25f,-5.0f);
618 
619  glTexCoord2f(0.0f,1.0f);
620  glVertex3f(0.0f,0.25f,-5.0f);
621 
622  glEnd();
623 
624  glBindTexture(GL_TEXTURE_2D, 0);
625  glPopAttrib();
626 
627  if(face_detected)
628  {
629  //draw yaw indicator
630  glColor3ub(255,0,0);
631  glBegin(GL_QUADS);
632  glVertex3f(0.5f,0.25f,-5.0f);
633  glVertex3f(0.5f-30.0f*yaw,0.25f,-5.0f);
634  glVertex3f(0.5f-30.0f*yaw,0.3f,-5.0f);
635  glVertex3f(0.5f,0.3f,-5.0f);
636 
637  //draw midline of the yaw indicator
638  glColor3ub(0,255,0);
639  glBegin(GL_QUADS);
640  glVertex3f(0.5f-0.01f,0.25f,-5.0f);
641  glVertex3f(0.5f+0.01f,0.25f,-5.0f);
642  glVertex3f(0.5f+0.01f,0.3f,-5.0f);
643  glVertex3f(0.5f-0.01f,0.3f,-5.0f);
644 
645  glEnd();
646  //if(yaw > 0.0f)
647  // sprintf(tmpmsg,"------------| YAW %4.1f mm | ROLL %+5.1f degrees | Velocity %+5.1f mm/s",yaw*1000.0f,roll,velocity*1000.0f);
648  //else
649  // sprintf(tmpmsg," |------------ YAW %4.1f mm | ROLL %+5.1f degrees | Velocity %+5.1f mm/s",yaw*1000.0f,roll,velocity*1000.0f);
650  }
651 
652  glPopMatrix();
653  glMatrixMode(GL_MODELVIEW);
654  glPopMatrix();
655 
656 
657 
658  glClear(GL_DEPTH_BUFFER_BIT);
659 
660 
661  return TRUE;
662 
663 
664 }
665 
666 void COpenGLWnd::ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
667 {
668  if (height==0) // Prevent A Divide By Zero By
669  {
670  height=1; // Making Height Equal One
671  }
672 
673  mWindowWidth = width;
674  mWindowHeight = height;
675  int animViewPos,animViewWidth,animViewHeight;
676 
677  animViewPos = (int)((1-ANIM_VIEW_SIZE)*width);
678  animViewWidth = (int)(ANIM_VIEW_SIZE*width);
679  animViewHeight = (animViewWidth * 3) / 4; // 3/4 aspect ratio
680  if(animViewHeight > height)
681  animViewHeight = height;
682 
683  animViewPos = 0;
684  animViewWidth = width;
685  animViewHeight = height;
686 
687  glViewport(animViewPos,0,animViewWidth,animViewHeight); // Reset The Current Viewport
688 
689  glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
690  glLoadIdentity(); // Reset The Projection Matrix
691 
692 // gluPerspective(mVGLR_PERSPECT_FOVY, (GLfloat)animViewWidth/(GLfloat)animViewHeight, mVGLR_PERSPECT_NEAR_PLANE, mFarPlane);
693  float f = 3.0f;
694  GLfloat x_offset = 1;
695  GLfloat y_offset = 1;
696  width = animViewWidth;
697  height = animViewHeight;
698  if (width > height)
699  x_offset = ((GLfloat)width)/((GLfloat)height);
700  else if (width < height)
701  y_offset = ((GLfloat)height)/((GLfloat)width);
702  //set frustum specs
703  GLfloat frustum_near = mVGLR_PERSPECT_NEAR_PLANE;
704  GLfloat frustum_far = mFarPlane;
705  GLfloat frustum_x = x_offset*frustum_near/f;
706  GLfloat frustum_y = y_offset*frustum_near/f;
707  //set frustum
708  glMatrixMode(GL_PROJECTION);
709  glLoadIdentity();
710  glFrustum(-frustum_x,frustum_x,-frustum_y,frustum_y,frustum_near,frustum_far);
711  glMatrixMode(GL_MODELVIEW);
712  //clear matrix
713  glLoadIdentity();
714  //camera in (0,0,0) looking at (0,0,1) up vector (0,1,0)
715  gluLookAt(0,0,0,0,0,1,0,1,0);
716 
717  glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
718 }
719 
720 void COpenGLWnd::displayMessage(char *userMessage,char *caption,int type)
721 {
722  MessageBox(userMessage,caption,type);
723 }
724 
726 {
727  // set the context
728  wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC1);
729  glViewport(0,0,mWindowWidth,mWindowHeight); // Reset The Current Viewport
730 
731  // clear the frame buffer before next drawing
732  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
733 
734  //verify color depth - FaceTracker needs 8 bits for each color
735  static bool verified = false; // verify only once
736  if(!verified)
737  {
738  verified = true;
739  GLint redBits=0, greenBits=0, blueBits=0;
740 
741  glGetIntegerv (GL_RED_BITS, &redBits);
742  glGetIntegerv (GL_GREEN_BITS, &greenBits);
743  glGetIntegerv (GL_BLUE_BITS, &blueBits);
744 
745  if(redBits < 8 || greenBits < 8 || blueBits < 8)
746  {
747  MessageBox("The color depth of your graphics system is too low. FaceTracker2 requires 32 bits per pixel (True Color) for correct operation. Please change your graphics settings to True Color or 32 bit color depth.","Error",MB_ICONERROR);
748  exit(0);
749  }
750  }
751 }
752 
754 {
755  SwapBuffers(m_pDC->GetSafeHdc());
756  wglMakeCurrent(NULL, NULL);
757 }
758 
759 void COpenGLWnd::checkOpenGLSize(unsigned int &width, unsigned int &height)
760 {
761  if(mWindowWidth < (int)width || mWindowHeight < (int)height)
762  {
763  //MessageBox("The application window is too small for tracking and this may result in degraded tracking performance. It is recommended to restart the tracker and increase the application window size to full screen; alternatively, this warning message can be avoided by decreasing the max_init_image_width and max_work_image_width parameters in the tracker configuration file.","Warning",MB_ICONWARNING);
764  }
765  width = mWindowWidth;
766  height = mWindowHeight;
767 }
768 
769 
770 int picker_coordinates_array[10];
771 int picker_coordinates_count;
772 int picker_cancel;
773 IplImage* tmpImage;
774 IplImage* textImage;
775 
776 CvFont font;
777 
778 
HGLRC m_hRC1
Rendering context for tracking.
void SetContext()
Pre-render setup function.
HGLRC m_hRC
Rendering context for drawing the 3D model.
The OpenGL-enabled window with rendering and basic interaction.
void ReSizeGLScene(GLsizei width, GLsizei height)
Initialise.
void RenderToScreen()
Render the model to screen.
bool DrawBackgroundImage()
Draw the background image on the screen.
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
Initialize OpenGL when window is created.
void displayMessage(char *userMessage, char *caption, int type)
Implementation of a virtual function from VisageSDK::TrackerGUIInterface.
void swapOpenGLBuffers()
This function is called by method displayResults when it needs to swap OpenGL buffers.
BOOL InitOpenGL()
Initialize OpenGL.
void Close()
Closes down and resets renderer.
float renderingFrameRate
Frame rate.
void setOpenGLContext()
This function is called by method displayResults when it needs to clear the OpenGL window and set the...
CRITICAL_SECTION RenderingSection
Critical section.