如何在非托管DirectX中将纹理映射到柱面?

时间:2011-04-07 04:46:31

标签: c# visual-studio directx direct3d texture-mapping

在C#.net中,我有一个动态直径和长度的网格圆柱体,我正在尝试将纹理映射到它。我花了大部分时间试图找出如何做到这一点,但没有成功找到关于谷歌的任何信息。

气缸纹理具有jpg的顶部区域,并且侧面具有jpg的其余部分。 我需要沿着圆柱的顶部边缘定位jpgs图像边缘。例如。顶部为红色,使用一个图像为绿色。

任何人都可以帮我将VertexBuffer点映射到纹理吗?

C#.Net 2008 DirectX 9(非托管)

我已经发布了我的工作解决方案

2 个答案:

答案 0 :(得分:1)

虽然this tutorial在VB中,但它清楚地解释了这个过程。

计算纹理坐标可能相当有用;这就是为什么通常这是通过3D建模软件完成的,这样您就可以轻松地,更重要的是,可视化地调整映射。

如果您有任何问题,请与我们联系。

修改

用于将纹理坐标添加到DirecxtX生成的柱面see this

答案 1 :(得分:0)

好的,我终于明白了。我之前有一些代码正在运行但不完全是我想要的代码 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;
    }