OpenGL 平面圆绘制与贴图

本文介绍了如何在OpenGL中使用GL_TRIANGLE_FAN绘制带有纹理的圆形。关键在于正确分配纹理坐标,确保它们位于0到1之间,并与圆心角相关。通过调整纹理坐标,实现了将图像贴图到圆形上,最后展示了运行效果。
摘要由CSDN通过智能技术生成
                两种画圆的方法,DrawCircle2()为一般的做法,利用GL_LINE_STRIP实现,
void DrawCircle2(float cx, float cy, float r, int num_segments){ glBegin(GL_LINE_STRIP); for (int i = 0; i <= num_segments; i++) {  glVertex2f(cx + r * cos((2 * M_PI * i) / num_segments),       cy + r * sin((2 * M_PI * i) / num_segments)); } glEnd();}
DrawCircle1()则是利用GL_LINES_LOOP实现,
void DrawCircle1(float cx, float cy, float r, int num_segments) {  float theta = 2 * M_PI / float(num_segments);  float tangetial_factor = tanf(theta);//calculate the tangential factor  float radial_factor = cosf(theta);//calculate the radial factor  float x = r;//we start at angle = 0  float y = 0;  glBegin(GL_LINE_LOOP);  for(int ii = 0; ii < num_segments; ii++)  {   glVertex2f(x + cx, y + cy);//output vertex   //calculate the tangential vector   //remember, the radial vector is (x, y)   //to get the tangential vector we flip those coordinates and negate one of them   float tx = -y;   float ty = x;   //add the tangential vector   x += tx * tangetial_factor;   y += ty * tangetial_factor;   //correct using the radial factor   x *= radial_factor;   y *= radial_factor;  }  glEnd(); }

但是上面两个函数都只是画出了两个圆圈,想要给circle贴图,必须画出的是一个区域,所以可以利用GL_TRIANGLE_FAN绘制,要实现纹理映射,关键是纹理坐标的分配:

圆心纹理坐标为:(0.5, 0.5)选取图片的中心。

圆圈上的点的分配:

纹理坐标必须在0,1之间,而且这些纹理坐标和圆的半径没有关系,只和圆心角有关。

因为-1< cos(delta_angle*i) <1,则==> 0 <= (cos(delta_angle*i) + 1.0)*0.5 <= 1

同理:0 <= (sin(delta_angle*i) + 1.0)*0.5 <= 1

GLvoid draw_circle(const GLfloat radius,const GLuint num_vertex){ GLfloat vertex[4];  GLfloat texcoord[2]; const
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值