2 D Vs. 3 D CS 4 7 3 1 Lecture 2 : � 2D: � 3 D I ntro to 2 D, 3 D, OpenGL and GLUT ( Part I ) � Flat � (x,y,z) values on screen � (x,y) color values on screen � Perspective: objects have distances from viewer � Objects no depth or distance Emmanuel Agu from viewer Creating 3 D 3 D Modeling exam ple: Robot Ham m er � St art wit h 3D shapes ( m odeling) � Basic shapes(cube, sphere, etc), meshes, etc � Scale them (may also stretch them) � Position them (rotate them, translate, etc) hammer A Robot Hammer! � Then, render scene ( realism ) � Perspective � Color and shading � Shadows lower arm � Texture mapping � Fog base � Transparency and blending � Anti-aliasing � Pract ical not e: m odeling and rendering packages being sold ( Maya, 3D st udio m ax, et c) 1
3 D Modeling exam ple: Polygonal Mesh 3 D Effects exam ple: Texturing Original: 424,000 60,000 triangles 1000 triangles triangles (14%). (0.2%) (courtesy of Michael Garland and Data courtesy of Iris Development.) 3 D Effects exam ple: Shadow s OpenGL Basics � OpenGL’s prim ary function – rendering � Rendering? – Convert geom etric/ m athem atical object descriptions into im ages OpenGL can render: � • Geom etric prim itives ( lines, dots, etc) • Bitmap images ( .bmp, .jpg, etc) 2
OpenGL Basics OpenGL: Event - driven Application Program m ing I nterface (API ) Program only responds to events � � Do nothing until event occurs � � Low- level graphics rendering API Example Events: � � Widely used – will be used in this class • m ouse clicks Maxim al portability � • keyboard stroke • w indow resize • Display device independent Programmer: � • W indow system independent based ( W indow s, X, etc) � defines event s • Operating system independent ( Unix, W indow s, etc) � act ions t o be t aken � Event- driven � System: � m aintains an event queue � takes program m er- defined actions OpenGL: Event - driven OpenGL: Event - driven � Sequential program � How in OpenGL? • Start at m ain( ) • Programmer registers callback functions • Perform actions 1, 2, 3…. N • Callback function called when event occurs • End � Exam ple: � Event- driven program • Declare a function myMouse to respond to mouse click • I nitialize • Register it: Tell OpenGL to call it when mouse clicked • W ait in infinite loop • Code? glutMouseFunc(myMouse); • W ait till defined event occurs • Take defined actions World’s m ost popular event- driven program ? � 3
GL Utility Toolkit ( GLUT) GL Utility Toolkit ( GLUT) OpenGL No bells and whistles � � • is window system independent • No sliders • Concerned only with drawing • No dialog boxes • No window management functions (create, resize, etc) • No menu bar, etc • Very portable To add bells and whistles, need other API : � � GLUT: • X window system • Minimal window management: fast prototyping • Apple: AGL • Interfaces with different windowing systems • Microsoft : WGL, etc • Allows easy porting between windowing systems Program Structure GLUT: Opening a w indow � Configure and open window (GLUT) � GLUT used to open window � I nitialize OpenGL state • glutInit(&argc, argv); � Register input callback functions (GLUT) initializes • • Render • glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); • Resize sets display mode (e.g. single buffer with RGB) • • Input: keyboard, mouse, etc • glutInitWindowSize(640,480); � My initialization sets window size (WxH) • • Set background color, clear color, drawing color, point size, • glutInitPosition(100,150); establish coordinate system, etc. sets upper left corner of window • � glutMainLoop( ) • glutCreateWindow(“my first attempt”); • Waits here infinitely till action is selected open window with title “my first attempt” • 4
OpenGL Skeleton GLUT Callback Functions void main(int argc, char** argv){ Register all events your program will react to � / / First initialize toolkit, set display mode and create window Event occurs = > system generates callback � Callback: routine system calls when event occurs � glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); � No registered callback = no action glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); / / … then register callback functions, / / … do my initialization / / .. wait in glutMainLoop for events } GLUT Callback Functions Exam ple: Rendering Callback � GLUT Callback functions in skeleton � Do all your drawing in the display function • glutDisplayFunc(myDisplay): window contents need to be � Called initially and when picture changes (e.g.resize) redrawn � First, register callback in m ain( ) function • glutReshapeFunc(myReshape): called when window is reshaped glutDisplayFunc( display ); • glutMouseFunc(myMouse): called when mouse button is pressed • glutKeyboardFunc(mykeyboard): called when keyboard is Then, im plem ent display function � pressed or released void display( void ) { // put drawing stuff here glutMainLoop( ): program draws initial picture and � ……. glBegin( GL_LINES ); enters infinite loop till event glVertex3fv( v[0] ); glVertex3fv( v[1] ); …………… glEnd(); } 5
References OpenGL Skeleton void main(int argc, char** argv){ Hill, chapter 2 � / / First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); / / … now register callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit( ); glutMainLoop( ); } 6
Recommend
More recommend