簡體   English   中英

如何在 OpenGL 中旋轉 2D 正方形?

[英]How to rotate a 2D square in OpenGL?

我有一個基本的正方形。 我怎樣才能旋轉它?

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


這是我的正交投影矩陣。

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);
 // ...
}

我有很多正方形,但我不想將它們全部旋轉。 寬度和高度是 100px,我怎樣才能把我的正方形變成這個樣子?

我知道我可以通過使用轉換矩陣來實現這一點。 我在使用 svg 時在 Web 開發中做到了,但我不知道如何在 OpenGl 中實現它。 我想知道如何在 OpenGL 中將矩陣和向量相乘。

我想知道如何在 OpenGL 中將矩陣和向量相乘。

您已經將矩陣與向量相乘

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

您所要做的就是將您的 MVP 矩陣與您的旋轉矩陣相乘,然后再與您的向量相乘

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

旋轉矩陣也應該是一個 4x4 矩陣

我嘗試了圖書館nalgebranalgebra-glm API cgmath 讓我很困惑。

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) );

在此處輸入圖片說明

謝謝大家的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM