简体   繁体   English

尝试在 3D 空间中旋转 2D 形状时出现问题

[英]Problem when trying to rotate a 2D shape in a 3D space

i'm working on a batch renderer and i'm having trouble implementing a rotation feature for 2D shapes like a rectangle or a triangle.我正在开发批处理渲染器,但在为矩形或三角形等 2D 形状实现旋转功能时遇到了麻烦。 I'm using a Vertex* (struct Vertex code below) to store the position and color values, which works pretty well, until i got to rotations.我正在使用 Vertex*(下面的结构 Vertex 代码)来存储位置和颜色值,效果很好,直到我开始旋转。

struct Vertex
{
    glm::vec3 position;
    glm::vec4 color;
};

I tried to make a mat4 and use the corresponding rotation function accordingly, which worked well when i set the model view projection in the shader, but converting the data to a Vertex wasn't working and very laggy.我尝试制作一个 mat4 并相应地使用相应的旋转功能,当我在着色器中设置模型视图投影时效果很好,但是将数据转换为顶点不起作用并且非常滞后。 Therefore i thought i use some math that i found online, but my squares that i drew were streched to rectangles sometimes and the positions were off.因此,我想我使用了一些我在网上找到的数学方法,但是我绘制的正方形有时会被拉伸成矩形并且位置不正确。 I put in alot of hours now and tried different stuff, but nothing worked.我现在投入了很多时间并尝试了不同的东西,但没有任何效果。 That's why i hope someone can maybe find an obvious bug in my code or direct me to a good source where i can read about this topic (the stuff i found was either irrelevant for my purposes or hard to understand).这就是为什么我希望有人能在我的代码中找到一个明显的错误,或者将我引导到一个很好的来源,我可以在那里阅读这个主题(我发现的东西要么与我的目的无关,要么难以理解)。

Here is my summarized code, i tried to keep it as short as possible:这是我总结的代码,我尽量保持简短:

void rotateX(float angle) 
{
    yr = yr * cos(angle) - zr * sin(angle);
    zr = zr * cos(angle) + yr * sin(angle);
}

void rotateY(float angle)
{
    xr = xr * cos(angle) - zr * sin(angle);
    zr = zr * cos(angle) + xr * sin(angle);
}

void rotateZ(float angle)
{
    xr = xr * cos(angle) - yr * sin(angle);
    yr = yr * cos(angle) + xr * sin(angle);
}

std::array<Vertex, 4> createQuad(glm::vec3 pos, glm::vec3 rotation, glm::vec4 rgba)
{
    float xr = 1.0f;
    float yr = 1.0f;
    float zr = 1.0f;

    rotateX(rotation.x);
    rotateY(rotation.y);
    rotateZ(rotation.z);

    Vertex v0;
    v0.position = glm::vec3(pos.x,      pos.y + yr, pos.z);
    v0.color = rgba;

    Vertex v1;
    v1.position = glm::vec3(pos.x + xr, pos.y + yr, pos.z);
    v1.color = rgba;

    Vertex v2;
    v2.position = glm::vec3(pos.x + xr, pos.y, pos.z + zr);
    v2.color = rgba;

    Vertex v3;
    v3.position = glm::vec3(pos.x,      pos.y, pos.z + zr);
    v3.color = rgba;

    return { v0, v1, v2, v3 };
}

Other notes:其他注意事项:

  1. When i remove the rotate()'s and the '+ yr' the code works fine.当我删除rotate()'s 和'+ yr' 时,代码工作正常。 I'm not 100% sure about the '+ yr', but i don't know how to rotate around the y-axis then.我不是 100% 确定“+ yr”,但我不知道如何围绕 y 轴旋转。
  2. Accessing the elements of the mat4 with '[][]' wasn't giving me the right values and it was REALLY laggy for some reason.使用 '[][]' 访问 mat4 的元素并没有给我正确的值,而且由于某种原因它真的很滞后。 That's why i'm trying to avoid that if possible and go with raw math.这就是为什么我尽可能避免这种情况并使用原始数学。

I'm going to bed for now, it's really late and i have a headache... hopefully my question wasn't too nooby or stupid, in that case i'm sorry for bothering you.我现在要睡觉了,真的很晚了,我头疼...希望我的问题不是太笨拙或愚蠢,在这种情况下,我很抱歉打扰你。 Thanks in advance for any help!在此先感谢您的帮助!

First, change your rotate functions to accept a vector as an argument, rather than apply the rotations to globals (or whatever those are).首先,更改您的rotate函数以接受向量作为参数,而不是将旋转应用于全局变量(或任何其他变量)。

Then instead of one vector:然后代替一个向量:

r = {1, 1, 1}

use two:使用两个:

r = {1, 0, 0}
s = {0, 1, 0}

Rotate them both (using the same angle arguments), then construct the square.旋转它们(使用相同的角度参数),然后构造正方形。 (I don't know the syntax of a glm::vec3 , but this is simple vector addition): (我不知道glm::vec3的语法,但这是简单的向量加法):

Vertex v0 = pos;

Vertex v1 = pos + r;

Vertex v2 = pos + r + s;

Vertex v3 = pos + s;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM