Open GL CS 418 Computer Graphics John C

  • Slides: 24
Download presentation
Open. GL CS 418 Computer Graphics John C. Hart

Open. GL CS 418 Computer Graphics John C. Hart

Open. GL • Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages:

Open. GL • Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: • Runs on everything, including cell phones (Open. GL/ES) Alternatives: • Microsoft’s Direct 3 D – limited to MSWindows • Sun’s Java 3 D – slower, implemented on top of Open. GL

Library Layers APP GLUT GLU GL OS Driver HW EXT • Open. GL =

Library Layers APP GLUT GLU GL OS Driver HW EXT • Open. GL = GL + GLU – Basic low-level GL routines implemented using OS graphics routines – Timesaving higher-level GLU routines implemented using GL routines • GLUT opens and manages Open. GL windows and adds helper functions • Open. GL Extensions provide direct device-dependent access to hardware

Vertex Pipeline Model Coords Model Xform Viewing Xform World Coords Homogeneous Divide Still Clip

Vertex Pipeline Model Coords Model Xform Viewing Xform World Coords Homogeneous Divide Still Clip Coords Window Coordinates Window to Viewport Viewing Coords Clipping Perspective Distortion Clip Coords Viewport Coordinates

Viewport Coordinates • Physical per-pixel integer coordinates • Also called screen or device coordinates

Viewport Coordinates • Physical per-pixel integer coordinates • Also called screen or device coordinates (0, VRES-1) (HRES-1, VRES-1) gl. Viewport(x, y, w, h) • x, y – lower left pixel • w – width • h – height • Sometimes (0, 0) is in the upper left corner (e. g. for mouse input) (0, 0) (HRES-1, 0)

Window Coordinates • Logical, mathematical floating-point coordinates (-1, 1) (1, 1) gl. Ortho(l, r,

Window Coordinates • Logical, mathematical floating-point coordinates (-1, 1) (1, 1) gl. Ortho(l, r, b, t, n, f) • left, right, bottom, top • near, far: limits depth • glu. Ortho 2 D(l, r, b, t) calls gl. Ortho(l, r, b, t, -1, 1) • To use per-pixel coordinates, call: glu. Ortho 2 D(-. 5, w-. 5, h-. 5); (-1, -1) (1, -1)

Open. GL State (l, t) (r, t) (l, b) (r, b) • Open. GL

Open. GL State (l, t) (r, t) (l, b) (r, b) • Open. GL commands change its internal state variables which control how it turns geometric models into pictures • E. g. the window-to-viewport transformation gl. Viewport(x, y, w, h) Open. GL State: gl. Ortho(l, r, b, t, n, f) x, y, w, h, l, r, b, t, n, f, … (x, y+h-1) W 2 V (x+w-1, y+h-1) • Can query this state: GLfloat buf[4]; gl. Get. Floatv(GL_VIEWPORT, buf); x = buf[0]; y = buf[1]; w = buf[2]; h = buf[3]; (x, y) (x+w-1, y)

Open. GL Commands gl[u]Fubar[234][dfis][v] • [u]: GLU v. GL command • [234]: dimension •

Open. GL Commands gl[u]Fubar[234][dfis][v] • [u]: GLU v. GL command • [234]: dimension • [dfisb]: data type – d: GLdouble (double) – f: GLfloat (float) – i: GLint (int) – s: string • [v]: vector (reference v. value) Examples: gl. Vertex 3 f(-0. 5, 3. 14159, 2); gl. Vertex 2 i(200, 350); GLdouble point[4]; gl. Vertex 4 dv(point);

Points (-1, 1) (1, 1) gl. Begin(GL_POINTS); gl. Vertex 2 f(-. 6, 1. );

Points (-1, 1) (1, 1) gl. Begin(GL_POINTS); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Lines (-1, 1) (1, 1) gl. Begin(GL_LINES); gl. Vertex 2 f(-. 6, 1. );

Lines (-1, 1) (1, 1) gl. Begin(GL_LINES); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Line Strip (-1, 1) (1, 1) gl. Begin(GL_LINE_STRIP); gl. Vertex 2 f(-. 6, 1.

Line Strip (-1, 1) (1, 1) gl. Begin(GL_LINE_STRIP); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Line Loop (-1, 1) (1, 1) gl. Begin(GL_LINE_LOOP); gl. Vertex 2 f(-. 6, 1.

Line Loop (-1, 1) (1, 1) gl. Begin(GL_LINE_LOOP); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Polygon Open. GL only supports convex polygons (and really only triangles) (-1, 1) (1,

Polygon Open. GL only supports convex polygons (and really only triangles) (-1, 1) (1, 1) gl. Begin(GL_POLYGON); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Quads (-1, 1) (1, 1) gl. Begin(GL_QUADS); gl. Vertex 2 f(-. 6, 1. );

Quads (-1, 1) (1, 1) gl. Begin(GL_QUADS); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Quads gl. Begin(GL_QUADS); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-.

Quads gl. Begin(GL_QUADS); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End();

Quads Lines should never pass through a vertex. (-1, 1) (1, 1) gl. Begin(GL_QUADS);

Quads Lines should never pass through a vertex. (-1, 1) (1, 1) gl. Begin(GL_QUADS); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(. 6, 1. ); gl. Vertex 2 f(-. 6, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(. 2, . 6); gl. End(); (-1, -1) (1, -1)

Triangles (-1, 1) (1, 1) gl. Begin(GL_TRIANGLES); gl. Vertex 2 f(-. 6, 1. );

Triangles (-1, 1) (1, 1) gl. Begin(GL_TRIANGLES); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Triangles Lines should never pass through a vertex. (-1, 1) (1, 1) gl. Begin(GL_TRIANGLES);

Triangles Lines should never pass through a vertex. (-1, 1) (1, 1) gl. Begin(GL_TRIANGLES); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, . 6); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); … gl. End(); (-1, -1) (1, -1)

Triangle Strip (-1, 1) (1, 1) gl. Begin(GL_TRIANGLE_STRIP); gl. Vertex 2 f(-. 6, 1.

Triangle Strip (-1, 1) (1, 1) gl. Begin(GL_TRIANGLE_STRIP); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 6, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(-. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 6, 1. ); gl. End(); (-1, -1) (1, -1)

Triangle Strip First two vertices prime the pump, then every new vertex creates a

Triangle Strip First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous two vertices (-1, 1) (1, 1) gl. Begin(GL_TRIANGLE_STRIP); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(-. 2, . 6); gl. Vertex 2 f(-. 2, -. 6); gl. Vertex 2 f(. 6, -1. ); gl. Vertex 2 f(. 6, -. 6); gl. End(); … (-1, -1) (1, -1)

First two vertices prime the pump, then every new vertex creates a triangle connecting

First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous vertex and the first vertex Triangle Fan (-1, 1) (1, 1) gl. Begin(GL_TRIANGLE_FAN); gl. Vertex 2 f(-. 2, . 6); gl. Vertex 2 f(-. 6, 1. ); gl. Vertex 2 f(. 2, . 6); gl. Vertex 2 f(. 2, -. 6); gl. Vertex 2 f(-. 2, -. 6); gl. End(); … (-1, -1) (1, -1)

Assigning Color gl. Color 3 f(0, 0, 1); gl. Begin(GL_POLYGON); gl. Vertex 2 f(-1,

Assigning Color gl. Color 3 f(0, 0, 1); gl. Begin(GL_POLYGON); gl. Vertex 2 f(-1, 1); gl. Vertex 2 f(-1, -1); gl. Vertex 2 f(1, -1); gl. End(); gl. Begin(GL_POLYGON); gl. Color 3 f(0, 1, 0); gl. Vertex 2 f(-1, 1); gl. Color 3 f(0, 0, 1); gl. Vertex 2 f(-1, -1); gl. Color 3 f(1, 0, 0); gl. Vertex 2 f(1, -1); gl. End(); gl. Color 3 f(1, 0, 0); gl. Begin(GL_POLYGON); gl. Vertex 2 f(-1, 1); gl. Vertex 2 f(-1, -1); gl. Vertex 2 f(1, -1); gl. End(); gl. Color 3 f(0, 0, 0); gl. Begin(GL_LINE_LOOP); gl. Vertex 2 f(-1, 1); gl. Vertex 2 f(-1, -1); gl. Vertex 2 f(1, -1); gl. End();

Indexed Face Set • Popular file format – VRML, Wavefront “. obj”, etc. •

Indexed Face Set • Popular file format – VRML, Wavefront “. obj”, etc. • Ordered list of vertices – Prefaced by “v” (Wavefront) – Spatial coordinates x, y, z – Index given by order • List of polygons – Prefaced by “f” (Wavefront) – Ordered list of vertex indices – Length = # of sides – Orientation given by order (x 3, y 3, z 3) (x 1, y 1, z 1) (x 4, y 4, z 4) (x 2, y 2, z 2) v x 1 y 1 z 1 v x 2 y 2 z 2 v x 3 y 3 z 3 v x 4 y 4 z 4 f 123 f 243

Other Attributes • Vertex normals – Prefixed w/ “vn” (Wavefront) – Contains x, y,

Other Attributes • Vertex normals – Prefixed w/ “vn” (Wavefront) – Contains x, y, z of normal – Not necessarily unit length – Not necessarily in vertex order – Indexed as with vertices • Texture coordinates – Prefixed with “vt” (Wavefront) – Not necessarily in vertex order – Contains u, v surface parameters • Faces – Uses “/” to separate indices – Vertex “/” normal “/” texture – Normal and texture optional – Can eliminate normal with “//” (x 2, y 2, z 2) (a 2, b 2, c 2) (u 2, v 2) (x 0, y 0, z 0) (a 0, b 0, c 0) (u 0, v 0) v x 0 y 0 z 0 v x 1 y 1 z 1 v x 2 y 2 z 2 vn a 0 b 0 c 0 vn a 1 b 1 c 1 vn a 2 b 2 c 2 vt u 0 vt u 1 vt u 2 v 2 (x 1, y 1, z 1) (a 1, b 1, c 1) (u 1, v 1) f 0/0/0 1/1/1 2/2/2 f 0/0/0 1/0/1 2/0/2