c - 在OpenGL中实现缠绕规则

标签 c opengl

我有这样的代码,用户在 OpenGL 中使用鼠标指针绘制一些多边形。现在,绘制此图后,我想要求用户填充多边形,然后实现缠绕规则来判断多边形是从多边形内部还是外部填充。谁能告诉我如何实现这一目标?

#include <cstdlib>
#include <iostream>
#include<GL/glut.h>
GLsizei winWidth=400,winHeight=300;
GLint endPtCtr=0;
using namespace std;

class scrPt
{
public:
    GLint x,y;
};
void init(void)
{
    glClearColor(0.0,0.0,1.0,1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,200.0,0.0,150.0);
}
void displayFcn(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
}
void winReshapeFcn(GLint newWidth,GLint newHeight)
{
    glViewport(0,0,newWidth,newHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));
    winWidth=newWidth;
    winHeight=newHeight;
}
void drawLineSegment(scrPt endPt1,scrPt endPt2)
{
    glBegin(GL_LINES);
    glVertex2i(endPt1.x,endPt1.y);
    glVertex2i(endPt2.x,endPt2.y);
    glEnd();
}
void polyLine(GLint button,GLint action,GLint xMouse,GLint yMouse)
{
    static scrPt endPt1,endPt2;
    if(endPtCtr==0)
    {
        if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN)
        {
            endPt1.x=xMouse;
            endPt1.y=winHeight-yMouse;
            endPtCtr=1;
        }
        else
            if(button==GLUT_RIGHT_BUTTON)
                exit(0);
    }
    else
        if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN)
        {
            endPt2.x=xMouse;
            endPt2.y=winHeight-yMouse;
            drawLineSegment(endPt1,endPt2);
            endPt1=endPt2;
        }
        else
            if(button==GLUT_RIGHT_BUTTON)
                exit(0);
    glFlush();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(winWidth,winHeight);
    glutCreateWindow("Draw Interactive Polylines");
    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(winReshapeFcn);
    glutMouseFunc(polyLine);
    glutMainLoop();
}

最佳答案

您最好的选择可能是使用 OpenGL 实用程序库(“GLU”)提供的曲面 segmentation 器。您可以通过 gluTessProperty 将缠绕规则指定为奇数、非零、负数、正数或大于或等于 2 中的任何一个。曲面 segmentation 具有将任意多边形(例如用户描述的多边形)转换为 OpenGL 可以渲染的一组凸多边形的效果。

GLU 提供为 a system library在 Windows 上,通常可在 Linux 派生的操作系统上使用,并且如果无法满足所有这些要求,您可以相对轻松地将 MESA 实现直接包含到您的项目中。例如,我已将其部署在 iPad 应用程序中。

补充:如果您使用 Dev C,那么您应该已经拥有 libglu32.a 库。大概通过 -lglu32 链接到该链接。然后您应该能够使用 here 给出的代码和指南(特别注意绕线规则的结束语)或here

关于c - 在OpenGL中实现缠绕规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8261016/

相关文章:

c - 如何以 "try"读取 C 中的输入

c++ - 在OpenGL/GLSL 4.3中读取和更新纹理缓冲区

opengl - 我可以在单个渲染窗口中同时使用已弃用的 OpenGL 和现代 OpenGL 吗?

c - typedef with function this 被问到但无法理解?

c - 从 fgets 获取第一个单词

c - 在 C 中,有没有一种方法可以确保在没有 pthread_once 的情况下只调用一次函数?

OpenGL 4.0 GPU 绘图功能?

c - RTOS MicroC 任务分配

opengl - 使用具有多个采样器制服的单个纹理图像单元

c++ - 如何在 OpenGL 中使用 GLM?