Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.

Similar presentations


Presentation on theme: "OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects."— Presentation transcript:

1 OpenGL (I)

2 What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.

3 Interactive computer graphics system that allows us to access graphics hardware Easy to use Programs run efficiently Hardware-independent Graphics API (Application Programming Interface) A library of functions Others: DirectX (Microsoft), Java3D What is OpenGL (OGL)?

4 OGL is independent of any specific window system  basic window operations are not included Graphics user interface programming: GLUT = OGL Utility Toolkit Simple but easy to use What is OpenGL (OGL)?

5 Programming Environment OGL is usually installed on a MS Windows machine. Programming environment MS Visual Studio.Net 2003 Libraries we will use OGL (basic API tool) GLU (OGL Utility Library) GLUT (OGL Utility Toolkit, a windowing toolkit that handles window system operations)

6 OGL Data Types To make more portable, OGL provides internal data types

7 Basic OGL Syntax Functions are prefixed with gl, glBegin, glClear, glClearColor Constants in capital letters, and the underscore is used as a separator GL_2D, GL_LINES, GL_TRIANGLES Built-in data-type names begin with GL GLbyte, GLshort, GLint, GLboolean

8 First OGL Program: DrawDots

9 Our first program is not interactive. It consists of three functions: main, myDisplay, myInit. (see accompanying demo program) int 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 window position glutCreateWindow("my first attemp"); // open the screen window glutDisplayFunc(myDisplay); // register redraw function myInit();// additional initializations as necessary glutMainLoop();// go into a perpetual loop }

10 Creating Window for Drawing The first five function calls use GLUT to open a window for drawing Coordinate system in OGL sx sy (150, 100)

11 First OGL Program: DrawDots Draw primitives Display callback function myDisplay void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); // clear the screen glBegin(GL_POINTS); glVertex2i(100, 50); // draw three dots glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush();// send all output to display }

12 First OGL Program: DrawDots Draw primitives glBegin(GLenum mode) mode can be GL_POINTS, GL_LINES, GL_POLYGON, etc.

13 First OGL Program: DrawDots Initialization void myInit() { glClearColor(1.0, 0.0, 0.0, 0.0); // set red background color glColor3f(0.0, 1.0, 0.0); // set the drawing color glPointSize(10.0); // a 'dot' is 10 by 10 pixels // The following lines establish the coordinate system. // Details will be covered later. glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 640, 0, 480); }

14 First OGL Program: DrawDots OpenGL is a state machine It keeps track of many state variables Current size of a point, current color of drawing, current background color, etc. Color of drawing is specified using glColor3f(red, green, blue) ; (range: [0, 1]) Background color is set with glClearColor(red, green, blue, alpha). glClear clears the entire window to the background color (cf. myDisplay ). The value of a state variable remains active until a new value is given.

15 Line Drawing glLineWidth(2.0);// set line thickness glBegin(GL_LINES); glVertex2i(10, 20);// first horizontal line glVertex2i(40, 20); glVertex2i(20, 10);// first vertical line glVertex2i(20, 40); // four more calls to glVertex here for the other two lines glEnd();

16 Drawing Modes glBegin(GLenum mode)

17 Skeleton of an interactive OGL program using GLUT int 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(150, 100); // set window position 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 }

18 Event-driven Programming & Callbacks Event-driven programming A property of most window-based programs The programs respond to various events Click of a mouse, resizing of a window, etc. The system automatically manages an event queue Deal with events first-come, first-served Programmers write functions (called callback functions) that will be called when events occur.

19 Event-driven Programming & Callbacks Callback functions in GLUT For example, glutMouseFunc(myMouse) Register function myMouse() as the function to be executed when a mouse event occurs. Prefix glut indicates that this function is part of GLUT If a particular program does not make use of a mouse, the corresponding callback function need not be registered.

20 Event-driven Programming & Callbacks glutDisplayFunc(myDisplay) Whenever the system determines that a window should be redrawn, it issues a “redraw” event. Redraw event happens When the window is first opened When the window is exposed by moving another window off it When your program explicitly requests that it be redrawn (through the call glutPostRedisplay ) (useful for animation) myDisplay() is registered as the callback function for a redraw event. Coordinate system in OpenGL

21 Event-driven Programming & Callbacks glutReshapeFunc(myReshape) Screen windows can be resized by the user, usually by dragging a corner of the window to a new position with the mouse. myReshape(int width, int height) is automatically passed arguments reporting the new width and height of the reshaped window. Coordinate system in OpenGL

22 Event-driven Programming & Callbacks glutMouseFunc(myMouse) When one of the mouse button is pressed or released, a mouse event is issued. myMouse(int button, int state, int x, int y) is automatically passed arguments describing the location of the mouse and the nature of the action initiated by pressing the button. Coordinate system in OpenGL

23 Event-driven Programming & Callbacks glutKeyboardFunc(myKeyboard) Keyboard event is issued when some key is pressed or released. myKeyboard(unsigned char key, int x, int y) is automatically passed arguments that tell which key was pressed. It is also passed the data indicating the location of the mouse at the time the key was pressed. Coordinate system in OpenGL

24 Summary of Common Callbacks

25 Mouse Interaction Let’s see a demo for mouse & keyboard callbacks glutMouseFunc(myMouse) Mouse button is pressed or released. glutMotionFunc(myMouse) Mouse is moved while one of the buttons is pressed. void myMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { glBegin(GL_POINTS); glVertex2i(x, screenHeight - y); glEnd(); glFlush(); } Note the y value is the number of pixels down from the top of the window!

26 Mouse Interaction Mouse motion example “freehand” drawing with a fat brush See second demo program void myMovedMouse(int mouseX, int mouseY) { GLint x = mouseX; GLint y = screenHeight - mouseY;// flip it as usual GLint brushSize = 20; glRecti(x, y, x + brushSize, y + brushSize); glFlush(); }

27 Keyboard Interaction void myKeyboard(unsigned char key, int mouseX, int mouseY) { GLint x = mouseX; GLint y = screenHeight - mouseY;// flip it as usual switch(key) // value of key is the ASCII value of the key pressed { case 'p':// draw a dot at the mouse position glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush(); break; case 'E': exit(-1); // terminate the program }

28 Other GLUT Techniques Recommended GLUT tutorial http://www.lighthouse3d.com/opengl/glut/ Topics we didn’t cover but useful Animation: void glutIdleFunc(void (*func)(void)) Popup menus: int glutCreateMenu(void (*func)(int value)); Bitmap fonts: void glutBitmapCharacter(void *font, int character) Sub windows: int glutCreateSubWindow(int parentWindow, int x, int y, int width, int height); Recommended reading material Chapter 1 & 2 of OpenGL red bookOpenGL red book

29 Recommended Resources OpenGL official Website OpenGL Utility Toolkit (GLUT) (download GLUT) OpenGL Utility Toolkit (GLUT) Nice GLUT Tutorial NEHE: OpenGL tutorial NEHE OpenGL red book


Download ppt "OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects."

Similar presentations


Ads by Google