1 / 23

CS430 Computer Graphics

CS430 Computer Graphics. Graphics Programming and OpenGL. Topics. Get Started with Graphics Programming OpenGL Drawing Basic Primitives Interaction. Get Started with Graphics Programming. What are required? Software Graphics library (libraries) Hardware Display to show graphics

anika-combs
Télécharger la présentation

CS430 Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Get Started with Graphics Programming • What are required? • Software • Graphics library (libraries) • Hardware • Display to show graphics • Input device(s) for interaction • Programmer’s passion (and patient :) • willing to spend time writing and testing programs to produce different graphics with different parameters (i.e., doing experiments) to master graphics programming

  4. Get Started with Graphics Programming • Steps • Initialization • Set up display mode (graphics mode) • Set up coordinate system • Coordinates x and y in (x, y) measured in pixels • Basic primitives • point(x,y,color), line(x1,y1,x2,y2), moveto(x,y), lineto(x,y), and cp: current pen position

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

  6. OpenGL • Tool to support device-independent graphics programming • Same program works on a different environment with OpenGL library installed • Examples • X-Windows – UNIX – SUN • X-Windows – Linux – PC (MESA) • MS-Windows - PC • API, underlying hardware/OS details hidden

  7. OpenGL • Powerful 3D graphics drawing • Suitable for 2D graphics, too • Unified approach to producing pictures • Libraries • GLU: OpenGL Utility Library • Routines using lower-level OpenGL commands • Part of OpenGL • GLUT: OpenGL Utility Toolkit • Window-system-independent toolkit • Written by Mark Kilgard

  8. Window-Based Programming • Event-driven programming • Event queue receives messages • Callback functions created for events and executed when events occur

  9. Window-Based Programming • 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. Opening a window for drawing //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 }

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

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

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

  14. Command Suffixes and Data Types

  15. Example on Data Type • 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. States (Attributes) • Set vertex size • glPointSize(size) • Set primitive (foreground) color • glColor3f(red, green, blue) • E.g., glColor3f(0.0, 0.0, 0.0) • Set background color • glClearColor(red, green, blue, alpha) • Clear entire window to background color • glClear(GL_COLOR_BUFFER_BIT)

  17. Coordinate System • 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. 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. More Primitives • glBegin(constant) vertices glEnd(); defines various primitives with different values of constant • GL_POLYGON: filled polygon • GL_TRIANGLES: triangles • GL_QUAS: quadrilaterals :

  20. Interaction • Mouse • glutMouseFunc(myMouse) • Motion • glutMotionFunc(myMovedMouse) • Keyboard • glutKeyboardFunc(myKeyboard)

  21. Mouse • Prototype of myMouse • void myMouse(int button, int state, int x, int y) • button • GLUT_LEFT_BUTTON • GLUT_MIDDLE_BUTTON • GLUT_RIGHT_BUTTON • state • GLUT_UP • GLUT_DOWN • (x, y) • Location measured in pixel from upper left corner

  22. Motion • Prototype of myMotion • void myMovedMouse(int x, int y) • (x, y) • Location measured in pixel from upper left corner

  23. Keyboard • Prototype of myKeyboard • void myKeyboard(unsigned int key, int x, int y) • key • ASCII value of the key pressed • (x, y) • Location of the mouse from upper left corner when the key is pressed

More Related