我正在嘗試製作坦克遊戲。我已經成功加載了一個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];
}
如果我畫邊界框與此渲染循環,然後我的邊界框被繪製,但轉換不適當地應用。我可以看到邊界框在翻譯中正確移動,但旋轉不正確。
這裏有什麼問題?
編輯:一些截圖
請問您可以添加一些截圖來澄清問題到底是什麼? – 2013-05-05 09:51:24
我剛剛做到了。但很難看到。紅色的bbox是用opengl渲染變換函數繪製的,白色的是通過將bbox位置乘以變換矩陣來計算的。 – MichelReap 2013-05-05 09:57:05
你可以顯示你的'multVectorByMatrix'函數嗎?我認爲問題在那裏,因爲你需要將矩陣乘以矢量,而不是相反,正如從名字看來的那樣。 – 2013-05-05 10:09:18