使用OpenGL C ++绘制圆 [英] Drawing a circle using OpenGL C++

查看:200
本文介绍了使用OpenGL C ++绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用圆心及其半径的坐标在特定位置绘制一个圆.我发现的所有方法都使用glut,但都没有将圆定位在特定点上. 我想提一下,我是新手,如果我做错了什么,我会很高兴知道这一点. 这是我到目前为止所做的:

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:

类构造器

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

画圆法

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

主要方法

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

推荐答案

实际创建圆顶点的代码

as cos(x)sin(x)函数返回的值是[0..1],而不是乘以某个值将使我们绕过该值的单选.添加或减去xy值会将圆心移动到特定位置. fragments值指定更好的圆的半径.

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

这篇关于使用OpenGL C ++绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆