Sign in to follow this  

problem rendering meshes

This topic is 3291 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

Hello all this is my first post, I am new to direct x programming so don't pick on me too much. I subscribed to directxtutorials.com and I am following the lessons. I've run into several problems already that I've gotten through but this one I haven't found a solution to. Every time I try to render a mesh (other than the tea pot) The program will compile but crash hard. These are dx 9 programs running on the latest sdk with visual studio 2008 on vista 64 here is the source: main.cpp: LPDIRECT3DSURFACE9 z_buffer = NULL; // the pointer to the z-buffer // mesh declarations LPD3DXMESH meshSpaceship; // define the mesh pointer D3DMATERIAL9* material; // define the material object DWORD numMaterials; // stores the number of materials in the mesh // function prototypes void initD3D(HWND hWnd); // sets up and initializes Direct3D void render_frame(void); // renders a single frame void cleanD3D(void); // closes Direct3D and releases memory void init_graphics(void); // 3D declarations void init_light(void); // sets up the light and the material // the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd; WNDCLASSEX wc; ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WindowProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = L"WindowClass1"; RegisterClassEx(&wc); hWnd = CreateWindowEx(NULL, L"WindowClass1", L"Our Direct3D Program", WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); // set up and initialize Direct3D initD3D(hWnd); // enter the main loop: MSG msg; while(TRUE) { DWORD starting_point = GetTickCount(); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } render_frame(); // check the 'escape' key if(KEY_DOWN(VK_ESCAPE)) PostMessage(hWnd, WM_DESTROY, 0, 0); while ((GetTickCount() - starting_point) < 25); } // clean up DirectX and COM cleanD3D(); return msg.wParam; } // this is the main message handler for the program 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); } // this function initializes and prepares Direct3D for use void initD3D(HWND hWnd) { d3d = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = FALSE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWnd; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferWidth = SCREEN_WIDTH; d3dpp.BackBufferHeight = SCREEN_HEIGHT; d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer // create a device class using this information and the info from the d3dpp stuct d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); init_graphics(); // call the function to initialize the triangle init_light(); // call the function to initialize the light and material d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light return; } // this is the function used to render a single frame void render_frame(void) { d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->BeginScene(); // SET UP THE TRANSFORMS D3DXMATRIX matView; // the view transform matrix D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0.0f, 4.0f, 8.0f), // the camera position &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), // the horizontal field of view SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio 1.0f, // the near view-plane 100.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection static float index = 0.0f; index+=0.03f; // an ever-increasing float value D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); // set the world transform // draw the spaceship for(DWORD i = 0; i < numMaterials; i++) // loop through each subset { d3ddev->SetMaterial(&material[i]); // set the material for the subset meshSpaceship->DrawSubset(i); // draw the subset } d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); return; } // this is the function that cleans up Direct3D and COM void cleanD3D(void) { meshSpaceship->Release(); // close and release the spaceship mesh d3ddev->Release(); // close and release the 3D device d3d->Release(); // close and release Direct3D return; } // this is the function that puts the 3D models into video RAM void init_graphics(void) { LPD3DXBUFFER bufShipMaterial; D3DXLoadMeshFromX(L"spaceship 2.x", // load this file D3DXMESH_SYSTEMMEM, // load the mesh into system memory d3ddev, // the Direct3D Device NULL, // we aren't using adjacency &bufShipMaterial, // put the materials here NULL, // we aren't using effect instances &numMaterials, // the number of materials in this model &meshSpaceship); // put the mesh here // retrieve the pointer to the buffer containing the material information D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer(); // create a new material buffer for each material in the mesh material = new D3DMATERIAL9[numMaterials]; for(DWORD i = 0; i < numMaterials; i++) // for each material... { material[i] = tempMaterials[i].MatD3D; // get the material info material[i].Ambient = material[i].Diffuse; // make ambient the same as diffuse } return; } // this is the function that sets up the lights and materials void init_light(void) { D3DLIGHT9 light; // create the light struct ZeroMemory(&light, sizeof(light)); // clear out the struct for use light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light' light.Diffuse.r = 0.5f; // .5 red light.Diffuse.g = 0.5f; // .5 green light.Diffuse.b = 0.5f; // .5 blue light.Diffuse.a = 1.0f; // full alpha (we'll get to that soon) D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f}; // the direction of the light light.Direction = vecDirection; // set the direction d3ddev->SetLight(0, &light); // send the light struct properties to light #0 d3ddev->LightEnable(0, TRUE); // turn on light #0 return; }

Share this post


Link to post
Share on other sites
Besides adding #define UNICODE it is no different than the source supplied to me and the only other item in the program is the mesh file which is large and I won't include it unless requested. here is the output from visual studio



'spaceship2.exe': Loaded 'C:\Users\Ferocious\Documents\Visual Studio 2008\Projects\spaceship2\Debug\spaceship2.exe', Symbols loaded.
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\secur32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\d3d9.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\version.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\d3d8thk.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\D3DX9_40.dll'
'spaceship2.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcr90d.dll', Symbols loaded.
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\nvd3dum.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\IPHLPAPI.DLL'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\dhcpcsvc.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\dnsapi.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\winnsi.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\dhcpcsvc6.dll'
'spaceship2.exe': Loaded 'C:\Windows\SysWOW64\powrprof.dll'
First-chance exception at 0x00e01f29 in spaceship2.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x00e01f29 in spaceship2.exe: 0xC0000005: Access violation reading location 0xcccccccc.
The thread 'Win32 Thread' (0x1158) has exited with code -805306369 (0xcfffffff).
The thread 'Main Thread' (0x1004) has exited with code -805306369 (0xcfffffff).
The program '[2896] spaceship2.exe: Native' has exited with code -805306369 (0xcfffffff).

Share this post


Link to post
Share on other sites
Quote:
Original post by randomblame
Hello all this is my first post, I am new to direct x programming so don't pick on me too much. I subscribed to directxtutorials.com and I am following the lessons. I've run into several problems already that I've gotten through but this one I haven't found a solution to. Every time I try to render a mesh (other than the tea pot) The program will compile but crash hard. These are dx 9 programs running on the latest sdk with visual studio 2008 on vista 64 here is the source:
I'd very strongly advise against DirectXTutorial.com. There's absolutely no error checking there, and numerous other problems, which is why it's crashing. What line does it crash on? What is the exact error message? Is there any output from the Debug Runtimes? What does Your Debugger tell you?

EDIT: Ah, I see you have the error in your debug output. That's because you're using an uninitialised variable. When the code crashes, it'll break to the debugger on the line that caused the error, that line has an invalid pointer in it, you can use the debugger and debug runtimes to trace back through the code to find out why it's invalid. I'd hazard a guess that D3DXLoadMeshFromX() is failing because it can't find the file, so your mesh pointer is never initialised.

Share this post


Link to post
Share on other sites
thanks a lot for the reply steve, I just set up xp on another partition and I'm installing vs 2005 and an older dx sdk to see if that doesn't fix the weirdness I was thinking it was due to the new sdk and the library files not working the way the older code thought it would.


Do you have any suggestions on what I could use as a resource rather than directxtutorials.com? I've been searching for some good free lessons and couldn't find any so I just coughed up the 25 bucks for the extra lessons on there, starting to regret that.

I'll go through and look at what you suggested here in a sec

Share this post


Link to post
Share on other sites
Ok I'm getting closer to a solution, I have found that all his meshes and my own meshes that I exported from 3dsmax with panda are causing dxviewer to crash... I checked it out and it seems to be finding and linking the mesh however the meshes are causing the crashes I think. Is there a newer version of .x format that it's expecting?


Edit:: The august 2006 dxsdk fixed the dxviewer issue however it still crashes when I run it, compiles fine just crashes.

Share this post


Link to post
Share on other sites

This topic is 3291 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.

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