Ejemplo n.º 1
0
void MyPrimitive::GenerateSphere(float a_fRadius, int a_nSubdivisions, vector3 a_v3Color)
{
	//Sets minimum and maximum of subdivisions
	if (a_nSubdivisions < 1)
	{
		GenerateCube(a_fRadius * 2, a_v3Color);
		return;
	}
	if (a_nSubdivisions > 12)
		a_nSubdivisions = 12;

	Release();
	Init();

	//Your code starts here
	vector3 base1Center = vector3(0, 0, 0);
	vector<vector3> centerRingPoints = GenerateNGon(a_fRadius, a_nSubdivisions, base1Center);
	vector<vector3> prevUpperRingPoints = centerRingPoints;
	vector<vector3> prevLowerRingPoints = centerRingPoints;

	vector3 topCenter = vector3(0, 0, a_fRadius);
	vector3 bottomCenter = vector3(0, 0, -a_fRadius);

	float radIncrement = (3.1459 / 2) / (a_nSubdivisions / 2);
	for (size_t i = a_nSubdivisions / 2; i > 0; i--)
	{
		float z = a_fRadius * cos(i * radIncrement);
		float xRadius = a_fRadius * sin(i * radIncrement);

		vector<vector3> upperRingPoints = GenerateNGon(xRadius, a_nSubdivisions, vector3(0, 0, z));
		vector<vector3> lowerRingPoints = GenerateNGon(xRadius, a_nSubdivisions, vector3(0, 0, -z));

		for (size_t j = 0; j < a_nSubdivisions; j++)
		{
			int rightIndex = j;
			int leftIndex = (j + 1) % a_nSubdivisions;

			AddQuad(prevUpperRingPoints[rightIndex], prevUpperRingPoints[leftIndex], upperRingPoints[rightIndex], upperRingPoints[leftIndex]);
			AddQuad(prevLowerRingPoints[leftIndex], prevLowerRingPoints[rightIndex], lowerRingPoints[leftIndex], lowerRingPoints[rightIndex]);

			if (i == 1) {
				AddVertexPosition(upperRingPoints[rightIndex]);
				AddVertexPosition(upperRingPoints[leftIndex]);
				AddVertexPosition(topCenter);

				AddVertexPosition(bottomCenter);
				AddVertexPosition(lowerRingPoints[leftIndex]);
				AddVertexPosition(lowerRingPoints[rightIndex]);
				
			}
		}

		prevUpperRingPoints = upperRingPoints;
		prevLowerRingPoints = lowerRingPoints;
	}


	//Your code ends here
	CompileObject(a_v3Color);
}
Ejemplo n.º 2
0
void MyPrimitive::GenerateCone(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	Release();
	Init();

	//Your code starts here
	vector<vector3> basePoints = GenerateNGon(a_fRadius, a_nSubdivisions, vector3(0, 0, -a_fHeight / 2));
	vector3 vertexPoint = vector3(0, 0, a_fHeight / 2);
	vector3 baseCenter = vector3(0, 0, -a_fHeight / 2);

	for (size_t i = 0; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(basePoints[i]);
		AddVertexPosition(basePoints[(i + 1) % a_nSubdivisions]);
		AddVertexPosition(vertexPoint);

		AddVertexPosition(baseCenter);
		AddVertexPosition(basePoints[(i + 1) % a_nSubdivisions]);
		AddVertexPosition(basePoints[i]);
	}
	

	//Your code ends here
	CompileObject(a_v3Color);
}
//C--D
//|  |
//A--B
//This will make the triang A->B->C and then the triang C->B->D
void MyPrimitive::AddQuad(vector3 a_vBottomLeft, vector3 a_vBottomRight, vector3 a_vTopLeft, vector3 a_vTopRight)
{
	AddVertexPosition(a_vBottomLeft);
	AddVertexPosition(a_vBottomRight);
	AddVertexPosition(a_vTopLeft);

	AddVertexPosition(a_vTopLeft);
	AddVertexPosition(a_vBottomRight);
	AddVertexPosition(a_vTopRight);
}
Ejemplo n.º 4
0
void PolygonClass::GeneratePoligon(int a_nSides, float a_fSize)
{
	GLfloat theta = 0;
	for (int i = 0; i < a_nSides; i++) 
	{
		theta += static_cast<GLfloat>(2*M_PI/a_nSides);
		AddVertexPosition(vector3(	static_cast<GLfloat>(cos(theta)) * a_fSize,
							static_cast<GLfloat>(sin(theta)) * a_fSize,
							0.0f));
		AddVertexColor( 1.0f, 1.0f, 1.0f);
		AddVertexUV( 0.0f, 0.0f);
		AddVertexNormal(0.0f, 0.0f, 0.0f);
		AddVertexTangent(0.0f, 0.0f, 0.0f);
	}
	InitGPU();
}
Ejemplo n.º 5
0
void MyPrimitive::GenerateCylinder(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	Release();
	Init();

	//Your code starts here
	vector3 base1Center = vector3(0, 0, -a_fHeight / 2);
	vector<vector3> base1Points = GenerateNGon(a_fRadius, a_nSubdivisions, base1Center);

	vector3 base2Center = vector3(0, 0, a_fHeight / 2);
	vector<vector3> base2Points = GenerateNGon(a_fRadius, a_nSubdivisions, base2Center);

	for (size_t i = 0; i < a_nSubdivisions; i++)
	{
		int rightIndex = i;
		int leftIndex = (i + 1) % a_nSubdivisions;

		AddVertexPosition(base1Center);
		AddVertexPosition(base1Points[leftIndex]);
		AddVertexPosition(base1Points[rightIndex]);

		AddQuad(base2Points[leftIndex], base2Points[rightIndex], base1Points[leftIndex], base1Points[rightIndex]);

		AddVertexPosition(base2Points[rightIndex]);
		AddVertexPosition(base2Points[leftIndex]);
		AddVertexPosition(base2Center);
	}

	//Your code ends here
	CompileObject(a_v3Color);
}
void MyPrimitive::GenerateCone
(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_vColor)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	//Clean up Memory
	Release();
	Init();

	//Your Code Goes Here instead of the next three lines
	//Calculating the bottom vertices
	std::vector<vector3 > vertex;
	vertex.push_back(vector3(0.0f, -a_fHeight / 2.0f, 0.0f));

	GLfloat theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fRadius,
			-a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fRadius);
		vertex.push_back(temp);
	}

	vertex.push_back(vector3(0.0f, a_fHeight / 2.0f, 0.0f));

	//Bottom Faces
	for (int i = 1; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(vertex[0]);
		AddVertexPosition(vertex[i + 1]);
		AddVertexPosition(vertex[i]);
	}
	AddVertexPosition(vertex[0]);
	AddVertexPosition(vertex[1]);
	AddVertexPosition(vertex[a_nSubdivisions]);

	//Side Faces
	for (int i = 1; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(vertex[i]);
		AddVertexPosition(vertex[i + 1]);
		AddVertexPosition(vertex[a_nSubdivisions + 1]);
	}

	AddVertexPosition(vertex[a_nSubdivisions]);
	AddVertexPosition(vertex[1]);
	AddVertexPosition(vertex[a_nSubdivisions + 1]);

	//Compile the object in this color and assign it this name
	CompileObject(a_vColor);
}
void MyPrimitive::GenerateSphere(float a_fRadius, int a_nSubdivisions, vector3 a_vColor)
{
	//Sets minimum and maximum of subdivisions
	if (a_nSubdivisions < 1)
	{
		GenerateCube(a_fRadius * 2, a_vColor);
		return;
	}
	if (a_nSubdivisions > 6)
		a_nSubdivisions = 6;

	//Clean up Memory
	Release();
	Init();

	//Your Code Goes Here instead of the next three lines
	float fValue = 0.5f;
	vector3 pointA(-fValue, -fValue, fValue); //0
	vector3 pointB(fValue, -fValue, fValue); //1
	vector3 pointC(-fValue, fValue, fValue); //2

	//left to right List of vector3
	std::vector<vector3> vectorAB;
	vectorAB.push_back(pointA);
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		vector3 temp(pointB - pointA);
		temp /= a_nSubdivisions + 1;
		temp *= (i + 1);
		vectorAB.push_back(temp + pointA);
	}
	vectorAB.push_back(pointB);

	//height increments
	float fHeight = pointC.y - pointA.y;
	fHeight /= a_nSubdivisions + 1;

	//List of Lists
	std::vector<std::vector<vector3>> list;
	list.push_back(vectorAB);
	for (int j = 0; j < a_nSubdivisions + 1; j++)
	{
		std::vector<vector3> temp = list[0];
		float increment = fHeight * (j + 1);
		for (int i = 0; i < a_nSubdivisions + 2; i++)
		{
			temp[i].y += increment;
		}
		list.push_back(temp);
	}

	//Creating the patch of quads
	for (int j = 0; j < a_nSubdivisions + 1; j++)
	{
		for (int i = 0; i < a_nSubdivisions + 1; i++)
		{
			AddQuad(list[j][i], list[j][i + 1], list[j + 1][i], list[j + 1][i + 1]);
		}
	}
	
	int nVertices = static_cast<int>(m_lVertexPos.size());

	//normalizing the vectors to make them round
	for (int i = 0; i < nVertices; i++)
	{
		m_lVertexPos[i] = glm::normalize(m_lVertexPos[i]);
		m_lVertexPos[i] *= a_fRadius;
	}

	//RightSideFace
	std::vector<vector3> right;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 90.0f, vector3(0.0f, 1.0f, 0.0f));
		right.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}


	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(right[i]);
	}

	//LeftSideFace
	std::vector<vector3> left;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), -90.0f, vector3(0.0f, 1.0f, 0.0f));
		left.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(left[i]);
	}

	//BackSideFace
	std::vector<vector3> back;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 180.0f, vector3(0.0f, 1.0f, 0.0f));
		back.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(back[i]);
	}

	//TopSideFace
	std::vector<vector3> top;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), -90.0f, vector3(1.0f, 0.0f, 0.0f));
		top.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(top[i]);
	}

	//BottomSideFace
	std::vector<vector3> bottom;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 90.0f, vector3(1.0f, 0.0f, 0.0f));
		bottom.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(bottom[i]);
	}

	//Compile the object in this color and assign it this name
	CompileObject(a_vColor);
}
void MyPrimitive::GenerateTube(float a_fOuterRadius, float a_fInnerRadius, float a_fHeight, int a_nSubdivisions, vector3 a_vColor)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	//Clean up Memory
	Release();
	Init();

	//Your Code Goes Here instead of the next three lines
	//Calculating the top vertices
	std::vector<vector3 > vertexTopOuter;
	GLfloat theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fOuterRadius,
			a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fOuterRadius);
		vertexTopOuter.push_back(temp);
	}

	std::vector<vector3 > vertexTopInner;
	theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fInnerRadius,
			a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fInnerRadius);
		vertexTopInner.push_back(temp);
	}

	//Bottom Outer
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexTopInner[i]);
		AddVertexPosition(vertexTopOuter[i]);
		AddVertexPosition(vertexTopOuter[i + 1]);
	}
	AddVertexPosition(vertexTopInner[a_nSubdivisions - 1]);
	AddVertexPosition(vertexTopOuter[a_nSubdivisions - 1]);
	AddVertexPosition(vertexTopOuter[0]);

	//Bottom Inner
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexTopOuter[i + 1]);
		AddVertexPosition(vertexTopInner[i + 1]);
		AddVertexPosition(vertexTopInner[i]);
	}
	AddVertexPosition(vertexTopOuter[0]);
	AddVertexPosition(vertexTopInner[0]);
	AddVertexPosition(vertexTopInner[a_nSubdivisions - 1]);

	//Calculating the bottom vertices
	std::vector<vector3 > vertexBottomOuter;
	theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fOuterRadius,
			-a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fOuterRadius);
		vertexBottomOuter.push_back(temp);
	}

	std::vector<vector3 > vertexBottomInner;
	theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fInnerRadius,
			-a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fInnerRadius);
		vertexBottomInner.push_back(temp);
	}

	//Bottom Outer
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexBottomOuter[i + 1]);
		AddVertexPosition(vertexBottomOuter[i]);
		AddVertexPosition(vertexBottomInner[i]);
	}
	AddVertexPosition(vertexBottomOuter[0]);
	AddVertexPosition(vertexBottomOuter[a_nSubdivisions - 1]);
	AddVertexPosition(vertexBottomInner[a_nSubdivisions - 1]);


	//Bottom Inner
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexBottomInner[i]);
		AddVertexPosition(vertexBottomInner[i + 1]);
		AddVertexPosition(vertexBottomOuter[i + 1]);
	}
	AddVertexPosition(vertexBottomInner[a_nSubdivisions - 1]);
	AddVertexPosition(vertexBottomInner[0]);
	AddVertexPosition(vertexBottomOuter[0]);

	//Vertical Inner
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexTopInner[i]);
		AddVertexPosition(vertexBottomInner[i + 1]);
		AddVertexPosition(vertexBottomInner[i]);
	}
	AddVertexPosition(vertexTopInner[a_nSubdivisions - 1]);
	AddVertexPosition(vertexBottomInner[0]);
	AddVertexPosition(vertexBottomInner[a_nSubdivisions - 1]);

	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexTopInner[i + 1]);
		AddVertexPosition(vertexBottomInner[i + 1]);
		AddVertexPosition(vertexTopInner[i]);
	}
	AddVertexPosition(vertexTopInner[0]);
	AddVertexPosition(vertexBottomInner[0]);
	AddVertexPosition(vertexTopInner[a_nSubdivisions - 1]);

	//Vertical Outer
	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexBottomOuter[i]);
		AddVertexPosition(vertexBottomOuter[i + 1]);
		AddVertexPosition(vertexTopOuter[i]);
	}
	AddVertexPosition(vertexBottomOuter[a_nSubdivisions - 1]);
	AddVertexPosition(vertexBottomOuter[0]);
	AddVertexPosition(vertexTopOuter[a_nSubdivisions - 1]);

	for (int i = 0; i < a_nSubdivisions - 1; i++)
	{
		AddVertexPosition(vertexTopOuter[i + 1]);
		AddVertexPosition(vertexTopOuter[i]);
		AddVertexPosition(vertexBottomOuter[i + 1]);
	}
	AddVertexPosition(vertexTopOuter[0]);
	AddVertexPosition(vertexTopOuter[a_nSubdivisions - 1]);
	AddVertexPosition(vertexBottomOuter[0]);

	//Compile the object in this color and assign it this name
	CompileObject(a_vColor);
}
void MyPrimitive::GenerateCylinder(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_vColor)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	//Clean up Memory
	Release();
	Init();

	//Calculating the bottom vertices
	std::vector<vector3 > vertexB;
	vertexB.push_back(vector3(0.0f, -a_fHeight / 2.0f, 0.0f));
	GLfloat theta = 0;
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		theta += static_cast<GLfloat>(2 * PI / a_nSubdivisions);
		vector3 temp = vector3(static_cast<GLfloat>(cos(theta)) * a_fRadius,
			-a_fHeight / 2.0f,
			-static_cast<GLfloat>(sin(theta)) * a_fRadius);
		vertexB.push_back(temp);
	}

	//calculating the top vertices
	std::vector<vector3 > vertexU;
	vertexU.push_back(vector3(0.0f, a_fHeight / 2.0f, 0.0f));
	theta = 0;
	vector3 vHeight = vector3(0.0f, a_fHeight, 0.0f);
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		vertexU.push_back(vertexB[i + 1] + vHeight);
	}

	//Bottom Faces
	for (int i = 1; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(vertexB[0]);
		AddVertexPosition(vertexB[i + 1]);
		AddVertexPosition(vertexB[i]);
	}
	AddVertexPosition(vertexB[0]);
	AddVertexPosition(vertexB[1]);
	AddVertexPosition(vertexB[a_nSubdivisions]);

	//Top Faces
	for (int i = 1; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(vertexU[0]);
		AddVertexPosition(vertexU[i]);
		AddVertexPosition(vertexU[i + 1]);
	}
	AddVertexPosition(vertexU[0]);
	AddVertexPosition(vertexU[a_nSubdivisions]);
	AddVertexPosition(vertexU[1]);

	//Side Faces
	for (int i = 1; i < a_nSubdivisions; i++)
	{
		AddVertexPosition(vertexB[i]);
		AddVertexPosition(vertexB[i + 1]);
		AddVertexPosition(vertexU[i]);

		AddVertexPosition(vertexU[i]);
		AddVertexPosition(vertexB[i + 1]);
		AddVertexPosition(vertexU[i + 1]);
	}
	AddVertexPosition(vertexB[a_nSubdivisions]);
	AddVertexPosition(vertexB[1]);
	AddVertexPosition(vertexU[a_nSubdivisions]);

	AddVertexPosition(vertexU[a_nSubdivisions]);
	AddVertexPosition(vertexB[1]);
	AddVertexPosition(vertexU[1]);

	//Compile the object in this color and assign it this name
	CompileObject(a_vColor);
}
Ejemplo n.º 10
0
void MyPrimitive::GenerateSphere(float a_fRadius, int a_nSubdivisions, vector3 a_v3Color)
{
	////Sets minimum and maximum of subdivisions
	//if (a_nSubdivisions < 1)
	//{
	//	GenerateCube(a_fRadius * 2, a_v3Color);
	//	return;
	//}
	//if (a_nSubdivisions > 6)
	//	a_nSubdivisions = 6;

	Release();
	Init();

	//Your code starts here
	vector3 topPoint(0.00f, a_fRadius, 0.00f);
	vector3 bottomPoint(0.00f, -a_fRadius, 0.00f);
	std::vector<std::vector<vector3>> spherePoints; //holds all other vertices on the sphere

	//add the middle ring of vertices; if the number of subdivisions is even, there are two middle rings
	if (a_nSubdivisions % 2 == 0)
	{
		std::vector<vector3> topMiddleRing;
		for (int x = 0; x < a_nSubdivisions; x++)
		{
			//calculate vertex positions
			float angle = ((2 * PI) / a_nSubdivisions) * x;
			float x_Value = a_fRadius * sin(angle);
			float z_Value = a_fRadius * cos(angle);

			topMiddleRing.push_back(vector3(x_Value, a_fRadius / (2 * a_nSubdivisions), z_Value));
		}
		std::vector<vector3> bottomMiddleRing;
		for (int x = 0; x < a_nSubdivisions; x++)
		{
			//calculate vertex positions
			float angle = ((2 * PI) / a_nSubdivisions) * x;
			float x_Value = a_fRadius * sin(angle);
			float z_Value = a_fRadius * cos(angle);

			bottomMiddleRing.push_back(vector3(x_Value, -a_fRadius / (2 * a_nSubdivisions), z_Value));
		}
		spherePoints.push_back(topMiddleRing);
		spherePoints.push_back(bottomMiddleRing);
	}
	else
	{
		std::vector<vector3> middleRing;
		for (int x = 0; x < a_nSubdivisions; x++)
		{
			//calculate vertex positions
			float angle = ((2 * PI) / a_nSubdivisions) * x;
			float x_Value = a_fRadius * sin(angle);
			float z_Value = a_fRadius * cos(angle);

			middleRing.push_back(vector3(x_Value, 0.00f, z_Value));
		}
		spherePoints.push_back(middleRing);
	}

	//add more rings to the sphere equal to the number of subdivisions minus existing rings
	//rings can be added in pairs moving away from the middle ring/rings in both directions,
	//so the loop will run half the number of times
	int existingRings = spherePoints.size();
	for (int x = 0; x < (a_nSubdivisions -  existingRings)/ 2; x++)
	{
		std::vector<vector3> upperRing;
		std::vector<vector3> lowerRing;
		//get an adjusted radius for this pair of rings
		float adjustedRadius = (a_fRadius / ((a_nSubdivisions / 2) + 1)) * ((a_nSubdivisions / 2) - x);

		for (int y = 0; y < a_nSubdivisions; y++)
		{
			//calculate vertex positions
			float angle = ((2 * PI) / a_nSubdivisions) * y;
			float x_Value = adjustedRadius * sin(angle);
			float z_Value = adjustedRadius * cos(angle);

			upperRing.push_back(vector3(x_Value, a_fRadius - adjustedRadius, z_Value));
			lowerRing.push_back(vector3(x_Value, -a_fRadius + adjustedRadius, z_Value));
		}
		spherePoints.push_back(upperRing);
		spherePoints.push_back(lowerRing);
	}

	//draw quads around the sphere moving away from the middle ring/rings
	if (a_nSubdivisions % 2 == 0)
	{
		//draw middle ring of quads
		for (int x = 0; x < spherePoints[0].size(); x++)
		{
			if (x != spherePoints[0].size() - 1)
			{
				AddQuad(spherePoints[1][x], spherePoints[1][x + 1], spherePoints[0][x], spherePoints[0][x + 1]);
			}
			else
			{
				AddQuad(spherePoints[1][x], spherePoints[1][0], spherePoints[0][x], spherePoints[0][0]);
			}
		}
		//draw more rings of quads
		for (int x = 0; x < spherePoints.size() - 2; x++)
		{
			if (x % 2 == 0)
			{
				for (int y = 0; y < spherePoints[x].size(); y++)
				{
					if (y != spherePoints[x].size() - 1)
					{
						AddQuad(spherePoints[x][y], spherePoints[x][y + 1], spherePoints[x + 2][y], spherePoints[x + 2][y + 1]);
					}
					else
					{
						AddQuad(spherePoints[x][y], spherePoints[x][0], spherePoints[x + 2][y], spherePoints[x + 2][0]);
					}
				}
			}
			else
			{
				for (int y = 0; y < spherePoints[x].size(); y++)
				{
					if (y != spherePoints[x].size() - 1)
					{
						AddQuad(spherePoints[x + 2][y], spherePoints[x + 2][y + 1], spherePoints[x][y], spherePoints[x][y + 1]);
					}
					else
					{
						AddQuad(spherePoints[x + 2][y], spherePoints[x + 2][0], spherePoints[x][y], spherePoints[x][0]);
					}
				}
			}
		}
	}
	else
	{

	}

	//draw the tris for the top and bottom
	for (int x = 0; x < spherePoints[spherePoints.size() - 1].size(); x++)
	{
		AddVertexPosition(bottomPoint);
		if (x == spherePoints[spherePoints.size() - 1].size() - 1)
		{
			AddVertexPosition(spherePoints[spherePoints.size() - 1][0]);
		}
		else
		{
			AddVertexPosition(spherePoints[spherePoints.size() - 1][x + 1]);
		}
		AddVertexPosition(spherePoints[spherePoints.size() - 1][x]);
	}
	for (int x = 0; x < spherePoints[spherePoints.size() - 2].size(); x++)
	{
		AddVertexPosition(spherePoints[spherePoints.size() - 2][x]);
		if (x == spherePoints[spherePoints.size() - 2].size() - 1)
		{
			AddVertexPosition(spherePoints[spherePoints.size() - 2][0]);
		}
		else
		{
			AddVertexPosition(spherePoints[spherePoints.size() - 2][x + 1]);
		}
		AddVertexPosition(topPoint);
	}

	//float fValue = 0.5f;
	////3--2
	////|  |
	////0--1
	//vector3 point0(-fValue, -fValue, fValue); //0
	//vector3 point1(fValue, -fValue, fValue); //1
	//vector3 point2(fValue, fValue, fValue); //2
	//vector3 point3(-fValue, fValue, fValue); //3

	//AddQuad(point0, point1, point3, point2);

	//Your code ends here
	CompileObject(a_v3Color);
}
Ejemplo n.º 11
0
void MyPrimitive::GenerateCylinder(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
	if (a_nSubdivisions < 3)
		a_nSubdivisions = 3;
	if (a_nSubdivisions > 360)
		a_nSubdivisions = 360;

	Release();
	Init();

	//Your code starts here
	vector3 topMid(0.00f, a_fHeight / 2, 0.00f); //mid-top vertex
	vector3 baseMid(0.00f, -(a_fHeight / 2), 0.00f); //mid-base vertex
	std::vector<vector3> topPoints; //top vertices
	std::vector<vector3> basePoints; //base vertices
	for (int x = 0; x < a_nSubdivisions; x++)
	{
		//calculate vertex positions
		float angle = ((2 * PI) / a_nSubdivisions) * x;
		float x_Value = a_fRadius * sin(angle);
		float z_Value = a_fRadius * cos(angle);

		basePoints.push_back(vector3(x_Value, -(a_fHeight / 2), z_Value));
	}
	for (int x = 0; x < a_nSubdivisions; x++)
	{
		//calculate vertex positions
		float angle = ((2 * PI) / a_nSubdivisions) * x;
		float x_Value = a_fRadius * sin(angle);
		float z_Value = a_fRadius * cos(angle);

		topPoints.push_back(vector3(x_Value, a_fHeight / 2, z_Value));
	}
	//create base
	for (int x = 0; x < basePoints.size(); x++)
	{
		AddVertexPosition(baseMid);
		if (x == basePoints.size() - 1)
		{
			AddVertexPosition(basePoints[0]);
		}
		else
		{
			AddVertexPosition(basePoints[x + 1]);
		}
		AddVertexPosition(basePoints[x]);
	}
	//create top
	for (int x = 0; x <topPoints.size(); x++)
	{
		AddVertexPosition(topMid);
		AddVertexPosition(topPoints[x]);
		if (x == topPoints.size() - 1)
		{
			AddVertexPosition(topPoints[0]);
		}
		else
		{
			AddVertexPosition(topPoints[x + 1]);
		}
	}
	//create sides
	for (int x = 0; x < basePoints.size(); x++)
	{
		if (x != basePoints.size() - 1)
		{
			AddQuad(basePoints[x], basePoints[x + 1], topPoints[x], topPoints[x + 1]);
		}
		else
		{
			AddQuad(basePoints[x], basePoints[0], topPoints[x], topPoints[0]);
		}
	}

	//float fValue = 0.5f;
	////3--2
	////|  |
	////0--1
	//vector3 point0(-fValue, -fValue, fValue); //0
	//vector3 point1(fValue, -fValue, fValue); //1
	//vector3 point2(fValue, fValue, fValue); //2
	//vector3 point3(-fValue, fValue, fValue); //3

	//AddQuad(point0, point1, point3, point2);

	//Your code ends here
	CompileObject(a_v3Color);
}