OpenCV for Ios 學習筆記(9)-用OPENGL渲染AR場景2

本文原始地址:OpenCV for Ios 學習筆記(9)-用OPENGL渲染AR場景2


drawFrame的操作步驟是:

1.清除場景。

2.啓動正射投影繪製背景。

3.在視口繪製最後一個從相機獲取到的圖像。

4.根據相機內在參數設置透視投影。

5.把每個偵測到的標記的座標系移動到標記的3維位置(把4x4的變換矩陣應用到opengl的模型矩陣上)。

6.呈現一個任意的3維物體。

7.展示幀緩存。


 我們將會在幀準備好時調用drawFrame - 當一個新的相機幀被上傳到視頻內存中,並且標記監測的步驟已經完成。

   下面的就是drawFrame的代碼:

  1. - (void)drawFrame  
  2. {  
  3.     //啓動激活的幀緩存  
  4.     [m_glview setFramebuffer];  
  5.       
  6.     // 繪製背景  
  7.     [self drawBackground];  
  8.       
  9.     // 在檢測到的標記處繪製3維物體  
  10.     [self drawAR];  
  11.       
  12.     // 呈現幀緩存  
  13.     bool ok = [m_glview presentFramebuffer];  
  14.       
  15.     int glErCode = glGetError();  
  16.     if (!ok || glErCode != GL_NO_ERROR)  
  17.     {  
  18.         std::cerr << "opengl 檢測出錯,錯誤代碼是:" << glErCode << std::endl;  
  19.     }  
  20. }  

繪製一個背景對我們來說相當容易。我們啓用正視投影並繪製一個全屏的當前幀的圖像紋理。下面是opengl es1的代碼:

  1. - (void) drawBackground  
  2. {  
  3.     //獲取視口的寬高  
  4.     GLfloat w = m_glview.bounds.size.width;  
  5.     GLfloat h = m_glview.bounds.size.height;  
  6.       
  7.     //四個頂點座標  
  8.     const GLfloat squareVertices[] =  
  9.     {  
  10.         0, 0,  
  11.         w, 0,  
  12.         0, h,  
  13.         w, h  
  14.     };  
  15.       
  16.     //紋理頂點  
  17.      static const GLfloat textureVertices[] =  
  18.      {  
  19.      1, 0,  
  20.      1, 1,  
  21.      0, 0,  
  22.      0, 1  
  23.      };  
  24.       
  25.     //正視矩陣  
  26.     static const GLfloat proj[] =  
  27.     {  
  28.         0, -2.f/w, 0, 0,  
  29.         -2.f/h, 0, 0, 0,  
  30.         0, 0, 1, 0,  
  31.         1, 1, 0, 1  
  32.     };  
  33.     /* 
  34.      glMatrixMode - 指定哪一個矩陣是當前矩陣 
  35.       
  36.      mode 指定哪一個矩陣堆棧是下一個矩陣操作的目標,可選值: GL_MODELVIEW、GL_PROJECTION、GL_TEXTURE. 
  37.      說明 
  38.      glMatrixMode設置當前矩陣模式: 
  39.      GL_MODELVIEW,對模型視景矩陣堆棧應用隨後的矩陣操作. 
  40.      GL_PROJECTION,對投影矩陣應用隨後的矩陣操作. 
  41.      GL_TEXTURE,對紋理矩陣堆棧應用隨後的矩陣操作. 
  42.      與glLoadIdentity()一同使用 
  43.      glLoadIdentity():該函數的功能是重置當前指定的矩陣爲單位矩陣。 
  44.      在glLoadIdentity()之後我們爲場景設置了透視圖。glMatrixMode(GL_MODELVIEW)設置當前矩陣爲模型視圖矩陣,模型視圖矩陣儲存了有關物體的信息。 
  45.      */  
  46.     glMatrixMode(GL_PROJECTION);  
  47.     glLoadMatrixf(proj);  
  48.       
  49.     glMatrixMode(GL_MODELVIEW);  
  50.     glLoadIdentity();  
  51.       
  52.     glDepthMask(FALSE);  
  53.     glDisable(GL_COLOR_MATERIAL);  
  54.       
  55.     glEnable(GL_TEXTURE_2D);  
  56.     glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId);  
  57.       
  58.     // 更新屬性值.  
  59.     glVertexPointer(2, GL_FLOAT, 0, squareVertices);  
  60.     glEnableClientState(GL_VERTEX_ARRAY);  
  61.     glTexCoordPointer(2, GL_FLOAT, 0, textureVertices);  
  62.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
  63.       
  64.     glColor4f(1,1,1,1);  
  65.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
  66.       
  67.     glDisableClientState(GL_VERTEX_ARRAY);  
  68.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);  
  69.     glDisable(GL_TEXTURE_2D);  
  70. }  


在場景中渲染虛擬物體是很機靈的事。首先我們需要根據相機內在參數,適應OpenGL投影矩陣。如果沒有這步,我們將得到錯誤的透射投影,這會使虛擬的物體看起來不那麼自然,好像“飄在空中”,沒有真實感!正確的投影是所有增強現實應用的必備。

下面是根據相機內參創建OpenGL投影矩陣的一片代碼:

  1. - (void)buildProjectionMatrix:(Matrix33)cameraMatrix: (int)screen_width: (int)screen_height: (Matrix44&) projectionMatrix  
  2. {  
  3.     float near = 0.01;  // 近裁剪距離  
  4.     float far  = 100;  // 遠裁剪距離  
  5.       
  6.     // 相機的參數  
  7.     float f_x = cameraMatrix.data[0]; // Focal length in x axis  
  8.     float f_y = cameraMatrix.data[4]; // Focal length in y axis (usually the same?)  
  9.     float c_x = cameraMatrix.data[2]; // Camera primary point x  
  10.     float c_y = cameraMatrix.data[5]; // Camera primary point y  
  11.       
  12.     projectionMatrix.data[0] = - 2.0 * f_x / screen_width;  
  13.     projectionMatrix.data[1] = 0.0;  
  14.     projectionMatrix.data[2] = 0.0;  
  15.     projectionMatrix.data[3] = 0.0;  
  16.       
  17.     projectionMatrix.data[4] = 0.0;  
  18.     projectionMatrix.data[5] = 2.0 * f_y / screen_height;  
  19.     projectionMatrix.data[6] = 0.0;  
  20.     projectionMatrix.data[7] = 0.0;  
  21.       
  22.     projectionMatrix.data[8] = 2.0 * c_x / screen_width - 1.0;  
  23.     projectionMatrix.data[9] = 2.0 * c_y / screen_height - 1.0;  
  24.     projectionMatrix.data[10] = -( far+near ) / ( far - near );  
  25.     projectionMatrix.data[11] = -1.0;  
  26.       
  27.     projectionMatrix.data[12] = 0.0;  
  28.     projectionMatrix.data[13] = 0.0;  
  29.     projectionMatrix.data[14] = -2.0 * far * near / ( far - near );  
  30.     projectionMatrix.data[15] = 0.0;  
  31. }  
在我們載入這個矩陣到OpenGL管道後,接下來我們繪製物體。

任何一個變換都能夠被4x4矩陣呈現並且載入到OpenGL模型視圖矩陣,這一步將會把座標系移動到世界座標系中的標記處。

例如,讓我們在每個標記的上方繪製一個座標軸-它將會展示標記的在空間中的方向,並用漸變的矩形填充整個標記

。這個視覺化操作將會像預期一樣給我們視覺上反饋。

下面是drawAR函數的實現:

  1. - (void) drawAR  
  2. {  
  3.     Matrix44 projectionMatrix;  
  4.     //相機的內參 屏幕的寬高 待輸出的投影矩陣  
  5.     [self buildProjectionMatrix:m_calibration.getIntrinsic():m_frameSize.width :m_frameSize.height :projectionMatrix];  
  6.       
  7.     glMatrixMode(GL_PROJECTION);  
  8.     glLoadMatrixf(projectionMatrix.data);  
  9.       
  10.     glMatrixMode(GL_MODELVIEW);  
  11.     glLoadIdentity();  
  12.       
  13.     glDepthMask(TRUE);  
  14.     glEnable(GL_DEPTH_TEST);  
  15.     //glDepthFunc(GL_LESS);  
  16.     //glDepthFunc(GL_GREATER);  
  17.       
  18.     glEnableClientState(GL_VERTEX_ARRAY);  
  19.     glEnableClientState(GL_NORMAL_ARRAY);  
  20.       
  21.     glPushMatrix();  
  22.     glLineWidth(3.0f);  
  23.       
  24.     //三條軸  
  25.     float lineX[] = {0,0,0,1,0,0};  
  26.     float lineY[] = {0,0,0,0,1,0};  
  27.     float lineZ[] = {0,0,0,0,0,1};  
  28.       
  29.     const GLfloat squareVertices[] = {  
  30.         -0.5f, -0.5f,  
  31.         0.5f,  -0.5f,  
  32.         -0.5f,  0.5f,  
  33.         0.5f,   0.5f,  
  34.     };  
  35.     const GLubyte squareColors[] = {  
  36.         255, 255,   0, 255,  
  37.         0,   255, 255, 255,  
  38.         0,     0,   0,   0,  
  39.         255,   0, 255, 255,  
  40.     };  
  41.       
  42.     //遍歷標記變換  
  43.     for (size_t transformationIndex=0; transformationIndex<m_transformations.size(); transformationIndex++)  
  44.     {  
  45.         const Transformation& transformation = m_transformations[transformationIndex];  
  46.           
  47.         Matrix44 glMatrix = transformation.getMat44();  
  48.           
  49.         glLoadMatrixf(reinterpret_cast<const GLfloat*>(&glMatrix.data[0]));  
  50.           
  51.         // 繪製數據  
  52.         glVertexPointer(2, GL_FLOAT, 0, squareVertices);  
  53.         glEnableClientState(GL_VERTEX_ARRAY);  
  54.         glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);  
  55.         glEnableClientState(GL_COLOR_ARRAY);  
  56.           
  57.         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
  58.         glDisableClientState(GL_COLOR_ARRAY);  
  59.           
  60.         float scale = 0.5;  
  61.         glScalef(scale, scale, scale);  
  62.           
  63.         glTranslatef(0, 0, 0.1f);  
  64.           
  65.         glColor4f(1.0f, 0.0f, 0.0f, 1.0f);  
  66.         glVertexPointer(3, GL_FLOAT, 0, lineX);  
  67.         glDrawArrays(GL_LINES, 0, 2);  
  68.           
  69.         glColor4f(0.0f, 1.0f, 0.0f, 1.0f);  
  70.         glVertexPointer(3, GL_FLOAT, 0, lineY);  
  71.         glDrawArrays(GL_LINES, 0, 2);  
  72.           
  73.         glColor4f(0.0f, 0.0f, 1.0f, 1.0f);  
  74.         glVertexPointer(3, GL_FLOAT, 0, lineZ);  
  75.         glDrawArrays(GL_LINES, 0, 2);  
  76.     }  
  77.       
  78.     glPopMatrix();  
  79.     glDisableClientState(GL_VERTEX_ARRAY);  
  80. }  


作爲一個例子的結尾,我們將演示我們的成果並總結。

OpenCV for Ios 學習筆記(10)-標記檢測總結

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章