2013-05-05 3 views
0

전차 게임을 만들려고합니다. 성공적으로 OBJ 모델을로드하고 원점에있는 모델의 테두리 상자를 계산했습니다.경계 상자에 변형 적용

이제 게임 논리에서 내 모델에 적용한 변환을 테두리 상자의 원래 좌표에 적용하려고합니다. 이를 위해 모델을 그리기 전에 modelview 행렬을 가져온 다음 BBox를 정의하는 두 벡터에이 행렬을 곱합니다. (변환이 glTranslate & glRotate 기능에 의해 렌더링에 적용되는) 내 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(); 


} 

: 여기

내 탱크를 그리는 코드입니다. 당신이 볼 수 있듯이, 나는 또한 여기 내 ModelView 매트릭스 잡아. 그런 다음 다음

I (이것은 내 전체 디스플레이 함수이다)이 행렬을 적용

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

몇 가지 스크린 샷을 추가 할 수 있습니까? 명확히하기 위해 정확히 무엇이 문제입니까? –

+0

방금 ​​했어요. 그러나보기가 어렵습니다. 빨간색 bbox는 OpenGL 렌더링 변환 함수로 그려지며, 흰색은 bbox 위치에 변환 행렬을 곱한 값으로 계산됩니다. – MichelReap

+0

'multVectorByMatrix' 함수를 보여줄 수 있습니까? 나는 그 문제가 있다고 생각한다. 행렬에 벡터를 곱해야하기 때문에, 그 반대도 마찬가지이다. –

답변

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

관련 문제