Skip to content

Instantly share code, notes, and snippets.

@solidiquis
Last active December 13, 2021 09:46
Show Gist options
  • Save solidiquis/d99108985d71a5c8ac79739846bce880 to your computer and use it in GitHub Desktop.
Save solidiquis/d99108985d71a5c8ac79739846bce880 to your computer and use it in GitHub Desktop.
Why are my vertices outside of clip space?
// Highly condensed version of my code with only the relevant bits.
// Vertices are ending up outside of clip space after transforming with MVP
// matrices despite using the same numbers and glm functions as
// https://learnopengl.com/Getting-started/Coordinate-Systems
// Libs used: glium/glutin and nalgebra_glm as glm;
let cube: [Vertex; 4] = [
// Front face:
Vertex { coord: [0.5, 0.5, 0.0], rgba: [1.0, 1.0, 1.0, 1.0] },
Vertex { coord: [0.5, -0.5, 0.0], rgba: [1.0, 1.0, 1.0, 1.0] },
Vertex { coord: [-0.5, -0.5, 0.0], rgba: [1.0, 1.0, 1.0, 1.0] },
Vertex { coord: [-0.5, 0.5, 0.0], rgba: [1.0, 1.0, 1.0, 1.0] },
];
let indices: [u8; 6] = [
1, 2, 3,
0, 1, 3
];
let vbo = VertexBuffer::new(&display, &cube)?;
let ibuf = IndexBuffer::new(&display, PrimitiveType::TrianglesList, &indices)?;
let m_matrix = glm::rotate(
&glm::TMat4::from_element(1.0),
0.0,
&glm::vec3(1.0, 0.0, 0.0)
);
let v_matrix = glm::translate(
&glm::TMat4::from_element(1.0),
&glm::vec3(0.0, 0.0, -3.0)
);
let p_matrix = glm::perspective(WIN_WIDTH / WIN_HEIGHT, PI / 4.0, 0.1, 100.0);
let model: [[f32; 4]; 4] = *m_matrix.as_ref();
let view: [[f32; 4]; 4] = *v_matrix.as_ref();
let projection: [[f32; 4]; 4] = *p_matrix.as_ref();
let m = m_matrix.clone();
let v = v_matrix.clone();
let p = p_matrix.clone();
dbg!(p * v * m * glm::vec4(0.5, 0.5, 0.0, 1.0));
// Debugging... why are my vertices outside of clip space?
// [
// 3.6213198,
// 4.8284273,
// -2.4044042,
// -2.0,
// ],
let uniforms = uniform! { m: model, v: view, p: projection };
let vert_shader_GLSL = r#"
#version 330 core
layout (location = 0) in vec3 coord;
uniform mat4 m;
uniform mat4 v;
uniform mat4 p;
in vec4 rgba;
out vec4 color;
void main() {
gl_Position = p * v * m * vec4(coord, 1.0);
color = rgba;
}
"#;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment