Ejemplo n.º 1
0
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);

}
Ejemplo n.º 2
0
Terrain::Vertices Terrain::GetSmoothedVertices(const Terrain::Vertices& vertices, int segments) {
    Vertices tangentsForward;
    tangentsForward.resize(vertices.size());
    
    Vertices tangentsBackward;
    tangentsBackward.resize(vertices.size());
    
    const float amount = 0.125f;
    
    for (int i=0; i<vertices.size(); i++) {
        const Vector2& vertex = vertices[i];
        const Vector2& next = vertices[i==vertices.size()-1 ? 0 : i + 1];
        const Vector2& prev = vertices[i==0 ? vertices.size()-1 : i - 1];
        
        Vector2 toNext = (next - vertex);
        Vector2 toPrev = (prev - vertex);
        
        float toNextLen = toNext.Length();
        float toPrevLen = toPrev.Length();
        
        toNext /= toNextLen;
        toPrev /= toPrevLen;
        
        tangentsForward[i] = vertex + (-toPrev + toNext).Normalized() * toNextLen * amount;
        tangentsBackward[i] = vertex + (toPrev - toNext).Normalized() * toPrevLen * amount;
    }
    
    Terrain::Vertices smoothedVertices;
    
    for (int i=0; i<vertices.size(); i++) {
        int nextIndex = i==vertices.size()-1 ? 0 : i + 1;
        float dt = 1.0f / segments;
        for (int s=0; s<segments; s++) {
            smoothedVertices.push_back(Vector2::Bezier(vertices[i], tangentsForward[i], vertices[nextIndex], tangentsBackward[nextIndex], s * dt));
        }
    }

    return smoothedVertices;
}
Ejemplo n.º 3
0
void
insert_constraints_using_spatial_sort(SDG& sdg)
{
  typedef typename Points_container::const_iterator Points_iterator;
  typedef std::vector<Points_iterator> Indices;
  typedef std::vector<typename SDG::Vertex_handle> Vertices;

  Sort_traits_2<K, Points_iterator> sort_traits;

  Indices indices;
  indices.reserve(points.size());
  for(Points_iterator it = points.begin(); it != points.end(); ++it) {
    indices.push_back(it);
  }
  std::random_shuffle(indices.begin(), indices.end());
  CGAL::spatial_sort(indices.begin(), indices.end(),
                     sort_traits);

  std::cerr << "Inserting " << points.size() << " points...";
  CGAL::Timer timer;
  timer.start();
  Vertices vertices;
  vertices.resize(points.size());
  typename SDG::Vertex_handle hint;
  for(typename Indices::const_iterator
        pt_it_it = indices.begin(), end = indices.end();
      pt_it_it != end; ++pt_it_it) {
    typename SDG::Vertex_handle vh = sdg.insert(**pt_it_it, hint);
    hint = vh;
    vertices[*pt_it_it - points.begin()] = vh;
  }
  timer.stop();
  std::cerr << " done (" << timer.time() << "s)\n";

  std::cerr << "Inserting " << constraints.size() << " constraints...";

  timer.reset();
  timer.start();
  for(typename Constraints_container::const_iterator
        cit = constraints.begin(), end = constraints.end();
      cit != end; ++cit) {
    const typename SDG::Vertex_handle& v1 = vertices[cit->first];
    const typename SDG::Vertex_handle& v2 = vertices[cit->second];
    if(v1 != v2)
      sdg.insert(v1, v2);
  }

  timer.stop();
  std::cerr << " done (" << timer.time() << "s)\n";
}
Ejemplo n.º 4
0
Terrain::Vertices Terrain::CalculateNormals(const Terrain::Vertices& vertices) {
    Vertices normals;
    normals.resize(vertices.size());
    
    for (int i=0; i<vertices.size(); i++) {
        const Vector2& vertex = vertices[i];
        const Vector2& next = vertices[i==vertices.size()-1 ? 0 : i + 1];
        const Vector2& prev = vertices[i==0 ? vertices.size()-1 : i - 1];
        
        Vector2 toNextNormal(next.y - vertex.y, vertex.x - next.x);
        Vector2 toPrevNormal(vertex.y - prev.y, prev.x - vertex.x);
        toNextNormal.Normalize();
        toPrevNormal.Normalize();
        
        normals[i] = (toNextNormal + toPrevNormal).Normalized();
    }
    return normals;
}
Ejemplo n.º 5
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);

}