Sign in to follow this  
falcon93

Drawing a Texture

Recommended Posts

falcon93    121
Hi everyone!

Well, I'm stuck. I've tried learning the basics in DirectX 11 programming for about 3 weeks now, but so far, the only thing I've actually managed to do is a window. I've followed plenty of tutorials about initializing DirectX and then draw a picture on my window, but every try has just resulted in errors or crashs. I've rewritten my code so many times now that I've lost count... I've been programming in C# and XNA beore, but I never realised that it would be this huge difference between them. I would be so happy if someone could actually help me out from this swamp :(

As said, I've managed to write the code for a 800x600 window that successfully shows up. My goal now is just to draw i simple image in that window. I know that I have to somehow initialize DirectX and then create a sort of variable which will hold my texture, load the texture and finally draw it. I know that it isn't just that easy, for example I have to somehow create a square from 2 sets of triangles. The problem is that the tutorials I've found is just like a big shock to me, and they doesn't even work. I mean, 300-400 lines of code shouldn't be necessary for just drawing a simple image, even if it's C++ and DirectX, right?

I've posted my current code below. Could anyone please tell me, with maybe an example, the [i]most simple [/i]way of drawing an image on my window? All help would be really nice, but examples and even step-by-step help would be even more helpfull :wink:

Please help,
Falcon93

[code]<BR>#include <Windows.h><BR><BR><BR>LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);<BR><BR><BR>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)<BR>{<BR>WNDCLASSEX wc;<BR>HWND hWnd;<BR>MSG msg;<BR><BR>ZeroMemory(&wc, sizeof(WNDCLASSEX));<BR><BR>wc.cbSize = sizeof(WNDCLASSEX);<BR>wc.hbrBackground = (HBRUSH)COLOR_WINDOW;<BR>wc.hCursor = LoadCursor(NULL, IDC_ARROW);<BR>wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);<BR>wc.hInstance = hInstance;<BR>wc.lpfnWndProc = WindowProc;<BR>wc.lpszClassName = L"MFGCLASS";<BR>wc.style = CS_VREDRAW | CS_HREDRAW;<BR><BR>RegisterClassEx(&wc);<BR><BR>hWnd = CreateWindowEx(NULL, L"MFGCLASS", L"MyFirstGame", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);<BR><BR>ShowWindow(hWnd, nCmdShow);<BR><BR>while(true)<BR>{<BR> if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))<BR> {<BR> TranslateMessage(&msg);<BR> DispatchMessage(&msg);<BR><BR> if (msg.message == WM_QUIT) break;<BR> }<BR><BR> else<BR> {<BR> }<BR>}<BR><BR>return msg.wParam;<BR>}<BR><BR><BR>LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<BR>{<BR>switch(message)<BR>{<BR> case WM_DESTROY: PostQuitMessage(0); return 0;<BR>}<BR><BR>return DefWindowProc(hWnd, message, wParam, lParam);<BR>}<BR>[/code]

Share this post


Link to post
Share on other sites
dpadam450    2357
You should get a template code file for a basic DX program. There should be one that displays a triangle somewhere. Download it and use it for your starting point. None of the code you posted is actually DX and is just hard to understand windows code.

Also, get a damn book. Grabbing parts of information from random people is not going to give you a clear step by step goal to a final understanding. A book will say draw a triangle, ok next chapter you should know how to draw triangle, add an image on it, cool you know this now do this. books.

Share this post


Link to post
Share on other sites
yewbie    677
Its kind of a loaded question =p

Using just DirectX11 there isn't really going to be a "simple" way of drawing your texture to your screen.

I personally cannot comment on specifics of DX11, but in DX9 you need to do the following to get a simple image to draw onto the screen using 3d as a 2d render (3d is a lot more complex)

- Get your vertex format setup
- Determine hardware capabilities
- Create your device
- Create vertex buffer
- Load a texture from file with D3DXCreateTextureFromFileEx
- BeginScene()
- Lock your vertex buffer
- Setup your vertices
- Unlock your vertex buffer
- Set your texture
- Draw your primitives
- EndScene()
- Present()

Its quite abit of setup and it took me awhile to get it working, many things could have changed with DX11.
Also keep in mind that this is setting up Dx9 to render as a 2d render in 3d so it may not apply to use but its a good example of what is involved.

Articles like this one helped me alot:
http://www.gamedev.net/page/resources/_/reference/programming/directx/directx-graphics/2d-rendering-in-directx-8-r1434

Share this post


Link to post
Share on other sites
falcon93    121
Ok, so I've jumped back two stages and today I tried creating a triangle out of three vartices with DX9 instead. However, now I've discovered a probelm that isn't related to programming, and I think that this is one thing that may have messed up my work before. The problem is between my two computers, my laptop and my desktop.

Yesterday when I just wrote the code for the window, it worked perfectly and then I worked on my desktop. However, today I continued my work on the laptop, and followed this tutorial: [url="http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B.aspx"]http://www.directxtu...asics/dx9B.aspx[/url]

I successfully created that colored triangle on my laptop, and built the program without any problems. When I later was going to continue the work on my desktop, I couldn't build the program anymore. It still works on my laptop, but not on my desktop. The errors I get on my desktop is:


[quote name="Visual Studio - Output Window"]1>Main.obj : error LNK2019:
unresolved external symbol _Direct3DCreate9@4 referenced in function "void
__cdecl initD3D(struct HWND__*)" (initD3D@@YAXPAUHWND__@@@Z)[/quote]


[quote name="Visual Studio - Error Window - Error #1"]
error LNK2019: unresolved external symbol _Direct3DCreate9@4 referenced in
function "void __cdecl initD3D(struct HWND__ *)"
(?initD3D@@YAXPAUHWND__@@@Z) C:\Users\Simon
Blom\Desktop\MyFirstGame\MyFirstGame\Main.obj[/quote]


[quote name="Visual Studio - Error Window - Error #1"]
error LNK1120: 1 unresolved externals C:\Users\Simon
Blom\Desktop\MyFirstGame\Debug\MyFirstGame.exe[/quote]


Please help me, what is wrong? I have installed the DirectX SDK (June 2010) on both computers and I've set up the directories the same on both computers aswell. What is wrong? :(

Share this post


Link to post
Share on other sites
yewbie    677
I am unfamiliar with building things with PYGame, but it looks like your library was not included on the machine spitting the errors
is visual studio you would need to tell it to link d3d9.lib d3dx9.lib

I am thinking thats the problem you are running into.

Share this post


Link to post
Share on other sites
falcon93    121
The errors on my desktop computer has been solved, stupid me :wink: My laptop runs x86 Windows 7, while my desktop runs x64 Windows 7. Due to this, I thought that I could use the x64 library file on my desktop, instead for the x86 library file I used on my laptop. That was the problem. I changed the Library Directories path to:
[i]Microsoft DirectX SDK (June 2012)\Lib\x86[/i] and now it works.

Ok, so back to the programming. I've followed a tutorial (DirectX 9) and managed to initialize DirectX, creating a triangle from 3 vertices and color it. However the tutorial then justs goes stright into 3D programming instead of telling how to create a quad and give it a texture. I've pasted my new code below, and I think that I've now completed most of the steps you gave me Yewbie. How will I now create a quad and assign a texture to it?


[code]
#include <Windows.h>
#include <d3d9.h>


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600


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


LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;


void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void);


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


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);

initD3D(hWnd);

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

if (msg.message == WM_QUIT) break;

render_frame();
}

cleanD3D();

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 initD3D(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);

init_graphics();
}


void render_frame(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->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

d3ddev->EndScene();

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


void cleanD3D(void)
{
v_buffer->Release();
d3ddev->Release();
d3d->Release();
}


void init_graphics(void)
{
CUSTOMVERTEX vertices[] =
{
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

VOID* pVoid;

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

Share this post


Link to post
Share on other sites
falcon93    121
Hidden
[quote name='falcon93' timestamp='1310147454' post='4832866']
The errors on my desktop computer has been solved, stupid me :wink: My laptop runs x86 Windows 7, while my desktop runs x64 Windows 7. Due to this, I thought that I could use the x64 library file on my desktop, instead for the x86 library file I used on my laptop. That was the problem. I changed the Library Directories path to:
[i]Microsoft DirectX SDK (June 2012)\Lib\x86[/i] and now it works.

Ok, so back to the programming. I've followed a tutorial (DirectX 9) and managed to initialize DirectX, creating a triangle from 3 vertices and color it. However the tutorial then justs goes stright into 3D programming instead of telling how to create a quad and give it a texture. I've pasted my new code below, and I think that I've now completed most of the steps you gave me Yewbie. How will I now create a quad and assign a texture to it?


[code]
#include <Windows.h>
#include <d3d9.h>


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600


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


LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;


void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void);


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


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);

initD3D(hWnd);

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

if (msg.message == WM_QUIT) break;

render_frame();
}

cleanD3D();

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 initD3D(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);

init_graphics();
}


void render_frame(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->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

d3ddev->EndScene();

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


void cleanD3D(void)
{
v_buffer->Release();
d3ddev->Release();
d3d->Release();
}


void init_graphics(void)
{
CUSTOMVERTEX vertices[] =
{
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

VOID* pVoid;

v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}
[/code]
[/quote]

Share this post


Link to post
yewbie    677
I use the following for my custom vertex format:
[code]
//Custom vertex format
const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW| D3DFVF_DIFFUSE | D3DFVF_TEX1;
//Custom vertex
struct TLVERTEX
{
float x;
float y;
float z;
float rhw;
D3DCOLOR colour;
float u;
float v;
};[/code]

And something like this will make a square:
[code]
int x = 50;
int y = 50;
vertexBuffer->Lock(0, 0, (void **)&vertices, NULL);//Lock the vertex buffer
TLVERTEX* vertices; //my vertices
vertices[0].colour = D3DCOLOR_ARGB (255,255,255,255);
vertices[0].x = x;
vertices[0].y = y;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;//1 - 0
vertices[0].v = 0.0f;//1 - 0

vertices[1].colour = D3DCOLOR_ARGB (255,255,255,255);
vertices[1].x = x + 64;
vertices[1].y = y;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;//0 - 1
vertices[1].v = 0.0f;//0 - 0

vertices[2].colour = D3DCOLOR_ARGB (255,255,255,255);
vertices[2].x = x + 64;
vertices[2].y = y + 64;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;//0 - 1
vertices[2].v = 1.0f;//0 - 0

vertices[3].colour = D3DCOLOR_ARGB (255,255,255,255);
vertices[3].x = x;
vertices[3].y = y + 64;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;//0 - 1
vertices[3].v = 1.0f;//0 - 0
vertexBuffer->Unlock();//Unlock the vertex buffer

d3dDevice->SetTexture ( 0, MyTexture );
d3dDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);[/code]

MyTexture can be obtained via


[code]
IDirect3DTexture9 *d3dTexture;
D3DCOLOR colorkey = 0xFFFF00FF;
colorkey = D3DCOLOR_XRGB(0,0,0);
// Load image from file
if (FAILED(D3DXCreateTextureFromFileEx (D3d, "Texture.jpg", 0,0, 1, 0,D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, 0,colorkey, NULL, NULL, &d3dTexture)))
{
cout << "Failed to load texture" << endl;
return;
}
[/code]

So whats going on here is this:
The U and the V are the texture coordinates thats how we specify how the texture is drawn to the surface of our triangles, notice I used triangle fan to draw them so we only have to create 4 vertexes.

Share this post


Link to post
Share on other sites
falcon93    121
Thanks very much for the code samples. I've successfully managed to create a quad from 4 vertices now. However, I have a small problem with the D3DXCreateTextureFromFileEx method. I've dubble checked all parameters but it still gives me these two errors when I compile:


[quote name="Visual Studio - Error List Window - Error #1"]
error LNK2019: unresolved external symbol _D3DXCreateTextureFromFileExW@56 referenced in function "void __cdecl InitializeGraphics(void)" (?InitializeGraphics@@YAXXZ) C:\Users\Simon
Blom\Desktop\MyFirstGame\MyFirstGame\Main.obj[/quote]


[quote name="Visual Studio - Error List Window - Error #2"]
error LNK1120: 1 unresolved externals C:\Users\Simon Blom\Desktop\MyFirstGame\Debug\MyFirstGame.exe 1
[/quote]


I've pasted my code below so you can check if everything is correct. The method is located on line 150. And thanks very much for helping me :)


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


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600


#pragma comment (lib, "d3d9.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;
vertices[0].V = 0;

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 = 0;
vertices[1].V = 0;

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 = 0;
vertices[2].V = 0;

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;
vertices[3].V = 0;

D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, 0, NULL, NULL, NULL, &texture);
}


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]

Share this post


Link to post
Share on other sites
falcon93    121
Ok, so I've managed to solve my problem with the build error. The problem was that I hadn't referenced to the d3dx9 library, so I simply add this snippet:

[code]
#pragma comment (lib, "d3dx9.lib")
[/code]


However, now I have a new problem again (yes, it seems the problems don't want to let go from me). I can successfully build my program now, but the texture isn't showing up, just my quad in black color. I've tried to put the .jpg image in both my project folder and the debug folder, but none makes any difference. What have I done wrong? This is my current code:


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


#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;
vertices[0].V = 0;

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 = 0;
vertices[1].V = 0;

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 = 0;
vertices[2].V = 0;

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;
vertices[3].V = 0;

D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, 0, NULL, NULL, NULL, &texture);
}


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]

Share this post


Link to post
Share on other sites
falcon93    121
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:


[code]
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;
}
[/code]

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?

Share this post


Link to post
Share on other sites
yewbie    677
Check the return value your getting from it:


[b]Return Value[/b]
Type: [b][url="http://msdn.microsoft.com/en-us/library/bb401631.aspx"][b]HRESULT[/b][/url][/b]

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.




[url="http://msdn.microsoft.com/en-us/library/bb172802%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url]

Also it won't change your vertices, all it does is apply that texture to the face of your vertices.

Share this post


Link to post
Share on other sites
yewbie    677
Also I just noticed your
[code]vertices[x].U = 0;
vertices[x].V = 0;[/code]

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.

Share this post


Link to post
Share on other sites
falcon93    121
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:

[code]
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;
[/code]



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:

[code]
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;
}
[/code]


When I run the program, the Message box says "D3DX - Invalid data". What does that mean and how can I solve it?

Share this post


Link to post
Share on other sites
yewbie    677
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.
[url="http://www.gamedev.net/topic/310939-d3dxerr_invaliddata-when-loading-a-texture/"]http://www.gamedev.n...ding-a-texture/[/url]

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.

Share this post


Link to post
Share on other sites
falcon93    121
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?

Share this post


Link to post
Share on other sites
falcon93    121
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:
[img]http://i55.tinypic.com/mlgtix.png[/img]


Texture - "texture.jpg":
[img]http://i53.tinypic.com/2z8vsqc.jpg[/img]


Please help, any ideas?

Share this post


Link to post
Share on other sites
yewbie    677
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:

[code] //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 );[/code]

Share this post


Link to post
Share on other sites
falcon93    121
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]



[quote name='yewbie' timestamp='1310411767' post='4833917']
edit: you know what im using some different texture states, trying adding these:

[code] //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 );[/code]
[/quote]

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

Share this post


Link to post
Share on other sites
yewbie    677
Oh wait I think I might just be really dumb.

You used a DWORD for your color instead of D3DCOLOR

Actually its the ordering, it must be in this order

struct CUSTOMVERTEX { float X, Y, Z, RHW; D3DCOLOR COLOR; float U, V; };

just change that line and your code should work.

because when we declare this:
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)

Its telling direct X that we want (raw coordinates, color, texture cordianates)
You were giving it (Raw cordinates, texture cordinates, and dword color) and it was freaking out.

Share this post


Link to post
Share on other sites
falcon93    121
Yes, that worked really good. Now my texture is atleast showing up. Thank you very much :)


The texture however is not scaled, but I guess that is something I'll have to program, in other words, it's not an error. I guess that I'll have to load the texture before I create the quad, and then get the dimensions of the image and by this place the vertices enough far from each other to get the correct scale? I read that IDirect3DTexture9::GetLevelDesc(); can get me information about the textures size etc. But how do I use it? I've created a D3DSURFACE_DESC variable, but I cannot save info like this:

d3dsurface_desc = IDirect3DTexture9::GetLevelDesc(0);

How do I save the return value correct?

Share this post


Link to post
Share on other sites
Aardvajk    13207
[quote name='falcon93' timestamp='1310425328' post='4834044']
The texture however is not scaled, but I guess that is something I'll have to program, in other words, it's not an error. I guess that I'll have to load the texture before I create the quad, and then get the dimensions of the image and by this place the vertices enough far from each other to get the correct scale? I read that IDirect3DTexture9::GetLevelDesc(); can get me information about the textures size etc. But how do I use it? I've created a D3DSURFACE_DESC variable, but I cannot save info like this:

d3dsurface_desc = IDirect3DTexture9::GetLevelDesc(0);

How do I save the return value correct?
[/quote]

With respect, the first step is to learn to read the documentation. If you keep having to post and await responses for things like this, it will take you years to get anywhere.

GetLevelDesc() is also a method that needs to be invoked on a texture instance, where as you are trying to call it as a static method above. This approach of trying to "guess" correct syntax will make your progress slow to a crawl and lead to naught but frustration.

Look at GetLevelDesc() in the docs. It does not return a surface description. It takes a pointer to a surface description structure as the second argument and returns a HRESULT that indicates success or failure:

[quote name="MSDN docs"]
HRESULT GetLevelDesc(
UINT Level,
D3DSURFACE_DESC * pDesc
);
Parameters
Level
[in] Identifies a level of the texture resource. This method returns a surface description for the level specified by this parameter.
pDesc
[out] Pointer to a D3DSURFACE_DESC structure, describing the returned level.
Return Values
If the method succeeds, the return value is D3D_OK. D3DERR_INVALIDCALL is returned if one of the arguments is invalid.
[/quote]

So:

[source lang="cpp"]
void f()
{
D3DSURFACE_DESC desc;

HRESULT res=tex->GetSurfaceDesc(0,&desc);

if(!FAILED(res))
{
// desc should now be correctly filled out
}
}
[/source]

Hope this helps, but your post above suggests that perhaps your fundamental C++ needs to be improved before you can hope to have any success with a library as complex as D3D.

[EDIT] Hmm, so a lower-case r in paretheses is converted to the registered trademark symbol, even in a source block, eh? Helpful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this