Polygons in OpenGL
This this application the user can capture an image from their isight and with the click of a mouse can draw a vertex from the current mouse lcoation. The area is then filled with pink, any concave polygon can be used and is partitioned into a convex polygon.

click below to see our code:
#include <CGAL/basic.h> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Partition_traits_2.h> #include <CGAL/Partition_is_valid_traits_2.h> #include <CGAL/polygon_function_objects.h> #include <CGAL/partition_2.h> #include <CGAL/point_generators_2.h> #include <CGAL/random_polygon_2.h> #include <cassert> #include <list> typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Partition_traits_2Traits; typedef CGAL::Is_convex_2 Is_convex_2; typedef Traits::Polygon_2 Polygon_2; typedef Traits::Point_2 Point_2; typedef Polygon_2::Vertex_const_iterator Vertex_iterator; typedef std::list Polygon_list; typedef CGAL::Partition_is_valid_traits_2 Validity_traits; typedef CGAL::Creator_uniform_2 Creator; typedef CGAL::Random_points_in_square_2 Point_generator; #include "ofMain.h" #include "ofCvMain.h" #include "myCvGrayscaleImage.h" #include "myCvColorImage.h" #define NOTE_NUM 240 class testApp : public ofSimpleApp{ public: void setup(); void update(); void draw(); void keyPressed (int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(); ofVideoGrabber vidGrabber; myCvColorImage colorImg; bool showFreezeImg; // VIDEO CAMERA //some capture frame dimensions int iwidth; int iheight; CvSize isize; //CGAL Polygon_2 polygon; Polygon_list partition_polys; Traits partition_traits; Validity_traits validity_traits; void make_polygon(Polygon_2& polygon); void calcPartitions(); void drawPoly(Polygon_2& polygon); void drawPartitions(Polygon_list& partition_polys); }; void testApp::setup(){ ofSetFrameRate(60); ofSetVerticalSync(true); iwidth = 320; iheight = 240; isize = cvSize(iwidth, iheight); vidGrabber.setVerbose(true); vidGrabber.initGrabber(iwidth, iheight); colorImg.allocate(iwidth, iheight); make_polygon(polygon); calcPartitions(); showFreezeImg = false; } void testApp::make_polygon(Polygon_2& polygon) { polygon.push_back(Point_2(391, 374)); polygon.push_back(Point_2(240, 431)); /*polygon.push_back(Point_2(252, 340)); polygon.push_back(Point_2(374, 320)); polygon.push_back(Point_2(289, 214)); polygon.push_back(Point_2(134, 390)); polygon.push_back(Point_2( 68, 186)); polygon.push_back(Point_2(154, 259)); polygon.push_back(Point_2(161, 107)); polygon.push_back(Point_2(435, 108)); polygon.push_back(Point_2(208, 148)); polygon.push_back(Point_2(295, 160)); */polygon.push_back(Point_2(421, 212)); polygon.push_back(Point_2(441, 303)); } void testApp::calcPartitions() { //CGAL::random_polygon_2(50, std::back_inserter(polygon), // Point_generator(100)); //make_polygon(polygon); if(polygon.size() > 2) { CGAL::optimal_convex_partition_2(polygon.vertices_begin(), polygon.vertices_end(), std::back_inserter(partition_polys), partition_traits); } /* assert(CGAL::partition_is_valid_2(polygon.vertices_begin(), polygon.vertices_end(), partition_polys.begin(), partition_polys.end(), validity_traits)); */ } void testApp::drawPoly(Polygon_2& polygon) { glColor3f(0.0, 0.0, 0.0); glLineWidth(1.5f); glPointSize(4); glBegin(GL_POINTS); for( Vertex_iterator pit = polygon.vertices_begin(); pit != polygon.vertices_end(); pit++) { Point_2 p = *pit; glVertex2f(p.x(), p.y()); } Point_2 p = *polygon.vertices_begin(); glVertex2f(p.x(), p.y()); glEnd(); } void testApp::drawPartitions(Polygon_list& partition_polys) { glColor3f(0.0, 0.0, 0.0); glLineWidth(1.5f); for(Polygon_list::iterator i = partition_polys.begin(); i != partition_polys.end(); ++i) { Polygon_2 poly = *i; glBegin(GL_LINE_STRIP); for( Vertex_iterator pit = poly.vertices_begin(); pit != poly.vertices_end(); pit++) { Point_2 p = *pit; glVertex2f(p.x(), p.y()); } Point_2 p = *poly.vertices_begin(); glVertex2f(p.x(), p.y()); glEnd(); glColor4f(1.0, 0.24, 0.58, 0.6); glBegin(GL_POLYGON); for( Vertex_iterator pit = poly.vertices_begin(); pit != poly.vertices_end(); pit++) { Point_2 p = *pit; glVertex2f(p.x(), p.y()); } p = *poly.vertices_begin(); glVertex2f(p.x(), p.y()); glEnd(); } } void testApp::update(){ ofBackground(100,100,100); ofEnableAlphaBlending(); if (!showFreezeImg){ vidGrabber.grabFrame(); colorImg.setFromPixels(vidGrabber.getPixels(), iwidth, iheight); } } void testApp::draw(){ ofSetColor(0xffffff); //colorOut.draw(0, 0, ofGetWidth(), ofGetHeight()); //drawImage(colorOut.getCvImage(), 0, ofGetHeight(), ofGetWidth(), ofGetHeight()); colorImg.draw( 0, 0, ofGetWidth(), ofGetHeight()); //grayImage.draw(360,20); /* ofSetColor(0xff0000); char reportStr[1024]; sprintf(reportStr, "shape/sound mapping\nfps: %f\nthreshold: %d", ofGetFrameRate(), threshold); ofDrawBitmapString(reportStr, 20, 600); */ drawPoly(polygon); drawPartitions(partition_polys); } void testApp::keyPressed (int key){ switch (key){ case 's': vidGrabber.videoSettings(); break; case 'f': if(showFreezeImg) { showFreezeImg = false; } else { showFreezeImg = true; } case ' ': polygon.clear(); partition_polys.clear(); calcPartitions(); } } void testApp::mouseMoved(int x, int y ){} void testApp::mouseDragged(int x, int y, int button){} void testApp::mousePressed(int x, int y, int button){ polygon.push_back(Point_2(x, y)); partition_polys.clear(); calcPartitions(); } void testApp::mouseReleased(){}