I made rotating cube using glium
, but only one face is colored.
I am using Gouraud shading(from glium
tutorial) for lighting.
The cube is made by defined 24 vertices and 6 normals.
Rotation is made by two matrices:
let m = [
[1.0, 0.0, 0.0, 0.0],
[0.0, t.cos(), -t.sin(), 0.0],
[0.0, t.sin(), t.cos(), 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];
let n = [
[t.cos(), 0.0, t.sin(), 0.0],
[0.0, 1.0, 0.0, 0.0],
[-t.sin(), 0.0, t.cos(), 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];
Fragment shader(glsl):
#version 150
in vec3 v_normal;
out vec4 color;
uniform vec3 u_light;
void main() {
float brightness = dot(normalize(v_normal), normalize(u_light));
vec3 dark_color = vec3(0.6, 0.0, 0.0);
vec3 regular_color = vec3(1.0, 0.0, 0.0);
color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}
light:
let light = [-1.0, 0.4, 0.9f32];
The light should hit another face of cube in some point of rotation. I try changing direction of light, that don't change anything. Have anybody some idea?
for more info about the cube see this: my previous q. (some info like rotation is not actual here)
I think you have your normal direction in eye space and light direction in world space. So you have to convert light direction into camera (eye) space before passing into the shader, something like that (pseudo code):
normalize(VIEW_MATRIX * light)
Also, for the brightness I think you don't want to have negative value, so it have to be in a form
max(dot(N, L), 0.0)
I would recommend to read an introduction article about lighting, for example on learnOpenGL - https://learnopengl.com/Lighting/Basic-Lighting where you can see a description on what direction are for and how to prepare them. or some GLSL tutorials about having light direction in camera space - https://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights/