使用 OpenGL C++ 绘制圆

Drawing a circle using OpenGL C++

我想用圆心和半径的坐标在特定位置画一个圆。我发现的所有方法都使用过剩,其中 none 将圆圈定位在特定点。 我想提一下,我是这方面的新手,如果我做错了什么,我很乐意知道。 这是我到目前为止所做的:

class Constructor

Mesh::Mesh(Vertex * vertices, unsigned int numVertices) {
    m_drawCont = numVertices;
    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);

    //PUT ALL OF OUR VERTEX DATA IN THE ARRAY
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArray(0);
}

Draw Circle Method

void Mesh::DrawCircle() {
    glBindVertexArray(m_vertexArrayObject);
    glDrawArrays(GL_LINE_LOOP, 0, m_drawCont);
    glBindVertexArray(0);
}

Main method

int main(int argc, char **argv) {
    Display display(800, 600, "Window1");
    Shader shader("./res/basicShader");
    Vertex vertices2[3000];

    for (int i = 0; i < 3000; i++) {
        vertices2[i] = Vertex(glm::vec3(cos(2 * 3.14159*i / 1000.0), sin(2 * 3.14159*i / 1000.0), 0));
    }

    Mesh mesh3(vertices2, sizeof(vertices2) / sizeof(vertices2[0]));

    while (!display.IsClosed()) {
        display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
        shader.Bind();
        mesh3.DrawCircle();
        display.Update();
    }
}

And this is the output image

实际创建圆顶点的代码

因为 cos(x)sin(x) 函数 returns 值是 [0..1] 而不是乘以某个值会给我们圆圈与该值的收音机。添加或减去 xy 值会将圆心移动到特定位置。 fragments value 指定圆的 detalization greater-better。

std::vector<Vertex> CreateCircleArray(float radius, float x, float y, int fragments)
{
     const float PI = 3.1415926f;

     std::vector<Vertex> result;

     float increment = 2.0f * PI / fragments;

     for (float currAngle = 0.0f; currAngle <= 2.0f * PI; currAngle += increment)
     {
         result.push_back(glm::vec3(radius * cos(currAngle) + x, radius * sin(currAngle) + y, 0));
     }

     return result;
}