#include #include #include /*******************************************************/ /* */ /* In this demo students try to set the hermitian */ /* parameters to match a "standard" curve. */ /* */ /* Run the program and type 'h' to see instructions */ /* on user interface. */ /********************************************************/ // function prototypes void display(void); void reshape(int width, int height); void keyboard(unsigned char key, int x, int y); void motion(int x, int y); void init(void); float xVal[4]={0, 10, 0, 0}; float yVal[4]={0, 0, 0, 0}; int drawVal = 2; float zoom = 1; int main(int argc, char **argv) { // set up window glutInitWindowSize(400, 400); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Hermitian Curves"); // register callback functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); // initalize opengl parameters init(); keyboard('i',0,0); keyboard('h',0,0); // loop until something happens glutMainLoop(); return 0; } void init() { // initialize viewing system glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-20,20,-20,20) ; glMatrixMode(GL_MODELVIEW); // shading model glEnable(GL_SMOOTH); // initialize background color to black glClearColor(0.0,0.0,0.0,0.0); } void reshape(int newWidth, int newHeight) { newWidth; newHeight; // preserve aspect ratio if (newWidth < newHeight) glViewport(0,0,newWidth,newWidth); else glViewport(0,0,newHeight,newHeight); } void display() { float hermitian[4][4]={{2, -2, 1, 1}, {-3, 3, -2, -1}, {0, 0, 1, 0}, {1, 0, 0, 0}}; int i, j; float t,tPrime; const float samples=50; float xCoeff[4]; float yCoeff[4]; // clear buffers glClear(GL_COLOR_BUFFER_BIT ); // clear modelview matrix glLoadIdentity(); // draw axis glColor3f(1,1,1); glBegin(GL_LINES); glVertex2f(0,100); glVertex2f(0,-100); glVertex2f(-100,0); glVertex2f(100,0); glEnd(); // now draw curves glScalef(zoom,zoom,1); if (drawVal == 0 || drawVal == 2) { // compute current coefficients for (i=0; i<4; i++) { xCoeff[i]=0; for (j=0;j<4;j++) xCoeff[i] += hermitian[i][j]*xVal[j]; } // compute current coefficients for (i=0; i<4; i++) { yCoeff[i]=0; for (j=0;j<4;j++) yCoeff[i] += hermitian[i][j]*yVal[j]; } glColor3f(0,0,1); glBegin(GL_LINE_STRIP); for (tPrime=0; tPrime<=samples; tPrime++) { t=tPrime/samples; glVertex2f(xCoeff[0]*t*t*t + xCoeff[1]*t*t + xCoeff[2]*t + xCoeff[3], yCoeff[0]*t*t*t + yCoeff[1]*t*t + yCoeff[2]*t + yCoeff[3]); } glEnd(); } // new draw "standard curve" -- students need to match this if (drawVal==1 || drawVal == 2) { float stdXVal[4]={-10, 5, -30, 10}; float stdYVal[4]={7,-8,10, 45}; // compute current coefficients for (i=0; i<4; i++) { xCoeff[i]=0; for (j=0;j<4;j++) xCoeff[i] += hermitian[i][j]*stdXVal[j]; } // compute current coefficients for (i=0; i<4; i++) { yCoeff[i]=0; for (j=0;j<4;j++) yCoeff[i] += hermitian[i][j]*stdYVal[j]; } glColor3f(1,0,0); glBegin(GL_LINE_STRIP); for (tPrime=0; tPrime<=samples; tPrime++) { t=tPrime/samples; glVertex2f(xCoeff[0]*t*t*t + xCoeff[1]*t*t + xCoeff[2]*t + xCoeff[3], yCoeff[0]*t*t*t + yCoeff[1]*t*t + yCoeff[2]*t + yCoeff[3]); } glEnd(); } // draw to screen glutSwapBuffers(); } void keyboard(unsigned char key, int mouseX, int mouseY) { switch (key){ /* case 'a': drawVal = 2; break; case 'b': drawVal = 0; break; case 'r': drawVal = 1; break; */ case '0': cout << "Enter new start point (x,y): " << endl; cin >> xVal[0] >> yVal[0]; break; case '1': cout << "Enter new end point (x,y): " << endl; cin >> xVal[1] >> yVal[1]; break; case '2': cout << "Enter derivative at start (dx,dy): " << endl; cin >> xVal[2] >> yVal[2]; break; case '3': cout << "Enter derivative at end (dx,dy): " << endl; cin >> xVal[3] >> yVal[3]; break; case 'x': case 'X': cout << "Enter x[0], x[1], x'[0], x'[1]: "; cin >> xVal[0] >> xVal[1] >> xVal[2] >> xVal[3]; break; case 'y': case 'Y': cout << "Enter y[0], y[1], y'[0], y'[1]: "; cin >> yVal[0] >> yVal[1] >> yVal[2] >> yVal[3]; break; case 'z': zoom -=1; if (zoom<1) zoom=1; break; case 'Z': zoom +=1; break; case 'h': cout << "Use '0' to change the starting point." << endl; cout << "Use '1' to change the ending point." << endl; cout << "Use '2' to change the derivative at the start." << endl; cout << "Use '3' to change the derivative at the start." << endl;\ cout << "Use z or Z to zoom in or out." << endl; cout << "Use '?' to print current parameters." << endl; cout << "Use 'i' for information." << endl; break; case 'i': cout << "You can change the parameters of the blue hermitian curve." <