Example #1
0
/* project from sphere to texture cube. First argument is (unit) vector 
   location on sphere. Returns the index of the face on which projected point lies, 
   and the texture coordinates of the intersection on the face.
   Note the cube faces "overhang" to avoid having to deal with triangles which
   project onto a cube edge or corner. */
void projecttocube(float *onsphere, int *facenum, float *tex)
{
	int face, nearest_face,i;
	float mu, d_intersect, faceside, dmin;
	float facenormal[3], xlocal[3], ylocal[3], intersection[3][6];
	float xintersect, yintersect, nearest_intersection[3];
	
	//dimensions of the (overhanging) cube faces
	faceside = 2.0f*(1.0f+flange);
	int firsttime = 1;

	//step through faces
	for (face=0;face<6;face++)
	{	
		for (i=0;i<3;i++) 
		{
			facenormal[i] = cubecoords.local_z[face][i];
		}	
	
		//compute scalar product of face normal and unit location vector.
		//if this is less than or equal to zero, we can rule out this face index.			
		mu = scalarprod(onsphere,facenormal);
		if (mu>0.0f) 
		{
			d_intersect = 0.0f;
			for (i=0;i<3;i++) 
			{
				intersection[i][face] = onsphere[i]/mu;				
				d_intersect += intersection[i][face]*intersection[i][face];
			}
			if (firsttime || d_intersect<dmin)
			{
				dmin = d_intersect;
				nearest_face = face;						
				firsttime = 0;
			}
		}
	}		
	*facenum = nearest_face;

	for (i=0;i<3;i++) 
	{
		nearest_intersection[i] = intersection[i][nearest_face];
		xlocal[i] = cubecoords.local_x[nearest_face][i];
		ylocal[i] = cubecoords.local_y[nearest_face][i];
	}	
	xintersect = scalarprod(nearest_intersection,xlocal);
	yintersect = scalarprod(nearest_intersection,ylocal);	
	
	//corresponding (u,v) texture coords
	tex[0] = xintersect/faceside+0.5f;
	tex[1] = yintersect/faceside+0.5f;
	if ((tex[0]<0.0f || tex[0]>1.0f) || (tex[1]<0.0f || tex[1]>1.0f)) 
	{
		printf("ERROR: texture coords. out of range in projecttocube (u,v): %f %f\n",tex[0],tex[1]);
		exit(EXIT_FAILURE);
	}	
}
Example #2
0
// return oriented vector angle in range [-pi..pi], pole must be orthogonal to a and b
double vectAngle(const Coord &a, const Coord &b, const Coord &pole)
{ 
  double nab = 1./(norm(a)*norm(b)) ;
  
  Coord a_cross_b=crossprod(a, b)*nab ;
  double sinVect ;
  if (scalarprod(a_cross_b, pole) >= 0) sinVect=norm(a_cross_b) ;
  else sinVect=-norm(a_cross_b) ;
  double cosVect=scalarprod(a,b)*nab ;

  return atan2(sinVect,cosVect) ;
}
Example #3
0
/* project from sphere to a specified face of the texture cube. 
   This function projects vertices of triangles which have already been assigned 
   to a texture cube face (since the first vertex was projected to that face).
   First argument is (unit) vector location on sphere. Second argument is the 
   desired face. Returns the texture coordinates of the intersection on the face */
void projecttoface(float *onsphere, int whichface, float *tex)
{
	int face,nearest_face,i;
	float mu,d_intersect,faceside,dmin;
	float facenormal[3],xlocal[3],ylocal[3],intersection[3][6];
	float xintersect,yintersect,nearest_intersection[3];
	
	//dimensions of the (overhanging) cube faces
	faceside = 2.0f*(1.0f+flange);
	for (i=0;i<3;i++) 
	{
		facenormal[i] = cubecoords.local_z[whichface][i];
	}	
	
	//compute scalar product of face normal and unit location vector.
	mu = scalarprod(onsphere,facenormal);
	if (mu>0.0f)
	{
		for (i=0;i<3;i++)
		{
			nearest_intersection[i] = onsphere[i]/mu;
		}
	}
	else
	{
		printf("something seriously wrong with vertices. See projecttoface\n");
		exit(EXIT_FAILURE);
	}
	
	for (i=0;i<3;i++) 
	{
		xlocal[i] = cubecoords.local_x[whichface][i];
		ylocal[i] = cubecoords.local_y[whichface][i];
	}	
	xintersect = scalarprod(nearest_intersection,xlocal);
	yintersect = scalarprod(nearest_intersection,ylocal);
	
	//corresponding (u,v) texture coords
	tex[0] = xintersect/faceside+0.5f;
	tex[1] = yintersect/faceside+0.5f;
	if ((tex[0]<0.0f || tex[0]>1.0f) || (tex[1]<0.0f || tex[1]>1.0f)) 
	{
		printf("texture coords. out of range in projecttoface (u,v): %f %f\n",tex[0],tex[1]);
		exit(EXIT_FAILURE);
	}	
}
Example #4
0
double angle(const Coord &a, const Coord &b, const Coord &pole)
{ 
	return scalarprod(crossprod(a, b), pole);
}