Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what's the detail about diffuse light #168

Open
linhaiwei123 opened this issue Apr 7, 2017 · 1 comment
Open

what's the detail about diffuse light #168

linhaiwei123 opened this issue Apr 7, 2017 · 1 comment

Comments

@linhaiwei123
Copy link

this is the code in vertex.glsl
`precision mediump float;

attribute vec3 position;
attribute vec3 normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 inverseModel;
uniform mat4 inverseView;
uniform mat4 inverseProjection;

uniform vec3 ambient;
uniform vec3 diffuse;
uniform vec3 lightDirection;

varying vec3 fragNormal;

void main() {
gl_Position = projectionviewmodel*vec4(position, 1);
fragNormal = normal;
}
this is the code in fragment.glslprecision mediump float;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 inverseModel;
uniform mat4 inverseView;
uniform mat4 inverseProjection;

uniform vec3 ambient;
uniform vec3 diffuse;
uniform vec3 lightDirection;

varying vec3 fragNormal;

void main() {
float brightness = dot(fragNormal,lightDirection);
gl_FragColor = vec4(ambient + diffuse * max(brightness, 0.0),1);
// gl_FragColor = vec4(1,1,1,1);
}`
and i can't pass the test
anyone can give me some tips?

@WWRS
Copy link

WWRS commented Dec 17, 2017

Okay, so after mucking about with the diffuse problem for a full day, I've figured it out:

The problem is actually that the normals they give you are data normals. You need to convert those to view normals. The tutorial doesn't cover this well, but you need to perform a transform of the normals by the transpose of the inverse of the model transform and the transpose of the inverse of the view transform (in that order), then normalize that vector (in 3D).

I'll leave you to figure out how to actually do that. Hint: If v is a vector and m is a matrix, transpose(m) * v == v * m.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants