0

I have a basic square. How can i rotate it ?

let vertices = vec![
    x, y, uv1.0, uv1.0, layer, // top left
    // ---
    x + w, y, uv2.0, uv2.1, layer, // top right
    // ---
    x + w, y + h, uv3.0, uv3.1, layer, // bottom right
    // ---
    x, y + h, uv4.0, uv4.1, layer // bottom left


This is my orthographic projection matrix.

let c_str_vert = CString::new("MVP".as_bytes()).unwrap();
let modelLoc = gl::GetUniformLocation(shaderProgram, c_str_vert.as_ptr());
let model = cgmath::ortho(0.0, SCR_WIDTH as f32, SCR_HEIGHT as f32, 0.0, -1.0, 1.0);
gl::UniformMatrix4fv(modelLoc, 1, gl::FALSE, model.as_ptr());
#version 430 core
layout(location = 0) in vec2 position;
// ...
uniform mat4 MVP;
void main() {
 gl_Position = MVP * vec4(position.x, position.y, 0.0, 1.0);
 // ...
}

I have a lot of squares but I don't want to rotate them all. width and height is 100px, how can I turn my square to make it look like this?

I know that I can achieve this by using transformation matrices. I did it in web development while working on svg, but I have no idea how I can get this working in OpenGl. I would like to know how can i multiply matrix and vector in OpenGL.

Vahe
  • 149
  • 1
  • 3
  • 10
  • 1
    See [How to use Pivot Point in Transformations](https://stackoverflow.com/questions/56804658/how-to-use-pivot-point-in-transformations/56806370#56806370) – Rabbid76 Sep 17 '20 at 14:43
  • I've just very basic rust knowledge and I'm not familiar with [cgmat](https://docs.rs/cgmath/0.17.0/cgmath/) at all. If you want to rotate around a pivot, you have to _translate(pivot) * rotation(axis, angle) * translate(-pivot)_. To setup the 3 matrices you can use `from_translation` and `from_axis_angle` (or `from_angle_z`). See [cgmath/src/matrix.rs](https://github.com/rustgd/cgmath/blob/master/src/matrix.rs) – Rabbid76 Sep 17 '20 at 14:57

2 Answers2

2

I would like to know how can i multiply matrix and vector in OpenGL.

You already multiplying matrices with a vector

gl_Position = MVP * vec4(position.x, position.y, 0.0, 1.0);

All you have to do is multiply your MVP matrix with your rotation matrix and than with your vector

gl_Position = MVP * rotationMatrix * vec4(position.x, position.y, 0.0, 1.0);

The rotation Matrix should be also a 4x4 matrix

phil
  • 21
  • 3
0

i tried the library nalgebra and nalgebra-glm. API cgmath confuses me.

let mut ortho = glm::ortho(0.0, SCR_WIDTH as f32, SCR_HEIGHT as f32, 0.0, -1.0, 1.0);

ortho = glm::translate(&ortho, &glm::vec3(0.0, 50.0, 0.0));

let rad = 30.0 * (PI / 360.0);

ortho = glm::rotate(&ortho, rad, &glm::vec3(0.0, 0.0, 1.0));

ortho = glm::translate(&ortho, &glm::vec3(0.0, -50.0, 0.0) );

enter image description here

Thank you for all the answers

Vahe
  • 149
  • 1
  • 3
  • 10