C++ 无法从文件创建着色器资源视图
我试图编写一些代码,从名为TextureList.txt的配置文件中读取文件名列表,然后使用这些文件为每个纹理创建着色器资源视图。下面的列表显示了我当前拥有的内容:C++ 无法从文件创建着色器资源视图,c++,directx,shader,directx-11,resource-management,C++,Directx,Shader,Directx 11,Resource Management,我试图编写一些代码,从名为TextureList.txt的配置文件中读取文件名列表,然后使用这些文件为每个纹理创建着色器资源视图。下面的列表显示了我当前拥有的内容: LPCSTR textures[MAX_TEXTURES]; std::ifstream texturesToLoad; texturesToLoad.open("TextureList.txt"); int numberOfTextures = 0; while(!texturesToLoad.eof()) { s
LPCSTR textures[MAX_TEXTURES];
std::ifstream texturesToLoad;
texturesToLoad.open("TextureList.txt");
int numberOfTextures = 0;
while(!texturesToLoad.eof())
{
std::string textureName;
std::getline(texturesToLoad, textureName);
textures[numberOfTextures] = textureName.c_str();
numberOfTextures++;
char debugMsg[100];
sprintf(debugMsg, "numberOfTextures = %d\n", numberOfTextures);
OutputDebugString(debugMsg);
OutputDebugString(textureName.c_str());
OutputDebugString("\n");
}
for(int i = 0; i < numberOfTextures; i++)
{
d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
textures[i], 0, 0, &colorMap_[i], 0 );
if( FAILED( d3dResult ) )
{
DXTRACE_MSG( "Failed to load the texture image!" );
return false;
}
}
while循环底部的调试显示文件被正确读取,它获得所有文件名并计算正确的纹理数;但是,当它尝试创建第一个资源视图时失败
谁能告诉我哪里出了问题
我不知道这是否有帮助,但如果我使用以下行将for循环上方的所有内容替换为硬编码文件名,它就会起作用:
const LPCSTR纹理[3]={tex1.dds,tex2.dds,tex3.dds} 顺便说一句,与其使用texure加载程序,不如使用来自的。d3dResult是什么?HRESULT为您提供了一个关于它失败原因的强烈提示。嗨,我再次查看了它给我的错误,我似乎发现了问题。我将LPCSTR指向textureName,它在while循环的每次迭代中都超出了范围。因此,当我尝试加载纹理时,LPCSTR不再指向有效内存。谢谢你的帮助