user9589156
user9589156

Reputation:

rotating a 2d circle function

im trying to add rotate to my function.

I have no idea how i can rotate it in my function

void draw_filled_circle(const RenderListPtr& render_list, const Vec2& position, float radius, CircleType type, Color color, float rotate){
    float pi;
    if (type == FULL) pi = D3DX_PI;         // Full circle
    if (type == HALF) pi = D3DX_PI / 2;     // 1/2 circle
    if (type == QUARTER) pi = D3DX_PI / 4;  // 1/4 circle

    const int segments = 32;
    float angle = rotate * D3DX_PI / 180;
    Vertex v[segments + 1];

    for (int i = 0; i <= segments; i++){
        float theta = 2.f * pi * static_cast<float>(i) / static_cast<float>(segments);
        v[i] = Vertex{
            position.x + radius * std::cos(theta),
            position.y + radius * std::sin(theta),
            color
        };
    }
    add_vertices(render_list, v, D3DPT_TRIANGLEFAN);
}

Upvotes: 1

Views: 305

Answers (1)

robthebloke
robthebloke

Reputation: 9668

In general you don't rotate anything by modifying the vertices directly. Instead, you use a matrix (The model-view-projection matrix) to transform the data. The 3 combined matrices boil down to:

  • The Model Matrix: This is the matrix that is used to position and orient the geometry in world space. If it is just rotation you are after, then set this matrix to be a rotation matrix.
  • The View matrix: This is the inverse matrix of your camera location.
  • The projection matrix: this flattens the 3D vertex locations into 2D coordinates on the screen.

You usually combine all 3 matrices together into a single MVP matrix which you use to do all 3 transformations in a single operation. This doc explains some of the basics: https://learn.microsoft.com/en-us/windows/win32/dxtecharts/the-direct3d-transformation-pipeline

D3DXMATRIX proj_mat; //< set with projection you need
D3DXMATRIX view_mat; //< invert the matrix for the camera position & orientation
D3DXMATRIX model_mat; //< set the rotation here
D3DXMATRIX MVP;
MVP = model_mat * view_mat * proj_mat; //< combine into a single MVP to set on your shader

If you REALLY want to rotate the vertex data in the buffer, then you can set the model_mat to be some rotation matrix, and then multiply each vertex by model_mat. The issue with doing that is that it's very slow to update (you need to rebuild the entire buffer each frame, and the GPU has circuitry design to transform vertices via a matrix)

Upvotes: 1

Related Questions