spearman008
spearman008

Reputation: 87

Draw a polygon from mouse clicks

I'm trying to draw a a polygon on the screen with vertices determined by mouse clicks. A left click adds a vertex to the polygon, and a right click will add the last vertex to the polygon connecting it to the first and creating the shape.

I currently have two vectors, one for x-coordinates and one for y-coordinates, and I am looping through the vectors creating a line loop. A -1 in the vector determines the end of a polygon and the start of a new one. This is a function that is then called in the display function.

Eventually I have to scan convert these polygons, and then clip them in a user defined window using the Sutherland Hodgman algorithm, but I'm having trouble even getting the polygons to show up.

glBegin(GL_LINE_LOOP);
for (int i = 0; i < xCo.size(); i++)
{
    if (xCo[i + 1] != -1)
    {
        glVertex2f(xCo[i], yCo[i]);
        glVertex2f(xCo[i + 1], yCo[i + 1]);
    }
    else
    {
        glVertex2f(xCo[i + 1], yCo[i + 1]);
        glVertex2f(xCo[0], yCo[0]);
    }
}
glEnd();
glFlush();
xCo.clear();
yCo.clear();

Upvotes: 1

Views: 2775

Answers (1)

genpfault
genpfault

Reputation: 52084

Use structs instead of separate arrays and float comparisons:

#include <glm/glm.hpp>

typedef vector< glm::vec2 > Poly;
void drawPoly( const Poly& poly )
{
    if( poly.size() == 1 )
        glBegin( GL_POINTS );
    else
        glBegin( GL_LINE_STRIP );

    for( const auto& pt : poly )
    {
        glVertex2f( pt.x, pt.y );
    }

    glEnd();
}

In context:

#include <GL/glut.h>
#include <glm/glm.hpp>
#include <vector>

typedef std::vector< glm::vec2 > Poly;
void drawPoly( const Poly& poly )
{
    if( poly.size() == 1 )
        glBegin( GL_POINTS );
    else
        glBegin( GL_LINE_STRIP );

    for( const auto& pt : poly )
    {
        glVertex2f( pt.x, pt.y );
    }

    glEnd();
}

typedef std::vector< Poly > Polys;
Polys polys( 1 );
void mouse( int button, int state, int x, int y )
{
    if( GLUT_UP == state && GLUT_LEFT_BUTTON == button )
    {
        polys.back().push_back( glm::vec2( x, y ) );
        glutPostRedisplay();
    }
    if( GLUT_UP == state && GLUT_RIGHT_BUTTON == button )
    {
        polys.back().push_back( polys.back().front() );
        polys.push_back( Poly() );
        glutPostRedisplay();
    }
}

void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, w, h, 0, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3ub( 255, 255, 255 );
    for( const auto& poly : polys )
    {
        drawPoly( poly );
    }

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInitWindowSize( 640, 480 );
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutMouseFunc( mouse );
    glutMainLoop();
    return 0;
}

Upvotes: 1

Related Questions