我的phong片段着色器沒有遮擋任何東西,只是將所有東西都變成黑色。 這是我的片段着色器這個GLSL程序是否正確?我的立方體是黑色的
precision mediump float;
varying vec3 vposition;
varying vec3 vnormal;
varying vec4 vcolor;
varying vec3 veyePos;
void main() {
vec3 lightPos = vec3(0,0,0);
vec4 s = normalize(vec4(lightPos,1) - vec4(veyePos,1));
vec4 r = reflect(-s,vec4(vnormal, 1));
vec4 v = normalize(-vec4(veyePos, 1));
float spec = max(dot(v,r),0.0);
float diff = max(dot(vec4(vnormal,1),s),0.0);
vec3 diffColor = diff * vec3(1,0,0);
vec3 specColor = pow(spec,3.0) * vec3(1,1,1);
vec3 ambientColor = vec3(0.1,0.1,0.1);
gl_FragColor = vec4(diffColor + 0.5 * specColor + ambientColor, 1);
}
這是我的頂點着色器
uniform mat4 uMVPMatrix;
uniform mat4 uMVMatrix;
uniform vec3 eyePos;
attribute vec4 aPosition;
attribute vec4 aColor;
attribute vec4 aNormal;
varying vec4 vcolor;
varying vec3 vposition;
varying vec3 vnormal;
varying vec3 veyePos;
void main() {
mat4 normalMat = transpose(inverse(uMVMatrix));
vec4 vertPos4 = uMVMatrix * vec4(vec3(aPosition), 1.0);
vposition = vec3(vertPos4)/vertPos4.w;
vcolor = aColor;
veyePos = eyePos;
vnormal = vec3(uMVMatrix * vec4(vec3(aNormal),0.0));
gl_Position = uMVPMatrix * aPosition;
}
MVMatrix是模型視圖矩陣 MVPMatrix是模型 - 視圖 - 投影矩陣
不知道爲什麼它是黑色的,但所有的點產品應該是3D,而不是同質4D – jozxyqk
什麼了着色器編譯和鏈接日誌說?在ES2中將整數('1')轉換爲浮點類型('vec4')是可疑的。 – genpfault
它成功編譯了兩個着色器,將那些vec4(_,1)更改爲vec4(_,0)有什麼區別? – samsun96