Directx programming, failure on returning from a function? o.0

I've been programming a Directx program and made a function to read in a .obj file (a little bit edited by me for simplicity, y'know, to reach deadlines haha) and it does so fine, it writes it to a test file so I could check and it does that fine, then at the end of the function there is an unhandled exception, access violation writing location thing :/, I've added a breakpoint and gone through to check its not my file reading and there doesn't seem a problem with it, it only breaks on the closing } at the end of the function.

Here's the code:
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
void getModel()
	{
		FILE* dataFile = fopen(fileName, "r");
		FILE* fileOut = fopen("cubey.txt", "w");

		if (dataFile == NULL)
		{
			cout << "Input file failed to open or is missing.\n";
			exit(1);
		}

		string type;
		float x, y, z;
		int v1, vt1, vn1, v2, vt2, vn2, v3, vt3, vn3; 

		fscanf(dataFile, "%d %d %d %d", &numVertices, &numTex, &numNormals, &numFaces);

		vertices = new CUSTOMVERTEX[numVertices];
		indices = new short[numFaces];

		D3DXVECTOR3* v = new D3DXVECTOR3[numVertices];
		D3DXVECTOR2* vt = new D3DXVECTOR2[numTex];
		D3DXVECTOR3* vn = new D3DXVECTOR3[numNormals];

		for(int i = 0; i < numVertices; i++)
		{
			fscanf(dataFile, "%s %f %f %f", &type, &x, &y, &z);
			v[i].x = x;
			v[i].y = y;
			v[i].z = z;

			fprintf(fileOut, "%f %f %f\n", x, y, z);
		}
		for(int i = 0; i < numTex; i++)
		{
			fscanf(dataFile, "%s %f %f", &type, &x, &y);
			vt[i].x = x;
			vt[i].y = y;

			fprintf(fileOut, "%f %f\n", x, y);
		}
		for(int i = 0; i < numNormals; i++)
		{
			fscanf(dataFile, "%s %f %f %f", &type, &x, &y, &z);
			vn[i].x = x;
			vn[i].y = y;
			vn[i].z = z;

			fprintf(fileOut, "%f %f %f\n", x, y, z);
		}
		for(int i = 0; i < numFaces; i++)
		{
			fscanf(dataFile, "%s %d/%d/%d %d/%d/%d %d/%d/%d", &type, &v1, &vt1, &vn1, &v2, &vt2, &vn2, &v3, &vt3, &vn3);

			fprintf(fileOut, "%d,%d,%d %d,%d,%d %d,%d,%d\n", v1, vt1, vn1, v2, vt2, vn2, v3, vt3, vn3);
		}

		delete [] v;
		delete [] vt;
		delete [] vn;
		fclose(dataFile);
		fclose(fileOut);
	}

Wild guess here but is it OK to do this with a string? Would it be better to fscanf into a char array?
1
2
char type[10];
fscanf(dataFile, "%s %f %f %f", type, &x, &y, &z);


the other thing is would it be better to open the output file useing "wt" for text output rather than just "w".
hmm, from looking at it them minor changes wouldnt do much to my problem but they seem to have solved it haha :D thanks!!, been doing c++ for a while now but then there are still things i cant get the hang of fully -.- :P and directx makes it more confusing for me, im an opengl guy :P

thanks again :)
cool,
Strings I assume have an overloaded = operator, somebody in the know could explain this better, but &type for a string would point to a string object, not the data it contains I think.

The other thing I noticed was on line 53 with the fscanf and the %d/%d/%d %d/%d/%d which I've not seen before..
Last edited on
yeah that makes sense :D

and in the obj file, the faces are listed as v1/vt1/vn1 ... 2... 3

so I put the /s in because i think that reads them but does nothing? haha, I'm just going from stuff I've found, i have like a million tabs open :S

would "%d/%d/%d" read in something like 1/2/3? or am i mistaken again? haha
I'm not sure as I've only ever used the DirectX librarys for .X files, not importing an obj. I think it should be the same as all your other input specifiers, so %f for a float and %s for a string %d for an int etc, %d is fine for the list of face indicies as there should, hopefully not be a half index for a face vertex ;-)
Last edited on
yeah thats why i used ints :P, but like i said, the indices lines are like:

f 53/704/154 55/706/154 54/707/154

in my file so im using each int to link up all the vertex positions, tex coords and normals into one structure that i made heh
new problem!! D:, ive sorted most of the program out and figured out some of the stuff i didn't know before, but now i have a function to draw objects separately but it just draws a black screen, just wondered if anyone knows why this function isnt working:

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
void drawObject(modelObject* obj)
{
	VOID* pVoid; // a void pointer
	// create a vertex buffer interface called v_buffer
	d3ddev->CreateVertexBuffer(obj->numVertices*sizeof(CUSTOMVERTEX), 0, 0, D3DPOOL_MANAGED, &v_buffer, NULL);

	
	// lock v_buffer and load the vertices into it
	v_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, obj->vertices, sizeof(obj->vertices));
	v_buffer->Unlock();

	// create a index buffer interface called i_buffer
	d3ddev->CreateIndexBuffer(obj->numVertices*sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &i_buffer, NULL);
	// lock i_buffer and load the indices into it
	i_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, obj->indices, sizeof(obj->indices));
	i_buffer->Unlock();

	
	
	d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
	d3ddev->SetIndices(i_buffer);
	d3ddev->SetVertexDeclaration(Decl);
	// Begin passes.
	UINT numPasses = 0;
	mFX->Begin(&numPasses, 0);
	for(UINT i = 0; i < numPasses; i++)
	{	
		mFX->BeginPass(i);
		d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, obj->numVertices, 0, obj->numFaces);
		mFX->EndPass();
	}

	mFX->End();
}


im not fully sure how a lot of the mfx stuff works but yeah, not sure if im even drawing it right :/
I'm clutching at straws again here squishy, but it worked out OK last time ;-)

You're missing the vertext FVF when you create the vertex buffer for v_buffer, I'm not sure how much of a problem that would be, I'll say though that it helps to know if things are being made or failing too.
The FAILED() macro can help you here, an example of how I've used this below.

if(FAILED(hr=m_pDirect3DDevice->CreateVertexBuffer(VERTEXBUFFER_SIZE_3D*_3DVertSize,0,D3DFVF_3DVERTEX,D3DPOOL_MANAGED,&p3DVB,NULL)))
{
.. report an error hr being the D3D error code
}

I'm assuming you've already begun a scene before calling this and have valid world,view and projection matricies setup?

If not, start of with something more simple like drawing a 2D line on the screen to get the hang of how DX works, I've never tried using OpenGL so maybe you're missing some steps that DX needs doing that OGL dosn't, if you see what I mean.
honestly i havent a clue what im doing :/ my tutor just completely failed to explain how to draw multiple objects -.-, what i wanna know how to do is have a function in my object class (that contains all the vertices and details about the object) and then be able to call it somewhere but i dont know the orders or anything, its confusing because i did a program last year made in C and openGL and it did this but in far less lines and it made sense, WTH directX?!?! urgh lol
Topic archived. No new replies allowed.