/* NURB curve (1-dimension) Written by Caroline Geiersbach and Scott D. Anderson scott.anderson@acm.org Summer 2003 */ #include #include #include #include #include "tw.h" //for picking int count; int p; //number of points the user wants int numPoints; //specify all control points here twTriple points[15] = { {-7,-3,1}, {-6,-3,1}, {-5,0,1}, {-4,-5,1}, {-3,5,1}, {-2,-3,1}, {-1,3,1},{0,-16,1}, {1,0,1},{2,-3,1}, {3,-10,1}, {4,13,1},{5,-10,1}, {6,-3,1},{7,-4,1}, }; void drawCurve(twTriple* p, int numPoints, int color) { int i; glMap1f(GL_MAP1_VERTEX_3,0,1,3,numPoints,(const GLfloat*) p); twColorName(color); glBegin(GL_LINE_STRIP); for(i=0; i<=30; i++) glEvalCoord1f((GLfloat)i/30.0); glEnd(); } void display(void) { int i; glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); twCamera(); twColorName(TW_GRAY); glLineWidth(1); glBegin(GL_LINE_STRIP); //lines between points for(i=0;iscreen[0]-4 && yscreen[1]-4) return i; } return 15; } void pick(int x, int y, int element) { if(p<15) { twTriple world; //new coords for element twTriple A,B,V; twTriple winA = {x,y,0}; twTriple winB = {x,y,1}; twUnProject(A,winA); twUnProject(B,winB); twVector(V,B,A); twPointOnLine(points[element],A,V,0.5); } } static int mouse_x, mouse_y; void myMouse(int button, int state, int x, int y) { y=650-y; if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){ mouse_x=x; mouse_y=y; p = testClick(x,y); } } void myMotion(int x, int y) { y=650-y; pick(x,y,p); glutPostRedisplay(); } void printCoords(unsigned char key, int x, int y) { int i; printf("Your current control points are: \n"); for(i=0; i