Skip to main content
gamedev.net
🔒 Locked

Problems locking the vertex buffer

Started by Mephs May 27, 2002 at 6:23 AM 4 replies 300+ views
Original Post
Mephs
Mephs
Hi, just writing as I have problems locking my vertex buffer. I have used the D3DXCreateMesh command to successfully create a mesh to work from, then in attempting to obtain a pointer to the meshs'' vertex buffer with the comman below, problems arise. If I comment out the following line in my program it works fine... if I don''t comment the line out, the program attempts to start up and then drops back to the desktop (and yes I do proceed this call with an unlock directly after) Mesh->LockVertexBuffer(D3DLOCK_DISCARD,(BYTE **)&pVB) The program compiles successfully with or without the command yet doesnt run correctly with it. I am using memcpy just before unlocking the vertex buffer to fill it with the data I have pre-defined (a box with 16 vertices and 8 faces just to test it out). A similar problem happened previous to this and I discovered it was because I needed to pass a pointer to the class function so that it had reference to the existing device... I made the mistake of declaring a device directly inside the class function without using the one that existed, this led to exactly the same dropping back to desktop scenario. the full function is below if needed, thanks in advance if anyone can help at all, Steve AKA Mephs
  
BOOL cMap::Load(cGraphics *Graphics, ID3DXMesh *Mesh, LPDIRECT3DVERTEXBUFFER8 *pVB)
{
	//LPDIRECT3DVERTEXBUFFER8 pVB;


	
	typedef struct {
	  FLOAT x, y, z;		// 3-D coordinates

	  FLOAT xn, yn, zn;		// Normals

	  FLOAT u, v;			// Texture coordinates

	} sVertex2;

	#define VERTEXFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
	const DWORD VERTEXFVF2 (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1);
	// Fill the vertex buffer

	sVertex2 CubeVerts[16] = {
		{ -100.0f,  100.0f, -100.0f, 0.0f,0.0f,-1.0f, 0.0f,0.0f},
		{  100.0f,  100.0f, -100.0f, 0.0f,0.0f,-1.0f, 1.0f,0.0f},
		{ -100.0f, -100.0f, -100.0f, 0.0f,0.0f,-1.0f, 0.0f,1.0f},
		{  100.0f, -100.0f, -100.0f, 0.0f,0.0f,-1.0f, 1.0f,1.0f},
		{  100.0f,  100.0f, -100.0f, 1.0f,0.0f,0.0f, 0.0f,0.0f},
		{  100.0f,  100.0f,  100.0f, 1.0f,0.0f,0.0f, 1.0f,0.0f},
		{  100.0f, -100.0f, -100.0f, 1.0f,0.0f,0.0f, 0.0f,1.0f},
		{  100.0f, -100.0f,  100.0f, 1.0f,0.0f,0.0f, 1.0f,1.0f},
		{  100.0f,  100.0f,  100.0f, 0.0f,0.0f,1.0f, 0.0f,0.0f},
		{ -100.0f,  100.0f,  100.0f, 0.0f,0.0f,1.0f, 1.0f,0.0f},
		{  100.0f, -100.0f,  100.0f, 0.0f,0.0f,1.0f, 0.0f,1.0f},
		{ -100.0f, -100.0f,  100.0f, 0.0f,0.0f,1.0f, 1.0f,1.0f},
		{ -100.0f,  100.0f,  100.0f, -1.0f,0.0f,0.0f, 0.0f,0.0f},
		{ -100.0f,  100.0f, -100.0f, -1.0f,0.0f,0.0f, 1.0f,0.0f},
		{ -100.0f, -100.0f,  100.0f, -1.0f,0.0f,0.0f, 0.0f,1.0f},
		{ -100.0f, -100.0f, -100.0f, -1.0f,0.0f,0.0f, 1.0f,1.0f}
	};
	
	
	D3DXCreateMesh(8,16,D3DXMESH_MANAGED,&VERTEXFVF2,Graphics->GetDeviceCOM(),&Mesh);
	//Graphics->GetDeviceCOM()->CreateVertexBuffer(

	//	16 * sizeof(sVertex2),

	//	D3DUSAGE_WRITEONLY, VERTEXFVF,

	//	D3DPOOL_MANAGED, &pVB);

	Mesh->LockVertexBuffer(D3DLOCK_DISCARD,(BYTE **)&pVB);
	memcpy(pVB,CubeVerts,sizeof(CubeVerts));
	Mesh->UnlockVertexBuffer();
	
	return TRUE;
}
  
Mephs
Mephs
Well an update on my problem... I thought it was cos I used the D3DXMESH_MANAGED flag in creating the mesh, ie because I''d specified that flag it wasn''t allowing the vertex buffer to be modified. But alas, it doesnt seem to make a difference what I set the value to. It seems kinda strange that the program is crashing to desktop without any error message of any kind... can anyone please help with this??? Sorry to be a pest but I''ve gone to a lot of trouble finding tutorials on how to deal with locking the vertex buffer... I follow them almost to the exact letter and yet the result is always the same... this odd premature closure of the program if I let it attempt to lock the vertex buffer.
Mephs
Mephs
Well... hip hip hooray!! I figured out the problem myself!!

Problem was it didnt like the FVF structure, so instead i used the CreateMeshFVF function and manually specified the FVF I wanted... then it loaded perfectly.

BUT!! (and what a huge but!!) I then proceeded to use the DrawSubset(0); command to render the mesh. Supposedly upon creation a mesh assigns all faces a default subset of 0. So in theory this command should render the entire model with whichever material was set last. It doesn''t... it just performs the same drop to desktop thing it did when I got the FVF wrong. I am assuming it is something to do with memory as I generate exactly the same problem if I specify the wrong size for the memcpy function, and as it deals with a lot of pointers I could quite easily have messed something up. Can anyone pleeeease help me here... every time I get one step closer to being able to render my model... the same drop to desktop thing occurs with a different command!! I''m almost at a loss but at least I''ve made some kind of progress
Mephs
Mephs
Sorry to bump this up again but... Im sooooooooo stuck!! I still have no idea why I get kicked to the desktop. As before the Mesh loads fine but as soon as I call Mesh2->DrawSubset(0) even after setting my own attribute buffer to define all faces as subset 0, it kicks me to desktop.. Not even an error to indicate what might be causing it. If nobody is able to help me work out what is causing the problem, does anyone have any hints as to how to identify what is causing this command to create such trouble? Some kind of error catching function or anything!?? I''m at a complete dead end and the only article I can find on the subject results in the same problem.

If an exact description of the problem helps, what happens is that the window pops up, I can see the brightness change, so I know direct3D has been initialised fine... then the window seems to minimize itself and execution halts.. no error but it just stops running as if I''d never executed it. The code compiles perfectly with 0 errors and 0 warnings, so I literally don''t know what to do other than ask if anyone has the slightest clue what might be causing it?

If I don''t get any replies this time... I don''t have a clue what to do besides writing my own entire engine from scratch (which I''d rather not at the moment). Speaking of which, if it makes a difference I''m trying to incorporate this function to load a custom model into an ID3DXMesh structure, but I''m starting off lightly with a pre-defined vertex structure that I''m using to populate a ID3DXMesh object for rendering. Everything in my code is laid out (to the best of my knowledge) identically to how its supposed to be done according to the tutorial and what I''ve read from the SDK docs...

...help
......please!!

PS I hope bumping this up one last time doesnt offend anyone but I''m totally utterly stuck so.. umm hope u dont mind =/
Syrillix
Syrillix
well im not sure what exactly the problem with your code is but you might wanna try adding error checking to your function calls.. almost all the DX functions return a HRESULT which you can check and convert to string to print out to a message box using
char * error = DXGetErrorDescription8(HRESULT hr);
MessageBox(NULL, error, "error", MB_OK);

that should at least help figure out where its failing and what the error is and maybe give you some indication of how to fix it

Get busy livin'' or get busy dyin''... - Shawshank Redemption
Get busy livin' or get busy dyin'... - Shawshank RedemptionIf a man is talking in the forest, and no woman is around to hear him, is he still wrong? - UnknownFulcrum
Mephs
Mephs
hehe thanks for having a go but I''ve tried that as I think I did say. Though now I have found that if I make the call within the cMap::Load function it works fine, so I''m guessing it''s something to do with the way I''ve structured my class. So I guess my next step is to try and make it more compatible with the engine I''m using (it wont work from its own render function.. only the load function which is no good!!) So I''m gonna try to work out how to load the model into an sMesh structure for rendering, which will be more complex in itself but should... hopefully be more compatible. As it is if I called the load function every frame I still didn''t see anything even if I put the draw subset function inside it, I''m guessing this is because it hasnt had it''s own transformations set to place it @ 0,0,0 in the world or wherever I''m wanting it to go.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.