Upload
phoebe-poole
View
231
Download
4
Tags:
Embed Size (px)
Citation preview
Chi-Cheng Lin, Winona State University
CS430 Computer Graphics
Graphics Programming and
OpenGL
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 programmingSame program works on a different
environment with OpenGL library installedExamples
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 programmingEvent queue receives messagesCallback 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
//appropriate #includes go here -see Appendix 1void 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
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 sizeglPointSize(size)
Set primitive (foreground) colorglColor3f(red, green, blue)E.g., glColor3f(0.0, 0.0, 0.0)
Set background colorglClearColor(red, green, blue, alpha)
Clear entire window to background colorglClear(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 constantGL_POLYGON: filled polygonGL_TRIANGLES: trianglesGL_QUAS: quadrilaterals
:
20
Interaction
MouseglutMouseFunc(myMouse)
MotionglutMotionFunc(myMovedMouse)
KeyboardglutKeyboardFunc(myKeyboard)
21
Mouse
Prototype of myMousevoid 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 myMotionvoid myMovedMouse(int x, int y)(x, y)
Location measured in pixel from upper left corner
23
Keyboard
Prototype of myKeyboardvoid 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