Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Load Texture Problem

This topic is 5241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a quite trouble problem, Here is my code, I am a new user and I want to write my program step by step, I now to write a texture 3d program. Now, I start to load the texture. I have a texture called "1.bmp" in the same folder of the program, but when I run my follow program, it has problem, can anyone tell me what am I wrong? Please help me and thx so much #include <windowsx.h> #include <d3d9.h> #include <d3dx9.h> #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE) bool InitWindow(HINSTANCE); LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); HRESULT InitD3D(HWND,const char *); HRESULT InitD3DVertex(); void Render3D(); void SetWorld(); void SetView(); void SetProject(); LPDIRECT3D9 p_D3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 p_D3DD = NULL; // Our rendering device LPDIRECT3DVERTEXBUFFER9 p_D3DVB = NULL; LPDIRECT3DTEXTURE9 p_D3DT = NULL; HWND hWnd; struct MYVERTEX { float x, y, z; DWORD color; }; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { if (InitWindow(hInstance) == false) return 0; if (FAILED(InitD3D(hWnd, "1.bmp"))) return 0; MSG msg; ZeroMemory(&msg, sizeof(MSG)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { Render3D(); } } return 0; } bool InitWindow(HINSTANCE hInstance) { WNDCLASSEX wcx; wcx.cbClsExtra = 0; wcx.cbSize = sizeof(WNDCLASSEX); wcx.cbWndExtra = 0; wcx.hbrBackground = NULL; wcx.hCursor = LoadCursor(NULL, IDI_APPLICATION); wcx.hIcon = NULL; wcx.hIconSm = NULL; wcx.hInstance = hInstance; wcx.lpfnWndProc = WindowProc; wcx.lpszClassName = "Maze"; wcx.lpszMenuName = NULL; wcx.style = CS_CLASSDC; if (!RegisterClassEx(&wcx)) return false; hWnd = CreateWindowEx(NULL,"Maze","Maze Game", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL); if (hWnd == NULL) return false; ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); return true; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch( uMsg ) { case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, uMsg, wParam, lParam ); } HRESULT InitD3D(HWND hWnd, const char *szTextureFilePath) { if ((p_D3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if (FAILED(p_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &p_D3DD))) { MessageBox(hWnd, "Device", "D3D", MB_OK); return E_FAIL; } p_D3DD->SetRenderState( D3DRS_LIGHTING, false ); p_D3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); p_D3DD->SetRenderState( D3DRS_ZENABLE, TRUE ); MessageBox(NULL,szTextureFilePath, "Test", MB_OK); if( FAILED( D3DXCreateTextureFromFile( p_D3DD, szTextureFilePath, &p_D3DT ) ) ) { MessageBox(NULL,"Test", "Test", MB_OK); return E_FAIL; } MessageBox(NULL,"Test", "Test", MB_OK); if (FAILED(InitD3DVertex())) return E_FAIL; return S_OK; } HRESULT InitD3DVertex() { MYVERTEX vertex[] = { //Bottom { -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0) }, { -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0) }, { 5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255) }, { 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(255, 255, 255) }, { 0.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255) }, { -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0) }, { 5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0) }, { 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) }, { -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) }, //­n¥hµf²Ä¤@­ÓÂI { -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 255, 0) }, // { -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0) }, // { -5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0) }, // { 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) }, // { 5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 255, 255) }, }; void *p_Vertex; if (FAILED(p_D3DD->CreateVertexBuffer(10*sizeof(vertex), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &p_D3DVB, NULL))) return E_FAIL; if (FAILED(p_D3DVB->Lock(0, sizeof(vertex), (void**)&p_Vertex, 0 ))) return E_FAIL; memcpy(p_Vertex, vertex, sizeof(vertex)); p_D3DVB->Unlock(); return S_OK; } void Render3D() { p_D3DD->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0L); if (SUCCEEDED(p_D3DD->BeginScene())) { SetWorld(); SetView(); SetProject(); // p_D3DD->SetTexture(0, p_D3DT); p_D3DD->SetStreamSource(0, p_D3DVB, 0, sizeof(MYVERTEX)); p_D3DD->SetFVF(D3DFVF_CUSTOMVERTEX); p_D3DD->DrawPrimitive(D3DPT_TRIANGLESTRIP,0, 2); p_D3DD->DrawPrimitive(D3DPT_TRIANGLEFAN, 4, 4); p_D3DD->EndScene(); // p_D3DD->SetTexture(0, NULL); } p_D3DD->Present( NULL, NULL, NULL, NULL); } void SetWorld() { D3DXMATRIX maxWorld; D3DXMATRIX maxWorldPos; D3DXMATRIX maxWorldX; D3DXMATRIX maxWorldY; D3DXMATRIX maxWorldZ; D3DXMatrixIdentity(&maxWorld); //D3DXMatrixTranslation(&maxWorldPos, 0.0f, 0.0f, 10.0f); //D3DXMatrixRotationX(&maxWorldX, timeGetTime()/500.0f); D3DXMatrixRotationX(&maxWorldX, timeGetTime()/1000.0f); //D3DXMatrixRotationZ(&maxWorldZ, timeGetTime()/500.0f); //D3DXMatrixMultiply(&maxWorld, &maxWorldY, &maxWorldX); D3DXMatrixMultiply(&maxWorld, &maxWorld, &maxWorldX); p_D3DD->SetTransform(D3DTS_WORLD, &maxWorld); } void SetView() { D3DXMATRIX maxView; D3DXMatrixLookAtLH(&maxView, &D3DXVECTOR3(0.0f, 0.0f, -50.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0F, 1.0f, 0.0f)); p_D3DD->SetTransform(D3DTS_VIEW, &maxView); } void SetProject() { D3DXMATRIX maxProjection; D3DXMatrixPerspectiveFovLH(&maxProjection, D3DX_PI / 4, 4.0f / 3.0f, 1.0f, 500.0f); p_D3DD->SetTransform(D3DTS_PROJECTION, &maxProjection); }

Share this post


Link to post
Share on other sites
Advertisement
First, please, use the [ source][ /source] tags.

Second, could you tell us exactly what''s wrong? We don''t even know if it''s a build error, an early exit, a black screen, anything.

I like pie.

Share this post


Link to post
Share on other sites
The wrong of the program is : When the program run the LoadTexture sentence, it will occur a messagbox by microsoft. The message tell me the memory cannot read, and it will stop...something like that....

Share this post


Link to post
Share on other sites
Im not sure that I have a real answer for you but I made a new Win32 project in VS.net 2003 and copied your source directly in. The program fundementaly works just fine, I didnt get any errors. The only problem I see is that you havent done anything with your texture. you load it but nothing else. I can offer only this, you must set your FVF to Accept Texture Coordinates. Your Source then mine.

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
struct MYVERTEX
{
float x, y, z;
DWORD color;
};

should be

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
struct MYVERTEX
{
float x, y, z;
float Tu,Tv;
DWORD color;
};


and you must set the texture to be active.

p_D3DD->SetTexture(0,(IDirect3DBaseTexture9*)p_D3DT);
p_D3DD->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1)

ummmmm i think thats all ive got if you get stumped or this doesnt help you can check out the Tutorials at DrunkenHyenea the web site that Ken mentioned, or you can get me on MSN messenger with the email hippiehunter@msn.com.

:EDIT its kindof funny that ratterbox and i sort of posted at the same time it just took me a little longer to get my post writen.


[edited by - HippieHunter on January 14, 2004 1:52:16 AM]

[edited by - HippieHunter on January 14, 2004 1:54:32 AM]

Share this post


Link to post
Share on other sites
To:HippieHunter

As you say, you copy my code before, and can you run it without any problem when you don''t change any code from me? If I just call the texture sentence and don''t assign the FVF has texture, I think the output should have a 3D object that have 1 square in the bottom and 4 triangle ? Is that right?

Share this post


Link to post
Share on other sites
I hope im correctly understanding what your saying here, but here goes. You are asking if your 3d primative output is supposed to be a triangle fan(4 primatives in one call) and a triangle list(the square on the bottom)? And you are asking about what your program did on my computer? Well on my computer it rendered the primatives you have given it just fine, but it isnt really doing what you want. As far as i know you are trying to render the pyramid textured but i see a pyramid with vertex colors. And back to the first question yes that is what you want to do, but you still need to tell D3D that you want to texture the primatives. This can be done by specifying the Texture coordinates when you define your vertices. And by changeing your FVF as i showed in my first post. As with before i highly recomend checking out Kens tutorials, or Talk to me on MSN if you need help understanding the tutorial(sorry but i noticed your English, its ok but i can forsee you maybe haveing trouble dealing with English only D3D tutorials. You might try an online translator, i know they are not perfect but they usualy get the job done.) Hope this goes well for you.

Share this post


Link to post
Share on other sites
In my program, I want to step by step to be my first 3D game. Therefore, I read some tutorial from web site and write my 3d program myself.

In my first step, I need to create a 3D pyramid object. And I success to display it. Then, I want to paste some texture to my 3d pyramid object. But I will not change all code first, I will change my code little and little. Therefore, at the end of my 3d program will do that I want a pyramid pasted some texture.
At the moment, do you understand what I mean? I know my english is not good. I am afraid you or other people don''t undertand what I say.

First of change for Paste Texture on pyramid. I just create a variable D3DTexture9 and called a function CreateTextureFromFile and assign a "1.bmp". As my expected output, it will the same as the before that I create pyramid. Becasue the variable and the CreateTextureFromFile method is not affect the whole program. On the other words, I don''t use the "SetTexture" method or I don''t really use the texture performance. Unfortunely, When I finish the code before I posted. It is not work, it have a warning message by Microsoft. I don''t capture the picture of the messagebox, but the meaning is like that "The memory cannot read" and display two button, one is "Close" and the other is "Debug"............... Therefore, I write the MessageBox sentence inside the if Statement that called CreateTextureFromFile. On the other words, When I comment the "CreateTextureFromFile", it is work and ok in Visual C++

Here is my problem, and I don''t know what''s I wrong, So that, if you can run my program correctly without any error in your Visual C++. Please tell me or if you know what problem that you can tell me

I hope you or other people understand what I say and tell me what''s worng I have?

Thank You so Much

Share this post


Link to post
Share on other sites
I made an empty project and put your codes in it without any modification. compiling fine,running fine,I got this screen,is this the scene you wanted to show?

http://bbs.gzeasy.com/uploads/post-19-1074152491.png

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!