1

I want to draw a circle in a specific position using the coordinates of the centre of the circle and its radius. All the methods that i found are using glut and none of them position the circle in a specific point. I wanna mention that I'm new to this things and if I'm doing something wrong, I would be happy to know it. This is what I did so far:

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

MikeMB
  • 20,029
  • 9
  • 57
  • 102
Alex Chihaia
  • 103
  • 2
  • 15
  • And what exactly is the question? You already draw a circle in normalized device space. You actually draw it even 3 times. – derhass Nov 22 '15 at 19:50

1 Answers1

4

The code which actually creates circle vertices

as cos(x) and sin(x) function returns values is [0..1] than multiplication to some value will give us circle with radios of that value. Adding or subtracting x and y values will move the center of the circle to a specific position. fragments value specifies detalization of circle 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;
}
Mykola
  • 3,343
  • 6
  • 23
  • 39
  • I wrote this and i get nothing. What am I doing wrong?`std::vector vertices5 = createCircleArray(20, 0, 0, 100); Vertex * vertices4 = &vertices5[0]; Mesh mesh4(vertices4, sizeof(vertices5)/sizeof(vertices5[0]));` – Alex Chihaia Nov 22 '15 at 20:30
  • @Alex Chihaia: to get size of the vector call `size()` method. – Mykola Nov 22 '15 at 20:32
  • @Alex Chihaia: + `std::vector` is a dynamic container – Mykola Nov 22 '15 at 20:33
  • @Alex Chihaia: `Vertex* pVertices = new Vertex[vertices4.size()];` – Mykola Nov 22 '15 at 20:34
  • @Alex Chihaia: than `std::copy(vertices4.begin(), vertices4.end(), pVertices);` – Mykola Nov 22 '15 at 20:36
  • @Alex Chihaia: and at last `Mesh mesh4(pVertices, vertices4.size());` – Mykola Nov 22 '15 at 20:37
  • @Alex Chihaia: I am glad can help you. Good luck! – Mykola Nov 22 '15 at 20:44
  • Thank you so much for this!! I've been trying to wrap my head around various tutorials on this subject that are either outdated or just don't work for me for whatever reason, and screaming at the computer for a **full day**! +1 for you! – Kenny83 Dec 22 '19 at 18:40
  • @Kenny83, I'm glad to help you!) – Mykola Dec 24 '19 at 13:33