使用 SlimDX 从 RGBA 值创建纹理
这是我第一篇关于堆栈溢出的文章! 我在我的团队正在制作的游戏中使用 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;
}
我相信纹理的数据正在设置,但我不能确定,因为没有显示任何内容。我知道我的渲染器可以正常工作,因为我可以很好地从文件加载纹理,但我似乎遇到了一个找不到的问题。提前感谢您的帮助!
this is my first post to stack overflow!
I'm using SlimDX for a game that my team is making and I've run into an issue. I'm trying to create a ShaderResourceView from RGBA values in a Color4 object. I've searched looked for answers to my issue and this is as far as I have gotten.
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;
}
I believe that the texture's data is being set but I can't be sure because nothing is being displayed. I know that my renderer works as I can load texture from a file perfectly fine but I seem to have a problem that I cannot find. Thanks for your help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的代码一直都是正确的。我感觉自己像个白痴,但我发现了我的问题,我没有设置纹理的 alpha 值,所以它实际上是被绘制的,我只是看不到它 >_<;总是犯简单的错误,是吗?不过还是感谢所有看过的人。
I've had the code right all along. I feel like an idiot but I found out my problem, I wasn't setting the alpha value for the texture so it was actually being drawn I just couldn't see it >_<; Simple mistakes always eh? Thanks to all that viewed however.