opengl画圆实现

void DrawCircle()
{
GLint n = 1000;
GLfloat R = 0.05;
GLfloat Pi = 3.14159;
GLint i;
glBegin(GL_POLYGON);
//glColor3f(1.0, 0.0, 0.0);
for(i = 0; i < n; i++)
{
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
}
glEnd();
}


</

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用QtOpenGL绘制一个圆,可以使用OpenGL的绘图函数来实现。下面是一个简单的示例代码,演示了如何在QtOpenGL窗口中绘制一个圆: ```cpp #include <QtOpenGL> #include <QOpenGLWidget> class MyGLWidget : public QOpenGLWidget { public: void paintGL() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_POLYGON); float radius = 0.5f; int numSegments = 100; float theta = 2.0f * 3.1415926f / numSegments; float cosTheta = cos(theta); float sinTheta = sin(theta); float x = radius; float y = 0.0f; for (int i = 0; i < numSegments; i++) { glVertex2f(x, y); float newX = x * cosTheta - y * sinTheta; float newY = x * sinTheta + y * cosTheta; x = newX; y = newY; } glEnd(); } void initializeGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } void resizeGL(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QSurfaceFormat format; format.setVersion(2, 1); format.setProfile(QSurfaceFormat::CompatibilityProfile); format.setDepthBufferSize(24); QSurfaceFormat::setDefaultFormat(format); MyGLWidget widget; widget.show(); return a.exec(); } ``` 这是一个简单的QtOpenGL窗口,继承自QOpenGLWidget类。在`paintGL`函数中,我们使用OpenGL的绘制函数来绘制一个圆。使用`glBegin(GL_POLYGON)`开始定义一个多边形,并在循环中绘制多边形的各个顶点。每个顶点的坐标通过三角函数计算得出,使得多边形的边缘形成一个圆。 在`initializeGL`函数中初始化OpenGL的状态,包括设置清除颜色。在`resizeGL`函数中设置视口和投影矩阵。 最后,在主函数中创建一个MyGLWidget对象并显示出来。 注意:该示例代码仅为演示绘制圆的基本原理,实际使用时可能需要根据具体需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值