常用OpenGL Shader 脚本


1, Rotation

mat4 RotateMatrix( const in float angle, const in vec3 axis )
{
	vec3 n = normalize( axis );
	float theta = radians( angle );
	float c = cos( theta );
	float s = sin( theta );
	mat3 R;
	R[0] = n.xyz*n.x*(1.0-c) + vec3( c, n.z*s, -n.y*s );
	R[1] = n.xyz*n.y*(1.0-c) + vec3( -n.z*s, c, n.x*s );
	R[2] = n.xyz*n.z*(1.0-c) + vec3( n.y*s, -n.x*s, c );
	return mat4( R[0], 0.0,
	R[1], 0.0,
	R[2], 0.0,
	0.0, 0.0, 0.0, 1.0 );
}


Ortho Matrix

mat4 OrthoMatrix( const in float left, const in float right,
		const in float bottom, const in float top,
		const in float zNear, const in float zFar )
{
	vec3 delta = vec3( right, top, zFar ) - vec3( left, bottom, zNear );
	vec3 sum = vec3( right, top, zFar ) + vec3( left, bottom, zNear );
	vec3 ratio = sum / delta;
	vec3 twoRatio = 2.0 / delta;
	return mat4( twoRatio.x, 0.0, 0.0, 0.0,
			0.0, twoRatio.y, 0.0, 0.0,
			0.0, 0.0, -twoRatio.z, 0.0,
			-ratio.x, -ratio.y, -ratio.z, 1.0 );
}


你可能感兴趣的:(常用OpenGL Shader 脚本)