Mushahid Hussain
Mushahid Hussain

Reputation: 4055

Implement winding rule in OpenGL

I have this code in which a user draws some polygon using mouse pointer in OpenGL. Now after drawing this I would like to ask the user to fill the polygon and then implement winding rule to tell whether the polygon is filled from inside or outside the polygon. Can anyone tell me how I can achieve this?

#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();
}

Upvotes: 2

Views: 1039

Answers (1)

Tommy
Tommy

Reputation: 100632

Your best bet is probably to use the tessellator provided by the OpenGL Utility Library ('GLU'). You can specify the winding rule via gluTessProperty as any of odd, non-zero, negative, positive or greater than or equal to two. Tessellation has the effect of turning an arbitrary polygon such as the one your user has described into a set of convex polygons that OpenGL can render.

GLU is provided as a system library on Windows, is usually available on Linux-derived OSs and failing all of that you can relatively easily include the MESA implementation directly into your project. I've deployed it in iPad applications, for example.

Additions: if you're using Dev C, then you should already have the libglu32.a library. Link to that, presumably via -lglu32. You should then be able to use the code and guidance given here (note especially the closing remarks on winding rules) or here.

Upvotes: 4

Related Questions