본문으로 바로가기

OpenGL의 geometry shader에서 사용하기 위해서 만들어본 코드..

P1 -> P2의 Vector에 수직인 방향 Vector를 구하는 방법..

이런 arrow header를 그릴때 필요함..

vec3 CreatePerp(vec3 P1, vec3 P2)
{
	vec3 Q = normalize(P2 - P1);

	// create vectors U and V that are (1) mutually perpendicular and (2) perpendicular to Q
	vec3 U, V;
	
	// create a perpendicular vector on the XY plane
	if (Q[0] != 0) {  
		// there are an infinite number of potential vectors; arbitrarily select y = 1
		U[0] = -Q[1]/Q[0];
		U[1] = 1;
		U[2] = 0;

	// create a perpendicular vector on the YZ plane
	} else if (Q[1] != 0) {  
		U[0] = 0;
		U[1] = -Q[2]/Q[1];
		U[2] = 1;

	// assume Q[2] != 0; create a perpendicular vector on the XZ plane
	} else {  
		U[0] = 1;
		U[1] = 0;
		U[2] = -Q[0]/Q[2];
	}
	// The cross product of two vectors is perpendicular to both, so to find V:
	V[0] = Q[1]*U[2] - Q[2]*U[1];
	V[1] = Q[2]*U[0] - Q[0]*U[2];
	V[2] = Q[0]*U[1] - Q[1]*U[0];

	// normalize U and V:
	float UL = length(U);
	float VL = length(V);
	U /= UL;
	V /= VL;

	return V;
}