(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 :(

Viewed 418

I'm trying to rotate an object, or a group of 4 vertices that're inside a Batch Renderer (it's a dynamic one so it can update their verts and indices at any time).

I'm currently using a method called "Rodriguez Matrix" which i learned how to use it thanks to this StackExchange post

And it works really well but the problem is that the center of all of the objects in the batch is (0, 0, 0) instead of their own position. And i can't find a solution online so this is my first time trying to ask a question here!

(Also i'm using a library called GLM to make the transformation of the object)

So here's the code, the UpdateObject method is called by the batch in a for loop (because there's a group of objects in there) so i don't think it's necessary to show all of the entire system but yes the "Object.cpp" which is the one that keeps all the info of the objects (containing the Rodriguez Matrix function)

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

The Vertex is a struct of 2 "Vector3", one for the position, other one for the color, and a "Vector2" for the Texture Coordinates.

So that's the issue, if someone can help me or give me an answer, that'll be great :'D

Best Regards. Nacho :D

1 Answers

This is a simple example of how to rotate a rectangle around a point using Translation and Rotation matrices. I hope this help you:

#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;
}
Related