在DirectX中创建和使用纹理 [英] Creating and using a Texture in DirectX

查看:102
本文介绍了在DirectX中创建和使用纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用代码创建纹理,将其转换为着色器资源视图,然后将其应用于平面,但是我得到的只是一个黑色正方形.我尝试在msdn上使用示例代码无济于事,还尝试使用unsigned char和float(下面显示了float,因为这是我实现最终目标所需要的).

以下是尝试创建纹理的代码:

  bool TerrainClass :: CreateTexture(ID3D11Device * _device){ID3D11Texture2D *纹理;D3D11_TEXTURE2D_DESC tdesc;D3D11_SUBRESOURCE_DATA数据;float * buf =(float *)malloc(m_terrainWidth * m_terrainHeight * 4 * sizeof(float));for(int i = 0; i< m_terrainWidth * m_terrainHeight * 4; i ++)buf [i] = 1.0f;data.pSysMem =(无效*)buf;data.SysMemPitch = m_terrainWidth * 4;data.SysMemSlicePitch = m_terrainWidth * m_terrainHeight * 4;tdesc.Width = m_terrainWidth;tdesc.Height = m_terrainHeight;tdesc.MipLevels = 1;tdesc.ArraySize = 1;tdesc.SampleDesc.Count = 1;tdesc.SampleDesc.Quality = 0;tdesc.Usage = D3D11_USAGE_DEFAULT;tdesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;tdesc.CPUAccessFlags = 0;tdesc.MiscFlags = 0;//尝试创建2D纹理if(FAILED(_device-> CreateTexture2D(& tdesc,& data,& texture))))返回false;//将fbm制成的纹理分配给地形的纹理之一if(!(m_Textures-> ChangeTexture(_device,texture,2)))返回false;delete [] buf;返回true;} 

除非我误解了,那应该是白色的纹理.然后将纹理传递到该函数的纹理数组中:

  bool TextureArrayClass :: ChangeTexture(ID3D11Device * _device,ID3D11Texture2D * _texture,int _i){if(FAILED(_device-> CreateShaderResourceView(_texture,NULL,& m_textures [_i])))){返回false;}返回true;} 

应将着色器资源视图设置为我刚刚创建的纹理.因此,我对哪里出错了完全不知所措,有什么想法吗?

解决方案

data.SysMemPitch 应该以字节为单位,因此 m_terrainWidth * 4 * sizeof(float).与 SysMemSlicePitch 相同,但是您可以将其设置为零,因为只需要它用于纹理立方体,体积或阵列.您还应该验证 <您正在运行的code> D3D_FEATURE_LEVEL 支持以您使用它的方式使用 R32G32B32A32 (我假设为 Sample ).通过查看此页面或调用 CheckFormatSupport .如果您的硬件不支持您正在使用的模式,请切换到更广泛支持的格式,例如 B8G8R8A8_UNORM (并确保同时更改初始数据以匹配格式布局).

I'm trying to create a texture using code, turn it into a shader resource view then apply it to a plane, however all I'm getting is a black square. I tried using the sample code on msdn to no avail, also tried using unsigned char and float (float is shown below as that is what I'll need to use for my end goal).

Here is the code trying to create the texture:

    bool TerrainClass::CreateTexture(ID3D11Device* _device)
{
    ID3D11Texture2D *texture;
    D3D11_TEXTURE2D_DESC tdesc;
    D3D11_SUBRESOURCE_DATA data;

    float *buf = (float *)malloc(m_terrainWidth * m_terrainHeight * 4 * sizeof(float));

    for(int i = 0; i < m_terrainWidth * m_terrainHeight * 4; i++)
        buf[i] = 1.0f;

    data.pSysMem = (void *)buf;
    data.SysMemPitch = m_terrainWidth * 4;
    data.SysMemSlicePitch = m_terrainWidth * m_terrainHeight * 4;

    tdesc.Width = m_terrainWidth;
    tdesc.Height = m_terrainHeight;
    tdesc.MipLevels = 1;
    tdesc.ArraySize = 1;
    tdesc.SampleDesc.Count = 1;
    tdesc.SampleDesc.Quality = 0;
    tdesc.Usage = D3D11_USAGE_DEFAULT;
    tdesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
    tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

    tdesc.CPUAccessFlags = 0;
    tdesc.MiscFlags = 0;

    //attempt to create the 2d texture
    if(FAILED(_device->CreateTexture2D(&tdesc,&data,&texture)))
        return false;

    //assign the texture made from fbm to one of the textures for the terrain
    if(!(m_Textures->ChangeTexture(_device, texture, 2)))
        return false;

    delete[] buf;

    return true;
}

Unless I'm misunderstanding that should be a white texture made. The texture is then passed into the texture array to this function:

bool TextureArrayClass::ChangeTexture(ID3D11Device* _device, ID3D11Texture2D* _texture, int _i)
{
        if(FAILED(_device->CreateShaderResourceView(_texture,NULL, &m_textures[_i])))
        {
            return false;
        }
        return true;
}

Which should set the shader resource view to be the texture I just created. So I'm completely lost as to where I've gone wrong, any thoughts?

解决方案

data.SysMemPitch should be in bytes, so m_terrainWidth * 4 * sizeof(float). Same with SysMemSlicePitch but you can set that to zero since you only need that for a texture cube, volume, or array. You should also verify that the D3D_FEATURE_LEVEL you're running on supports using R32G32B32A32 in the way you're using it (to Sample I assume). Verify support by reviewing this page or calling CheckFormatSupport. If your hardware doesn't support the pattern you're using, switch to a more broadly supported format like B8G8R8A8_UNORM (and be sure to change your initial data as well to match the format layout).

这篇关于在DirectX中创建和使用纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆