int ParticleSystem::createSphere(int numParticles, float maxspray, Vertices &vtx, Indices &ind)

{
	int i;
	Vector3f pos;
	Vector3f norm;
	Vector2f texCoord;
	Vector4f colour;
	Vector3f wander;

	vtx.resize(numParticles);
	std::cout << "   the vector's size is: " << vtx.size() << std::endl;
	std::cout << "   the vector's capacity is: " << vtx.capacity() << std::endl;
	std::cout << "   the vector's maximum size is: " << vtx.max_size() << std::endl;

	ind.resize(0);

	srand(time(0));
	float trad = 0.4; // Defines the starting point of the particles
	/* Create a set of points which will be the particles */
	/* This is similar to drawing a torus: we will sample points on the surface of the torus */

	float u, v, w, theta, phi, spray; // Work variables
	for (int i = 0; i < numParticles; i++){
			
		// Randomly select two numbers to define a point on the torus
		u = ((double) rand() / (RAND_MAX));
        v = ((double) rand() / (RAND_MAX));
            
		// Use u and v to define the point on the torus
        theta = u * 2.0f*M_PI;
		phi = v * 2.0f*M_PI;
        norm = Vector3f(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi));

		pos = Vector3f(norm.x*trad, norm.y*trad, norm.z*trad);
		colour = Vector4f(((float)i) / ((float)numParticles), 0.0f, 1.0f - (((float)i) / ((float)numParticles)), 1.0f);
		texCoord = Vector2f(0, 0); //not used for particels
		// Now sample a point on a sphere to define a direction for points to wander around
		u = ((double) rand() / (RAND_MAX));
        v = ((double) rand() / (RAND_MAX));
		w = ((double) rand() / (RAND_MAX));
			
		theta = u * 2*M_PI;
		phi = acos(2.0*v * -1.0);
		spray = maxspray*pow((float) w, (float) (1.0/3.0)); // Cubic root
		wander = Vector3f(spray*sin(theta)*sin(phi), spray*cos(theta)*sin(phi), spray*cos(phi));

		norm = wander;
		vtx[i] = Vertex(pos, colour, norm, texCoord);
	}

	return(0);

}
Esempio n. 2
0
int Sphere::createSphere(int numLong, int numLat, Vertices &vtx, Indices &ind, Vector4f colour)

{
	int i, j, k;
	int numRows;
	int numCols;
	int numVtx;
	int numTriangles;
	Vector3f pos;
	Vector4f col;
	Vector3f norm;
	Vector2f texCoord;
	float alpha;
	float beta;
	float deltaAlpha;
	float deltaBeta;

	Vector4f noColour = (-1, -1, -1, -1);

	numRows = numLat * 2;  // number of horizonal slabs
	numCols = numLong;	// number of vertical slabs

	numVtx = (numRows + 1) * (numCols + 1);
	vtx.resize(numVtx);
	cout << "   the vector's size is: " << vtx.size() << endl;
	cout << "   the vector's capacity is: " << vtx.capacity() << endl;
	cout << "   the vector's maximum size is: " << vtx.max_size() << endl;


	numTriangles = numRows * numCols * 2;
	ind.resize(numTriangles * 3);

	// Fill the vertex buffer with positions
	k = 0;
	alpha = 0.0f;  // angle of latitude starting from the "south pole"
	deltaAlpha = (float)90.0 / numLat; // increment of alpha
	beta = 0;   // angle of the longtidute 
	deltaBeta = (float)360.0 / (numLong);	// increment of beta
	float dTexX = 1.0 / numCols;
	float dTexY = 1.0 / numRows;

	for (i = 0, alpha = -90; i <= numRows; i++, alpha += deltaAlpha) {
		for (j = 0, beta = 0; j <= numCols; j++, beta += deltaBeta) {
			pos.x = cos(DegreeToRadians(alpha))*cos(DegreeToRadians(beta));
			pos.y = cos(DegreeToRadians(alpha))*sin(DegreeToRadians(beta));
			pos.z = sin(DegreeToRadians(alpha));
		
			//spheres normals are just the point - the center, but the center is at 0,0 so we just normalize the point
			norm = Vector3f(pos.x, pos.y, pos.z);
			norm.normalize();

			texCoord = Vector2f(j*dTexX, i*dTexY);

			if (colour == noColour){
				vtx[k] = Vertex(pos, Vector4f(pos, 1.0), norm, texCoord);
			}
			else{
				vtx[k] = Vertex(pos, colour, norm, texCoord);
			}
			
			k++;
		}
	}

	// fill the index buffer

	k = 0;
	for (i = 0; i < numRows; i++) {
		for (j = 0; j < numCols; j++) {
			// fill indices for the quad
			// change by making a quad function
			ind[k++] = i * (numCols + 1) + j;
			ind[k++] = i * (numCols + 1) + j + 1;
			ind[k++] = (i + 1) * (numCols + 1) + j + 1;

			ind[k++] = i * (numCols + 1) + j;
			ind[k++] = (i + 1) * (numCols + 1) + j + 1;
			ind[k++] = (i + 1) * (numCols + 1) + j;
		}
	}

	return(0);

}