Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

D3DXCreateTextureFromFile does not work.

Hi!
I am learning DXAPI (currently DirectX9) and c++ using Visual Studio 10 and currently I am trying to set texture onto my sqare that I have already rendered on the screen with DrawPrimitive method. So I took first best tutorial and did all the steps mentioned there, I:
- declare the IDirect3DTexture9 pointer;
- call [myDevice]->D3DXCreateTextureFromFile method;
- and set the texture with [myDevice]->SetTexture method.
- yes, and I have also changed my FVF format to include texture coordinates, as well as I changed the vertex structure.
- the texture with the correct name is in the project's folder (and it is not white:)).
Nothing happens. I mean the program runs but the texture is not there, the square is white. The reason is because D3DXCreateTextureFromFile method writes nothing into my texture pointer, so it is NULL when i set the textures to the device.
Now the reason why I am writing this to you is because I managed to get my code working. _My_very_same_code_. In another project! This is insane! The very same code is loading textures in one project and does not do this in another! I took the good project from a book or something.
Now I have perused the properties of both projects and found NO differences. It simply works in the book's project and does not work in mine!
Could anyone possibly explain what is the matter?
If there is a way to send you the projects for comparison please tell me (I cannot find it).
Here is the code I use (u need to set the directories (DXSDK/Include and /Lib) and include d3d9.lib and d3dx9.lib to run it):

[code]
#include
#include

//--------------------------------------GLOBAL-VARIABLES----------------------------------------//

// WinAPI global variables:
HWND hWnd = NULL; // Our main window;
MSG msg; // Creates a MSG variable, needed for handling the events-messages in the window.
HRESULT hr; // Is needed to quickly check if a function succeded (optional).

// DX global interfaces:
LPDIRECT3D9 g_pD3DObject = NULL; // Declaring the Direct3D interface pointer (this allows to operate with D3D API);
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL; // Declaring the rendering device pointer (this allows our app to "see" the rendering hardware - GPU or CPU)


IDirect3DVertexBuffer9* gVB;
IDirect3DTexture9* g_pTexture;


struct vertex //Creating a temporary class for our vertices (needs to be changed).
{
float x, y, z;
// FLOAT nx, ny, nz; // Normal.
// D3DCOLOR diffuse; // Colour being diffused (most likely will not be used).
FLOAT u, v; // Texture coordinates.
};
#define VERTEXFVF (D3DFVF_XYZ|/*D3DFVF_DIFFUSE|*/D3DFVF_TEX1)




//----------------------------------GLOBAL-VARIABLES-FINSIHED------------------------------------//

// Functions needed to initialize, release or operate the WinAPI and DirectX modules.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HRESULT InitialiseDirectX(HWND hWnd)
{
// Create the D3D object
g_pD3DObject=Direct3DCreate9(D3D_SDK_VERSION);
if (g_pD3DObject==NULL)
return FALSE;

// Fill out some presentation parameters for running in a window
D3DPRESENT_PARAMETERS presParams;
ZeroMemory(&presParams,sizeof(presParams));

presParams.Windowed=TRUE;
presParams.SwapEffect=D3DSWAPEFFECT_DISCARD;
presParams.BackBufferFormat=D3DFMT_UNKNOWN;
presParams.BackBufferCount=1;

// Create the D3D device
HRESULT hr=g_pD3DObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &g_pD3DDevice);
if (FAILED(hr))
{
// Some cards may not do hardware vertex processing so fall back to software:
hr=g_pD3DObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presParams, &g_pD3DDevice);
if (FAILED(hr))
return FALSE;
}



// Filling a temporary array of vertices with the data.
vertex vertices[] =
{
{-3, -4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 0, 0},
{-3, 4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 0, 1},
{ 3, -4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 1, 0},
{ 3, 4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 1, 1},
};

// Creating a vertex buffer
void* vb_vertices;
g_pD3DDevice->CreateVertexBuffer(6*sizeof(vertex), D3DUSAGE_WRITEONLY,
VERTEXFVF, D3DPOOL_DEFAULT,
&gVB,NULL);

// Filling the actual vertex buffer.
gVB->Lock(0,sizeof(vertices),(void**)&vb_vertices,0);
memcpy(vb_vertices,vertices,sizeof(vertices));
gVB->Unlock();

D3DXMATRIX mWorld;

// Specifying the rendering options.
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
g_pD3DDevice->SetStreamSource(0, gVB, 0, sizeof(vertex));
g_pD3DDevice->SetFVF(VERTEXFVF);

// Filling a transposition matrix.
D3DXMatrixTranslation(&mWorld, 0, 0, 0);

// Showing the "World" matrix to the device.
g_pD3DDevice->SetTransform(D3DTS_WORLD, &mWorld);

D3DXCreateTextureFromFile(g_pD3DDevice, "Tree.bmp", &g_pTexture);
g_pD3DDevice->SetTexture(0, g_pTexture);


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++//

D3DXMATRIX matCam;
D3DXMatrixTranslation(&matCam, 0, 0, 100);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matCam);

D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 5000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);


//+++++++++++++++ Other rendering options +++++++++++++++//
// Setting the rendering texture.
//g_pD3DDevice->SetTexture(0, g_pTexture);



return TRUE;
}
void CleanupDirectX()
{
if (g_pD3DObject)
g_pD3DObject->Release();

if (g_pD3DDevice)
g_pD3DDevice->Release();
}
HRESULT InitWinAPI(HINSTANCE& hInstance, int& nCmdShow)
/**************************************************************************************************
Desc: Creates and registers window calss as well as the window itself
**************************************************************************************************/
{
// Describe and register our window class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0L;
wcex.cbWndExtra = 0L;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = 0;
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = "SomeWindowClass";
wcex.hIconSm = 0;

if (!RegisterClassEx(&wcex))
return 0;

// Create the window
hWnd=CreateWindow("SomeWindowClass", "WindowName", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

if (!hWnd)
return 0;

return TRUE;
}

//-----------------------------Section-for-the-User's-functions----------------------------------//


//--------------------------Section-for-the-User's-functions-finished----------------------------//



/**************************************************************************************************
Desc: This is the main entry point of the program.
**************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
//---------------------------------Initialization-phase--------------------------------------//
// Cleaning the memory for the msg variable;
ZeroMemory(&msg, sizeof(msg));

// Initialize and create the window.
InitWinAPI(hInstance, nCmdShow);

// Before displaying our window create the D3D device.
if (!InitialiseDirectX(hWnd))
return 0;

// Display the window and force an initial paint.
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//--------------------------------Initialization-finished------------------------------------//



while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET/* | D3DCLEAR_ZBUFFER*/,D3DCOLOR_XRGB(0,0,255),1.0f,0);
g_pD3DDevice->BeginScene();

g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);

g_pD3DDevice->EndScene();
g_pD3DDevice->Present(NULL,NULL,NULL,NULL);

}
}
// Clean up DirectX
CleanupDirectX();


return msg.wParam;
}
[/code]

Comments

  • nebgastnebgast Posts: 54Member
    Code works fine(other than the fact that the texture is upside down), make sure that the path to the image is correct. In the case of your code the image needs to be next to the .vcxproj file of the application. If it is somewhere else make sure to modify the file name to include the pathing to the location of the image.

    i.e. "../Tree.bmp" if the image is up one directory
Sign In or Register to comment.