Failing to Create a Texture from File
Hi All,
I'm writing some DirectX 10 stuff and I keep coming across a problem with my texture loading class. Within the class, I have a pointer to a private ID3D10Texture2D (ID3D10Texture2D *Texture). Unfortunately, the method for loading an image doesn't seem to work...
void Texture2D::Image(LPCWSTR FileName, ID3D10Device *gDevice)
{
// Empty the Texture and Resource Pointers
Texture = NULL;
ID3D10Resource *Resource = NULL;
// Load the Texture into a Temporary Resource
HRESULT hResult = D3DX10CreateTextureFromFile(gDevice, FileName, NULL, NULL, &Resource, NULL);
if (FAILED(hResult))
{
OutputDebugString(TEXT("Error Creating Texture from File\n"));
}
// Translate the Resource into a Texture
Resource->QueryInterface(__uuidof(ID3D10Texture2D), (LPVOID*)&Texture);
Resource->Release();
}
It doesn't seem to read anything into the Resource Object and I can't understand why! Please help!
Yes, I've stored it within the Debug directory with the Executable etc.
Oh, and I figured I should put the whole class up here in case it's a problem with my other code...
Header:
Source File:
Oh, and I figured I should put the whole class up here in case it's a problem with my other code...
Header:
#ifndef _TEXTURE2D_H_#define _TEXTURE2D_H_// Include Direct3D Headers#include <d3d10.h>#include <d3dx10.h>#include <stdio.h>class Texture2D{public: Texture2D(){}; // Constructor ~Texture2D(){}; // Destructor void Image( LPCWSTR FileName, ID3D10Device *gDevice); // "Real" Constructor void Render(ID3D10Device *gDevice, ID3D10Texture2D *BackBuffer); // Render Functionprivate: ID3D10Texture2D *Texture;};#endif
Source File:
#include "Texture2D.h"// Constructor Loads Image File and Creates Texture Objectvoid Texture2D::Image(LPCWSTR FileName, ID3D10Device *gDevice){ // Empty the Texture and Resource Pointers Texture = NULL; ID3D10Resource *Resource = NULL; // Load the Texture into a Temporary Resource HRESULT hResult = D3DX10CreateTextureFromFile(gDevice, FileName, NULL, NULL, &Resource, NULL); if (FAILED(hResult)) { OutputDebugString(TEXT("Error Creating Texture from File\n")); } // Translate the Resource into a Texture Resource->QueryInterface(__uuidof(ID3D10Texture2D), (LPVOID*)&Texture); Resource->Release();}// Render Functionvoid Texture2D::Render(ID3D10Device *gDevice, ID3D10Texture2D *BackBuffer){ // Create an Object with the Texture Information D3D10_TEXTURE2D_DESC Description; // Create a Source Region from the Texture Information D3D10_BOX SourceRegion; // Set the Source Region Parameters SourceRegion.left = 0; SourceRegion.right = Description.Width; SourceRegion.top = 0; SourceRegion.bottom = Description.Height; SourceRegion.front = 0; SourceRegion.back = 1; // Copy the Image to the Back Buffer to Render gDevice->CopySubresourceRegion(BackBuffer, 0, 0, 0, 0, Texture, 0, &SourceRegion);}
Quote:Is the filename a valid path for the currect execute directory?Quote:Yes, I've stored it within the Debug directory with the Executable etc.
I would double-check that. Something like:
void Texture2D::Image(LPCWSTR FileName, ID3D10Device *gDevice){ std::ifstream file(FileName); if (!file.is_open()) // Failed to open file //...}
Also you might want to look at the HRESULT value and see if it tells you anything useful. In fact, it looks like there's a special value for not being able to open file - D3D10_ERROR_FILE_NOT_FOUND.
Also, are you using the debug runtimes?
I have the line
In my MainApp.cpp file (the one which contains the Win32 Window Code and sets up the device etc.). Does that mean I'm using the debug runtimes (sorry, I'm still quite new to DirectX coding)?
In my debug directory, I have the following files:
-Debug
--bgimg.bmp
--Scorpion Game Engine.exe
--Scorpion Game Engine.ilk
--Scorpion Game Engine.pdb
And in my initialisation code, I have the following lines (where Test is declared as a global variable at the top of the file):
#ifdef _DEBUG CreateDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;#endif
In my MainApp.cpp file (the one which contains the Win32 Window Code and sets up the device etc.). Does that mean I'm using the debug runtimes (sorry, I'm still quite new to DirectX coding)?
In my debug directory, I have the following files:
-Debug
--bgimg.bmp
--Scorpion Game Engine.exe
--Scorpion Game Engine.ilk
--Scorpion Game Engine.pdb
And in my initialisation code, I have the following lines (where Test is declared as a global variable at the top of the file):
Test = new Texture2D(); Test->Image(TEXT("bgimg.bmp"), gDevice);
Quote:I have the line... Does that mean I'm using the debug runtimes?
I'm not sure. Just in case, here you'll find the instructions to enable the debug runtimes.
I still suggest that you see for yourself if the file can be found (like in my code snippet), and in addition look at the HRESULT that the function returns. When dealing with file loading in VC++, I've had some nasty suprises in the past so it's always better to double-check these things.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement