void MarchingCubesGenerator::sortVerts(McFan fan_out, McFan fan_in, int numberOfVerts, McFan fanNorm, bool flipped, int onPoint)
{
	bool temp = false;
	bool markedVerts[6];
	for(int i=0; i<6; i++)
	{
		markedVerts[i] = false;
	}
	McVertex tempVert;
	for(int i=0; i<numberOfVerts; i++)
	{
		if(i==0)
		{
			memcpy(fan_out[0], fan_in[0], sizeof(McVertex));
			markedVerts[0] = true;
		}
		else
		{
			temp = false;
			for(int j=0; j<numberOfVerts; j++)
			{
				
				if((markedVerts[j] == false) && (temp == false))
				{
					for(int k=0; k<3; k++)
					{
					
						if(((fan_out[i-1][k]==1) && (fan_in[j][k] == 1)) || ((fan_out[i-1][k]==0) && (fan_in[j][k] == 0)) )
						{
							temp = true;
							markedVerts[j] = true;
						}
					}

					if(temp == true)
					{
						memcpy(tempVert, fan_in[j], sizeof(McVertex));
						memcpy(fan_out[i], fan_in[j], sizeof(McVertex));
						
					
					}
				}
			}
			
		}
	}
	
	getFaceNormals(fan_out, fanNorm, numberOfVerts, flipped, onPoint);
	getVertNormals(fan_out, fanNorm, numberOfVerts, flipped);
}
Example #2
0
void SilhouetteEdges::genEdgeVertices(ID3DXMesh* mesh,
									  ID3DXBuffer* adjBuffer)
{
	// 3 edges per face and 4 vertices per edge
	_numVerts = mesh->GetNumFaces() * 3 * 4;
	_device->CreateVertexBuffer(
		_numVerts * sizeof(EdgeVertex),
		D3DUSAGE_WRITEONLY,
		0, // using vertex declaration
		D3DPOOL_MANAGED,
		&_vb,
		0);

	MeshVertex* vertices = 0;
	mesh->LockVertexBuffer(0, (void**)&vertices);

	WORD* indices = 0;
	mesh->LockIndexBuffer(0, (void**)&indices);

	EdgeVertex* edgeVertices = 0;
	_vb->Lock(0, 0, (void**)&edgeVertices, 0);

	for(int i = 0; i < mesh->GetNumFaces(); i++)
	{
		D3DXVECTOR3 currentFaceNormal;
		D3DXVECTOR3 adjFaceNormals[3];

		getFaceNormals(mesh, adjBuffer, &currentFaceNormal, adjFaceNormals, i);

		// get the indices for this face
		WORD index0 = indices[i * 3];
		WORD index1 = indices[i * 3 + 1];
		WORD index2 = indices[i * 3 + 2];

		// get the vertices for this face
		MeshVertex v0 = vertices[index0];
		MeshVertex v1 = vertices[index1];
		MeshVertex v2 = vertices[index2];
		
		// A        B
		// *--------*
		// |  edge  |
		// *--------*
		// C        D
		// note, C and D are duplicates of A and B respectively, 
		// such that the quad is degenerate.  The vertex shader
		// will un-degenerate the quad if it is a silhouette edge.

		// compute edge0 v0->v1, note adjacent face
		// normal is faceNormal0
		EdgeVertex A0, B0, C0, D0;

		A0.position    = v0.position;
		A0.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		A0.faceNormal1 = currentFaceNormal;
		A0.faceNormal2 = adjFaceNormals[0];

		B0.position    = v1.position;
		B0.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		B0.faceNormal1 = currentFaceNormal;
		B0.faceNormal2 = adjFaceNormals[0];

		C0 = A0;
		C0.normal = v0.normal;

		D0 = B0;
		D0.normal = v1.normal;

		*edgeVertices = A0; ++edgeVertices;
		*edgeVertices = B0; ++edgeVertices;
		*edgeVertices = C0; ++edgeVertices;
		*edgeVertices = D0; ++edgeVertices;	

		// compute edge0 v1->v2, note adjacent face
		// normal is faceNormal1
		EdgeVertex A1, B1, C1, D1;

		A1.position    = v1.position;
		A1.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		A1.faceNormal1 = currentFaceNormal;
		A1.faceNormal2 = adjFaceNormals[1];

		B1.position    = v2.position;
		B1.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		B1.faceNormal1 = currentFaceNormal;
		B1.faceNormal2 = adjFaceNormals[1];

		C1 = A1;
		C1.normal = v1.normal;
		
		D1 = B1;
		D1.normal = v2.normal;

		*edgeVertices = A1; ++edgeVertices;
		*edgeVertices = B1; ++edgeVertices;
		*edgeVertices = C1; ++edgeVertices;
		*edgeVertices = D1; ++edgeVertices;	

		// compute edge0 v0->v2, note adjacent face
		// normal is faceNormal2
		EdgeVertex A2, B2, C2, D2;

		A2.position    = v0.position;
		A2.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		A2.faceNormal1 = currentFaceNormal;
		A2.faceNormal2 = adjFaceNormals[2];

		B2.position    = v2.position;
		B2.normal      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
		B2.faceNormal1 = currentFaceNormal;
		B2.faceNormal2 = adjFaceNormals[2];

		C2 = A2;
		C2.normal = v0.normal;
		
		D2 = B2;
		D2.normal = v2.normal;

		*edgeVertices = A2; ++edgeVertices;
		*edgeVertices = B2; ++edgeVertices;
		*edgeVertices = C2; ++edgeVertices;
		*edgeVertices = D2; ++edgeVertices;	
	}

	_vb->Unlock();
	mesh->UnlockVertexBuffer();
	mesh->UnlockIndexBuffer();
}