Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在非托管DirectX中将纹理映射到圆柱体?_C#_Visual Studio_Directx_Direct3d_Texture Mapping - Fatal编程技术网

C# 如何在非托管DirectX中将纹理映射到圆柱体?

C# 如何在非托管DirectX中将纹理映射到圆柱体?,c#,visual-studio,directx,direct3d,texture-mapping,C#,Visual Studio,Directx,Direct3d,Texture Mapping,在C#net中,我有一个直径和长度都是动态的网格圆柱体,我正在尝试将纹理映射到它。我花了一天的大部分时间试图找出如何做到这一点,但没有成功地找到任何关于谷歌的信息 圆柱体纹理具有jpg的顶部区域,侧面具有jpg的其余部分。 我需要沿圆柱体的上边缘定位jpgs图像边缘。使用一个图像,顶部为红色,侧面为绿色 有人能帮我把VertexBuffer点映射到纹理吗 C#Net 2008 DirectX 9(非托管) 我已经在下面发布了我的工作解决方案虽然在VB中,它清楚地解释了这个过程 计算纹理坐标可能是

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

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

有人能帮我把VertexBuffer点映射到纹理吗

C#Net 2008 DirectX 9(非托管)

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

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

计算纹理坐标可能是相当困难的工作;这就是为什么通常这是由三维建模软件完成的,因此您可以轻松地,更重要的是,直观地调整贴图

如果你有任何问题,请告诉我

编辑


为了向DirecxtX生成的圆柱体添加纹理坐标,我终于找到了答案。我以前有一些代码正在工作,但并不完全是我想要的

不管怎样,我只是做了一些修改

作为参考和来自谷歌的读者,请看这里

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;
    }
public static float ComputeBoundingSphere(网格网格,在Microsoft.DirectX.Vector3中心外)
{
//锁定顶点缓冲区
Microsoft.DirectX.GraphicsStream数据=null;
尝试
{
数据=mesh.LockVertexBuffer(LockFlags.ReadOnly);
//现在计算边界球体
返回Geometry.ComputeBoundingSphere(数据、网格、数字、,
mesh.VertexFormat,中心外);
}
最后
{
//确保解锁顶点缓冲区
如果(数据!=null)
mesh.UnlockVertexBuffer();
}
}
专用静态网格SetSphereCalTexture(网格)
{
Microsoft.DirectX.Vector3 vertexRay;
Microsoft.DirectX.Vector3网格中心;
双φ;
浮动u;
Microsoft.DirectX.Vector3 north=新的Microsoft.DirectX.Vector3(0f、0f、1f);
Microsoft.DirectX.Vector3赤道=新的Microsoft.DirectX.Vector3(0f,1f,0f);
Microsoft.DirectX.Vector3 northEquatorCross=Microsoft.DirectX.Vector3.Cross(北赤道);
计算边界球体(网格、网格中心外);
使用(VertexBuffer vb=mesh.VertexBuffer)
{
CustomVertex.PositionNormalTextured[]顶点=(CustomVertex.PositionNormalTextured[])vb.Lock(0,typeof(CustomVertex.PositionNormalTextured),LockFlags.None,mesh.numbertices);
尝试
{
对于(int i=0;i-0.9)
{
顶点[i].Tv=0.121f;//图像顶部的百分比
}
其他的
verts[i].Tv=(float)(phi/Math.PI);
if(vertexRay.Z==1.0f | | vertexRay.Z==1.0f)
{
顶点[i].Tu=0.5f;
}
其他的
{
u=(float)(Math.Acos(Math.Max(Math.Min((double)vertexRay.Y/Math.Sin(phi),1.0),-1.0))/(2.0*Math.PI));
//因为叉积只是给我们(1,0,0),也就是x轴
//点积给我们一个+ve或-ve的角度,我们可以比较x值和0
verts[i].Tu=(vertexRay.X>0f)?u:1-u;
}
}
}
最后
{
vb.Unlock();
}
}
回流网;
}

BTW:您所说的非托管DirectX是什么意思?您是创建了自己的包装器还是使用了SlimDX之类的库?嗯。。。本教程介绍如何将纹理附着到空心圆柱体。我的圆筒有顶部和底部。我正在使用Microsoft.DirectX.Direct3D.Mesh.Cylinder创建它。“非托管”是指我们使用的是原始DirectX库,而不是.Net托管版本。我添加了另一个链接,可能有助于您使用Microsoft.DirectX.Direct3D.Mesh.Cylinder。PInvoke,你是如何包装非托管库的?我不确定我们是如何包装的,这个项目从2004年就开始了。我最近才开始做这个项目,创建了一个不同的项目模块,并在显示器上实现了3D模型。(在这之后还有另一条评论,请按show more查看)。你给我发来的附加文章,我今天已经看过很多次了,但没有任何运气。它只显示圆柱体的实心灰色轮廓。只需计算纹理坐标。这是直截了当的。你到底有什么问题?你能发布一些失败的结果吗?我现在已经知道了。问题是,我的数学很烂,并且使用DirectX自动生成网格,所以我不知道网格是如何创建或布局的;)我已经在下面的回复中发布了我的新工作代码。这就是精神!如果你想做图形编程,你真的需要温习一下你的数学——这是没有办法的。