#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 camera_forward;
uniform vec3 camera_position;
mat4 getRotationMatrix(float angle, vec3 axis)
{
mat4 rmatrix;
vec3 a = normalize(axis);
float s = sin(angle);
float c = cos(angle);
rmatrix[0][0] = a.x*a.x + (1.0 - a.x*a.x)*c;
rmatrix[0][1] = a.x * a.y * (1.0 - c) - a.z * s;
rmatrix[0][2] = a.x * a.z * (1.0 - c) + a.y * s;
rmatrix[0][3] = 0.0;
rmatrix[1][0] = a.x * a.y * (1.0 -c) + a.z *s;
rmatrix[1][1] = a.y * a.y + (1.0 - a.y * a.y)*c;
rmatrix[1][2] = a.y * a.z * (1.0 -c) -a.x * s;
rmatrix[1][3] = 0.0;
rmatrix[2][0] = a.x * a.z *(1.0 -c) - a.y *s;
rmatrix[2][1] = a.y * a.z *(1.0 -c) + a.x * s;
rmatrix[2][2] = a.z * a.z + (1.0 - a.z * a.z) *c;
rmatrix[2][3] = 0.0;
rmatrix[3][0] = 0.0;
rmatrix[3][1] = 0.0;
rmatrix[3][2] = 0.0;
rmatrix[3][3] = 1.0;
return rmatrix;
}
void main()
{
TexCoords = aTexCoords;
vec3 OP=aPos-camera_position;
float dot_OP_forward=dot(OP,camera_forward);
float len_op=length(OP);
float len_farward=length(camera_forward);
float mul_length=len_op*len_farward;
float _cos=dot_OP_forward/mul_length;
float theta=acos(_cos);
vec3 axis_y=vec3(0.0,1.0,0.0);
vec3 n=-axis_y;
mat4 rotateMatrix = getRotationMatrix(theta, n);
vec4 pos = vec4(aPos, 1.0f);
gl_Position = projection * view * model *pos;
}
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
}
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;
layout (location = 2) in vec3 aNormal;
out vec3 FragPos;
out vec2 TexCoords;
out vec3 Normal;
out vec3 blend;
out vec2 coordinate;
out vec3 FragPos2;
out float len_o_to_AB;
out float len_p_to_AB;
uniform bool invertedNormals;
out vec3 P;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform bool isRotate;
uniform vec3 A;
uniform vec3 B;
uniform vec3 pO;
uniform vec3 pOA;
uniform vec3 pOC;
uniform vec3 v1;
uniform vec3 v2;
uniform vec3 forward;
uniform vec3 up;
uniform vec3 side;
uniform vec3 camPos;
uniform float rotateRatio;
uniform int WINDOW_HEIGHT;
uniform int WINDOW_WIDTH;
uniform bool isVertical;
uniform bool isTwoBend;
uniform float distance_o_d;
uniform float distance_AB_front;
mat4 getRotationMatrix(float angle, vec3 axis)
{
mat4 rmatrix;
vec3 a = normalize(axis);
float s = sin(angle);
float c = cos(angle);
rmatrix[0][0] = a.x*a.x + (1.0 - a.x*a.x)*c;
rmatrix[0][1] = a.x * a.y * (1.0 - c) - a.z * s;
rmatrix[0][2] = a.x * a.z * (1.0 - c) + a.y * s;
rmatrix[0][3] = 0.0;
rmatrix[1][0] = a.x * a.y * (1.0 -c) + a.z *s;
rmatrix[1][1] = a.y * a.y + (1.0 - a.y * a.y)*c;
rmatrix[1][2] = a.y * a.z * (1.0 -c) -a.x * s;
rmatrix[1][3] = 0.0;
rmatrix[2][0] = a.x * a.z *(1.0 -c) - a.y *s;
rmatrix[2][1] = a.y * a.z *(1.0 -c) + a.x * s;
rmatrix[2][2] = a.z * a.z + (1.0 - a.z * a.z) *c;
rmatrix[2][3] = 0.0;
rmatrix[3][0] = 0.0;
rmatrix[3][1] = 0.0;
rmatrix[3][2] = 0.0;
rmatrix[3][3] = 1.0;
return rmatrix;
}
vec2 caculateCoordinateP(vec3 p)
{
vec2 result;
mat4 transmatrix = projection * view * model;
vec4 pCamera = transmatrix * vec4(p, 1.0f);
vec3 pScreen;
pScreen.x = pCamera.x / pCamera.w;
pScreen.y = pCamera.y / pCamera.w;
result.x = (pScreen.x + 1) / 2 * WINDOW_WIDTH + 0.5;
result.y = (1 + pScreen.y) / 2 * WINDOW_HEIGHT + 0.5;
return result;
}
void main()
{
vec4 viewPos = view * model * vec4(aPos, 1.0);
FragPos = viewPos.xyz;
TexCoords = aTexCoords;
coordinate = caculateCoordinateP(aPos);
mat3 normalMatrix = transpose(inverse(mat3(view * model)));
Normal = normalMatrix * (invertedNormals ? -aNormal : aNormal);
gl_Position = projection * viewPos;
if(isRotate)
{
vec3 OA = A - camPos;
vec3 OB = B - camPos;
vec3 AP= -(aPos-A);
vec3 AO=-(camPos-A);
vec3 AB = B - A;
vec3 D = (A + B)/2.0f;
vec3 nDQ = normalize(cross(cross(OA,OB),AB));
vec3 nAB = nDQ;
float PAn=dot(AP,nAB);
float OAn=dot(AO,nAB);
len_p_to_AB=PAn/length(nAB);
len_o_to_AB=OAn/length(nAB);
P=aPos;
float k=(B.x-A.x)/(B.z-A.z);
float LABX=k*(P.z-A.z)+A.x;
float near;
if (isVertical)
near = length(AB)*0.5f * 1.1f;
else
near = (length(OA) + length(OB))/2.0f;
vec3 Q = D + near * nDQ;
vec3 QA = A - Q;
vec3 QB = B - Q;
float theta1 = dot(normalize(QA),normalize(OA));
float ctheta2 = dot(normalize(QB),normalize(OB));
theta1 = acos(theta1);
float theta2 = acos(ctheta2);
vec3 n1 = -nDQ;
vec3 n2 = normalize(AB);
vec3 n3 = normalize(cross(QA,QB));
vec3 viewUp = -n3;
vec3 DP = aPos - D;
vec3 QP = aPos - Q;
vec3 QD = D - Q;
float lQE = dot(QP,n1);
float lEG = dot(QP,n2);
float ptheta = lEG / lQE;
float maxtheta = length(B - D)/length(QD);
if(dot(QD,DP)>0)
{
if(abs(ptheta)<maxtheta){
float theta = theta1 - (theta1 - theta2) * (0.5 + ptheta/(2 * maxtheta));
theta *=abs(rotateRatio);
mat4 rotateMatrix = getRotationMatrix(theta, viewUp);
vec3 QM = (length(QD)/lQE) * QP;
vec3 DH = dot(QM, n2) * n2;
vec3 H = D + DH;
vec4 position = vec4(aPos-H , 1.0);
position = rotateMatrix * position;
vec3 newP = (position + vec4(H, 1.0)+vec4(distance_o_d,0.0,0.0,1.0)).xyz;
FragPos = (view * model * vec4(newP, 1.0)).xyz;
if(distance_o_d<-4.0&&P.x<LABX)
{
newP=vec3(1.0,1.0,1.0);
}
gl_Position = projection * view * model * vec4(newP, 1.0);
Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
vec3 AP = aPos - A;
vec3 G2P = dot(AP,n3) * n3;
vec3 AG = AP - G2P;
float theta3 = dot(normalize(AG),normalize(QA));
float theta4 = dot(normalize(AG),normalize(OA));
float _theta3 = acos(theta3);
float _theta4 = acos(theta4);
if(_theta3 < theta1 && theta3>0 && _theta4 < (abs(rotateRatio)*theta1) && theta4>0)
blend = vec3(0.0,0.0,0.0);
}
else{
if(ptheta > 0){
vec3 BP = aPos - B;
vec3 G2P = dot(BP,n3) * n3;
vec3 BG = BP - G2P;
float theta3 = dot(normalize(BG),normalize(AB));
theta3 = acos(theta3);
float thetaABQ = dot(normalize(AB),normalize(QB));
thetaABQ = acos(thetaABQ);
float theta = theta3/thetaABQ * theta2;
theta *=abs(rotateRatio);
mat4 rotateMatrix = getRotationMatrix(theta, viewUp);
vec3 QM = (length(QD)/lQE) * QP;
vec3 DH = dot(QM, n2) * n2;
vec3 H = D + DH;
vec4 position = vec4(aPos - H, 1.0);
position = rotateMatrix * position;
vec3 newP = (position + vec4(H, 1.0)+vec4(distance_o_d,0.0,0.0,1.0)).xyz;
FragPos = (view * model * vec4(newP, 1.0)).xyz;
coordinate = caculateCoordinateP(newP);
if(distance_o_d<-4.0&&aPos.x<LABX)
{
}
gl_Position = projection * view * model * vec4(newP, 1.0);
Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
}
if(ptheta < 0){
vec3 AP = aPos - A;
vec3 G1P = dot(AP,n3)*n3;
vec3 AG = AP - G1P;
float theta3 = dot(normalize(AG),normalize(-AB));
theta3 = acos(theta3);
float thetaBAQ = dot(normalize(-AB),normalize(QA));
thetaBAQ = acos(thetaBAQ);
float theta = theta3/thetaBAQ *theta1;
theta*=abs(rotateRatio);
mat4 rotateMatrix = getRotationMatrix(theta, viewUp);
vec3 QM = (length(QD)/lQE) * QP;
vec3 DH = dot(QM, n2) * n2;
vec3 H = D + DH;
vec4 position = vec4(aPos - H, 1.0);
position = rotateMatrix * position;
vec3 newP = (position + vec4(H, 1.0)+vec4(distance_o_d,0.0,0.0,1.0)).xyz;
FragPos = (view * model * vec4(newP, 1.0)).xyz;
coordinate = caculateCoordinateP(newP);
gl_Position = projection * view * model * vec4(newP, 1.0);
Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
}
}
}
if(distance_o_d<-4.0&&P.x<LABX)
{
vec4 position = vec4(aPos, 1.0);
vec3 newP = (position +vec4(distance_AB_front,0.0,0.0,1.0)).xyz;
FragPos = (view * model * vec4(newP, 1.0)).xyz;
coordinate = caculateCoordinateP(newP);
gl_Position = projection * view * model * vec4(newP, 1.0);
}
}
}```