我想使用圆心及其半径的坐标在特定位置绘制一个圆。我发现的所有方法都使用glut,但都没有将圆定位在特定点上。
我想提一下,我是新手,如果我做错了什么,我会很高兴知道。
这是我到目前为止所做的:
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();
}
}
最佳答案
实际创建圆顶点的代码
因为cos(x)
和sin(x)
函数返回的值是[0..1]
,而不是乘以某个值将使我们与该值的收音机相对应。添加或减去x
和y
值会将圆心移动到特定位置。 fragments
值指定更好的圆的最小化。
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;
}
关于c++ - 使用OpenGL C++绘制圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33858753/