110 likes | 544 Views
OpenGL Lines, Points. Soon Tee Teoh CS 116A. Programming API. OpenGL OpenGL is a C/C++ library. It is an interface to the graphics hardware. It is included in C++ packages. GLUT GLUT manages window in the OS, also manages mouse clicks. GLUI User interface: buttons, check-boxes.
E N D
OpenGL Lines, Points Soon Tee Teoh CS 116A
Programming API • OpenGL • OpenGL is a C/C++ library. It is an interface to the graphics hardware. It is included in C++ packages. • GLUT • GLUT manages window in the OS, also manages mouse clicks. • GLUI • User interface: buttons, check-boxes. • Download GLUT and GLUI yourself. Good training in downloading and installing packages
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; } Set up the display window Set up functions (these functions are automatically called) Give control to GLUT
#include "glut.h" gl.h is automatically included in glut.h #include "glui.h“ // this function is called to display graphics in the window // the window has been created by GLUT // the graphics will be drawn by OpenGL void sideGlutDisplay( void ) { glClearColor(0.0,0.0,0.0,0.0); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,1.0,0.0,1.0); // left, right, bottom, top glViewport(0,0,256,256); // startx, starty, xsize, ysize // coordinates begin from lower left corner of window glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glBegin(GL_QUADS); glVertex3f(0.2,0.2,0.0); glVertex3f(0.2,0.8,0.0); glVertex3f(0.8,0.8,0.0); glVertex3f(0.8,0.2,0.0); glEnd(); glutSwapBuffers(); } These are all OpenGL code Set up viewing Draw a rectangle Flush to screen
Setting up viewing in the windowThis is the job of OpenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,10.0,0.0,10.0); // left, right, bottom, top glViewport(0,0,256,256); // startx, starty, xsize, ysize // coordinates begin from lower left corner of window Window created by GLUT Viewport (5.0,2.0) 256 pixels 256 pixels
Draw a Point in OpenGL • 1. Set up Point attributes: • Color, size • 2. Draw the Point by specifying coordinates Viewport 1.0 0.0 1.0 glBegin(GL_POINTS); glPointSize(2); glColor3f(1.0,0.0,0.0); glVertex3f(0.1,0.5,0.0); glPointSize(5); glColor3f(0.0,0.0,1.0); glVertex3f(0.2,0.8,0.0); glEnd(); glViewport(0.0,1.0,0.0,1.0); // left, right, bottom, top 0.0
Draw a Line in OpenGL • 1. Set up Line attributes: • Color, width • 2. Draw the Line by specifying coordinates Viewport 1.0 0.0 1.0 glBegin(GL_POINTS); glLineWidth(2); glColor3f(1.0,0.0,0.0); glVertex3f(0.1,0.5,0.0); glVertex3f(0.2,0.8,0.0); glEnd(); glViewport(0.0,1.0,0.0,1.0); // left, right, bottom, top 0.0
glLineStrip • Draws a poly-line (3,5) glLineWidth(1) glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_STRIP); glVertex2i(1,1); glVertex2i(2,3); glVertex2i(4,4); glVertex2i(3,5); glEnd(); (4,4) (2,3) (1,1)
Handling Mouse Click 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; } • Handled by GLUT The function that you specify here is automatically called when mouse click detected.
Mouse function Window created by GLUT void sideGlutDisplay( void ) { glClearColor(0.0,0.0,0.0,0.0); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,1.0,0.0,1.0); // left, right, bottom, top glViewport(0,0,512,512); // startx, starty, xsize, ysize // coordinates begin from lower left corner of window glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glPointSize(2); glColor3f(1.0,0.0,0.0); glBegin(GL_POINTS); glVertex3f(gx,gy,0.0); glEnd(); glutSwapBuffers(); } 512 512 float gx, gy; void sideGlutMouse(int button, int state, int x, int y ) { if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) { gx = (float)x/512.0; gy = (float)(512-y)/512.0; } sideGlutDisplay(); glutPostRedisplay(); }
Mouse function Window created by GLUT void sideGlutDisplay( void ) { glClearColor(0.0,0.0,0.0,0.0); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,1.0,0.0,1.0); // left, right, bottom, top glViewport(0,0,512,512); // startx, starty, xsize, ysize // coordinates begin from lower left corner of window glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glPointSize(2); glColor3f(1.0,0.0,0.0); glBegin(GL_POINTS); glVertex3f(gx,gy,0.0); glEnd(); glutSwapBuffers(); } 45 x 512 503 512 503 45 float gx, gy; void sideGlutMouse(int button, int state, int x, int y ) { if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) { gx = (float)x/512.0; gy = (float)(512-y)/512.0; } sideGlutDisplay(); glutPostRedisplay(); } 0.982 0.912