140 likes | 352 Views
Circles, Ellipses, Curves. Soon Tee Teoh CS 116A. Circle. Circle equation: (x-xc) 2 + (y-yc) 2 = r 2 Can use mid-point algorithm to rasterize circle Useful to use polar coordinates: x = xc + r cos q y = yc + r sin q Sweep through q to draw circle: glBegin(GL_LINE_STRIP);
E N D
Circles, Ellipses, Curves Soon Tee Teoh CS 116A
Circle • Circle equation: (x-xc)2 + (y-yc)2 = r2 • Can use mid-point algorithm to rasterize circle • Useful to use polar coordinates: x = xc + r cos q y = yc + r sin q • Sweep through q to draw circle: glBegin(GL_LINE_STRIP); for (theta=0; theta<TWO_PI; theta+=0.01) { glVertex3f(xc+r*cos(theta),yc+r*sin(theta),0.0); } glEnd(); • Exercise: • How do you draw a filled circle using GL_TRIANGLES?
Bresenham’s Circle Algorithm • Adapt Line Algorithm to Circle • Start with first quadrant • Check the value of fcirc(x,y) to determine next pixel: end start fcirc(x,y) = x2 + y2 - r2
Ellipse • All points on an ellipse satisfies: • What’s the ellipse equation? d1 + d2 = constant d2 d1
Ellipse • Axis-aligned ellipse y ry 2 2 y - yc yc x - xc rx + = 1 rx ry x xc
Conic Section • Circles and ellipses belong to a class called “conic sections”
Conic Section • Conic section equation: • Discriminant B2 – 4AC Ax2 + By2 + Cxy + Dx + Ey + F = 0 < 0, generates ellipse = 0, generates parabola > 0, generates hyperbola
Parabola • A parabola is the set of all points in the plane equidistant from a given plane (called the directrix) and a given point not on the line (called the focus). • For a parabola opening to the right with vertex at (0, 0), the equation in Cartesian coordinates is:
Other curves • Hyperbola • Polynomials y = f(x) • x can go up to the nth degree • Splines: control points
GLUI notes #include <time.h> clock_t old_time; float delay = 1.0; void playbutton_cb(int id) { playing = id; glutSetWindow(win); glutPostRedisplay(); } void myGlutIdle( void ) { clock_t new_time; new_time = clock(); if (((double)(new_time-old_time)/(double)CLOCKS_PER_SEC)>delay) { old_time = new_time; if (playing) { // if playing flag is set, then update graphics here ... glutSetWindow(win); glutPostRedisplay(); } } } int main() { win = glutCreateWindow(“Game”); ... dynamics_glui = GLUI_Master.create_glui( "Animation", 0, controlx2, controly2 ); GLUI_Panel *dynamics_panel1 = dynamics_glui->add_panel( "", GLUI_PANEL_NONE ); GLUI_Panel *dynamics_animation_panel = dynamics_glui->add_panel_to_panel ( dynamics_panel2, "Animation" ); dynamics_glui->add_button_to_panel(dynamics_animation_panel, "Play", 1, playbutton_cb); dynamics_glui->add_button_to_panel(dynamics_animation_panel, “Stop", 0, playbutton_cb); … GLUI_Master.set_glutIdleFunc( myGlutIdle ); } Set current window to the display window Argument to the callback function Called every time button is pressed
GLUT Window Resizing int main(int argc, char* argv[]) { // ********* GLUT stuff ******************************************** glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); side_window = glutCreateWindow( "BGPViz" ); glutPositionWindow( 10 , 10 ); // starting x, y of window glutReshapeWindow( 512, 512 ); // x, y size of window glutDisplayFunc( sideGlutDisplay ); glutReshapeFunc( sideGlutReshape ); glutKeyboardFunc( sideGlutKeyboard ); glutMotionFunc( sideGlutMotion ); glutMouseFunc( sideGlutMouse ); // ********* GLUI stuff ********************************************* glui = GLUI_Master.create_glui( "Control Panel", 0, 520,20 ); /* name, flags, x, and y */ /*** Add invisible panel to hold rest of controls ***/ GLUI_Panel *panel1 = glui->add_panel( "", GLUI_PANEL_NONE ); /*** Start a new column in this panel ***/ glui->add_column_to_panel(panel1, false); /* 'false' means don't draw bar */ glui->add_button_to_panel(panel1, "Clear", 0, clear_cb); mode_chkbx[0] = glui->add_checkbox_to_panel(panel1, "OpenGL Line", &(DL.mode), 0, mode_cb); mode_chkbx[1] = glui->add_checkbox_to_panel(panel1, "DDL", &(DL.mode), 1, mode_cb); mode_chkbx[2] = glui->add_checkbox_to_panel(panel1, "Bresenham", &(DL.mode), 2, mode_cb); mode_cb(0); GLUI_Master.set_glutIdleFunc( myGlutIdle ); // ********* GLUT stuff ******************************************** glutMainLoop(); return 0; } This function called whenever the window size is changed.
GLUT Window Resizing 512 • Consider the following code int winx = 512; int winy = 512; void display_func() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,1.0,0.0,1.0); // left, right, bottom, top glViewport(0,0,winx,winy); // startx, starty, xsize, ysize glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawSquare(0.3,0.3,0.5); // startx, starty, size } void reshape_func(int x, int y) { winx = x; winy = y; } int main() { window = glutCreateWindow( “Hex" ); glutPositionWindow( 10 , 10 ); // starting x, y of window glutReshapeWindow( winx, winy); // x, y size of window glutDisplayFunc( display_func ); glutReshapeFunc( reshape_func ); … } 512 User resizes window 496 270
GLUT Window Resizing 512 • Now consider the following code int winx = 512; int winy = 512; void display_func() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,1.0,0.0,1.0); // left, right, bottom, top glViewport(0,0,winx,winy); // startx, starty, xsize, ysize glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawSquare(0.3,0.3,0.5); // startx, starty, size } void reshape_func(int x, int y) { winx = x; winy = y; if (winy < winx) winx = winy; if (winx < winy) winy = winx; } int main() { window = glutCreateWindow( “Hex" ); glutPositionWindow( 10 , 10 ); // starting x, y of window glutReshapeWindow( winx, winy); // x, y size of window glutDisplayFunc( display_func ); glutReshapeFunc( reshape_func ); … } 512 User resizes window 496 270
GLUT Window Resizing 512 • Alternatively … 512 Exercise: How would you write code to do this? User resizes window 496 270