Advertisement

11.09.2008 at 10:10AM PST, ID: 23889221 | Points: 500
[x]
Attachment Details

DirectX Access Violation Error

Asked by Poolcorp in 3D Game Programming, C++ Programming Language, DirectX Graphics & Game Programming

Tags: ,

Im new to DirectX and all im trying to do is load a model. However when I run the app I am getting a Access Violation error

Unhandled exception at 0x00d51e85 in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.

on the line containing "D3DXMATERIAL* d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();"

If anyone can point me on how to fix this and get the darn thing to work id be very appreciative.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
 
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
 
struct MODEL
{
    LPD3DXMESH mesh;		//the vertices stored in a mesh
    D3DMATERIAL9* materials;	//materials applied to polygons
    LPDIRECT3DTEXTURE9* textures;	//textures applied to polygons
    DWORD material_count;		//number of materials in mesh
};
 
// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
 
// mesh declarations
LPD3DXMESH mesh;    // define the mesh pointer
 
//define the material buffer
LPD3DXBUFFER matbuffer;
D3DXMATERIAL* d3dxMaterials;
 
 
//allocate the mesh buffer in memory
MODEL *model = (MODEL*)malloc(sizeof(MODEL));
 
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
 
// 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 = "Project 2b";
 
    RegisterClassEx(&wc);
 
    hWnd = CreateWindowEx(NULL, "Project 2a", "Phil Loyer - Project 2b",
                          WS_OVERLAPPEDWINDOW, 300, 300, 1024, 768,
                          NULL, NULL, hInstance, NULL);
 
	init_graphics();  
 
    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 = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
 
	
 
    // 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_light();   
 
    d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);  
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    
    d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    
 
    return;
}
 
 
// this is the function used to render a single frame
void render_frame(void)
{
 
    d3ddev->BeginScene();
 
      // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 3.0f, 6.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 
 
      // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)1024 / (FLOAT)768, // aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection
 
	int numOfMaterials = model->material_count;
	
	for(int i=0; i < numOfMaterials; i++)
	{
		d3ddev->SetMaterial(&model->materials[i]);
		d3ddev->SetTexture(0, model->textures[i]);
		model->mesh->DrawSubset(i);
	}
	
 
    d3ddev->EndScene(); 
 
    d3ddev->Present(NULL, NULL, NULL, NULL);
 
    return;
}
 
 
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    mesh->Release();    // close and release the sphere 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)
{	
	
 
	HRESULT result;
 
 
	//load the mesh file
	result = D3DXLoadMeshFromX(
    "motorbike1.x",               //filename
    D3DXMESH_SYSTEMMEM,     //mesh options
    d3ddev,                 //Direct3D device
    NULL,                   //adjacency buffer
    &matbuffer,             //material buffer
    NULL,                   //special effects
    &model->material_count, //number of materials
    &model->mesh);          //resulting mesh
 
 
 
	D3DXMATERIAL* d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();
 
	model->materials = new D3DMATERIAL9[model->material_count];
	model->textures  = new LPDIRECT3DTEXTURE9[model->material_count];
	
	int numOfMaterials = model->material_count;
 
	for(int i=0; i < numOfMaterials; i++)
	{
		//grab the material
		model->materials[i] = d3dxMaterials[i].MatD3D;
 
		//set ambient color for material 
		model->materials[i].Ambient = model->materials[i].Diffuse;
 
		model->textures[i] = NULL;
 
	
 
		if( d3dxMaterials[i].pTextureFilename != NULL && 
			lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
		{
			//load texture file specified in .x file
			result = D3DXCreateTextureFromFile(d3ddev, 
				d3dxMaterials[i].pTextureFilename, 
				&model->textures[i]);
		}
	}
 
 
 
    return;
}
 
 
// this is the function that sets up the lights and materials
void init_light(void)
{
    D3DLIGHT9 light;    // create the light struct
    D3DMATERIAL9 material;    // create the material 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
 
    ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
    material.Diffuse.r = material.Ambient.r = 1.0f;    // set the material to full red
    material.Diffuse.g = material.Ambient.g = 1.0f;    // set the material to full green
    material.Diffuse.b = material.Ambient.b = 1.0f;    // set the material to full blue
    material.Diffuse.a = material.Ambient.a = 1.0f;    // set the material to full alpha
 
    d3ddev->SetMaterial(&material);    // set the globably-used material to &material
 
    return;
}
[+][-]11.09.2008 at 10:16AM PST, ID: 22917114

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.09.2008 at 04:09PM PST, ID: 22918355

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.09.2008 at 10:04PM PST, ID: 22919394

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.09.2008 at 10:43PM PST, ID: 22919502

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.09.2008 at 10:45PM PST, ID: 22919510

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.10.2008 at 01:42PM PST, ID: 22925867

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081119-EE-VQP-49 - Hierarchy / EE_QW_EXPERT_20070906