(C++ 和 OpenGL)我试图在批处理渲染器中旋转一组顶点(它将模拟一个正方形),但它不是 100% 工作:(

(C++ & OpenGL) I'm trying to rotate a group of vertices (which it'll simulate a square) in a Batch Renderer, but it's not 100% working :(

我正在尝试旋转批处理渲染器中的一个对象或一组 4 个顶点(它是动态的,因此它可以随时更新它们的顶点和索引)。

我目前正在使用一种名为“Rodriguez Matrix”的方法,感谢 this StackExchange post

我学会了如何使用它

它工作得很好,但问题是批次中所有对象的中心是 (0, 0, 0) 而不是它们自己的位置。 而且我在网上找不到解决方案,所以这是我第一次尝试在这里提问!

(另外我正在使用一个名为 GLM 的库来转换对象)

所以这是代码,UpdateObject 方法由批处理在 for 循环中调用(因为那里有一组对象)所以我认为没有必要显示整个系统,但是是的“Object.cpp”这是保存对象所有信息的那个(包含 Rodriguez 矩阵函数)

Object::Object(glm::vec3 pos, glm::vec3 rot, glm::vec3 sca)
        {
            position = pos;
            scale = sca;
            rotation = rot;
        }

        glm::mat3 rodriguesMatrix(const double degrees, const glm::vec3& axis) {
            glm::mat3 v = glm::mat3(
                axis.x * axis.x, axis.x * axis.y, axis.x * axis.z,
                axis.x * axis.y, axis.y * axis.y, axis.y * axis.z,
                axis.x * axis.z, axis.y * axis.z, axis.z * axis.z
            );

            glm::mat3 v2 = glm::mat3(
                0, -axis.z, axis.y,
                axis.z, 0, -axis.x,
                -axis.y, axis.x, 0
            );
            glm::mat3 cosMat(1.0f * cos(degrees * M_PI));
            v *= (1 - cos(degrees * M_PI));
            v2 *= sin(degrees * M_PI);

            glm::mat3 rotation = cosMat + v + v2;

            return rotation;
        }

        Vertex* Object::UpdateObject(Vertex* target)
        {
            glm::mat3 rotationMatrix;
            rotationMatrix = rodriguesMatrix(glm::radians(rotation.x), glm::vec3(1.f, 0.f, 0.f));
            rotationMatrix = rodriguesMatrix(glm::radians(rotation.y), glm::vec3(0.f, 1.f, 0.f));
            rotationMatrix = rodriguesMatrix(glm::radians(rotation.z), glm::vec3(0.f, 0.f, 1.f));


            float size = 1.0f;
            target->position = rotationMatrix * glm::vec3(position.x - 0.5f * scale.x, position.y + 0.5f * scale.y, position.z);
            target->color = glm::vec3(1.0f, 0.2f, 0.2f);
            target->texcoord = glm::vec2(0.0f, 1.0f);
            target++;

            target->position = rotationMatrix * glm::vec3(position.x - 0.5f * scale.x, position.y - 0.5f * scale.y, position.z);
            target->color = glm::vec3(0.2f, 1.0f, 0.2f);
            target->texcoord = glm::vec2(0.0f, 0.0f);
            target++;

            target->position = rotationMatrix * glm::vec3(position.x + 0.5f * scale.x, position.y - 0.5f * scale.y, position.z);
            target->color = glm::vec3(0.2f, 0.2f, 1.0f);
            target->texcoord = glm::vec2(1.0f, 0.0f);
            target++;

            target->position = rotationMatrix * glm::vec3(position.x + 0.5f * scale.x, position.y + 0.5f * scale.y, position.z);
            target->color = glm::vec3(1.0f, 1.0f, 0.2f);
            target->texcoord = glm::vec2(1.0f, 1.0f);
            target++;

            return target;
        }

顶点是一个包含 2 个“Vector3”的结构,一个用于位置,另一个用于颜色,一个“Vector2”用于纹理坐标。

所以就是这个问题,如果有人能帮助我或给我一个答案,那就太好了:'D

此致。纳乔:D

这是一个简单示例,说明如何使用平移和旋转矩阵围绕一个点旋转矩形。希望对您有所帮助:

#include <iostream>
#include <glm/glm.hpp>
#include <glm/ext.hpp>

void rotateRectangleAroundSomePoint(glm::vec3 vertices[4], float angle, glm::vec3 rotationCenter, glm::vec3 axis)
{
    const glm::mat4 translationMatrix = glm::translate(glm::identity<glm::mat4>(), -rotationCenter);
    const glm::mat4 rotationMatrix = glm::rotate(glm::identity<glm::mat4>(), angle, axis);
    const glm::mat4 reverseTranslationMatrix = glm::translate(glm::identity<glm::mat4>(), rotationCenter);

    for (size_t i = 0; i < 4; i++) {

        vertices[i] = glm::vec3(
                    reverseTranslationMatrix * rotationMatrix * translationMatrix * glm::vec4(vertices[i], 1.0f));
    }
}

int main()
{

    glm::vec3 rectangleVertices[4] =
    {
        glm::vec3(1.0f, 1.0f, 0.0f),
        glm::vec3(3.0f, 1.0f, 0.0f),
        glm::vec3(3.0f, 2.0f, 0.0f),
        glm::vec3(1.0f, 2.0f, 0.0f),
    };

    rotateRectangleAroundSomePoint(rectangleVertices,
                                   glm::radians(90.0f),
                                   glm::vec3(2.0f, 1.5f, 0.0),
                                   glm::vec3(0.0f, 0.0f ,1.0f));

    for (size_t i = 0; i < 4; i++) {
        std::cout
                << rectangleVertices[i].x << " , "
                << rectangleVertices[i].y << " , "
                << rectangleVertices[i].z << std::endl;
    }

    return 0;
}