⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edittrimesh.cpp

📁 3D数学基础:图形与游戏开发书籍源码,里面有很多实用的代码,对做3D的同志很有意义
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		// Set the new count.  Any extra memory is
		// wasted for now...

		vCount = vc;
	}

}

//---------------------------------------------------------------------------
// EditTriMesh::setTriCount
//
// Set the triangle count.  If the list is grown, the new triangles at the
// end are initialized with default values.

void	EditTriMesh::setTriCount(int tc) {
	assert(tc >= 0);

	// Make sure we had enough allocated coming in

	assert(tCount <= tAlloc);

	// Check if we are growing the list

	if (tc > tCount) {

		// Check if we need to allocate more

		if (tc > tAlloc) {

			// We need to grow the list.  Figure out the
			// new count.  We don't want to constantly be
			// allocating memory every time a single tri
			// is added, but yet we don't want to allocate
			// too much memory and be wasteful.  The system
			// shown below seems to be a good compromise.

			tAlloc = tc * 4 / 3 + 10;
			tList = (Tri *)::realloc(tList, tAlloc * sizeof(*tList));

			// Check for out of memory.  You may need more
			// robust error handling...

			if (tList == NULL) {
				ABORT("Out of memory");
			}
		}

		// Initilaize the new triangles

		while (tCount < tc) {
			tList[tCount].setDefaults();
			++tCount;
		}
	} else {

		// Set the new count.  Any extra memory is
		// wasted for now...

		tCount = tc;
	}
}

//---------------------------------------------------------------------------
// EditTriMesh::setMaterialCount
//
// Set the material count.  If the list is grown, the new materials at the end
// are initialized with default values.  If the list is shrunk, any invalid
// faces are deleted.

void	EditTriMesh::setMaterialCount(int mc) {
	assert(mc >= 0);

	// Check if growing or shrinking the list

	if (mc > mCount) {

		// Grow the list.  For materials, we don't have any fancy
		// allocation like we do for the vertices and triangles.

		mList = (Material *)::realloc(mList, mc * sizeof(*mList));

		// Check for out of memory.  You may need more
		// robust error handling...

		if (mList == NULL) {
			ABORT("Out of memory");
		}

		// Initilaize the new materials

		while (mCount < mc) {
			mList[mCount].setDefaults();
			++mCount;
		}

	} else if (mc < mCount) {

		// Shrinking the list.  Go through
		// and mark invalid faces for deletion

		for (int i = 0 ; i < triCount() ; ++i) {
			Tri *t = &tri(i);
			if (t->material >= mc) {

				// Mark it for deletion

				t->mark = 1;

			} else {

				// It's OK

				t->mark = 0;
			}
		}

		// Delete the marked triangles

		deleteMarkedTris(1);

		// Set the new count.  For now, no need to
		// shrink the list.  We'll just waste it.

		mCount = mc;
	}

}

//---------------------------------------------------------------------------
// EditTriMesh::setPartCount
//
// Set the part count.  If the list is grown, the new parts at the end
// are initialized with default values.  If the list is shrunk, any invalid
// faces are deleted.

void	EditTriMesh::setPartCount(int pc) {
	assert(pc >= 0);

	// Check if growing or shrinking the list

	if (pc > pCount) {

		// Grow the list.  For parts, we don't have any fancy
		// allocation like we do for the vertices and triangles.

		pList = (Part *)::realloc(pList, pc * sizeof(*pList));

		// Check for out of memory.  You may need more
		// robust error handling...

		if (pList == NULL) {
			ABORT("Out of memory");
		}

		// Initilaize the new parts

		while (pCount < pc) {
			pList[pCount].setDefaults();
			++pCount;
		}

	} else if (pc < pCount) {

		// Shrinking the list.  Go through
		// and mark invalid faces for deletion

		for (int i = 0 ; i < triCount() ; ++i) {
			Tri *t = &tri(i);
			if (t->part >= pc) {

				// Mark it for deletion

				t->mark = 1;

			} else {

				// It's OK

				t->mark = 0;
			}
		}

		// Delete the marked triangles

		deleteMarkedTris(1);

		// Set the new count.  For now, no need to
		// shrink the list.  We'll just waste it.

		pCount = pc;
	}

}

//---------------------------------------------------------------------------
// EditTriMesh::addTri
//
// Add a new, default triangle.  The index of the new item is returned

int	EditTriMesh::addTri() {

	// Fetch index of the new one we will add

	int	r = tCount;

	// Check if we have to allocate

	if (tCount >= tAlloc) {

		// Need to actually allocate memory - use the other function
		// to do the hard work

		setTriCount(tCount+1);

	} else {

		// No need for heavy duty work - we can
		// do it quickly here

		++tCount;
		tList[r].setDefaults();
	}

	// Return index of new triangle

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::addTri
//
// Add triangle to the end of the list.  The index of the new item is returned

int	EditTriMesh::addTri(const Tri &t) {

	// Fetch index of the new one we will add

	int	r = tCount;

	// Check if we have to allocate

	if (tCount >= tAlloc) {

		// Need to actually allocate memory - use the other function
		// to do the hard work

		setTriCount(tCount+1);

	} else {

		// No need for heavy duty work - we can
		// do it quickly here

		++tCount;

		// No need to default - we are about to assign it
	}

	// Fill it in

	tList[r] = t;

	// Return index of new triangle

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::addVertex
//
// Add a new, default vertex.  The index of the new item is returned

int	EditTriMesh::addVertex() {

	// Fetch index of the new one we will add

	int	r = vCount;

	// Check if we have to allocate

	if (vCount >= vAlloc) {

		// Need to actually allocate memory - use the other function
		// to do the hard work

		setVertexCount(vCount+1);

	} else {

		// No need for heavy duty work - we can
		// do it quickly here

		++vCount;
		vList[r].setDefaults();
	}

	// Return index of new vertex

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::addvertex
//
// Add vertex to the end of the list.  The index of the new item is returned

int	EditTriMesh::addVertex(const Vertex &v) {

	// Fetch index of the new one we will add

	int	r = vCount;

	// Check if we have to allocate

	if (vCount >= vAlloc) {

		// Need to actually allocate memory - use the other function
		// to do the hard work

		setVertexCount(vCount+1);

	} else {

		// No need for heavy duty work - we can
		// do it quickly here

		++vCount;

		// No need to default - we are about to assign it
	}

	// Fill it in

	vList[r] = v;

	// Return index of new vertex

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::dupVertex
//
// Add a duplicate of a vertex to the end of the list.
//
// Notice that we do NOT make the mistake of coding this simply as:
//
//	return addVertex(vertex(srcVertexIndex))
//
// Because the vertices may shift in memory as the lists are reallocated,
// and our reference will be invalid.

int	EditTriMesh::dupVertex(int srcVertexIndex) {

	// Fetch index of the new one we will add

	int	r = vCount;

	// Check if we have to allocate

	if (vCount >= vAlloc) {

		// Need to actually allocate memory - use the other function
		// to do the hard work

		setVertexCount(vCount+1);

	} else {

		// No need for heavy duty work - we can
		// do it quickly here

		++vCount;

		// No need to default - we are about to assign it
	}

	// Make the copy

	vList[r] = vList[srcVertexIndex];

	// Return index of new vertex

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::addMaterial
//
// Add a material to the end of the list.  Not optimized

int	EditTriMesh::addMaterial(const Material &m) {

	// Grow list

	int	r = materialCount();
	setMaterialCount(r+1);

	// Fill it in

	mList[r] = m;

	// Return index of the new material

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::addPart
//
// Add a part to the end of the list.  Not optimized

int	EditTriMesh::addPart(const Part &p) {

	// Grow list

	int	r = partCount();
	setPartCount(r+1);

	// Fill it in

	pList[r] = p;

	// Return index of the new part

	return r;
}

//---------------------------------------------------------------------------
// EditTriMesh::markAllVertices
//
// Mark all vertices with the given value

void	EditTriMesh::markAllVertices(int mark) {
	for (int i = 0 ; i < vertexCount() ; ++i) {
		vertex(i).mark = mark;
	}
}

//---------------------------------------------------------------------------
// EditTriMesh::markAllTris
//
// Mark all triangles with the given value

void	EditTriMesh::markAllTris(int mark) {
	for (int i = 0 ; i < triCount() ; ++i) {
		tri(i).mark = mark;
	}
}

//---------------------------------------------------------------------------
// EditTriMesh::markAllMaterials
//
// Mark all materials with the given value

void	EditTriMesh::markAllMaterials(int mark) {
	for (int i = 0 ; i < materialCount() ; ++i) {
		material(i).mark = mark;
	}
}

//---------------------------------------------------------------------------
// EditTriMesh::markAllParts
//
// Mark all parts with the given value

void	EditTriMesh::markAllParts(int mark) {
	for (int i = 0 ; i < partCount() ; ++i) {
		part(i).mark = mark;
	}
}

//---------------------------------------------------------------------------
// EditTriMesh::deleteVertex
//
// Deletes one vertex from the vertex list.  This will fixup vertex
// indices in the triangles, and also delete triangles that referenced
// that vertex

void	EditTriMesh::deleteVertex(int vertexIndex) {

	// Check index.  Warn in debug build, don't crash release

	if ((vertexIndex < 0) || (vertexIndex >= vertexCount())) {
		assert(false);
		return;
	}

	// Scan triangle list and fixup vertex indices

	for (int i = 0 ; i < triCount() ; ++i) {
		Tri *t = &tri(i);

		// Assume it won't get deleted

		t->mark = 0;

		// Fixup vertex indices on this face

		for (int j = 0 ; j < 3 ; ++j) {

			// Do we need to delete it?

			if (t->v[j].index == vertexIndex) {
				t->mark = 1;
				break;
			}

			// Fixup vertex index?

			if (t->v[j].index > vertexIndex) {
				--t->v[j].index;
			}
		}
	}

	// Delete the vertex from the vertex array

	--vCount;
	memmove(&vList[vertexIndex], &vList[vertexIndex+1], (vCount - vertexIndex) * sizeof(*vList));

	// Delete the triangles that used it

	deleteMarkedTris(1);
}

//---------------------------------------------------------------------------
// EditTriMesh::deleteTri
//
// Deletes one triangle from the triangle list.

void	EditTriMesh::deleteTri(int triIndex) {

	// Check index.  Warn in debug build, don't crash release

	if ((triIndex < 0) || (triIndex >= triCount())) {
		assert(false);
		return;
	}

	// Delete it

	--tCount;
	memmove(&tList[triIndex], &tList[triIndex+1], (tCount - triIndex) * sizeof(*tList));
}

//---------------------------------------------------------------------------
// EditTriMesh::deleteMaterial
//
// Deletes one material from the material list.  Material indices in
// the triangles are fixed up and any triangles that used that material
// are deleted

void	EditTriMesh::deleteMaterial(int materialIndex) {

	// Check index.  Warn in debug build, don't crash release

	if ((materialIndex < 0) || (materialIndex >= materialCount())) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -