OpenGL: 纹理映射函数

/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
*   Map a point (x, y, z) on a cylinder of radius 1, height 1, that has its axis
*   of symmetry along the y-axis to the square [0,1]x[0,1].
*
* CHANGES
*
******************************************************************************/

static int cylindrical_image_map(VECTOR EPoint, IMAGE *Image, DBL *u, DBL  *v)
{
	DBL len, theta;
	DBL x = EPoint[X];
	DBL y = EPoint[Y];
	DBL z = EPoint[Z];

	if ((Image->Once_Flag) && ((y < 0.0) || (y > 1.0)))
	{
		return 0;
	}

	*v = fmod(y * Image->height, Image->height);

	/* Make sure this vector is on the unit sphere. */

	len = sqrt(x * x + y * y + z * z);

	if (len == 0.0)
	{
		return 0;
	}
	else
	{
		x /= len;
		z /= len;
	}

	/* Determine its angle from the point (1, 0, 0) in the x-z plane. */

	len = sqrt(x * x + z * z);

	if (len == 0.0)
	{
		return 0;
	}
	else
	{
		if (z == 0.0)
		{
			if (x > 0)
			{
				theta = 0.0;
			}
			else
			{
				theta = M_PI;
			}
		}
		else
		{
			theta = acos(x / len);

			if (z < 0.0)
			{
				theta = TWO_M_PI - theta;
			}
		}

		theta /= TWO_M_PI;  /* This will be from 0 to 1 */
	}

	*u = (theta * Image->width);

	return 1;
}



/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
*   Map a point (x, y, z) on a torus  to a 2-d image.
*
* CHANGES
*
******************************************************************************/

static int torus_image_map(VECTOR EPoint, IMAGE *Image, DBL *u, DBL  *v)
{
	DBL len, phi, theta;
	DBL r0;
	DBL x = EPoint[X];
	DBL y = EPoint[Y];
	DBL z = EPoint[Z];

	r0 = Image->Gradient[X];

	/* Determine its angle from the x-axis. */

	len = sqrt(x * x + z * z);

	if (len == 0.0)
	{
		return 0;
	}
	else
	{
		if (z == 0.0)
		{
			if (x > 0)
			{
				theta = 0.0;
			}
			else
			{
				theta = M_PI;
			}
		}
		else
		{
			theta = acos(x / len);

			if (z < 0.0)
			{
				theta = TWO_M_PI - theta;
			}
		}
	}

	theta = 0.0 - theta;

	/* Now rotate about the y-axis to get the point (x, y, z) into the x-y plane. */

	x = len - r0;

	len = sqrt(x * x + y * y);

	phi = acos(-x / len);

	if (y > 0.0)
	{
		phi = TWO_M_PI - phi;
	}

	/* Determine the parametric coordinates. */

	theta /= TWO_M_PI;

	phi /= TWO_M_PI;

	*u = (-theta * Image->width);

	*v = (phi * Image->height);

	return 1;
}


/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
*   Map a point (x, y, z) on a sphere of radius 1 to a 2-d image. (Or is it the
*   other way around?)
*
* CHANGES
*
******************************************************************************/

static int spherical_image_map(VECTOR EPoint, IMAGE *Image, DBL *u, DBL  *v)
{
	DBL len, phi, theta;
	DBL x = EPoint[X];
	DBL y = EPoint[Y];
	DBL z = EPoint[Z];

	/* Make sure this vector is on the unit sphere. */

	len = sqrt(x * x + y * y + z * z);

	if (len == 0.0)
	{
		return 0;
	}
	else
	{
		x /= len;
		y /= len;
		z /= len;
	}

	/* Determine its angle from the x-z plane. */

	phi = 0.5 + asin(y) / M_PI; /* This will be from 0 to 1 */


	/* Determine its angle from the point (1, 0, 0) in the x-z plane. */

	len = sqrt(x * x + z * z);

	if (len == 0.0)
	{
		/* This point is at one of the poles. Any value of xcoord will be ok... */

		theta = 0;
	}
	else
	{
		if (z == 0.0)
		{
			if (x > 0)
			{
				theta = 0.0;
			}
			else
			{
				theta = M_PI;
			}
		}
		else
		{
			theta = acos(x / len);

			if (z < 0.0)
			{
				theta = TWO_M_PI - theta;
			}
		}

		theta /= TWO_M_PI;  /* This will be from 0 to 1 */
	}

	*u = (theta * Image->width);
	*v = (phi * Image->height);

	return 1;
}

/*
* 2-D to 3-D Procedural Texture Mapping of a Bitmapped Image onto an Object:
*
* Simplistic planar method of object image projection devised by DKB and AAC.
*
* 1. Transform texture in 3-D space if requested. 2. Determine local object 2-d
* coords from 3-d coords by <X Y Z> triple. 3. Return pixel color value at
* that position on the 2-d plane of "Image". 3. Map colour value in Image
* [0..255] to a more normal colour range [0..1].
*/



/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* DESCRIPTION
*
*   Return 0 if there is no color at this point (i.e. invisible), return 1 if a
*   good mapping is found.
*
* CHANGES
*
******************************************************************************/

static int planar_image_map(VECTOR EPoint, IMAGE *Image, DBL *u, DBL  *v)
{
	DBL x = EPoint[X];
	DBL y = EPoint[Y];
	DBL z = EPoint[Z];

	if (Image->Gradient[X] != 0.0)
	{
		if ((Image->Once_Flag) && ((x < 0.0) || (x > 1.0)))
		{
			return 0;
		}

		if (Image->Gradient[X] > 0)
		{
			*u = fmod(x * Image->width, Image->width);
		}
		else
		{
			*v = fmod(x * Image->height, Image->height);
		}
	}

	if (Image->Gradient[Y] != 0.0)
	{
		if ((Image->Once_Flag) && ((y < 0.0) || (y > 1.0)))
		{
			return 0;
		}

		if (Image->Gradient[Y] > 0)
		{
			*u = fmod(y * Image->width, Image->width);
		}
		else
		{
			*v = fmod(y * Image->height, Image->height);
		}
	}

	if (Image->Gradient[Z] != 0.0)
	{
		if ((Image->Once_Flag) && ((z < 0.0) || (z > 1.0)))
		{
			return 0;
		}

		if (Image->Gradient[Z] > 0)
		{
			*u = fmod(z * Image->width, Image->width);
		}
		else
		{
			*v = fmod(z * Image->height, Image->height);
		}
	}

	return 1;
}


PS:这几个函数是在别人的库中抽取出来的,我并没有一一试验,如果有什么问题请及时通知我!

http://blog.csdn.net/ryfdizuo/article/details/5009350

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值