我试图了解 openGL 中的编码是如何工作的。我在互联网上找到了这段代码,我想清楚地理解它。
对于我的顶点着色器,我有:
顶点
uniform vec3 fvLightPosition;
varying vec2 Texcoord;
varying vec2 Texcoordcut;
varying vec3 ViewDirection;
varying vec3 LightDirection;
uniform mat4 extra;
attribute vec3 rm_Binormal;
attribute vec3 rm_Tangent;
uniform float fSinTime0_X;
uniform float fCosTime0_X;
void main( void )
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex * extra;
Texcoord = gl_MultiTexCoord0.xy;
Texcoordcut = gl_MultiTexCoord0.xy;
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
vec3 rotationLight = vec3(fCosTime0_X,0, fSinTime0_X);
ViewDirection = - fvObjectPosition.xyz;
LightDirection = (-rotationLight ) * (gl_NormalMatrix);
}
对于我的片段着色器,我在图片上创建了一个白色以在其中创建一个洞。:
uniform vec4 fvAmbient;
uniform vec4 fvSpecular;
uniform vec4 fvDiffuse;
uniform float fSpecularPower;
uniform sampler2D baseMap;
uniform sampler2D bumpMap;
varying vec2 Texcoord;
varying vec2 Texcoordcut;
varying vec3 ViewDirection;
varying vec3 LightDirection;
void main( void )
{
vec3 fvLightDirection = normalize( LightDirection );
vec3 fvNormal = normalize( ( texture2D( bumpMap, Texcoord ).xyz * 2.0 ) - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
vec3 fvViewDirection = normalize( ViewDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4 fvBaseColor = texture2D( baseMap, Texcoord );
vec4 fvTotalAmbient = fvAmbient * fvBaseColor;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
if(fvBaseColor == vec4(1,1,1,1)){
discard;
}else{
gl_FragColor = ( fvTotalDiffuse + fvTotalSpecular );
}
}
谁能向我解释一下一切都是做什么的?我理解它的基本思想。但不是经常为什么需要它,当你使用其他变量时会发生什么?发生的事情是茶壶周围的光在时间里来来去去。这如何与 cosinus 和 sinus 变量正确关联?如果我想让光线从上方射到茶壶底部怎么办?
还,
这些线是什么意思?
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
为什么在变量之前有一个减号?
ViewDirection = - fvObjectPosition.xyz;
为什么我们使用负旋转灯?
LightDirection = (-rotationLight) * (gl_NormalMatrix);
为什么他们使用 *2.0 ) - 1.0 来计算法线向量?Normal = normalize(gl_NormalMatrix * gl_Normal);是不可能的吗??
vec3 fvNormal = normalize((texture2D(bumpMap, Texcoord).xyz * 2.0) - 1.0);