记录

#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;  
	//1.计算人物和相机指向的夹角;
	vec3 OP=aPos-camera_position;//向量OP:起点为相机,终点为人物顶点的向量
	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);//theta为两者之间的夹角;
	
	//2.让其旋转

	vec3 axis_y=vec3(0.0,1.0,0.0);
	vec3 n=-axis_y;
	mat4 rotateMatrix = getRotationMatrix(theta, n);//绕y轴旋转一定的角度
	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;//o点到AB点的距离
out float len_p_to_AB;//p点到AB点的距离
uniform bool invertedNormals;
out vec3 P;
//out float LABX;//AB直线的斜率
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;
	//FragPos2 = (projection * viewPos).xyz;

	if(isRotate)
    {
			
		//不是twobend的情况,主要是看这里
		
		
			vec3 OA = A - camPos;
			vec3 OB = B - camPos;
			//vec3 AP= aPos-A;
			//vec3 AO=camPos-A;
			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;//AB平面的法向量
			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;//直线上AB的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;
		    //Q是决定旋转的最大角度的点,其与D的距离不能让其完成相应的效果

			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;
	//        ptheta = atan(ptheta);
			float maxtheta = length(B - D)/length(QD);
	//        maxtheta = atan(maxtheta);

			if(dot(QD,DP)>0)
			{
				///*if(!isVertical)*/ ptheta = abs(ptheta);
				//区域1变形
				if(abs(ptheta)<maxtheta){													//region 2
					float theta = theta1 - (theta1 - theta2) * (0.5 + ptheta/(2 * maxtheta));
					
					theta *=abs(rotateRatio);//这个地方控制它慢慢变形,只要把它注释掉就得到自己的目的了
					mat4 rotateMatrix = getRotationMatrix(theta, viewUp);//portal后面的变形与角度和viewup有关系;
					vec3 QM = (length(QD)/lQE) * QP;
					vec3 DH = dot(QM, n2) * n2;
					vec3 H = D + DH;
						
					vec4 position = vec4(aPos-H , 1.0);//先将aPos移动H(法线n2方向),
					position = rotateMatrix * position;//然后将满足刚开始的条件并且旋转一定的角度
					vec3 newP = (position + vec4(H, 1.0)+vec4(distance_o_d,0.0,0.0,1.0)).xyz;//然后将旋转一定角度后的顶点再平移回来,同时根据distance_o_d进行向前和向后的移动,从而实现整个过程
					//这里的newP是在AB窗口后面变形后的顶点,vec4(-1.0,0.0,0.0,1.0)决定其窗口后面的点到原来视点的距离;
				    FragPos = (view * model * vec4(newP, 1.0)).xyz;//这个起不了任何作用,
					//FragPos2 = (projection * view * model * vec4(newP, 1.0)).xyz;
					//coordinate = caculateCoordinateP(newP);
					//主要调整的是newp和gl_position;
				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);
					
						gl_Position = projection * view * model * vec4(newP, 1.0);
					
					//	Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
					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){//region 3//区域3不变形
						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;
						//去掉AB中间平面距离障碍物之间的,使得AB后面的场景向o点移动;o点不动
						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;
					//	FragPos2 = (projection * view * model * vec4(newP, 1.0)).xyz;
						coordinate = caculateCoordinateP(newP);
						if(distance_o_d<-4.0&&aPos.x<LABX)
					{
						//newP = (vec4(aPos, 1.0)+vec4(distance_o_d,0.0,0.0,1.0)).xyz;
					//	newP = aPos;
					}
					
						//fragPosInLightSpace = lightVPMatrix * model * vec4(newP, 1.0); 
						gl_Position = projection * view * model * vec4(newP, 1.0);
						Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
					}
					if(ptheta < 0){//region 1区域1不变形
						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);//和区域3原因一样,主要是我不想看它的变化,直接得到结果
						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;
						//FragPos2 = (projection * view * model * vec4(newP, 1.0)).xyz;
						coordinate = caculateCoordinateP(newP);

						//fragPosInLightSpace = lightVPMatrix * model * vec4(newP, 1.0); 
					    gl_Position = projection * view * model * vec4(newP, 1.0);
						Normal = transpose(inverse(mat3(view * rotateMatrix))) * aNormal;
					}
				}   
			}
			//将AB后面的那部分移动到后面,
    	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;//这里的这个数字不能超过distance_o_d,要不然中间有一端会有被拉伸的效果;
						FragPos = (view * model * vec4(newP, 1.0)).xyz;
						//FragPos2 = (projection * view * model * vec4(newP, 1.0)).xyz;
						coordinate = caculateCoordinateP(newP);

						//fragPosInLightSpace = lightVPMatrix * model * vec4(newP, 1.0); 
					    gl_Position = projection * view * model * vec4(newP, 1.0);
	}
	}
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值