OPENGL Open GL resources l www opengl org







































![Open. GL l l GLfloat pos[ ] = {x, y, z, w} • • Open. GL l l GLfloat pos[ ] = {x, y, z, w} • •](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-40.jpg)
![Open. GL l l GLfloat color[ ] = {r, g, b, a} • • Open. GL l l GLfloat color[ ] = {r, g, b, a} • •](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-41.jpg)

![Open. GL l l l l l GLfloat ka[ ] = {r, g, b, Open. GL l l l l l GLfloat ka[ ] = {r, g, b,](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-43.jpg)

- Slides: 44

OPENGL

Open. GL resources: l www. opengl. org l The Red Book, www. glprogramming. com/red/ l Manual pages, www. opengl. org/sdk/docs/man 3/

Open. GL l l l Open. GL core library Open. GL Utility (GLU) Open. GL Utility Toolkit (GLUT) Header files: l #include <GL/gl. h> l #include <GL/glut. h>

#include <GL/glut. h> // (or others, depending on the system in use) void init (void) { gl. Clear. Color (1. 0, 0. 0); Open. GL // Set display-window color to white. gl. Matrix. Mode (GL_PROJECTION); // Set projection parameters. glu. Ortho 2 D (0. 0, 200. 0, 150. 0); } void line. Segment (void) { gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (0. 0, 1. 0); gl. Begin (GL_LINES); gl. Vertex 2 i (180, 15); gl. Vertex 2 i (10, 145); gl. End ( ); gl. Flush ( ); // Clear display window. // Set line segment color to blue. // Specify line-segment geometry. // Process all Open. GL routines as quickly as possible. } void main (int argc, char** argv) { glut. Init (&argc, argv); // Initialize GLUT. glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glut. Init. Window. Position (50, 100); // Set top-left display-window position. glut. Init. Window. Size (400, 300); // Set display-window width and height. glut. Create. Window ("An Example Open. GL Program"); // Create display window. init ( ); glut. Display. Func (line. Segment); glut. Main. Loop ( ); } // Execute initialization procedure. // Send graphics to display window. // Display everything and wait.

Open. GL Point Functions • gl. Vertex*( ); * : 2, 3, 4 i (integer) s (short) f (float) d (double) Ex: gl. Begin(GL_POINTS); gl. Vertex 2 i(50, 100); gl. End(); Ex: int p 1[ ]={50, 100}; gl. Begin(GL_POINTS); gl. Vertex 2 iv(p 1); gl. End();

Open. GL Line Functions • • • GL_LINES GL_LINE_STRIP GL_LINE_LOOP Ex: gl. Begin(GL_LINES); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. End();

Open. GL gl. Begin(GL_LINES); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. End(); GL_LINES GL_LINE_STRIP p 3 p 1 p 2 p 5 p 4 p 1 p 2 p 4 GL_LINE_LOOP p 3 p 5 p 1 p 2 p 4

Open. GL GL_POINTS gl. Begin(GL_POINTS); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. End(); p 3 p 5 p 2 gl. Point. Size(size); size: 1, 2, … Ex: gl. Point. Size(2. 0); // 2 x 2 pixels p 1 p 4

Open. GL Polygon Functions gl. Rect*(x 1, y 1, x 2, y 2); • • • i (integer) s (short) f (float) d (double) v (vector) Ex: gl. Recti(200, 100, 50, 250); (50, 250) (200, 100) int v 1[ ]={200, 100}; int v 2[ ]={50, 250}; gl. Rectiv(v 1, v 2);

Open. GL Ex: gl. Begin (GL_POLYGON); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. Vertex 2 iv(p 6); gl. End(); p 6 p 5 p 1 p 4 p 2 p 3 GL_POLYGON

Open. GL Ex: gl. Begin (GL_TRIANGLES); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. Vertex 2 iv(p 6); gl. End(); p 6 p 5 p 1 p 4 p 2 p 3 GL_TRIANGLES

Open. GL Ex: p 6 gl. Begin (GL_TRIANGLES); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 6); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. End(); p 5 p 1 p 4 p 2 p 3 GL_TRIANGLES p 6 p 5 p 1 p 4 p 2 p 3 GL_TRIANGLE_STRIP p 5 p 1 p 4 p 2 p 3 GL_TRIANGLE_FAN

Open. GL Ex: gl. Begin (GL_QUADS); gl. Vertex 2 iv(p 1); gl. Vertex 2 iv(p 2); gl. Vertex 2 iv(p 3); gl. Vertex 2 iv(p 4); gl. Vertex 2 iv(p 5); gl. Vertex 2 iv(p 6); gl. Vertex 2 iv(p 7); gl. Vertex 2 iv(p 8); gl. End(); p 1 p 8 p 4 p 3 p 5 p 6 p 7 p 2 GL_QUADS GL_QUAD_STRIP

Open. GL RGB colors Red Green Blue Color 0 0 0 Black 0 0 1 Blue 0 1 0 Green 0 1 1 Cyan 1 0 0 Red 1 0 1 Magenta 1 1 0 Yellow 1 1 1 White

Open. GL Color Functions gl. Clear. Color(r, g, b, a); 0. 0 ≤ r, g, b, a ≤ 1. 0 specifies the color for color buffers gl. Clear(GL_COLOR_BUFFER_BIT); applies the clear color to the color buffers Ex: gl. Clear. Color(1. 0, 0. 0); gl. Color(GL_COLOR_BUFFER_BIT); // White

Open. GL Color Functions gl. Color 3 f(r, g, b); 0. 0 ≤ r, g, b ≤ 1. 0 Ex: gl. Color 3 f(1. 0, 0. 0); // Red gl. Color 3 ub(r, g, b); 0 ≤ r, g, b ≤ 255 Ex: gl. Color 3 ub(255, 0, 0); // Red

Open. GL Reshape Function glut. Reshape. Func(func); Ex: glut. Reshape. Func(my_reshape_func);

Open. GL – Transformations Matrix Operations gl. Matrix. Mode routine: • • Projection mode (GL_PROJECTION) Modelview mode (GL_MODELVIEW) Texture mode Color mode

Open. GL – Transformations Modelview Mode gl. Matrix. Mode(GL_MODELVIEW); designates 4 x 4 modelview matrix as the current matrix. gl. Load. Identity(); assigns the identity matrix to the current matrix.

Open. GL – Transformations Translate gl. Translatef (tx, ty, tz); Scale gl. Scalef (sx, sy, sz); Rotate gl. Rotatef (theta, vx, vy, vz);

Open. GL Modelview Matrix I gl. Color 3 f (0. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); // blue

Open. GL Modelview Matrix I I. T gl. Color 3 f (0. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Translatef(10. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); // blue // red

Open. GL Modelview Matrix I I. T. S gl. Color 3 f (0. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Translatef(10. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Scalef(2. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); // blue // red

Open. GL Modelview Matrix I I. T. S. R gl. Color 3 f (0. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Translatef(10. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Scalef(2. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); // blue // red gl. Color 3 f (0. 0, 1. 0, 0. 0); // green gl. Rotatef(20. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( );

Open. GL – Transformations Matrix Stack gl. Push. Matrix (); gl. Pop. Matrix ();

Open. GL Modelview Matrix Stack I I. T. S I. T. R I. T gl. Color 3 f (0. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Translatef(10. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Push. Matrix(); gl. Flush ( ); gl. Color 3 f (1. 0, 0. 0); gl. Scalef(2. 0, 0. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( ); // blue // red gl. Color 3 f (0. 0, 1. 0, 0. 0); // green gl. Pop. Matrix(); gl. Rotatef(20. 0, 1. 0); gl. Recti(0, 0, 80, 40); gl. Flush ( );

Open. GL Ex: ROBOT transform robot gl. Push. Matrix() transform head draw head gl. Pop. Matrix() gl. Push. Matrix() transform body gl. Push. Matrix() transform left_arm draw left_arm gl. Pop. Matrix() gl. Push. Matrix() transform right_arm draw right_arm gl. Pop. Matrix() draw body gl. Pop. Matrix()

Open. GL – 2 D Viewing Clipping Window glu. Ortho 2 D (xwmin, xwmax, ywmin, ywmax); float or double Viewport Function gl. Viewport (xvmin, yvmin, vp. Width, vp. Height); (xwmax, ywmax) Window vp. Height Viewport vp. Width (xvmin, yvmin) (xwmin, ywmin) integer

Open. GL – 2 D Viewing Create Display Window • • • glut. Init (&argc, &argv) glut. Init. Window. Position (x. Top. Left, y. Top. Left) glut. Init. Window. Size (width, height) window. ID=glut. Create. Window (“title”) glut. Destroy. Window (window. ID) Display Window Mode • glut. Init. Display. Mode (mode) Ex: glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB);

Open. GL – 2 D Viewing Display Functions • glut. Display. Function (d. Func) d. Func (callback function) describes what is to be displayed in the current window • glut. Post. Redisplay () Indicates that the contents of the current display window should be renewed • glut. Idle. Func (i. Func) i. Func is executed when there are no other events for the system to process • glut. Main. Loop ()

Open. GL – 3 D Viewing Projection l gl. Matrix. Mode (GL_PROJECTION) Orthogonal Projection l glu. Ortho (xwmin, xwmax, ywmin, ywmax, dnear, dfar) Perspective Projection l glu. Perspective (theta, aspect, dnear, dfar) • • • l theta: field-of-view angle (00 – 1800) aspect: aspect ratio of the clipping window (width/height) dnear, dfar: positions of the near and far planes (must have positive values) gl. Frustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar) l if xwmin = -xwmax and ywmin = -ywmax then symmetric view volume

Open. GL – 3 D Viewing l gl. Matrix. Mode (GL_MODELVIEW) l glu. Look. At (x 0, y 0, z 0, xref, yref, zref, Vx, Vy, Vz) Designates the origin of the viewing reference frame as, • the world coordinate position P 0=(x 0, y 0, z 0) • the reference position Pref=(xref, yref, zref) and • the viewup vector V=(Vx, Vy, Vz) • double-precision, floating-point values y yv w V Pref N xv zv P 0 xw zw

Open. GL – 3 D Viewing l glu. Look. At (x 0, y 0, z 0, xref, yref, zref, Vx, Vy, Vz) Default parameters are: • P 0=(0, 0, 0) • Pref=(0, 0, -1) • V=(0, 1, 0) yw yv V zw zv Pref P 0 xv xw

Open. GL float eye_x, eye_y, eye_z; void init (void) { gl. Clear. Color (1. 0, 0. 0); // Set display-window color to white. gl. Matrix. Mode (GL_PROJECTION); // Set projection parameters. gl. Load. Identity(); glu. Perspective(80. 0, 1. 0, 500. 0); // degree, aspect, near, far gl. Matrix. Mode (GL_MODELVIEW); eye_x = eye_y = eye_z = 60; glu. Look. At(eye_x, eye_y, eye_z, 0, 0, 0, 1, 0); // eye, look-at, view-up } void main (int argc, char** argv) { glut. Init (&argc, argv); // Initialize GLUT. glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glut. Init. Window. Position (50, 500); // Set top-left display-window position. glut. Init. Window. Size (400, 300); // Set display-window width and height. glut. Create. Window ("An Example Open. GL Program"); // Create display window. gl. Viewport (0, 0, 400, 300); init ( ); // Execute initialization procedure. glut. Display. Func (my_objects); // Send graphics to display window. glut. Reshape. Func(reshape); glut. Keyboard. Func(keybrd); glut. Main. Loop ( ); // Display everything and wait. }

Open. GL Functions Cube l glut. Solid. Cube (size) l glut. Wire. Cube (size) Cone l glut. Solid. Cone (base_radius, height, slices, stacks) l glut. Wire. Cone (base_radius, height, slices, stacks) Sphere l glut. Solid. Sphere (radius, slices, stacks) l glut. Wire. Sphere (radius, slices, stacks) Teapot l glut. Solid. Teapot (size) l glut. Wire. Teapot (size) Torus l glut. Solid. Torus (inner_radius, outer_radius, nsides, rings) l glut. Wire. Torus (inner_radius, outer_radius, nsides, rings)

Open. GL Functions Tetrahedron l glut. Solid. Tetrahedron () l glut. Wire. Tetrahedron () • 4 sided Octahedron l glut. Solid. Octahedron () l glut. Wire. Octahedron () • 8 sided Dodecahedron l glut. Solid. Dodecahedron () l glut. Wire. Dodecahedron () • 12 sided Icosahedron l glut. Solid. Icosahedron () l glut. Wire. Icosahedron () • 20 sided

Open. GL l l l gl. Polygon. Mode(GL_FRONT_AND BACK, GL_LINE) • displays both visible and hidden edges gl. Enable (GL_FOG) gl. Fogi (GL_FOG_MODE, GL_LINEAR) • generates fog effect. Uses depth cueing. gl. Enable (GL_CULL_FACE) gl. Disable (GL_CULL_FACE) gl. Cull. Face (mode) • • used for backface removal GL_BACK (default), GL_FRONT (if inside a building), GL_FRONT_AND_BACK

Open. GL l l l glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) • initializes GLUT, requests a depth buffer and a frame buffer gl. Clear (GL_DEPTH_BUFFER_BIT) • initially depth buffer has the background color. It must be cleared every time before displaying a new frame. gl. Enable (GL_DEPTH_TEST) gl. Disable (GL_DEPTH_TEST) • to activate depth buffer routines gl. Clear. Depth (max. Dept) • to set max. Depth to another value (0. 0 – 1. 0) • to adjust normalization values (defaults: near- 0. 0, far- 1. 0) gl. Depth. Range (near. Norm. Dept, far. Norm. Dept)

Diffuse and Specular Lightings I = Idiff + Ispec = ka Ia + kd Il (N. L) + ks Il (N. H)ns
![Open GL l l GLfloat pos x y z w Open. GL l l GLfloat pos[ ] = {x, y, z, w} • •](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-40.jpg)
Open. GL l l GLfloat pos[ ] = {x, y, z, w} • • (x, y, z): position w: type 1. 0 – local (positional light) 0. 0 – distant (directional light) gl. Lightfv(GL_LIGHT 1, GL_POSITION, pos) • GL_POSITION sets the light position and type gl. Enable (GL_LIGHT 1) gl. Disable (GL_LIGHT 1)
![Open GL l l GLfloat color r g b a Open. GL l l GLfloat color[ ] = {r, g, b, a} • •](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-41.jpg)
Open. GL l l GLfloat color[ ] = {r, g, b, a} • • (r, g, b): RGB color (I) a: alpha (used only if color blending routines are active) gl. Lightfv(GL_LIGHT 1, GL_AMBIENT, color) gl. Lightfv(GL_LIGHT 1, GL_DIFFUSE, color) gl. Lightfv(GL_LIGHT 1, GL_SPECULAR, color) • GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR set the light color

Open. GL l 1. 0 if light source is not spotlight fl, angatten = 0. 0 if Vobj. Vlight=cos a < cos ql (Vobj. Vlight )al otherwise GLfloat dir[ ] = {x, y, z} • (x, y, z): direction vector Vlight default direction (0. 0, -1. 0) l gl. Lightfv(GL_LIGHT 2, GL_SPOT_DIRECTION, dir) l gl. Lightfv(GL_LIGHT 2, GL_SPOT_CUTOFF, c) l • c: cut off angle θ (0 o-90 o) Vobj gl. Lightfv(GL_LIGHT 2, GL_SPOT_EXPONENT, e) • e: exponent value for angular intensity attenuation (0 -128) a ql Vlight
![Open GL l l l l l GLfloat ka r g b Open. GL l l l l l GLfloat ka[ ] = {r, g, b,](https://slidetodoc.com/presentation_image_h/ee2be7cab2f0d54176c207ec7abcc63e/image-43.jpg)
Open. GL l l l l l GLfloat ka[ ] = {r, g, b, a} GLfloat kd[ ] = {r, g, b, a} GLfloat ks[ ] = {r, g, b, a} • • (r, g, b): RGB color (k) a: alpha gl. Materialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ka) gl. Materialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, kd) gl. Materialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks) gl. Materialfv(GL_FRONT_AND_BACK, GL_SHININESS, s) • s: specular reflection exponent (0 -128) GLfloat emissioncolor[ ] = {r, g, b, a} gl. Materialfv(GL_FRONT, GL_EMISSION, emissioncolor)

Open. GL l l l gl. Color 4 f(R, G, B, A) • A: alpha parameter 1. 0 – transparent 0. 0 - opaque gl. Enable (GL_BLEND) gl. Disable (GL_BLEND)