Drawing a Texture

Started by
37 comments, last by falcon93 12 years, 10 months ago
Anyone that can help me please? 406 views but only 2 people have cared to answer? :(


I have took a closer look to my problem, and I know that I'm doing something wrong in this method, as the method fails. Could anyone please help me, that would be very much appreciated :unsure: The method is pasted below:



if(FAILED(D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture)))
{
vertices[0].X = 0;
}


I change the x-position of the first vertex just to see if it fails, and as the vertex is set to x = 0, I assume that the method fails?
Advertisement
Check the return value your getting from it:


Return Value
Type: HRESULT

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_NOTAVAILABLE, D3DERR_OUTOFVIDEOMEMORY, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.




http://msdn.microsof...v=vs.85%29.aspx

Also it won't change your vertices, all it does is apply that texture to the face of your vertices.
Also I just noticed your
vertices[x].U = 0;
vertices[x].V = 0;


Are not set properly on your vertices

look back at my code those need to be specified for the texture coordinates thats probably whats going on.
Firstly, I've fixed the vertices initialize code. I changed "0" to "0.0f", if it was that you meant? it did however no difference:


vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].X = 200;
vertices[0].Y = 200;
vertices[0].Z = 0;
vertices[0].RHW = 1;
vertices[0].U = 0.0f;
vertices[0].V = 0.0f;

vertices[1].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[1].X = 500;
vertices[1].Y = 200;
vertices[1].Z = 0;
vertices[1].RHW = 1;
vertices[1].U = 1.0f;
vertices[1].V = 0.0f;

vertices[2].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[2].X = 500;
vertices[2].Y = 500;
vertices[2].Z = 0;
vertices[2].RHW = 1;
vertices[2].U = 1.0f;
vertices[2].V = 1.0f;

vertices[3].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[3].X = 200;
vertices[3].Y = 500;
vertices[3].Z = 0;
vertices[3].RHW = 1;
vertices[3].U = 0.0f;
vertices[3].V = 1.0f;




I've also put together an error check for the hresult as I couldn't find a way to simply write the text error in a textbox:


HRESULT hRes = D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture);

switch (hRes)
{
case D3D_OK: MessageBox(NULL, L"Success", NULL, NULL); break;
case D3DERR_INVALIDCALL: MessageBox(NULL, L"D3D - Invalid call", NULL, NULL); break;
case D3DERR_NOTAVAILABLE: MessageBox(NULL, L"D3D - Not available", NULL, NULL); break;
case D3DERR_OUTOFVIDEOMEMORY: MessageBox(NULL, L"D3D - Out of video memory", NULL, NULL); break;
case D3DXERR_INVALIDDATA: MessageBox(NULL, L"D3DX - Invalid data", NULL, NULL); break;
case E_OUTOFMEMORY: MessageBox(NULL, L"E - Out of memory", NULL, NULL); break;
}



When I run the program, the Message box says "D3DX - Invalid data". What does that mean and how can I solve it?
In the code snipit above you had them all initialized to zero such as:
ertices[3].U = 0;
vertices[3].V = 0;

but you have it correct in what you just posted.


But this thread is relevant, it seems like its not finding your texture.
http://www.gamedev.n...ding-a-texture/

If your using visual studio 2010 you need to place your texture in the root project directory for your project instead of the debug directory. (Under default settings)
Also note that it is case sensitive so it won't find "Texture.jpg" or "texture.JPG" unless your specify.
Yes, I'm using Visual Studio 2010 and I have my project folder saved on my desktop. When I open my project folder, I have 3 folders and two other files:

Debug (folder)
ipch (folder)
MyFirstGame (folder)
MyFirstGame (SQL Database File)
MyFirstGame (Visual Studio Solution)

I've tried to place my "texture.jpg" image here, but it didn't do any difference.


I also tried placing the "texture.jpg" image in these three defference locations, but it doesn't do any difference either:
Debug\
MyFirstGame\
MyFirstGame\Debug\

None of these locations does any difference, what am I doing wrong?

Ok, so I tried placing the "texture.jpg" image in all four directories at the same time, and then it worked with the message box saying "Success". So tried deleting one of the images untill I just had one left and the message box still said "Success". I came up that the image needs to be placed in MyFirstGame\MyFirstGame\


However, don't be surprised, but it still doesn't work correctly. The message box says "Success", which means that the hresult gives no errors, but this is what my result is:


MyFirstGame - Window:
mlgtix.png


Texture - "texture.jpg":
2z8vsqc.jpg


Please help, any ideas?
Removed: not relevant anymore =p
Strange if it loaded it, it should be fine.

Also depending on your video card textures need to be in power of 2 sizes: 32x32, 64x64, 512x512 etc.
Unless your video card supports them, although I think Directx might take care of this for you if the texture is a strange size.

Could you post your entire code here so I can try to compile it?

edit: you know what im using some different texture states, trying adding these:

//Setup rendering states
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);

d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
Ok, I created a new folder on my desktop called "Game" and pasted the .exe and image inside. I run the .exe but still get the same results, message box saying "Success", but a strange result as on the screenshot :(


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


Here's my entire code (I have just one code file, main.cpp):

[source lang="cpp"]
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9tex.h>
#include <stdio.h>
#include <string>
#include <sstream>


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600


#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")


LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;


IDirect3DTexture9 *texture;


void InitializeD3D(HWND hWnd);
void InitializeGraphics(void);
void Update(void);
void Draw(void);
void Clean(void);


struct CUSTOMVERTEX { float X, Y, Z, RHW, U, V; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)


CUSTOMVERTEX vertices[4];


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"MFGCLASS";
wc.style = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL, L"MFGCLASS", L"MyFirstGame", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, nCmdShow);

InitializeD3D(hWnd);

while(true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if (msg.message == WM_QUIT) break;

Update();
Draw();
}

Clean();

return msg.wParam;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY: PostQuitMessage(0); return 0; break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}


void InitializeD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;

d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

InitializeGraphics();
}


void InitializeGraphics(void)
{
d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].X = 200;
vertices[0].Y = 200;
vertices[0].Z = 0;
vertices[0].RHW = 1;
vertices[0].U = 0.0f;
vertices[0].V = 0.0f;

vertices[1].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[1].X = 500;
vertices[1].Y = 200;
vertices[1].Z = 0;
vertices[1].RHW = 1;
vertices[1].U = 1.0f;
vertices[1].V = 0.0f;

vertices[2].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[2].X = 500;
vertices[2].Y = 500;
vertices[2].Z = 0;
vertices[2].RHW = 1;
vertices[2].U = 1.0f;
vertices[2].V = 1.0f;

vertices[3].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[3].X = 200;
vertices[3].Y = 500;
vertices[3].Z = 0;
vertices[3].RHW = 1;
vertices[3].U = 0.0f;
vertices[3].V = 1.0f;

HRESULT hRes = D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture);

switch (hRes)
{
case D3D_OK: MessageBox(NULL, L"Success", NULL, NULL); break;
case D3DERR_INVALIDCALL: MessageBox(NULL, L"D3D - Invalid call", NULL, NULL); break;
case D3DERR_NOTAVAILABLE: MessageBox(NULL, L"D3D - Not available", NULL, NULL); break;
case D3DERR_OUTOFVIDEOMEMORY: MessageBox(NULL, L"D3D - Out of video memory", NULL, NULL); break;
case D3DXERR_INVALIDDATA: MessageBox(NULL, L"D3DX - Invalid data", NULL, NULL); break;
case E_OUTOFMEMORY: MessageBox(NULL, L"E - Out of memory", NULL, NULL); break;
}
}


void Update(void)
{
VOID* pVoid;

v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}


void Draw(void)
{
d3ddev->Clear(9, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

d3ddev->BeginScene();

d3ddev->SetFVF(CUSTOMFVF);

d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

d3ddev->SetTexture(0, texture);

d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);
}


void Clean(void)
{
v_buffer ->Release();
d3ddev ->Release();
d3d ->Release();
}
[/source]




edit: you know what im using some different texture states, trying adding these:

//Setup rendering states
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);

d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );



Added these lines in my draw method, now the black box has disapeared, but the red "lines" are still there instead of the texture :(

This topic is closed to new replies.

Advertisement