Why is my rotating cube doesn't looks like rotating cube?
Do I need to move camera?
I don't have any idea what is wrong.
I am using glium
in rust
.
24 Vertices
const P: f32 = 0.5;
let vertex_buffer = glium::VertexBuffer::new(&display, &vec![
// front 0-3
Vertex { position: (-P, -P, 0.0) },
Vertex { position: (P, P, 0.0) },
Vertex { position: (P, -P, 0.0) },
Vertex { position: (-P, P, 0.0) },
// back 4-7
Vertex { position: (-P, -P, 1.0) },
Vertex { position: (P, P, 1.0) },
Vertex { position: (P, -P, 1.0) },
Vertex { position: (-P, P, 1.0) },
// up 8-11
Vertex { position: (-P, P, 0.0) },
Vertex { position: (P, P, 0.0) },
Vertex { position: (P, P, 1.0) },
Vertex { position: (-P, P, 1.0) },
// down 12-15
Vertex { position: (-P, -P, 0.0) },
Vertex { position: (P, -P, 0.0) },
Vertex { position: (P, -P, 1.0) },
Vertex { position: (-P, -P, 1.0) },
// right 16-19
Vertex { position: (P, -P, 0.0) },
Vertex { position: (P, -P, 1.0) },
Vertex { position: (P, P, 1.0) },
Vertex { position: (P, P, 0.0) },
// left 20-23
Vertex { position: (-P, -P, 0.0) },
Vertex { position: (-P, -P, 1.0) },
Vertex { position: (-P, P, 1.0) },
Vertex { position: (-P, P, 0.0) },
]
).unwrap();
Indices for generating 6 faces of cube(4 Vertices per face, 2 triangles per face):
let indices = glium::IndexBuffer::new(&display, glium::index::PrimitiveType::TrianglesList,
&[
// front
0, 2, 1,
0, 3, 1,
// back
4, 6, 5,
4, 7, 5,
// up
8, 9, 10,
8, 11, 10,
// down
12, 13, 14,
12, 15, 14,
// right
16, 17, 18,
16, 19, 18,
// left
20, 21, 22,
20, 23, 22u16,
]
).unwrap();
I am using Gouraud shading(from glium
tutorial) for lighting.
I find normals
for cube on the stackoverflow.
Normals
:
let normals = glium::VertexBuffer::new(&display, &vec![
// front
Normal { normal: (1.0, 0.0, 0.0) },
// back
Normal { normal: (0.0, -1.0, 0.0) },
// up
Normal { normal: (1.0, 0.0, -1.0) },
// down
Normal { normal: (0.0, 0.0, 1.0) },
// right
Normal { normal: (1.0, 0.0, 0.0) },
// left
Normal { normal: (-1.0, 0.0, 0.0) },
]
).unwrap();
Vertex shader(glsl
):
#version 150
in vec3 position;
in vec3 normal;
out vec3 v_normal;
uniform mat4 m;
void main() {
v_normal = transpose(inverse(mat3(m))) * normal;
gl_Position = m * vec4(position, 1.0);
}
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);
}
Rotating is done by 4x4 matrix:
let mut t: f32 = 0.0;
let mut s: f32 = 0.002;
// ... loop.run
t += s;
if t > 180.0 || t < -180.0 {
s = -s;
}
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 light = [-1.0, -0.4, 0.9f32];
// params
target.draw((&vertex_buffer, &normals), &indices, &program, &uniform! { m: m, u_light: light }, ¶ms).unwrap();
target.finish().unwrap();
Any ideas whats wrong? I am sorry for so long Q. which has so much of code, but I don't know what's else than this I can provide.