【问题标题】:Creating a texture from a RGBA value with SlimDX使用 SlimDX 从 RGBA 值创建纹理
【发布时间】:2011-12-22 16:19:41
【问题描述】:

这是我第一篇关于堆栈溢出的帖子! 我正在将 SlimDX 用于我的团队正在制作的游戏,但我遇到了问题。我正在尝试从 Color4 对象中的 RGBA 值创建一个 ShaderResourceView。我已经搜索了我的问题的答案,这就是我所得到的。

    private ShaderResourceView GetTexture(Device device, int width, int height, Color4 color)
    {
        //create the texture
        Texture2D texture = null;
        Texture2DDescription desc2 = new Texture2DDescription();
        desc2.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0); 
        desc2.Width = width;
        desc2.Height = height;
        desc2.MipLevels = 1;
        desc2.ArraySize = 1;
        desc2.Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm;
        desc2.Usage = ResourceUsage.Dynamic;
        desc2.BindFlags = BindFlags.ShaderResource;
        desc2.CpuAccessFlags = CpuAccessFlags.Write;
        texture = new Texture2D(device, desc2);


        // fill the texture with rgba values
        DataRectangle rect = texture.Map(0, MapMode.WriteDiscard, MapFlags.None);
        if (rect.Data.CanWrite)
        {
            for (int row = 0; row < texture.Description.Height; row++)
            {
                int rowStart = row * rect.Pitch;
                rect.Data.Seek(rowStart, System.IO.SeekOrigin.Begin);
                for (int col = 0; col < texture.Description.Width; col++)
                {
                    rect.Data.WriteByte((byte)color.Red);
                    rect.Data.WriteByte((byte)color.Green);
                    rect.Data.WriteByte((byte)color.Blue);
                    rect.Data.WriteByte((byte)color.Alpha);
                }
            }
        }
        texture.Unmap(0);

        // create shader resource that is what the renderer needs
        ShaderResourceViewDescription desc = new ShaderResourceViewDescription();
        desc.Format = texture.Description.Format;
        desc.Dimension = ShaderResourceViewDimension.Texture2D;
        desc.MostDetailedMip = 0;
        desc.MipLevels = 1;
        ShaderResourceView srv = new ShaderResourceView(device, texture, desc);

        return srv;
    }

我相信正在设置纹理的数据,但我不能确定,因为没有显示任何内容。我知道我的渲染器可以正常工作,因为我可以很好地从文件加载纹理,但我似乎有一个我找不到的问题。提前感谢您的帮助!

【问题讨论】:

  • 如果您在使用 SlimDX 渲染内容时遇到问题,可以使用PIX 来调试实际像素。在出现意外行为(例如屏幕上没有出现任何内容)的情况下,查看像素历史记录会很有用。但请注意,您只能使用 PIX 调试 32 位版本的 SlimDX。
  • 嗨。我可以看到你刚才问过这个问题,但我是 DirectX 和 SlimDX 的新手,想知道你是否可以让我知道你是如何为此初始化设备的。

标签: c# slimdx texture2d


【解决方案1】:

我的代码一直都是正确的。我觉得自己像个白痴,但我发现了我的问题,我没有设置纹理的 alpha 值,所以它实际上是在绘制的,我只是看不到它>_

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多