2013-05-05 62 views
0

我正在嘗試製作坦克遊戲。我已經成功加載了一個OBJ模型,並計算了原點模型的邊界框。將轉換應用於包圍盒

我現在試圖將我的模型在遊戲邏輯中完成的轉換應用到邊界框的原始座標。爲此,我在繪製模型之前抓取模型視圖矩陣,然後將這個矩陣乘以定義BBox的兩個向量。

這裏是吸引我的坦克代碼:

void drawTank() 
{ 
    bBox = calcBBox(modelo, 1); 

    glPushMatrix(); 
     glBindTexture(GL_TEXTURE_2D, texTank); 
     glScalef(0.2, 0.2, 0.2); 

     glTranslatef(posTank.x,posTank.y,posTank.z); 
     glRotatef(anguloTanque, 0, 1, 0); // rotate around Y (horizontal) 


     glRotatef(90, 0, 1, 0); 
     glRotatef(-90, 1, 0, 0); 
     glGetFloatv(GL_MODELVIEW_MATRIX, matrix); 
     glmDraw(modelo, GLM_TEXTURE | GLM_MATERIAL); 


     glColor3f(1,0,0); 
     drawBBox(bBox); 

    glPopMatrix(); 


} 

在這個片段中,我的BBOX正確繪製在坦克模型(變換是由glTranslate & glRotate功能渲染應用)。正如你所看到的,我也在這裏抓住我的ModelView矩陣。

然後我應用此矩陣,如下所示(這是我的整個顯示功能):

void Display(void) { 


    // Clear the window with current clearing color 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 


    glPushMatrix(); 
     camera(); 

     glEnable(GL_TEXTURE_2D); 

     //glTranslatef(0,-40,150); 

     //PLANE 
     glBindTexture(GL_TEXTURE_2D, texArena); 
      glBegin(GL_POLYGON); 
      glTexCoord2f(0.0f, 0.0f); 
      glVertex3f(-500, 0, -500); 
      glTexCoord2f(5.0f, 5.0f); 
      glVertex3f(500, 0, -500); 
      glTexCoord2f(5.0f, 0.0f); 
      glVertex3f(500, 0, 500); 
      glTexCoord2f(0.0f, 5.0f); 
      glVertex3f(-500, 0, 500); 
     glEnd(); 


     drawTank(); 

    glPopMatrix(); 



     point3D max = bBox.max; 
     point3D min = bBox.min; 
     point3D resultMax; 
     point3D resultMin; 
     //Transformacion 
     multVectorByMatrix(matrix, max, resultMax); 
     multVectorByMatrix(matrix, min, resultMin); 
     bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z; 
     bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z; 

     glPushMatrix(); 
      glColor3f(1,1,1); 
      drawBBox(bBox); 
     glPopMatrix(); 




    glFlush(); 

    glutSwapBuffers(); 

} 

其乘以矩陣的向量的函數:

void multVectorByMatrix(float* matrix, point3D vector, point3D &result) 
{ 
    result.x = (matrix[0] * vector.x) + 
       (matrix[4] * vector.y) + 
       (matrix[8] * vector.z) + 
       matrix[12]; 
    result.y = (matrix[1] * vector.x) + 
       (matrix[5] * vector.y) + 
       (matrix[9] * vector.z) + 
       matrix[13]; 
    result.z = (matrix[2] * vector.x) + 
       (matrix[6] * vector.y) + 
       (matrix[10] * vector.z) + 
       matrix[14]; 
} 

如果我畫邊界框與此渲染循環,然後我的邊界框被繪製,但轉換不適當地應用。我可以看到邊界框在翻譯中正確移動,但旋轉不正確。

這裏有什麼問題?

編輯:一些截圖 enter image description here enter image description here

+0

請問您可以添加一些截圖來澄清問題到底是什麼? – 2013-05-05 09:51:24

+0

我剛剛做到了。但很難看到。紅色的bbox是用opengl渲染變換函數繪製的,白色的是通過將bbox位置乘以變換矩陣來計算的。 – MichelReap 2013-05-05 09:57:05

+0

你可以顯示你的'multVectorByMatrix'函數嗎?我認爲問題在那裏,因爲你需要將矩陣乘以矢量,而不是相反,正如從名字看來的那樣。 – 2013-05-05 10:09:18

回答

2

您的問題是在這個代碼。

point3D max = bBox.max; 
    point3D min = bBox.min; 
    point3D resultMax; 
    point3D resultMin; 
    //Transformacion 
    multVectorByMatrix(matrix, max, resultMax); 
    multVectorByMatrix(matrix, min, resultMin); 
    bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z; 
    bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z; 

    glPushMatrix(); 
     glColor3f(1,1,1); 
     drawBBox(bBox); 
    glPopMatrix(); 

你從你的盒子兩個頂點,然後應用轉換到它,然後你用這個變換的頂點顯示一個對話框,這當然會被軸線對齊,因爲這是你可以從剛剛得到的唯一箱兩個相對的頂點。你可以在你的截圖中看到,你的bbox和正確的bbox有共同的頂點 - 這些正是你應用到你的轉換的頂點。所以,爲了得到一個正確的bbox,你需要獲得bbox的所有頂點並將這些轉換應用到所有的頂點。然後你會得到你想要的。

+0

就是這樣!非常感謝你 – MichelReap 2013-05-05 10:38:55