6 #include "OpenGLWndFaceTracker.h"
14 #include <sys/timeb.h>
15 #include <sys/types.h>
24 static char THIS_FILE[] = __FILE__;
27 #define ANIM_VIEW_SIZE 0.40f
28 #define SLIDER_RESOLUTION 200
34 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (
int interval);
35 PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
42 static CMainFrame* pMainWnd = 0;
43 struct _timeb timebuffer;
45 _ftime( &timebuffer );
47 long current_time = 1000 * (long)timebuffer.time + timebuffer.millitm;
48 static long last_times[50];
69 if(cnt == 50) cnt = 0;
73 last_times[cnt] = current_time;
75 glMatrixMode(GL_MODELVIEW);
78 glLoadMatrixf(modelViewMatrix);
85 glRotatef(mXRotAngle, 1.0f, 0.0f, 0.0f);
86 glRotatef(mYRotAngle, 0.0f, 1.0f, 0.0f);
107 KillTimer(ID_ROTATE);
111 mXAccel = mYAccel = mZAccel = 0;
127 : otvoren_dialog(false)
144 mVGLR_PERSPECT_FOVY = 45.0f;
145 mVGLR_PERSPECT_TAN_FOVY = tan(mVGLR_PERSPECT_FOVY / 2.0);
146 mVGLR_PERSPECT_NEAR_PLANE = 0.1f;
149 mBackfaceCull =
true;
155 InitializeCriticalSection(&RenderingSection);
158 COpenGLWnd::~COpenGLWnd()
163 wglDeleteContext(
m_hRC);
172 BOOL COpenGLWnd::SetupPixelFormat()
175 PIXELFORMATDESCRIPTOR pfdMain;
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;
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;
208 int m_nPixelFormat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), &pfdMain );
210 if ( m_nPixelFormat == 0 )
214 return ::SetPixelFormat( m_pDC->GetSafeHdc(), m_nPixelFormat, &pfdMain );
225 if(init)
return true;
229 if (m_pDC != NULL)
delete m_pDC;
230 m_pDC =
new CClientDC(
this);
236 if( !SetupPixelFormat() )
239 m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() );
240 m_hRC1 = ::wglCreateContext( m_pDC->GetSafeHdc() );
249 if( ::wglMakeCurrent( m_pDC->GetSafeHdc(),
m_hRC) == FALSE )
253 if( ::wglMakeCurrent( NULL, NULL ) == FALSE )
256 rendererBusy =
false;
293 void COpenGLWnd::AssertValid()
const
295 CView::AssertValid();
298 void COpenGLWnd::Dump(CDumpContext& dc)
const
313 if (CView::OnCreate(lpCreateStruct) == -1)
318 MessageBox(
"Error setting up OpenGL!",
"Init Error!",
319 MB_OK | MB_ICONERROR );
328 BOOL COpenGLWnd::OnEraseBkgnd(CDC* pDC)
335 BOOL COpenGLWnd::PreCreateWindow(CREATESTRUCT& cs)
337 cs.style |= (WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC);
339 cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS,
340 ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
342 return CView::PreCreateWindow(cs);
345 void COpenGLWnd::OnSize(UINT nType,
int cx,
int cy)
347 CView::OnSize(nType, cx, cy);
349 if ( 0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED )
370 wglMakeCurrent(m_pDC->GetSafeHdc(),
m_hRC);
375 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
381 void COpenGLWnd::SwapGLBuffers()
385 SwapBuffers(m_pDC->GetSafeHdc());
386 wglMakeCurrent(NULL, NULL);
389 int COpenGLWnd::LoadTexture(
char *fullPath)
392 File=fopen(fullPath,
"r");
403 IplImage* img = cvLoadImage(fullPath);
407 glGenTextures(1, &texture[0]);
412 glBindTexture(GL_TEXTURE_2D, texture[0]);
416 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
421 while((img->width / xPowSize) > 0) xPowSize <<= 1;
422 while((img->height / yPowSize) > 0) yPowSize <<= 1;
425 if(xPowSize > maxSize) xPowSize = maxSize;
426 if(yPowSize > maxSize) yPowSize = maxSize;
429 GLubyte* tempScaleImage =
new GLubyte[yPowSize * xPowSize * 3];
432 gluScaleImage(GL_RGB, img->width, img->height, GL_UNSIGNED_BYTE, img->imageData, xPowSize, yPowSize, GL_UNSIGNED_BYTE, tempScaleImage);
434 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xPowSize, yPowSize, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, tempScaleImage);
436 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
437 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
439 delete [] tempScaleImage;
440 cvReleaseImage(&img);
442 wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress(
"wglSwapIntervalEXT");
443 wglSwapIntervalEXT(0);
460 static int noImage = 0;
467 bgtexture[0] = LoadTexture(
"background.jpg");
468 if(bgtexture[0] == -1)
470 bgtexture[0] = LoadTexture(
"../OpenGL/data/background.jpg");
472 if(bgtexture[0] == -1)
479 int animViewWidth,animViewHeight;
481 animViewWidth = (int)(ANIM_VIEW_SIZE*mWindowWidth);
482 animViewHeight = (animViewWidth * 3) / 4;
483 if(animViewHeight > mWindowHeight)
484 animViewHeight = mWindowHeight;
486 animViewWidth = mWindowWidth;
487 animViewHeight = mWindowHeight;
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);
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]);
503 glMatrixMode(GL_MODELVIEW);
508 glMatrixMode(GL_PROJECTION);
511 glOrtho(0.0f,1.0f,0.0f,animViewHeight/(
float)animViewWidth,-10.0f,10.0f);
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;
519 GLfloat vertices[] = {
526 GLfloat vertices2[] = {
533 static GLfloat tex_coords[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
535 glEnableClientState(GL_VERTEX_ARRAY);
537 glVertexPointer(3, GL_FLOAT, 0, vertices2);
538 glDrawArrays(GL_QUADS, 0, 4);
541 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
543 glVertexPointer(3, GL_FLOAT, 0, vertices);
544 glTexCoordPointer(2, GL_FLOAT, 0, tex_coords);
546 glDrawArrays(GL_QUADS, 0, 4);
548 glDisableClientState(GL_VERTEX_ARRAY);
549 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
552 glMatrixMode(GL_MODELVIEW);
555 glBindTexture(GL_TEXTURE_2D, 0);
557 glClear(GL_DEPTH_BUFFER_BIT);
565 bool COpenGLWnd::DrawInstructionsAndInitStatus(
bool face_detected,
float yaw)
568 static int noImage = 0;
575 insttexture[0] = LoadTexture(
"InitializationInstructions.jpg");
576 if(insttexture[0] == -1)
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);
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]);
597 glMatrixMode(GL_MODELVIEW);
602 glMatrixMode(GL_PROJECTION);
605 glOrtho(0.0f,1.0f,0.0f,1.0f,-10.0f,10.0f);
607 glColor3ub(255,255,255);
610 glTexCoord2f(0.0f,0.0f);
611 glVertex3f(0.0f,0.0f,-5.0f);
613 glTexCoord2f(1.0f,0.0f);
614 glVertex3f(1.0f,0.0f,-5.0f);
616 glTexCoord2f(1.0f,1.0f);
617 glVertex3f(1.0f,0.25f,-5.0f);
619 glTexCoord2f(0.0f,1.0f);
620 glVertex3f(0.0f,0.25f,-5.0f);
624 glBindTexture(GL_TEXTURE_2D, 0);
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);
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);
653 glMatrixMode(GL_MODELVIEW);
658 glClear(GL_DEPTH_BUFFER_BIT);
673 mWindowWidth = width;
674 mWindowHeight = height;
675 int animViewPos,animViewWidth,animViewHeight;
677 animViewPos = (int)((1-ANIM_VIEW_SIZE)*width);
678 animViewWidth = (int)(ANIM_VIEW_SIZE*width);
679 animViewHeight = (animViewWidth * 3) / 4;
680 if(animViewHeight > height)
681 animViewHeight = height;
684 animViewWidth = width;
685 animViewHeight = height;
687 glViewport(animViewPos,0,animViewWidth,animViewHeight);
689 glMatrixMode(GL_PROJECTION);
694 GLfloat x_offset = 1;
695 GLfloat y_offset = 1;
696 width = animViewWidth;
697 height = animViewHeight;
699 x_offset = ((GLfloat)width)/((GLfloat)height);
700 else if (width < height)
701 y_offset = ((GLfloat)height)/((GLfloat)width);
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;
708 glMatrixMode(GL_PROJECTION);
710 glFrustum(-frustum_x,frustum_x,-frustum_y,frustum_y,frustum_near,frustum_far);
711 glMatrixMode(GL_MODELVIEW);
715 gluLookAt(0,0,0,0,0,1,0,1,0);
717 glMatrixMode(GL_MODELVIEW);
722 MessageBox(userMessage,caption,type);
728 wglMakeCurrent(m_pDC->GetSafeHdc(),
m_hRC1);
729 glViewport(0,0,mWindowWidth,mWindowHeight);
732 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
735 static bool verified =
false;
739 GLint redBits=0, greenBits=0, blueBits=0;
741 glGetIntegerv (GL_RED_BITS, &redBits);
742 glGetIntegerv (GL_GREEN_BITS, &greenBits);
743 glGetIntegerv (GL_BLUE_BITS, &blueBits);
745 if(redBits < 8 || greenBits < 8 || blueBits < 8)
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);
755 SwapBuffers(m_pDC->GetSafeHdc());
756 wglMakeCurrent(NULL, NULL);
759 void COpenGLWnd::checkOpenGLSize(
unsigned int &width,
unsigned int &height)
761 if(mWindowWidth < (
int)width || mWindowHeight < (
int)height)
765 width = mWindowWidth;
766 height = mWindowHeight;
770 int picker_coordinates_array[10];
771 int picker_coordinates_count;
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.