Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL.

Similar presentations


Presentation on theme: "Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL."— Presentation transcript:

1 Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL

2 2 Topics l Get Started with Graphics Programming l OpenGL l Drawing Basic Primitives l Interaction

3 3 Get Started with Graphics Programming l What are required? zSoftware yGraphics library (libraries) zHardware yDisplay to show graphics yInput device(s) for interaction zProgrammer’s passion (and patient :) ywilling to spend time writing and testing programs to produce different graphics with different parameters (i.e., doing experiments) to master graphics programming

4 4 Get Started with Graphics Programming l Steps zInitialization ySet up display mode (graphics mode) ySet up coordinate system –Coordinates x and y in (x, y) measured in pixels zBasic primitives ypoint(x,y,color), line(x1,y1,x2,y2), moveto(x,y), lineto(x,y), and cp: current pen position

5 5 Device-Independent Programming l Why device-independent programming? zIt’s hard to port one graphics application from one environment to another yDifferent commands required to drive different displays yDifferent libraries zDevice-independent programming make the task of porting (same program: compile  run  identical output) easy

6 6 OpenGL l Tool to support device-independent graphics programming zSame program works on a different environment with OpenGL library installed zExamples yX-Windows – UNIX – SUN yX-Windows – Linux – PC (MESA) yMS-Windows - PC l API, underlying hardware/OS details hidden

7 7 OpenGL l Powerful 3D graphics drawing l Suitable for 2D graphics, too l Unified approach to producing pictures l Libraries zGLU: OpenGL Utility Library yRoutines using lower-level OpenGL commands yPart of OpenGL zGLUT: OpenGL Utility Toolkit yWindow-system-independent toolkit yWritten by Mark Kilgard

8 8 Window-Based Programming l Event-driven programming zEvent queue receives messages zCallback functions created for events and executed when events occur

9 9 Window-Based Programming l Skeleton of even-driven OpenGL program void main() { initialize things Create a screen window glutDisplayFunc(myDisplay);//register the redraw function glutReshapeFunc(myReshape);//register the reshape function glutMouseFunc(myMouse);//register the mouse action function glutKeyboardFunc(myKeyboard);//register the keyboard action function perhaps initialize other things glutMainLoop();//enter the unending main loop } all of the callback functions are defined here

10 10 //appropriate #includes go here -see Appendix 1 void main(int argc, char** argv) { glutInit(&argc, argv);//initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode glutInitWindowSize(640, 480); //set window size glutInitWindowPosition(100, 150); //set the window position on screen glutCreateWindow("my first attempt"); //open the screen window //register the callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit(); //additional initializations as necessary glutMainLoop(); //go into a perpetual loop } Opening a window for drawing

11 11 Window-Based Programming l “my first attempt” window l Note that (0, 0) is at the lower left corner

12 12 Drawing Basic Primitives l Vertices glBegin(GL_POINTS); glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd();

13 13 Format of OpenGL Commands l Example: A GLU library routine begin with glu A GLUT library routine begins with glut

14 14 Command Suffixes and Data Types

15 15 Example on Data Type l Encapsulating OpenGL details in the generic function drawDot() void drawDot(int x,int y)  danger: passes ints {//draw dot at integer point (x,y) glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); } void drawDot(GLint x,GLint y)// OK here {//draw dot at integer point (x,y) glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); }

16 16 States (Attributes) l Set vertex size zglPointSize(size) l Set primitive (foreground) color zglColor3f(red, green, blue) zE.g., glColor3f(0.0, 0.0, 0.0) l Set background color zglClearColor(red, green, blue, alpha) l Clear entire window to background color zglClear(GL_COLOR_BUFFER_BIT)

17 17 Coordinate System l Set up in myInit() void myInit(void) { glClearColor(1.0, 1.0, 1.0, 0.0) glColor3f(0.0, 0.0, 0.0) glPointSize(4.0) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,640.0,0,480.0); }

18 18 What should be in myDisplay? void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT) glBegin(GL_POINTS); glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush();// send all output to display }

19 19 More Primitives l glBegin(constant) vertices glEnd(); defines various primitives with different values of constant zGL_POLYGON: filled polygon zGL_TRIANGLES: triangles zGL_QUAS: quadrilaterals :

20 20 Interaction l Mouse zglutMouseFunc(myMouse) l Motion zglutMotionFunc(myMovedMouse) l Keyboard zglutKeyboardFunc(myKeyboard)

21 21 Mouse l Prototype of myMouse zvoid myMouse(int button, int state, int x, int y) zbutton yGLUT_LEFT_BUTTON yGLUT_MIDDLE_BUTTON yGLUT_RIGHT_BUTTON zstate yGLUT_UP yGLUT_DOWN z(x, y) yLocation measured in pixel from upper left corner

22 22 Motion l Prototype of myMotion zvoid myMovedMouse(int x, int y) z(x, y) yLocation measured in pixel from upper left corner

23 23 Keyboard l Prototype of myKeyboard zvoid myKeyboard(unsigned int key, int x, int y) zkey yASCII value of the key pressed z(x, y) yLocation of the mouse from upper left corner when the key is pressed


Download ppt "Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL."

Similar presentations


Ads by Google