Jump to content
  • Advertisement
Sign in to follow this  
san__gohan

Iterate through a meshe's faces

This topic is 4832 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, I loaded the tiger.x mesh into my app. This mesh is indexed and I would like to make a copy of it in a vertex buffer in such a way that I don't need the index buffer any more. What I do is that I iterate through the index buffer so as to get the indices of the vertices, then i get the vertex associated with this index in the vertex buffer and I copy it in my new vertex buffer. In the end, i get a weird shape. Is this correct or did I miss something. Here's the code.

struct CELL_SHADING_CUTOM_VERTEX{
	FLOAT x, y, z;
	D3DVECTOR n1;//, n2, n3;
};

struct CUTOM_VERTEX{
	FLOAT x, y, z;
	D3DVECTOR n1;
};


hr = LoadMeshFromFile("tiger.x",&mesh,&textures,&materials,&numMaterials);
	hr = mesh->CloneMeshFVF(mesh->GetOptions(),/*mesh->GetFVF()*/D3DFVF_XYZ /*|D3DFVF_TEX1*/|D3DFVF_NORMAL ,device,&texturedMesh);
	D3DXComputeNormals(texturedMesh,(DWORD *)adj->GetBufferPointer());

D3DVERTEXELEMENT9 decl[]= {
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		//{0, 6*sizeof(float), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 1},
		//{0, 9*sizeof(float), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 2},
		D3DDECL_END()
	};


	hr = device->CreateVertexDeclaration(decl, &vDecl);
	if(FAILED(hr)){
		MessageBox(hWnd,TEXT("Unable to create declaration"),TEXT("Initialization error"),MB_OK);
		return;
	}

	int sizeV = texturedMesh->GetNumFaces()*3;

hr = device->CreateVertexBuffer( sizeV*sizeof(CELL_SHADING_CUTOM_VERTEX),
		0,0, D3DPOOL_DEFAULT, &vb, 0);
	if(FAILED(hr)){
		MessageBox(hWnd,TEXT("Unable to create vb"),TEXT("Initialization error"),MB_OK);
		return;
	}


	IDirect3DVertexBuffer9 *vb2 = 0;
	IDirect3DIndexBuffer9  *ib2 = 0;
	texturedMesh->GetVertexBuffer(&vb2);
	texturedMesh->GetIndexBuffer(&ib2);

	D3DINDEXBUFFER_DESC desc;
	ib2->GetDesc(&desc);

	CUTOM_VERTEX *vb2L = 0;
	USHORT *ib2L = 0;
	hr = vb2->Lock(0,0,(void **)&vb2L, D3DLOCK_READONLY);
	hr = ib2->Lock(0,0,(void **)&ib2L, D3DLOCK_READONLY);

	int tst = texturedMesh->GetNumFaces()*3;



	CELL_SHADING_CUTOM_VERTEX *vertices  = new CELL_SHADING_CUTOM_VERTEX[ sizeV];

	for(UINT i= 0; i< texturedMesh->GetNumFaces()*3; i++){
		USHORT a = ib2L;
		CUTOM_VERTEX cv = vb2L[a];
		CELL_SHADING_CUTOM_VERTEX scv = {
			cv.x,cv.y,cv.z, 
				cv.n1
			//D3DVECTOR(),
			//D3DVECTOR()
		};
		vertices = scv;

	}
	vb2->Unlock();
	ib2->Unlock();

	CELL_SHADING_CUTOM_VERTEX *verti;
	hr = vb->Lock(0,0,(void **)&verti, 0);
	if(FAILED(hr)){
		MessageBox(hWnd,TEXT("Unable to lock vb"),TEXT("Initialization error"),MB_OK);
		return;
	}
	memcpy(verti, vertices, sizeV);

	vb->Unlock();




Thanks

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!