Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO OPENGL

Similar presentations


Presentation on theme: "INTRODUCTION TO OPENGL"— Presentation transcript:

1 INTRODUCTION TO OPENGL

2 INTRODUCTION TO OPENGL
What is OpenGL API Functions Event Driven Programming Using OpenGL

3 OpenGL OpenGL is a platform-independent API that is Easy to use
Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies

4 OpenGL Interface Components of the OpenGL Interface
GL: core OpenGL functions GLU: graphics utility library (helpers for creating common objects, eg. Spheres) GLUT: GL Utility Toolkit (interface to windowing system) GLX: low-level interface to X Windows

5 Basic OpenGL Syntax Function names from GL library Symbolic constants
prefixed with gl Symbolic constants Capital letters with underscore OpenGL built-in data types glBegin, glClear, glCopyPixels GL_2D, GL_RGB, GL_POLYGON GLbyte, GLshort, Gint, GLfloat, GLdouble, GLboolean

6 GLUT OpenGL Utility Toolkit (GLUT)
Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars

7 GLUT Function names from GLUT library
prefixed with glut void glutInit (int argc, char ** argv) Initializes GLUT and should be called before any OpenGL functions. void glutCreateWindow (*char title) Creates a window on the screen with the title given by the argument.

8 Event Loops & Callback Function
Events – mouse, keyboard, windows events. Callback function – define how the program should react to specific events. void glutDisplayFunc (void (*func) (void)) The function func() is called each time there is a display callback

9 Event Loops & Callback Function
void glutMainLoop() Causes the program to enter an event-processing loop. Should be the last function in main().

10 OpenGL function format
function name dimensions glVertex3f(x,y,z) x,y,z are floats belongs to GL library

11 A Simple Program #include <GL/glut.h> void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop();

12 A Simple Program

13 Event Loop Note that the program defines a display callback function named mydisplay Every glut program must have a display callback The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened The main function ends with the program entering an event loop

14 USING RGB COLOR #include <GL\glut.h> void display() { glClear (GL_COLOR_BUFFER_BIT); glColor3f(1.0, 0.0, 0.0); glBegin (GL_POLYGON); glVertex2f (-0.5, -0.5); glVertex2f (-0.5, 0.5); glVertex2f (0.5, 0.5); glVertex2f (0.5, -0.5); glEnd (); glFlush (); } int main(int argc, char** argv) glutInit (&argc, argv); glutCreateWindow (“Simple"); glClearColor(1.0, 1.0, 1.0, 0.0); glutDisplayFunc (display); glutMainLoop ();

15 USING RGB COLOR Set a particular color: glColor3f(r, g, b);
Set a background color: glClearColor(r, g, b, 1); Clear the window to background color: glClear(GL_COLOR_BUFFER_BIT);

16 USING RGB COLOR

17 Program Structure Most OpenGL programs have a similar structure that consists of the following functions main(): defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement) init(): sets the state variables Viewing Attributes callbacks Display function Input and window functions

18 main #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); }

19 GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window (the rendering context) RGB color Single buffering Properties logically ORed together glutWindowSize in pixels glutWindowPosition from top-left corner of display glutCreateWindow create window with title “simple” glutDisplayFunc display callback glutMainLoop enter infinite event loop

20 Coordinate Systems The units in glVertex are determined by the application and are called object or problem coordinates The viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the image Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates OpenGL also uses some internal representations that usually are not visible to the application

21 Transformations and Viewing
In OpenGL, projection is carried out by a projection matrix (transformation) There is only one set of transformation functions so we must set the matrix mode first glMatrixMode (GL_PROJECTION) Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

22 Two- and three-dimensional viewing
In glOrtho(left, right, bottom, top, near, far) the near and far distances are measured from the camera Two-dimensional vertex commands place all vertices in the plane z=0 If the application is in two dimensions, we can use the function gluOrtho2D(left, right,bottom,top) In two dimensions, the view or clipping volume becomes a clipping window

23 OpenGL Primitives GL_POINTS GL_LINE_STRIP GL_POLYGON GL_LINES
GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN [Edward Angel, Interactive computer Graphics, 2009]

24 Event Driven Programming
Mouse Event Event that occurs when the mouse button is pressed or released. Mouse Motion Event Event that occurs when the mouse is moved while one of the buttons is pressed.

25 Mouse Event Contains the following information:
Button: The mouse button that is pressed – left, middle, right. State: The state of the button – up, down. Position: The position of the mouse when the event occurs (x, y).

26 Mouse Event Registering with MouseEvent: glutMouseFunc(myMouse);
Call-back function: void myMouse(int button, int state, int x, int y) Values for button: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON Values for state: GLUT_UP GLUT_DOWN x, y: screen coordinates of mouse position (origin at top-left corner)

27 Mouse Motion Event Contains the following information:
Position: The current position of the mouse as the mouse is being dragged holding one of the buttons pressed. The events are continuously generated as the mouse button is pressed and dragged.

28 Mouse Motion Event Registering with MouseMotionEvent:
glutMotionFunc(myMovedMouse); Call-back function: void myMovedMouse(int x, int y) x, y: screen coordinates of mouse position (origin at top-left corner)

29 Keyboard Event Contains the following information:
key: The ASCII value of the key pressed. Position: The current position of the mouse when the key is pressed.

30 Keyboard Event Registering with KeyboardEvent:
glutKeyboardFunc(myKeyboard); Call-back function: void myKeyboard(unsigned int key, int x, int y) Values for keys: ASCII values for normal keys GLUT_KEY_LEFT, GLUT_KEY_RIGHT, … (arrow keys) x, y: screen coordinates of mouse position (origin at top-left corner)

31 Window Events The Window Redraw Event occurs whenever the window needs to be redrawn. This happens when the window is first opened and when the window is exposed by moving another window off of it. The Window Reshape Event is generated when the window is resized with the mouse.

32 Window Events Registering with WindowRedrawEvent:
glutDisplayFunc(myDisplay); Registering with WindowReshapeEvent: glutReshapeFunc(Reshape);


Download ppt "INTRODUCTION TO OPENGL"

Similar presentations


Ads by Google