1

我为旋转的彩虹立方体制作了基本照明:

着色器:

attribute  vec4 vPosition;
attribute  vec4 vColor;
attribute  vec4 vNormal;

varying vec4 color;

uniform mat4 ModelView;
uniform mat4 Projection;
uniform mat3 NormalMatrix;
uniform vec4 lPosition;
uniform vec4 lDiffuse;
uniform vec4 Shininess;
uniform vec4 lSpecular;
uniform vec4 lAmbient;

varying vec3 v_normal;
varying vec3 v_eye;


void main() 
{

    vec4 ambient, diffuse, specular;
    vec3 N,L,E,H;

    //gl_Position = projection * view * model * vPosition;
    ambient = vColor*lAmbient;

    N = normalize((ModelView*vNormal).xyz);
    L = normalize((ModelView*lPosition).xyz - (ModelView*vPosition).xyz);
    diffuse = max(dot(L,N), 0.0)*(vColor*lDiffuse);

    E = -normalize((ModelView * vPosition).xyz);
    H = normalize(L + E);
    specular = max(pow(max(dot(N, H), 0.0), 20.0)
                *lSpecular, 0.0);

    color = vec4((ambient+diffuse+specular).rgb, 1.0);

    gl_Position = Projection * ModelView * vPosition;
} 

当立方体旋转时,我只能看到 2 面被着色,其余的都是黑色的。灯光始终处于同一位置(立方体的前面),但即使从一开始就被遮蔽的墙壁没有面向光源,也可以看到其余部分。建议?

// 编辑法线计算如下:

无效法线(int a,int b,int c,int d){

vec4 U, V;
U = vertices[b] - vertices[a];
V = vertices[c] - vertices[a];

vec4 firstNormal;
firstNormal.x = (U.y * V.z)-(U.z * V.y);
firstNormal.y = (U.z * V.x)-(U.x * V.z);
firstNormal.z = (U.x * V.y)-(U.y * V.x);
firstNormal.w = 1.0f;

normal[normalCount] = firstNormal;
normalCount++;

vec4 X, Y;

X = vertices[c] - vertices[a];
Y = vertices[d] - vertices[a];

vec4 secondNormal;
secondNormal.x = (X.y * Y.z)-(X.z * Y.y);
secondNormal.y = (X.z * Y.x)-(X.x * Y.z);
secondNormal.z = (X.x * Y.y)-(X.y * Y.x);
secondNormal.w = 1.0f;

normal[normalCount] = secondNormal;
normalCount++;

}

它们发送到 vbo abcd 的位置是立方体边角上的顶点

4

1 回答 1

0

1) what are your normals? you can have a bug there, it would show like that. you can check that by assigning and testing, in turns:

color = vNormal; 
color = N; 
color = L;

and so on.

2) you're using per-vertex, not per-fragment shading, which, on a cube, might not be the best idea - it's too imprecise.

于 2013-04-03T21:23:35.727 回答