C++/OpenGL - 2D - 如何在矩形边界框中剪辑一个圆

标签 c++ opengl

我只是想知道如何在矩形边界框中剪裁一个圆?我目前在我的程序中使用 Cohen–Sutherland 算法进行线剪裁,到目前为止我已经设法剪裁了矩形和多边形。但是,对于圆形剪裁,我不知道如何实现。我正在使用以下内容来构建我的圈子:

glBegin(GL_POLYGON);
double radius = 50;  
for(int angle = 0; angle <= 360; angle++ ){
    float const curve = 2 * PI * (float)angle / (float)360;
    glVertex2f(point.x + sin(curve) * radius, point.y + cos(curve) * radius);
}
glEnd();

我的裁剪算法与这里的相同:http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm .但是,它返回 2 个点,代表一条新线,稍后用于绘制裁剪形状。所以基本上我已经尝试这样做:

line Lines[360] // an array with size 360 with data type line, which is a struct holding two points (x1, y1, x2, y2) of the new line returned by my clipping function.

double radius = 50;
for(int angle = 0; angle < 360; angle++){
    float const currentCurve = 2 * PI * (float)angle / (float)360;
    float const nextCurve = 2 * PI * (float)(angle+1) / (float)360;
    int x1 = (int)(point[i].x + sin(currentCurve) * radius); // point is another struct holding only a single point. 
    y1 = (int)(point[i].y + cos(currentCurve) * radius);
    x2 = (int)(point[i+1].x+ sin(nextCurve) * radius);
    y2 = (int)(point[i+1].y + cos(nextCurve) * radius);=
    // Clip the points with the clipping algorithm:
    Lines[i] = Clipper(x1, y1, x2, y2);
}

// Once all lines have been clipped or not, draw:

glBegin(GL_POLYGON);
for(int i = 0; i < 360; i++){
    glVertex2f(Lines[i].x1, Lines[i].y1);
    glVertex2f(Lines[i].x2, Lines[i].y2);
}
glEnd();

请注意,我用鼠标在屏幕上画了一个圆圈,并将每个 360 点存储到一个名为 point 的结构数组中,它是链表的一部分。所以我有 1 个节点代表屏幕上的一个圆圈。

无论如何,在上面的情况下,我的圆圈没有被剪裁(或者根本没有绘制),我的应用程序在点击几下鼠标后崩溃了。

最佳答案

使用剪刀测试 - 阅读 glScissor():http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml

关于C++/OpenGL - 2D - 如何在矩形边界框中剪辑一个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9954820/

相关文章:

c++ - 如果基类构造函数受到保护,为什么我们不能在派生类函数中创建基类的对象

C++:如何为多维 vector 创建构造函数?

c++ - 使用 vector A 的位置和方向创建 vector B

java - 千个 Sprite 绘制效果不佳

c++ - MSVS/MSVC 静态模板类方法不警告

c++ - 使用 Boost::odeint 和 Eigen::Matrix 作为状态 vector

c++ - 在 C++ 中定义字符串并将它们作为参数传递给函数的不同方式的区别

OpenGL 3D 纹理坐标

OpenGL:为什么正方形纹理占用更少的内存

OPENGL 剪辑坐标