我在這裏看到了這個主題,但它並沒有真正幫助我解決問題:(我嘗試以相同的方式實現代碼,不幸的是我不能使用glLoadIdentity(),因爲我正在使用libqglviewer,並且出於某種奇怪的原因,如果我使用glLoadIdentity()對象(常規多維數據集)消失:(所以我的解決方法是我使用glPopMatrix和glPushMatrix保持世界矩陣每次我必須翻譯/旋轉一個對象原始矩陣正在被儲存起來,然後一旦完成翻譯/旋轉,我再次彈出它的右後方在對象的旋轉/平移之後重新計算AABB
到目前爲止,工作得很好,但是一旦我試圖實現邊界框的重新計算,我的盒子就從s開始作爲立方體的ame位置,但一旦我開始旋轉邊界框有時會「內爆」,因此我需要檢查最大/最小轉角,使它們不會小於立方體本身,但是當我旋轉相機時,邊界框轉移到一邊或另一邊。
我正在使用GLM來加載模型,不幸的是遺留OpenGL,我對Legacy OGL的工作原理有些熟悉,但我認爲重新計算邊界框時計算不正確。
此代碼是在我的渲染功能:
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(m_angle, m_torque.x, m_torque.y, m_torque.z);
glGetFloatv(GL_MODELVIEW_MATRIX, mvMatrix);
glmDraw(pModelCube, GLM_SMOOTH); // Renders the object
glPopMatrix();
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
bb->RecalculateBox(pModelCube, mvMatrix, BBid);
bb->DrawBox(bb->GetBoundingBox(BBid));
glPopMatrix();
這是我設置的邊框代碼:
void AABB::initBox(BoundingBox* b)
{
b->min.x = 10000.0f;
b->min.y = 10000.0f;
b->min.z = 10000.0f;
b->max.x = -10000.0f;
b->max.y = -10000.0f;
b->max.z = -10000.0f;
}
BoundingBox* AABB::CreateCollisionBox(GLMmodel* model, GLMgroup* object)
{
BoundingBox* box = (BoundingBox*)malloc(sizeof(BoundingBox));
initBox(box);
for(int i = 0; i < object->numtriangles; i++)
{
for(int j = 0; j < 3; j++)
{
GLuint index = model->triangles[object->triangles[i]].vindices[j];
GLfloat x = model->vertices[index*3 + 0];
GLfloat y = model->vertices[index*3 + 1];
GLfloat z = model->vertices[index*3 + 2];
if(box->min.x > x) box->min.x = x;
if(box->min.y > y) box->min.y = y;
if(box->min.z > z) box->min.z = z;
if(box->max.x < x) box->max.x = x;
if(box->max.y < y) box->max.y = y;
if(box->max.z < z) box->max.z = z;
}
}
return box;
}
void AABB::DrawBox(BoundingBox* b)
{
if(collision)
{
// Sets color to red
glColor3f(1.0f, 0.0f, 0.0f);
}
else
{
// Sets color to yellow
glColor3f(1.0f, 1.0f, 0.0f);
}
glBegin(GL_LINE_LOOP);
glVertex3f(b->max.x, b->max.y, b->min.z);
glVertex3f(b->min.x, b->max.y, b->min.z);
glVertex3f(b->min.x, b->min.y, b->min.z);
glVertex3f(b->max.x, b->min.y, b->min.z);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(b->max.x, b->min.y, b->max.z);
glVertex3f(b->max.x, b->max.y, b->max.z);
glVertex3f(b->min.x, b->max.y, b->max.z);
glVertex3f(b->min.x, b->min.y, b->max.z);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(b->max.x, b->max.y, b->min.z);
glVertex3f(b->max.x, b->max.y, b->max.z);
glVertex3f(b->min.x, b->max.y, b->max.z);
glVertex3f(b->min.x, b->max.y, b->min.z);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(b->max.x, b->min.y, b->max.z);
glVertex3f(b->min.x, b->min.y, b->max.z);
glVertex3f(b->min.x, b->min.y, b->min.z);
glVertex3f(b->max.z, b->min.y, b->min.z);
glEnd();
}
這最後一塊是我重新計算框:
void AABB::RecalculateBox(GLMmodel* model, float matrix[16], int &id)
{
BoundingBox* box = boxes[id]/*(BoundingBox*)malloc(sizeof(BoundingBox))*/;
initBox(box);
float dimensions[3];
// This will get the absolute dimensions of the object
glmDimensions(model, dimensions);
box->max.x = dimensions[0]/2.0f, box->min.x = -1.0f * box->max.x;
box->max.y = dimensions[1]/2.0f, box->min.y = -1.0f * box->max.y;
box->max.z = dimensions[2]/2.0f; box->min.z = -1.0f * box->max.z;
box->max.x = matrix[0] * box->max.x + matrix[4] * box->max.y + matrix[8] * box->max.z + matrix[12];
box->max.y = matrix[1] * box->max.x + matrix[5] * box->max.y + matrix[9] * box->max.z + matrix[13];
box->max.z = matrix[2] * box->max.x + matrix[6] * box->max.y + matrix[10] * box->max.z + matrix[14];
box->min.x = matrix[0] * box->min.x + matrix[4] * box->min.y + matrix[8] * box->min.z + matrix[12];
box->min.y = matrix[1] * box->min.x + matrix[5] * box->min.y + matrix[9] * box->min.z + matrix[13];
box->min.z = matrix[2] * box->min.x + matrix[6] * box->min.y + matrix[10] * box->min.z + matrix[14];
box->bounds[0] = Vec(box->max.x, box->max.y, box->min.z);
box->bounds[1] = Vec(box->min.x, box->max.y, box->min.z);
box->bounds[2] = Vec(box->min.x, box->min.y, box->min.z);
box->bounds[3] = Vec(box->max.x, box->min.y, box->min.z);
box->bounds[4] = Vec(box->max.x, box->min.y, box->max.z);
box->bounds[5] = Vec(box->max.x, box->max.y, box->max.z);
box->bounds[6] = Vec(box->min.x, box->max.y, box->max.z);
box->bounds[7] = Vec(box->min.x, box->min.y, box->max.z);
//This code below is from how my bounding box was calculated before.
/*for(int i = 0; i < object->numtriangles; i++)
{
for(int j = 0; j < 3; j++)
{
GLuint index = model->triangles[object->triangles[i]].vindices[j];
GLfloat x = model->vertices[index*3 + 0];
GLfloat y = model->vertices[index*3 + 1];
GLfloat z = model->vertices[index*3 + 2];
if(box->min.x > x) box->min.x = x;
if(box->min.y > y) box->min.y = y;
if(box->min.z > z) box->min.z = z;
if(box->max.x < x) box->max.x = x;
if(box->max.y < y) box->max.y = y;
if(box->max.z < z) box->max.z = z;
}
}*/
boxes[id] = box;
}
我一直在試圖關注這個帖子How to recalculate axis-aligned bounding box after translate/rotate?,但還沒有設法得到它窩rk與我的代碼上面。
任何關於我哪裏出錯的指針都會有幫助。