opengl创建天空盒实现反射和折射的效果

本文通过OpenGL技术创建天空盒,详细介绍了如何利用顶点和片元shader实现反射与折射效果,展示了最终的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图



天空盒顶点shader

attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;

uniform mat4 M;
uniform mat4 P;
uniform mat4 V;

varying vec3 V_Texcoord;
void main()
{
	V_Texcoord=pos;
	gl_Position=P*V*M*vec4(pos,1.0);
}

天空盒片元shader

varying vec3 V_Texcoord;
uniform samplerCube U_MainTexture;
void main()
{
	gl_FragColor=textureCube(U_MainTexture,V_Texcoord);
}

反射顶点shader


attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;

uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;

varying vec4 V_WorldPos;
varying vec3 V_Normal;

void main()
{
	V_WorldPos=M*vec4(pos,1.0);
	V_Normal=mat3(NM)*normal;//将法线坐标转换到世界空间
	gl_Position=P*V*M*vec4(pos,1.0);
}


反射片元shader

varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;

void main()
{
	//视线方向向量
	vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
	vec3 n=normalize(V_Normal);
	//求视线方向的反射向量
	vec3 r=reflect(eyeVec,n);
	
	vec4 color=textureCube(U_MainTexture,r);

	gl_FragColor=color;
}

折射片元shader

varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;

void main()
{
	vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
	vec3 n=normalize(V_Normal);
	vec3 r=refract(eyeVec,n,1.0/1.52);
	vec4 color=textureCube(U_MainTexture,r);

	gl_FragColor=colo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值