Jump to content

  • Log In with Google      Sign In   
  • Create Account


Drawing a Texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
38 replies to this topic

#21 falcon93   Members   -  Reputation: 121

Like
1Likes
Like

Posted 11 July 2011 - 02:28 PM

I'll take a look at it tonight, I'll reply again as soon I get it working >hopefully< :wink:

Thanks a lot for helping.

Sponsor:

#22 yewbie   Members   -  Reputation: 665

Like
1Likes
Like

Posted 11 July 2011 - 02:34 PM

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.

#23 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 11 July 2011 - 05:02 PM

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?

#24 Aardvajk   Crossbones+   -  Reputation: 5968

Like
2Likes
Like

Posted 12 July 2011 - 01:49 AM

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?


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:

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.


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.

#25 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 July 2011 - 02:23 AM

@ Aardvajk:

I've been programming in C# and XNA before, I however never imagined that it would be this huge difference between these two languages. The use of variables, arrays etc got very fast into my mind when I started with C++, as it was so much like C#. The more "new" things tend to be much harder to learn though. I still havn't learned what a pointer is, and specially all these signs: * , **, &, -> and if I don't remember wrong, there's even a '?'. What do all these do? When I worked with classes in C#, it was so much easier:

public MyClass my_class = new MyClass();
my_class.variable = "value";

I have however not encountered a statement like that since I started with C++.

The MSDN Documentation, is that something I have on my computer or is it the webpages I find when I search for methods, variables etc on google?

So, to move on to the correct track. I use the MSDN Documentation (where do I find it?) when I already know what I'm going to use. But what if I want to start with something totaly new, will the documentation help me there too or should I use google then?

Thanks for helping Posted Image

#26 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 July 2011 - 05:36 AM

Ok, I've successfully called the GetLevelDesc method, and I can now access the width and height.


I now use the width and height of the texture to set the distance between the vertices:

vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].X  = x;
vertices[0].Y  = y;
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  = x + surface.Width;
vertices[1].Y  = y;
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  = x + surface.Width;
vertices[2].Y  = y + surface.Height;
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  = x;
vertices[3].Y  = y + surface.Height;
vertices[3].Z  = 0;
vertices[3].RHW  = 1;
vertices[3].U  = 0.0f;
vertices[3].V  = 1.0f;


The texture itself is in normal scale, but the black quad is much bigger:

Posted Image


The width of the texture is 575 pixels. To check the width of the texture in-game I also made a small if-statement:

if (surface.Width > 575)
{
 MessageBox(NULL, L"Width is more than 575 pixels", NULL, NULL);
}

The messagebox pop up when I run the program. I've really searched around, trying to solve this problem myself, but I can't find anything helpfull :(

Anyone of you that have an idea?



#27 yewbie   Members   -  Reputation: 665

Like
0Likes
Like

Posted 12 July 2011 - 09:11 AM

So your wanting to know why the quad is drawing larger than the texture?
Thats really strange because your texture coordinates it should stretch to cover the whole quad that you specified.

What is it reading for the texture size?

#28 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 July 2011 - 12:17 PM

Yes, why is the black quad bigger than my texture, when I set the quads size from the size of the texture?

I'm not really sure what code part you are asking for, so I'll just paste my whole current code below. If you are looking for my D3DXCreateTextureFromFileEx method, it's on line 120.

[source lang="cpp"]#include <Windows.h>#include <d3d9.h>#include <d3dx9tex.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;D3DSURFACE_DESC surface;float x = 10;float y = 10;struct CUSTOMVERTEX { float X, Y, Z, RHW; D3DCOLOR COLOR; float U, V; };#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)CUSTOMVERTEX vertices[4];void InitializeD3D(HWND hWnd);void InitializeGraphics(void);void Update(void);void Draw(void);void Clean(void);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){D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture);texture->GetLevelDesc(0, &surface);d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);vertices[0].X = x;vertices[0].Y = y;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 = x + surface.Width;vertices[1].Y = y;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 = x + surface.Width;vertices[2].Y = y + surface.Height;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 = x;vertices[3].Y = y + surface.Height;vertices[3].Z = 0;vertices[3].RHW = 1;vertices[3].U = 0.0f;vertices[3].V = 1.0f;if (surface.Width > 575){ MessageBox(NULL, L"Width is more than 575 pixels", NULL, NULL);}}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->SetRenderState(D3DRS_LIGHTING, FALSE); d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); d3ddev->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA); d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); d3ddev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );*/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]

#29 yewbie   Members   -  Reputation: 665

Like
1Likes
Like

Posted 12 July 2011 - 12:28 PM

Your textures need to be in power of 2 sizes for them to work properly.
32x32, 64x64, 128x128, 256x256, 512x512 etc.

if you resize your image to 512x512 with your code it works fine.

I think what directx is doing is scaling the image to make it fix a power of 2 in some way.
I ran it , its up-scaling it to 1024x1024

#30 falcon93   Members   -  Reputation: 121

Like
1Likes
Like

Posted 12 July 2011 - 01:15 PM

Yes, I tried scaling my image to 512x512 and it worked just fine. I also noticed, using D3DX_DEFAULT_NONPOW2 for the width and height, will not scale the image. Thanks very much for the help :)


May I also ask you; You game me these render settings before, what are they actually doing?

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); 
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); 
d3ddev->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);
d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); 
d3ddev->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );

I noticed they didn't do any difference on just an ordinary image, but maybe they can give some cool results with other types of textures?

Thanks for helping :)

#31 yewbie   Members   -  Reputation: 665

Like
1Likes
Like

Posted 12 July 2011 - 02:59 PM

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
this turns off direct3d lighting because we are doing it manually.

d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
This enables the alpha channel, so if you have multiple overlapping textures if you change the alpha it will make one of them transparent/

d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
This inverts the source alpha value, and tells us to use destination for blending, im not 100% on these

d3ddev->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);
This I think tells directx to use the alpha of the source and destion on a blend, im not 100% on these

d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3ddev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
Not sure on these but I did need them for some reason

Here is a link to to an explantion on some of these:
http://www.toymaker.info/Games/html/texture_states.html

or even better:

http://msdn.microsoft.com/en-us/library/bb172599%28v=vs.85%29.aspx

#32 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 July 2011 - 03:19 PM

Ok, thanks very much for the explanation, and I'll take a closer look at the links you gave me :)


I'm renaming this thread from "I'm stuck" to "Drawing a texture" as it fits better from the purpose of this thread. I'll continue on with some keyboard input, 2D animations (sprite sheets) and sounds, lets see how that goes. Anyway, thanks so very much for all help :)

#33 yewbie   Members   -  Reputation: 665

Like
0Likes
Like

Posted 12 July 2011 - 04:03 PM

Good luck, happy programming!

#34 Aardvajk   Crossbones+   -  Reputation: 5968

Like
2Likes
Like

Posted 13 July 2011 - 10:35 AM

I still havn't learned what a pointer is, and specially all these signs: * , **, &, -> and if I don't remember wrong, there's even a '?'. What do all these do? When I worked with classes in C#, it was so much easier.


I would strenuously advise that Direct3D API is a bad place to try to learn about these things and that you spend a small amount of time with some simpler examples to get your head around C++ pointers. Due to the COM nature of the DX APIs (they can work with multiple languages, including C sadly) an understanding of pointers is quite important.

The MSDN Documentation, is that something I have on my computer or is it the webpages I find when I search for methods, variables etc on google?

So, to move on to the correct track. I use the MSDN Documentation (where do I find it?) when I already know what I'm going to use. But what if I want to start with something totaly new, will the documentation help me there too or should I use google then?


MSDN is online. But just Googling "GetLevelDesc" has the relevant link as the first choice.

#35 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 14 July 2011 - 12:37 PM

Do you know any good tutorials that covers mostly about pointers and maybe even provide some exersizes so I can check if I've understood it well?

Thanks for the MSDN link Posted Image I can't find the navigation path to DirectX though. Any ideas?

#36 yewbie   Members   -  Reputation: 665

Like
1Likes
Like

Posted 14 July 2011 - 12:44 PM

Thanks for the MSDN link Posted Image I can't find the navigation path to DirectX though. Any ideas?


I start from here:
http://msdn.microsoft.com/en-us/library/bb174336%28VS.85%29.aspx

#37 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 14 July 2011 - 04:04 PM

I start from here:
http://msdn.microsof...28VS.85%29.aspx




Thanks so much. I just noticed that the MSDN is like a gold cave full of information!

What about the pointers? Do you know any good tutorials that covers mostly about pointers? Exersizes would be great too Posted Image

#38 yewbie   Members   -  Reputation: 665

Like
1Likes
Like

Posted 15 July 2011 - 08:30 AM

Thanks so much. I just noticed that the MSDN is like a gold cave full of information!

What about the pointers? Do you know any good tutorials that covers mostly about pointers? Exersizes would be great too Posted Image


http://www.cplusplus.com/doc/tutorial/pointers/

Cplusplus.com has some really detailed infromation.

#39 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 15 July 2011 - 08:44 AM

Thanks a lot for that link, I'll read it and do some pointer exersizes in a console app.

Thanks very much! Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS