【问题标题】:How to draw part of a circle(Opengl/GLFW)如何绘制圆的一部分(Opengl/GLFW)
【发布时间】:2013-11-09 18:36:31
【问题描述】:

我正在尝试仅绘制 3d 圆的 扇区/部分 - 给出了 2 个角度(开始、结束)、圆心的坐标、圆的半径和宽度. 我在 Photoshop 中绘制了什么结果必须是这样的。

您能帮我做吗(2d 示例,带有 1 条简单的曲线,也可以)?只是想了解如何做到这一点...

p.s.抱歉英语不好

【问题讨论】:

    标签: c++ opengl glfw


    【解决方案1】:

    给定半径、圆心 (x0/y0) 和角度(以弧度为单位)的圆上点的计算公式

    float x = radius * cos(angle) + x0;
    float y = radius * sin(angle) + y0;
    

    使用它来构建相应的三角形带(参见例如http://en.wikipedia.org/wiki/Triangle_strip):

    float[] coordinates = new float[steps * 3];
    float t = start_angle;
    int pos = 0;
    for (int i = 0; i < steps; i++) {
      float x_inner = radius_inner * cos(t) + x0;
      float y_inner = radius_inner * sin(t) + y0;
    
      float x_outer = radius_outer * cos(t) + x0;
      float y_outer = radius_outer * sin(t) + y0;
    
      coordinates[pos++] = x_inner;
      coordinates[pos++] = y_inner;
      coordinates[pos++] = 0f;
    
      coordinates[pos++] = x_outer;
      coordinates[pos++] = y_outer;
      coordinates[pos++] = 0f;
    
      t += (end_angle - start_angle) / steps;
    }
    
    // Now you need to hand over the coordinates to gl here in your preferred way,
    // then call glDrawArrays(GL_TRIANGLE_STRIP, 0, steps * 2);
    

    【讨论】:

    • glBegin() 在 2013 年?真的吗?
    • @Stefan Haustein - 我无法让它工作:(...你能解释一下如何实现它吗...我是 OpenGl/GLFW 的新手
    • @ddriver:好吧,使用其他任何东西都不会使代码更短、更易读或更好地说明算法......
    • @NoSense:你有没有得到一个简单的 drawTriangle 示例(哪个)?
    • @StefanHaustein - 使用长期不推荐使用的功能仍然是非常糟糕的编程实践,除了使用glDrawElements() 不会使代码更长,对于当代适当的解决方案付出的代价很小。
    猜你喜欢
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2017-06-17
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多