Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Basics OpenGL’s primary function – Rendering

Similar presentations


Presentation on theme: "OpenGL Basics OpenGL’s primary function – Rendering"— Presentation transcript:

1 OpenGL Basics OpenGL’s primary function – Rendering
Rendering? – converting geometric/mathematical descriptions of objects into frame buffer values OpenGL can render: Geometric primitives Bitmaps and Images

2 Specifying Geometric primitives
Primitives are specified using glBegin(primType); // define your primitive here glEnd(); primType: GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, …

3 Primitive Types

4 Sample Example Void DrawQuad(Glfloat color[]) { glBegin(GL_QUADS);
glColor3fv(color); glVertex2f(0,0); glVertex2f(1.0, 0,0); glVertex2f(1.0, 1.0); glVertex2f(0.0, 1.0); glEnd(); }

5 OpenGL Command Formats
glVertex2f(x, y) number of Components/ Dimensions 2 – (x,y) 3 – (x,y,z) 4 – (x,y,z,w) or (r,g,b,a) Add ‘v’ for vector form glVertex2fv(v) B – byte ub – unsigned byte s – short us – unsigned short i – int ui – unsigned int f – float d – double

6 Shape Example

7 OpenGL state OpenGL is a state machine
can only be in one of a finite number of states states are sticky once a state is set it is in effect until it is set again OpenGL states are maintained in state variables States are read/write OpenGL defines default states usually to minimize rendering time

8 OpenGL State Variables
All rendering attributes are encapsulated in the OpenGL variables Rendering styles (color, point size, etc) Shading Lighting Texture mapping

9 OpenGL State Variables
Appearance is controlled by current state A typical manipulation of OpenGL states for each (primitive to render) { update OpenGL state (if necessary) render primitive }

10 Example Void DrawQuad(Glfloat color[]) { glBegin(GL_QUADS);
glColor3f(1,0,0); glVertex2f(0,0); glVertex2f(1.0, 0,0); glVertex2f(1.0, 1.0); glVertex2f(0.0, 1.0); glEnd(); }

11 Controlling current state
Setting State glColor3f(…) glPointSize(3) glLineStipple(repeat, pattern) glShadeModel(GL_SMOOTH) Enable Features glEnable(GL_LIGHTING) glEnable(GL_TEXGTURE_2D)

12 OpenGL Interface between the users application and the underlying hardware

13 OpenGL Rendering Pipeline

14 Your First Program 1BlankWindow.exe
Does nothing but open a 256 X 256 pixel screen window Clear the screen in the display() Set the clear color in the init()

15 OpenGL Primitives OpenGL coordinate system:
x increases to the right y increases from bottom to top glutInitWindowSize(256, 256); open a window 256 pixels wide by 256 pixels high visible pixels 0 <= x <= 255, 0 <= y <= 255

16 GLUT Primitives

17 OpenGL state We modified several OpenGL state variables already
glClearColor(0.3f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0f, 256.0f, 0.0f, 256.0f); glColor3f(1.0f, 1.0f, 1.0f);

18 More Simple Programs 2ThreePoints.exe 3RandomPoints
Draws three white points 3RandomPoints Draws 1000 points in random sizes, locations, and colors.

19 Data Types Fortunately, the size of OpenGL’s data types corresponds with SUN’s, g++, and Microsoft Truly portable code does not make this assumption Glbyte, Glshort, Glint, Glfloat, GLdouble

20 Coordinate System Examples have opened a screen window with specified dimensions (in pixels) Changed the color of some of the pixels in the screen window So far, we have just been dealing with pixel coordinates

21 Coordinate System Write a program to plot the function 4FlatLine.exe
for 4FlatLine.exe Is there a problem with the program? Print out the values and see if they make sense

22 Coordinate System The values look reasonable The problem is:
We want to plot points in pixel coordinates but are evaluating the function in fractional pixel coordinates The x value used to evaluate the function is from 0 to 100, which is okay The y value of the function goes from 1 to about 0, which is about 1 pixel!

23 Coordinate System We need to expand the function values so they correspond reasonably to available pixels in our window plot a scaled version of y, where the scale factor is 200 5ProperCurve.exe we are plotting the function: for

24 Coordinate System Now let’s plot a similar function: for 6YFixed.exe

25 Coordinate System We have a similar problem as before, the values of x are given in a fractions of pixels but we want to plot x over an integer range of pixels scale x by 50 before plotting its value 7ProperCurve.exe

26 Coordinate System Now let’s plot a similar function:
for What will the problem be? How is it fixed? 8ProperCurve.exe

27 Coordinate System There are two choices: shift x by 2, scale x by 50
scale x by 50, shift x by 100

28 Coordinate System We need to expand the function values so they reasonably correspond to the available pixels in our window shift x so only positive values are plotted scale x so we use 256 (or some reasonable fraction of 256) scale y so we use 256

29 Coordinate System -1 <= x <= 1
scale x by 1/2: <= x1 <= 0.5 shift x1 by 0.5: <= x2 <= 1.0 scale x2 by 175: 0.0 <= x3 <= 175 shift x3 by 25: <= x4 <= 200

30 Coordinate System In equation form: x varies from -1.0 to 1.0
x1 = x * 0.5 x2 = x == x * x3 = x2 * 175 == (x * ) * 175 x4 = x == (x * ) * x = -1, x4 = 0 x = 1, x4 = 200

31 Coordinate System Or, flipping the last equation around:
x4 = (x * ) * x4 = (((x + 1) / 2) * 175) + 25 Or, flipping the last equation around: x = (((x4 - 25) / 175) * 2) - 1 where 25 <= x4 <= 200 x4 = 25, x = -1 x4 = 200, x = 1

32 Coordinate System That takes care of scaling and shifting x, how about doing the same thing for y y ranges from 0 to 1, and we want to plot y for values between 25 and 200 0 <= y <= 1 scale by 175: 0 <= y1 <= 175 shift y1 by 25: 25 <= y2 <= 200 y2 = y * y = (y2 - 25) / 175, for 25 <= y2 <= 200

33 OpenGL Primitives Lines are communicated to OpenGL:
Using the enumerated type: GL_LINES as the argument to glBegin() specifying the two end points of the line as vertices between the glBegin() and glEnd() function calls

34 OpenGL Primitives GL_LINES GL_LINE_STRIP
Treats each pair of vertices as an independent line segment. Vertices 2n – 1 and 2n define line n N/2 lines are drawn. GL_LINE_STRIP Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n N – 1 lines are drawn.

35 OpenGL Primitives 9Lines.exe GL_LINE_LOOP
Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn. 9Lines.exe

36 OpenGL Primitives Author provides a 2D line drawing of a dinosaur
self contained file the 2D points points are vertices in polylines

37 OpenGL Primitives file format: number of polylines
number of vertices in polyline 1 vertex x1 y1 vertex x2 y2 number of vertices in polyline 2

38 OpenGL Primitives Developed a class to represent a polyline file in memory PolyLineObject class contains data members number of polylines in the object an array of pointers to polylines member functions read a polyline file write a polyline file draw a PolyLineObject using OpenGL

39 OpenGL Primitives Developed a class to represent a single polyline in memory PolyLine class contains data members number of vertices in the polyline an array of vertices, represented as generic Points

40 OpenGL Primitives Generic Point class (class template)
template parameters data type size or the number of dimensions data members number of dimensions array of values member functions constructors, destructor, operator=, operator[] various “shortcut” functions: X(), X(T val), ...

41 OpenGL Primitives number of polygons polyline pointers array
number of vertices vertex array pointer polyline pointer 1 polyline pointer 2 polyline pointer 3 polyline pointer n Point 1: x1, y1 Point 2: x2, y2 Point 3: x3, y3 Point m: xm, ym

42 OpenGL Primitives Write and draw are very similar to each other, and inverses of read each polyline is drawn using: glBegin(GL_LINE_STRIP); animation: move some of the vertices of the polylines and redraw

43 KeyPress Events Used because of limited GUI features (no menubar, sliders, dials, buttons, …) in GLUT simply add a callback to handleKeyPress events big switch statement to determine which key was pressed

44 KeyPress Events 10LineWidth.exe
Draws a line of varying width + add one to the current line width - subtract one from the current line width Esc to quit the program (traditional in GL programs) three arguments to the keyboard callback ASCII code for the key x and y location of the mouse when the key was pressed

45 KeyPress Events the keyboard callback needs to call the display function let GLUT do it: glutPostRedisplay marks the current window for redisplay glutMainLoop calls the display callback on the next iteration through the main loop no enumerated binding for the Ecs key, we need to switch on 27

46 Menu CallBacks GLUT provides cascading pop-up menus
popup menus can be arranged in a hierarchy of menus, sub-menus, sub-sub-menus, … identifiers for all sub-menus must be passed as an argument to the GLUT functions that create the parent menu 11LineColorMenu.exe 12LineColorMenuAndMouse.exe

47 Mouse Events GLUT can be informed of mouse events that occur in your window Button identifiers GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON Buttons have state GLUT_DOWN GLUT_UP

48 Mouse Events The mouse callback takes 4 arguments
int button: which button caused the event int state: GLUT_UP/GLUT_DOWN int x, int y: location in the window where the event occurred The mouse callback responds to mouse events only, not motion events mouse events are good for identifying locations, start/stop drawing (paint prog)

49 Mouse Events 13DrawLineWithMouse1.exe 14DrawLineWithMouse2.exe
Notice that height - mouse_y is used because the mouse_y is from the top of the screen rather than the bottom. glutPostRedisplay() is used to redraw the window. 14DrawLineWithMouse2.exe

50 Motion Event Mouse movement when any button is pressed
application parses the button 15DrawLineWithMouse3.exe Generally used for panning rotating closer/further

51 Joystick Events GLUT has two callbacks for joystick input:
glutJoystickFunc( functionName, pollInterval ); Registers a function with GLUT to call at the pollInterval  if the joystick has changed state. glutForceJoystickFunc() Forces glut to call the function registered with glutJoystickFunc immediately. This method should be placed in a timer to call the joystickFunc at regular intervals. If the joystick is held in a constant state, the joystickFunc is not invoked.

52 Joystick Events The joystick event handler function should have the following signature: joystickEventHandler(unsigned int buttonMask, int x, int y, int z) The joystick buttons are reported by the buttonMask parameter: GLUT_JOYSTICK_BUTTON_A (0x1), GLUT_JOYSTICK_BUTTON_B (0x2), GLUT_JOYSTICK_BUTTON_C (0x4), and GLUT_JOYSTICK_BUTTON_D (0x8)


Download ppt "OpenGL Basics OpenGL’s primary function – Rendering"

Similar presentations


Ads by Google