opengl - 如何在OpenGL中旋转2D正方形?

标签 opengl graphics rust

我有一个基本的广场。我如何旋转它?

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中将矩阵和向量相乘。

最佳答案

I would like to know how can i multiply matrix and vector in 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矩阵

关于opengl - 如何在OpenGL中旋转2D正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63939480/

相关文章:

c++ - GLSL布局std140填充困境

c++ - 堆叠 Cg 着色器

struct - 创建零大小结构的多种方法有什么区别?

serialization - 如何在不包含枚举变体名称的情况下序列化枚举?

c++ - 当 OpenGL 相机处于低高度(低 Z 坐标)时,为什么将窗口坐标映射到球体很困难?

View 空间中的 openGL 立方体贴图反射是错误的

java - 在LWJGL 3中渲染透视投影矩阵

PHP:使用 ImageMagick 将 alpha channel 转换为白色背景

swing - 绘制汽车时出现黑屏或找不到符号错误

从 &str 解析日期的 Rustacean 方法 [Rust]