我有一个光源照亮一个定义为
的平面glBegin(GL_QUADS);
glVertexAttrib3f(attribLoc, 0.0, 0.0, 1.0); // need to specify only once since the
// normal at all 4 vertexs' are the same
glVertex3f(-200.0, -200.0, 0.0);
glVertex3f(200.0, -200.0, 0.0);
glVertex3f(200.0, 200.0, 0.0);
glVertex3f(-200.0, 200.0, 0.0);
glEnd();
片段着色器仅计算漫反射组件。当光源
时,一切都很好
在飞机前面。当我将光源移到飞机后面时,
我仍然看到了一个亮点。我期待" 0"在这种情况下,每个像素的漫反射贡献。
diplay,reshape和shaders ......
void display(void){
int attribLoc;
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0.0, 1.0, 10.0);
shader.bind();
attribLoc = glGetAttribLocation(shader.id(), "app_to_vertex_normal");
glUniform3f(glGetUniformLocation(shader.id(), "LightPos"), 0.0, 0.0, -2.0);
// Setting the Material properties
glUniform3f(glGetUniformLocation(shader.id(), "mdiffuse"), 0.6, 0.6, 0.6);
// Setting the Light properties
glUniform3f(glGetUniformLocation(shader.id(), "ldiffuse"), 0.6, 0.6, 0.6);
glBegin(GL_QUADS);
glVertexAttrib3f(attribLoc, 0.0, 0.0, 1.0);
glVertex3f(-200.0, -200.0, 0.0);
glVertex3f(200.0, -200.0, 0.0);
glVertex3f(200.0, 200.0, 0.0);
glVertex3f(-200.0, 200.0, 0.0);
glEnd();
shader.unbind();
glutSwapBuffers();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-300.0, 300.0, -300.0, 300.0, 5.0, 20.0); //(Perspective)
glMatrixMode(GL_MODELVIEW);
}
The Vertex shader
#version 140
in vec3 app_to_vertex_normal;
out vec3 vertex_to_fragment_normal;
out vec3 vertex_in_eye_coord;
void main() {
// Set the position of the current vertex
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vertex_in_eye_coord = vec3(gl_ModelViewMatrix * gl_Vertex);
vertex_to_fragment_normal = gl_NormalMatrix*app_to_vertex_normal;
}
The fragment shader
#version 140
in vec3 vertex_to_fragment_normal;
in vec3 vertex_in_eye_coord;
uniform vec3 LightPos;
// Material properties
uniform vec3 mdiffuse;
// Light properties
uniform vec3 ldiffuse;
void main() {
vec3 vertex_to_light;
vec3 norm;
float diffuse_mult;
vec3 diffuse_comp;
// Diffuse component
norm = normalize(vertex_to_fragment_normal);
vertex_to_light = normalize(LightPos-vertex_in_eye_coord);
diffuse_mult = max(dot(vertex_to_light, norm), 0.0);
diffuse_comp = diffuse_mult*ldiffuse*mdiffuse;
gl_FragColor = vec4(diffuse_comp, 1.0);
}