brush_primit.cpp

来自「quake3工具源码。包括生成bsp文件」· C++ 代码 · 共 441 行 · 第 1/2 页

CPP
441
字号
				GetToken(false);
				f->brushprimit_texdef.coords[1][j]=atof(token);
			}
			GetToken (false);
			if (strcmp(token, ")"))
			{
				Warning ("parsing brush primitive");
				return;
			}
			GetToken (false);
			if (strcmp(token, ")"))
			{
				Warning ("parsing brush primitive");
				return;
			}
			// read the texturedef
			GetToken (false);
			//strcpy(f->texdef.name, token);
			f->texdef.SetName(token);
			if (TokenAvailable ())
			{
				GetToken (false);
				f->texdef.contents = atoi(token);
        GetToken (false);
				f->texdef.flags = atoi(token);
				GetToken (false);
				f->texdef.value = atoi(token);
			}
		}
	} while (1);
}

// compute a fake shift scale rot representation from the texture matrix
// these shift scale rot values are to be understood in the local axis base
void TexMatToFakeTexCoords( vec_t texMat[2][3], float shift[2], float *rot, float scale[2] )
{
#ifdef _DEBUG
	// check this matrix is orthogonal
	if (fabs(texMat[0][0]*texMat[0][1]+texMat[1][0]*texMat[1][1])>ZERO_EPSILON)
		Sys_Printf("Warning : non orthogonal texture matrix in TexMatToFakeTexCoords\n");
#endif
	scale[0]=sqrt(texMat[0][0]*texMat[0][0]+texMat[1][0]*texMat[1][0]);
	scale[1]=sqrt(texMat[0][1]*texMat[0][1]+texMat[1][1]*texMat[1][1]);
#ifdef _DEBUG
	if (scale[0]<ZERO_EPSILON || scale[1]<ZERO_EPSILON)
		Sys_Printf("Warning : unexpected scale==0 in TexMatToFakeTexCoords\n");
#endif
	// compute rotate value
	if (fabs(texMat[0][0])<ZERO_EPSILON)
	{
#ifdef _DEBUG
		// check brushprimit_texdef[1][0] is not zero
		if (fabs(texMat[1][0])<ZERO_EPSILON)
			Sys_Printf("Warning : unexpected texdef[1][0]==0 in TexMatToFakeTexCoords\n");
#endif
		// rotate is +-90
		if (texMat[1][0]>0)
			*rot=90.0f;
		else
			*rot=-90.0f;
	}
	else
	*rot = RAD2DEG( atan2( texMat[1][0], texMat[0][0] ) );
	shift[0] = -texMat[0][2];
	shift[1] = texMat[1][2];
}

// compute back the texture matrix from fake shift scale rot
// the matrix returned must be understood as a qtexture_t with width=2 height=2 ( the default one )
void FakeTexCoordsToTexMat( float shift[2], float rot, float scale[2], vec_t texMat[2][3] )
{
	texMat[0][0] = scale[0] * cos( DEG2RAD( rot ) );
	texMat[1][0] = scale[0] * sin( DEG2RAD( rot ) );
	texMat[0][1] = -1.0f * scale[1] * sin( DEG2RAD( rot ) );
	texMat[1][1] = scale[1] * cos( DEG2RAD( rot ) );
	texMat[0][2] = -shift[0];
	texMat[1][2] = shift[1];
}

// convert a texture matrix between two qtexture_t
// if NULL for qtexture_t, basic 2x2 texture is assumed ( straight mapping between s/t coordinates and geometric coordinates )
void ConvertTexMatWithQTexture( brushprimit_texdef_t *texMat1, qtexture_t *qtex1, brushprimit_texdef_t *texMat2, qtexture_t *qtex2 )
{
	float s1,s2;
	s1 = ( qtex1 ? static_cast<float>( qtex1->width ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->width ) : 2.0f );
	s2 = ( qtex1 ? static_cast<float>( qtex1->height ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->height ) : 2.0f );
	texMat2->coords[0][0]=s1*texMat1->coords[0][0];
	texMat2->coords[0][1]=s1*texMat1->coords[0][1];
	texMat2->coords[0][2]=s1*texMat1->coords[0][2];
	texMat2->coords[1][0]=s2*texMat1->coords[1][0];
	texMat2->coords[1][1]=s2*texMat1->coords[1][1];
	texMat2->coords[1][2]=s2*texMat1->coords[1][2];
}

// texture locking
void Face_MoveTexture_BrushPrimit(face_t *f, vec3_t delta)
{
	vec3_t texS,texT;
	vec_t tx,ty;
	vec3_t M[3]; // columns of the matrix .. easier that way
	vec_t det;
	vec3_t D[2];
	// compute plane axis base ( doesn't change with translation )
	ComputeAxisBase( f->plane.normal, texS, texT );
	// compute translation vector in plane axis base
	tx = DotProduct( delta, texS );
	ty = DotProduct( delta, texT );
	// fill the data vectors
	M[0][0]=tx; M[0][1]=1.0f+tx; M[0][2]=tx;
	M[1][0]=ty; M[1][1]=ty; M[1][2]=1.0f+ty;
	M[2][0]=1.0f; M[2][1]=1.0f; M[2][2]=1.0f;
	D[0][0]=f->brushprimit_texdef.coords[0][2];
	D[0][1]=f->brushprimit_texdef.coords[0][0]+f->brushprimit_texdef.coords[0][2];
	D[0][2]=f->brushprimit_texdef.coords[0][1]+f->brushprimit_texdef.coords[0][2];
	D[1][0]=f->brushprimit_texdef.coords[1][2];
	D[1][1]=f->brushprimit_texdef.coords[1][0]+f->brushprimit_texdef.coords[1][2];
	D[1][2]=f->brushprimit_texdef.coords[1][1]+f->brushprimit_texdef.coords[1][2];
	// solve
	det = SarrusDet( M[0], M[1], M[2] );
	f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
	f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
	f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
	f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
	f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
	f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
}

// call Face_MoveTexture_BrushPrimit after vec3_t computation
void Select_ShiftTexture_BrushPrimit( face_t *f, int x, int y )
{
	vec3_t texS,texT;
	vec3_t delta;
	ComputeAxisBase( f->plane.normal, texS, texT );
	VectorScale( texS, static_cast<float>(x), texS );
	VectorScale( texT, static_cast<float>(y), texT );
	VectorCopy( texS, delta );
	VectorAdd( delta, texT, delta );
	Face_MoveTexture_BrushPrimit( f, delta );
}

// texture locking
// called before the points on the face are actually rotated
void RotateFaceTexture_BrushPrimit(face_t *f, int nAxis, float fDeg, vec3_t vOrigin )
{
	vec3_t texS,texT;			// axis base of the initial plane
	vec3_t vRotate;				// rotation vector
	vec3_t Orig;
	vec3_t rOrig,rvecS,rvecT;	// (0,0) (1,0) (0,1) ( initial plane axis base ) after rotation ( world axis base )
	vec3_t rNormal;				// normal of the plane after rotation
	vec3_t rtexS,rtexT;			// axis base of the rotated plane
	vec3_t lOrig,lvecS,lvecT;	// [2] are not used ( but usefull for debugging )
	vec3_t M[3];
	vec_t det;
	vec3_t D[2];
	// compute plane axis base
	ComputeAxisBase( f->plane.normal, texS, texT );
	// compute coordinates of (0,0) (1,0) (0,1) ( initial plane axis base ) after rotation
	// (0,0) (1,0) (0,1) ( initial plane axis base ) <-> (0,0,0) texS texT ( world axis base )
	// rotation vector
	VectorSet( vRotate, 0.0f, 0.0f, 0.0f );
	vRotate[nAxis]=fDeg;
	VectorSet( Orig, 0.0f, 0.0f, 0.0f );
	VectorRotate( Orig, vRotate, vOrigin, rOrig );
	VectorRotate( texS, vRotate, vOrigin, rvecS );
	VectorRotate( texT, vRotate, vOrigin, rvecT );
	// compute normal of plane after rotation
	VectorRotate( f->plane.normal, vRotate, rNormal );
	// compute rotated plane axis base
	ComputeAxisBase( rNormal, rtexS, rtexT );
	// compute S/T coordinates of the three points in rotated axis base ( in M matrix )
	lOrig[0] = DotProduct( rOrig, rtexS );
	lOrig[1] = DotProduct( rOrig, rtexT );
	lvecS[0] = DotProduct( rvecS, rtexS );
	lvecS[1] = DotProduct( rvecS, rtexT );
	lvecT[0] = DotProduct( rvecT, rtexS );
	lvecT[1] = DotProduct( rvecT, rtexT );
	M[0][0] = lOrig[0]; M[1][0] = lOrig[1]; M[2][0] = 1.0f;
	M[0][1] = lvecS[0]; M[1][1] = lvecS[1]; M[2][1] = 1.0f;
	M[0][2] = lvecT[0]; M[1][2] = lvecT[1]; M[2][2] = 1.0f;
	// fill data vector
	D[0][0]=f->brushprimit_texdef.coords[0][2];
	D[0][1]=f->brushprimit_texdef.coords[0][0]+f->brushprimit_texdef.coords[0][2];
	D[0][2]=f->brushprimit_texdef.coords[0][1]+f->brushprimit_texdef.coords[0][2];
	D[1][0]=f->brushprimit_texdef.coords[1][2];
	D[1][1]=f->brushprimit_texdef.coords[1][0]+f->brushprimit_texdef.coords[1][2];
	D[1][2]=f->brushprimit_texdef.coords[1][1]+f->brushprimit_texdef.coords[1][2];
	// solve
	det = SarrusDet( M[0], M[1], M[2] );
	f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
	f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
	f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
	f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
	f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
	f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
}

// best fitted 2D vector is x.X+y.Y
void ComputeBest2DVector( vec3_t v, vec3_t X, vec3_t Y, int &x, int &y )
{
	double sx,sy;
	sx = DotProduct( v, X );
	sy = DotProduct( v, Y );
	if ( fabs(sy) > fabs(sx) )
	{
		x = 0;
		if ( sy > 0.0 )
			y =  1;
		else
			y = -1;
	}
	else
	{
		y = 0;
		if ( sx > 0.0 )
			x =  1;
		else
			x = -1;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?