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;
}
'블로그 (Blog) > 개발로그 (Devlogs)' 카테고리의 다른 글
디바이스 드라이버에서 atof 함수 구현 (0) | 2025.02.18 |
---|---|
MinGW 컴파일러에서 멀티코어 사용하기 (0) | 2025.02.18 |
AES Encryption/Decryption (0) | 2025.02.18 |
struct의 좋은 예(?) (0) | 2025.02.18 |
READ_REGISTER, WRITE_REGISTER… (0) | 2025.02.18 |