2011-04-07 7 views
3

C# .net 동적 인 직경과 길이를 가진 메쉬 실린더가 있는데 텍스처를 맵핑하려고합니다. 나는 하루 중 더 나은 시간을 보냈지 만 어떻게하는지 알아 내려했지만 Google에서 정보를 찾는 데 성공하지 못했습니다.관리되지 않는 DirectX에서 텍스처를 실린더에 매핑하는 방법은 무엇입니까?

실린더 텍스처에는 jpg의 상단 영역이 있고 측면에는 jpg의 나머지 부분이 있습니다. 실린더의 상단 가장자리를 따라 jpgs 이미지 가장자리를 배치해야합니다. 예. 하나의 이미지를 사용하여 위에 빨간색과 녹색을 표시합니다.

아무도 VertexBuffer 포인트를 텍스처에 매핑 할 수 있습니까?

(관리되지 않는) C# 닷넷 2008 다이렉트 X 9

this tutorial가 명확하게 과정을 설명 VB에서하지만 나는

+0

그냥 텍스처 좌표를 계산하십시오. 이것은 똑바로 보인다. 당신이 겪고있는 정확한 문제는 무엇입니까? 실패한 결과를 게시 할 수 있습니까? –

+0

나는 그것을 지금 이해했다. 문제는 내가 수학에 빠져서 DirectX를 사용하여 자동으로 메쉬를 생성했기 때문에 메쉬가 어떻게 생성되었거나 레이아웃되었는지를 알 수 없었습니다;) 아래에 새로운 작업 코드를 게시했습니다. –

+0

그것은 정신입니다! 그래픽 프로그래밍을 원한다면 수학을 다듬어야합니다. 주위를 둘러 볼 방법이 없습니다. –

답변

0

확인을 발생하는 좌표를, I는 마침내 알아 낸 것. 나는 이전에 작동했던 코드를 가지고 있었지만 정확히 내가 원하지 않는 코드를 가지고 있었다. http://channel9.msdn.com/coding4fun/articles/Ask-the-ZMan-Applying-Textures-Part-3

어쨌든, 나는 몇 가지 수정 작업을 수행했다.

Google에서 도착한 사람들을 위해 참조하십시오.

public static float ComputeBoundingSphere(Mesh mesh, out Microsoft.DirectX.Vector3 center) 
    { 
     // Lock the vertex buffer 
     Microsoft.DirectX.GraphicsStream data = null; 
     try 
     { 
      data = mesh.LockVertexBuffer(LockFlags.ReadOnly); 
      // Now compute the bounding sphere 
      return Geometry.ComputeBoundingSphere(data, mesh.NumberVertices, 
       mesh.VertexFormat, out center); 
     } 
     finally 
     { 
      // Make sure to unlock the vertex buffer 
      if (data != null) 
       mesh.UnlockVertexBuffer(); 
     } 
    } 

    private static Mesh SetSphericalTexture(Mesh mesh) 
    { 
     Microsoft.DirectX.Vector3 vertexRay; 
     Microsoft.DirectX.Vector3 meshCenter; 
     double phi; 
     float u; 


     Microsoft.DirectX.Vector3 north = new Microsoft.DirectX.Vector3(0f, 0f, 1f); 
     Microsoft.DirectX.Vector3 equator = new Microsoft.DirectX.Vector3(0f, 1f, 0f); 
     Microsoft.DirectX.Vector3 northEquatorCross = Microsoft.DirectX.Vector3.Cross(north, equator); 

     ComputeBoundingSphere(mesh, out meshCenter); 

     using (VertexBuffer vb = mesh.VertexBuffer) 
     { 
      CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vb.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, mesh.NumberVertices); 
      try 
      { 
       for (int i = 0; i < verts.Length; i++) 
       { 
        //For each vertex take a ray from the centre of the mesh to the vertex and normalize so the dot products work. 
        vertexRay = Microsoft.DirectX.Vector3.Normalize(verts[i].Position - meshCenter); 

        phi = Math.Acos((double)vertexRay.Z); 
        if (vertexRay.Z > -0.9) 
        { 
         verts[i].Tv = 0.121f; //percentage of the image being the top side 
        } 
        else 
         verts[i].Tv = (float)(phi/Math.PI); 

        if (vertexRay.Z == 1.0f || vertexRay.Z == -1.0f) 
        { 
         verts[i].Tu = 0.5f; 
        } 
        else 
        { 
         u = (float)(Math.Acos(Math.Max(Math.Min((double)vertexRay.Y/Math.Sin(phi), 1.0), -1.0))/(2.0 * Math.PI)); 
         //Since the cross product is just giving us (1,0,0) i.e. the xaxis 
         //and the dot product was giving us a +ve or -ve angle, we can just compare the x value with 0 
         verts[i].Tu = (vertexRay.X > 0f) ? u : 1 - u; 
        } 
       } 
      } 
      finally 
      { 
       vb.Unlock(); 
      } 
     } 
     return mesh; 
    } 
1

아래 내 작업 솔루션을 게시.

텍스처 좌표를 계산하는 것은 꽤 효과적 일 수 있습니다. 이것이 일반적으로 3D 모델링 소프트웨어로 수행되는 것이므로 매핑을 시각적으로 쉽게 조정할 수 있습니다.

궁금한 점이 있으면 알려주세요. 텍스처를 추가

EDIT

가 DirecxtX 실린더 see this

+0

좋은 정보! 지금 읽고있어. :) –

+0

BTW : 관리되지 않는 DirectX는 무엇을 의미합니까? 나만의 래퍼를 만들었나요? 아니면 SlimDX와 같은 라이브러리를 사용하고 있습니까? –

+0

Hmmm ...이 튜토리얼은 텍스처를 중공 실린더에 연결하는 것을 가리킨다. 내 실린더는 위아래가 있습니다. 나는 그것을 만들기 위해'Microsoft.DirectX.Direct3D.Mesh.Cylinder'를 사용하고 있습니다. 관리되지 않음은 .Net Managed 버전이 아니라 원래 DirectX 라이브러리를 사용하고 있음을 의미합니다. –

관련 문제