2010-12-07 3 views
1

스태틱 메시의 변형을 찾는 방법은 무엇입니까?
제가 피킹 기능을 위해 사용할 것입니다.
내가 이것을 시도했지만 작동하지 않았다 ... 사전에정적 메쉬의 변형을 찾는 방법은 무엇입니까?

void StaticMesh::Render(void) 
{ 
    D3DXMATRIX matWorld; 

    D3DXMatrixIdentity(&matWorld); 

    device->SetTransform(D3DTS_WORLD, &matWorld); 

    for(DWORD i = 0; i < numMaterials; i++) // loop through each subset 
    { 
     device->SetMaterial(&material[i]); // set the material for the subset 
     device->SetTexture(0, texture[i]); // ...then set the texture 

     mesh->DrawSubset(i); // draw the subset 
    } 

    s = &matWorld; // THIS WOULD BE THE TRANSFORMATION FOR THE OBJECT 
} 

감사합니다.

편집 2 : 여기
전체 클래스에게 있습니다

#include "StdAfx.h" 
#include "StaticMesh.h" 


StaticMesh::StaticMesh(LPDIRECT3DDEVICE9* dev) 
{ 
    d3ddev=dev; 
    device = *d3ddev; 
} 


StaticMesh::~StaticMesh(void) 
{ 
} 

void StaticMesh::Render(void) 
{ 
    LPDIRECT3DDEVICE9 device=*d3ddev; 
    D3DXMATRIX matWorld; 

    D3DXMatrixIdentity(&matWorld); 

    device->SetTransform(D3DTS_WORLD, &matWorld); 

    for(DWORD i = 0; i < numMaterials; i++) // loop through each subset 
    { 
     device->SetMaterial(&material[i]); // set the material for the subset 
     device->SetTexture(0, texture[i]); // ...then set the texture 

     mesh->DrawSubset(i); // draw the subset 
    } 

    s = matWorld; 
} 

StaticMesh* StaticMesh::LoadXFile(LPCWSTR fileName, LPDIRECT3DDEVICE9* dev) 
{ 
    StaticMesh *obj = this; 
    obj = new StaticMesh(dev); 
    obj->Load(fileName); 
    return obj; 
} 

void StaticMesh::Load(LPCWSTR fileName) 
{ 
    D3DXLoadMeshFromX(fileName, // load this file 
         D3DXMESH_SYSTEMMEM, // load the mesh into system memory 
         device, // the Direct3D Device 
         NULL, // we aren't using adjacency 
         &bufMeshMaterial, // put the materials here 
         NULL, // we aren't using effect instances 
         &numMaterials, // the number of materials in this model 
         &mesh); // put the mesh here 

    // retrieve the pointer to the buffer containing the material information 
    D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMeshMaterial->GetBufferPointer(); 

    // create a new material buffer and texture for each material in the mesh 
    material = new D3DMATERIAL9[numMaterials]; 
    texture = new LPDIRECT3DTEXTURE9[numMaterials]; 

    for(DWORD i = 0; i < numMaterials; i++) // for each material... 
    { 
     // Copy the material 
     material[i] = tempMaterials[i].MatD3D; 

     // Set the ambient color for the material (D3DX does not do this) 
     material[i].Ambient = material[i].Diffuse; 

     // Create the texture if it exists - it may not 
     texture[i] = NULL; 
     if (tempMaterials[i].pTextureFilename) 
     { 
      D3DXCreateTextureFromFileA(device, tempMaterials[i].pTextureFilename,&texture[i]); 
     } 
    } 
} 

void StaticMesh::CleanUp(void) 
{ 
    mesh->Release(); 
} 

편집 3 :

void GUIDialog::Picking(HWND hWnd, int status) 
{ 
    LPDIRECT3DDEVICE9 device = *d3ddev; 
    D3DXMATRIX matProj; 
    POINT pt; 
    D3DVIEWPORT9 vp; 
    D3DXMATRIX *matWorld=NULL; 
    D3DXMATRIX matView; 

    GetCursorPos(&pt); 
    ScreenToClient(hWnd, &pt); 
    device->GetTransform(D3DTS_PROJECTION, &matProj); 
    device->GetViewport(&vp); 
    device->GetTransform(D3DTS_VIEW, &matView); 


    for(int i=0; (int)edit.size() > i; i++) 
    { 
     matWorld=&edit.at(i)->staticMesh->s; 
     // Use inverse of matrix 
     D3DXVECTOR3 rayPos((float)pt.x, (float)pt.y,0); // near-plane position 
     D3DXVECTOR3 rayDir((float)pt.x, (float)pt.y,1); // far-plane position 
     D3DXVec3Unproject(&rayPos,&rayPos,&vp,&matProj,&matView,matWorld); 
     D3DXVec3Unproject(&rayDir,&rayDir,&vp,&matProj,&matView,matWorld); 
     rayDir -= rayPos; // make a direction from the 2 positions 
     D3DXVec3Normalize(&rayDir,&rayDir); 
     if(FAILED(D3DXIntersect(edit.at(i)->staticMesh->mesh, &rayPos, &rayDir, &edit.at(i)->staticMesh->hasHit, NULL, NULL, NULL, &edit.at(i)->staticMesh-> 
      distanceToCollision, NULL, NULL))) 
     { 
      PostQuitMessage(0); 
     }; 

     if(edit.at(i)->staticMesh->hasHit!=0&&status==WM_LBUTTONUP) 
     { 
      if(status==WM_LBUTTONUP) 
       EventProc(HIT, *edit.at(i)); 
     } 
    } 
} 

답변

1

음 코드 위의 올바른 matWorld이 (가) 오브젝트의 변환이다. 레이를 오브젝트 로컬 공간 (즉, matWorld 변환 이전의 공간)으로 변환하려면, 월드 와이드 레이를 matWorld의 역수로 곱하면됩니다.

+0

'D3DXVec3Unproject)'않습니다? – Ramilol

+0

@Ramilol : 가능합니다. 그러나 서로 다른 월드 행렬을 가진 객체가 여러 개인 경우 항등 행렬로 투영을 해제 한 다음 각 객체에 대한 행렬의 역행렬로 광선을 변환하는 것이 좋습니다. – Goz

+0

& Goz 나는 애니메이션 메쉬로 시도했지만 작동하지만 정적 메쉬 (애니메이션 없음)에서는 작동하지 않습니다. – Ramilol

관련 문제