示例#1
0
void DebugDrawer::buildTriangle(const Ogre::Vector3 *vertices, const Ogre::ColourValue& colour, float alpha)
{
    int index = addLineVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    addLineVertex(vertices[1], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    addLineVertex(vertices[2], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));

    for (int i = 0; i < 3; ++i) addLineIndices(index + i, index + ((i + 1) % 3));
}
void DebugDrawer::buildLine(const Ogre::Vector3& start,
                     const Ogre::Vector3& end,
                     const Ogre::ColourValue& colour,
                     float alpha)
{
        int i = addLineVertex(start, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
        addLineVertex(end, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
 
        addLineIndices(i, i + 1);
}
void DebugDrawer::buildTetrahedron(const Ogre::Vector3 &centre,
	                               float scale,
								   const Ogre::ColourValue &colour,
								   float alpha)
{
	int index = linesIndex;

	// Distance from the centre
	float bottomDistance = scale * 0.2f;
	float topDistance = scale * 0.62f;
	float frontDistance = scale * 0.289f;
	float backDistance = scale * 0.577f;
	float leftRightDistance = scale * 0.5f;

	addLineVertex(Ogre::Vector3(centre.x, centre.y + topDistance, centre.z),
		Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
	addLineVertex(Ogre::Vector3(centre.x, centre.y - bottomDistance, centre.z + frontDistance),
		Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
	addLineVertex(Ogre::Vector3(centre.x + leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
		Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
	addLineVertex(Ogre::Vector3(centre.x - leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
		Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));

	addLineIndices(index, index + 1);
	addLineIndices(index, index + 2);
	addLineIndices(index, index + 3);

	addLineIndices(index + 1, index + 2);
	addLineIndices(index + 2, index + 3);
	addLineIndices(index + 3, index + 1);
}
void DebugDrawer::buildCylinder(const Ogre::Vector3 &centre,
							  float radius,
	                          int segmentsCount,
							  float height,
							  const Ogre::ColourValue& colour,
							  float alpha)
{
	int index = linesIndex;
	float increment = 2 * Ogre::Math::PI / segmentsCount;
	float angle = 0.0f;
 
	// Top circle
	for (int i = 0; i < segmentsCount; i++)
	{
		addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y + height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
			Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
		angle += increment;
	}

	angle = 0.0f;

	// Bottom circle
	for (int i = 0; i < segmentsCount; i++)
	{
		addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y - height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
			Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
		angle += increment;
	}
 
	for (int i = 0; i < segmentsCount; i++)
	{
		addLineIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index);
		addLineIndices(segmentsCount + index + i, i + 1 < segmentsCount ? segmentsCount + index + i + 1 : segmentsCount + index);
		addLineIndices(index + i, segmentsCount + index + i);
	}
}
void DebugDrawer::buildCuboid(const Ogre::Vector3 *vertices,
                                                          const Ogre::ColourValue& colour,
                                                          float alpha)
{
    int index = addLineVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    for (int i = 1; i < 8; ++i) addLineVertex(vertices[i], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
 
    for (int i = 0; i < 4; ++i) addLineIndices(index + i, index + ((i + 1) % 4));
    for (int i = 4; i < 8; ++i) addLineIndices(index + i, i == 7 ? index + 4 : index + i + 1);
    addLineIndices(index + 1, index + 5);
    addLineIndices(index + 2, index + 4);
    addLineIndices(index,     index + 6);
    addLineIndices(index + 3, index + 7);
}
void DebugDrawer::buildCircle(const Ogre::Vector3 &centre,
							  float radius,
	                          int segmentsCount,
							  const Ogre::ColourValue& colour,
							  float alpha)
{
	int index = linesIndex;
	float increment = 2 * Ogre::Math::PI / segmentsCount;
	float angle = 0.0f;
 
	for (int i = 0; i < segmentsCount; i++)
	{
		addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y, centre.z + radius * Ogre::Math::Sin(angle)),
			Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
		angle += increment;
	}
 
	for (int i = 0; i < segmentsCount; i++)
		addLineIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index);
}
void IcoSphere::create(int recursionLevel)
{
	vertices.clear();
	lineIndices.clear();
	triangleIndices.clear();
	faces.clear();
	middlePointIndexCache.clear();
	index = 0;
 
	float t = (1.0f + Ogre::Math::Sqrt(5.0f)) / 2.0f;
 
	addVertex(Ogre::Vector3(-1.0f,  t,  0.0f));
	addVertex(Ogre::Vector3( 1.0f,  t,  0.0f));
	addVertex(Ogre::Vector3(-1.0f, -t,  0.0f));
	addVertex(Ogre::Vector3( 1.0f, -t,  0.0f));
 
	addVertex(Ogre::Vector3( 0.0f, -1.0f,  t));
	addVertex(Ogre::Vector3( 0.0f,  1.0f,  t));
	addVertex(Ogre::Vector3( 0.0f, -1.0f, -t));
	addVertex(Ogre::Vector3( 0.0f,  1.0f, -t));
 
	addVertex(Ogre::Vector3( t,  0.0f, -1.0f));
	addVertex(Ogre::Vector3( t,  0.0f,  1.0f));
	addVertex(Ogre::Vector3(-t,  0.0f, -1.0f));
	addVertex(Ogre::Vector3(-t,  0.0f,  1.0f));
 
	addFace(0, 11, 5);
	addFace(0, 5, 1);
	addFace(0, 1, 7);
	addFace(0, 7, 10);
	addFace(0, 10, 11);
 
	addFace(1, 5, 9);
	addFace(5, 11, 4);
	addFace(11, 10, 2);
	addFace(10, 7, 6);
	addFace(7, 1, 8);
 
	addFace(3, 9, 4);
	addFace(3, 4, 2);
	addFace(3, 2, 6);
	addFace(3, 6, 8);
	addFace(3, 8, 9);
 
	addFace(4, 9, 5);
	addFace(2, 4, 11);
	addFace(6, 2, 10);
	addFace(8, 6, 7);
	addFace(9, 8, 1);
 
	addLineIndices(1, 0);
	addLineIndices(1, 5);
	addLineIndices(1, 7);
	addLineIndices(1, 8);
	addLineIndices(1, 9);
 
	addLineIndices(2, 3);
	addLineIndices(2, 4);
	addLineIndices(2, 6);
	addLineIndices(2, 10);
	addLineIndices(2, 11);
 
	addLineIndices(0, 5);
	addLineIndices(5, 9);
	addLineIndices(9, 8);
	addLineIndices(8, 7);
	addLineIndices(7, 0);
 
	addLineIndices(10, 11);
	addLineIndices(11, 4);
	addLineIndices(4, 3);
	addLineIndices(3, 6);
	addLineIndices(6, 10);
 
	addLineIndices(0, 11);
	addLineIndices(11, 5);
	addLineIndices(5, 4);
	addLineIndices(4, 9);
	addLineIndices(9, 3);
	addLineIndices(3, 8);
	addLineIndices(8, 6);
	addLineIndices(6, 7);
	addLineIndices(7, 10);
	addLineIndices(10, 0);
 
	for (int i = 0; i < recursionLevel; i++)
	{
		std::list<TriangleIndices> faces2;
 
		for (std::list<TriangleIndices>::iterator j = faces.begin(); j != faces.end(); j++)
		{
			TriangleIndices f = *j;
			int a = getMiddlePoint(f.v1, f.v2);
			int b = getMiddlePoint(f.v2, f.v3);
			int c = getMiddlePoint(f.v3, f.v1);
 
			removeLineIndices(f.v1, f.v2);
			removeLineIndices(f.v2, f.v3);
			removeLineIndices(f.v3, f.v1);
 
			faces2.push_back(TriangleIndices(f.v1, a, c));
			faces2.push_back(TriangleIndices(f.v2, b, a));
			faces2.push_back(TriangleIndices(f.v3, c, b));
			faces2.push_back(TriangleIndices(a, b, c));
 
			addTriangleLines(f.v1, a, c);
			addTriangleLines(f.v2, b, a);
			addTriangleLines(f.v3, c, b);
		}
 
		faces = faces2;
	}
}
void IcoSphere::addTriangleLines(int index0, int index1, int index2)
{
	addLineIndices(index0, index1);
	addLineIndices(index1, index2);
	addLineIndices(index2, index0);
}