Joined: Tue Mar 27, 2007 10:55 pm Posts: 2272 Location: Earth Has thanked: 39 time Have thanks: 61 time
ConnectDots.c :
Code:
/* * ConnectDots.c * * This program draws straight lines connecting dots placed with mouse clicks. * * Author: Samuel R. Buss * * Software accompanying the book * 3D Computer Graphics: A Mathematical Introduction with OpenGL, * by S. Buss, Cambridge University Press, 2003. * * Software is "as-is" and carries no warranty. It may be used without * restriction, but if you modify it, please change the filenames to * prevent confusion between different versions. * Bug reports: Sam Buss, sbuss@ucsd.edu. * Web page: http://math.ucsd.edu/~sbuss/MathCG * * Usage: * Left click to place a control point. * Maximum number of control points allowed is currently set at 64. * Press "f" to remove the first control point * Press "l" to remove the last control point. * Press escape to exit. */
#define maxNumberOfPoints 64 float ArrayOfPoints[maxNumberOfPoints][2]; int currentNumberPoints = 0;
// Window size in pixels int frameHeight; int frameWidth;
void keysFunction (unsigned char key, int x, int y) { switch (key) {
case 27: // Escape key exit(0); break; } }
void removeFirstPoint() { int i; if ( currentNumberPoints>0 ) { // Remove the first point, slide the rest down currentNumberPoints--; for ( i=0; i<currentNumberPoints; i++ ) { ArrayOfPoints[i][0] = ArrayOfPoints[i+1][0]; ArrayOfPoints[i][1] = ArrayOfPoints[i+1][1]; } } }
// Left button presses place a control point. void myMouseFunc( int button, int state, int x, int y ) { if ( button==GLUT_LEFT_BUTTON && state==GLUT_DOWN ) { float xPos = ((float)x)/((float)(frameWidth-1)); float yPos = ((float)y)/((float)(frameHeight-1));
yPos = 1.0f-yPos; // Flip value since y position is from top row.
// Make big points and wide lines. (This may be commented out if desired.) glPointSize(8); glLineWidth(5);
// The following commands should induce OpenGL to create round points and // antialias points and lines. (This is implementation dependent unfortunately, and // may slow down rendering considerably.) // You may comment these out if you wish. glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
/* * ConnectDots.h * * Author: Samuel R. Buss * * Software accompanying the book * 3D Computer Graphics: A Mathematical Introduction with OpenGL, * by S. Buss, Cambridge University Press, 2003. * * Software is "as-is" and carries no warranty. It may be used without * restriction, but if you modify it, please change the filenames to * prevent confusion between different versions. * Bug reports: Sam Buss, sbuss@ucsd.edu. * Web page: http://math.ucsd.edu/~sbuss/MathCG */
// Function prototypes
void myKeyboardFunc( unsigned char key, int x, int y ); void myMouseFunc( int button, int state, int x, int y );