31
1 / 31 OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca Introduction to OpenGL © 1995-2015 Josef Pelikán & Alexander Wilkie CGG MFF UK Praha [email protected] http://cgg.mff.cuni.cz/~pepca/

Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

1 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Introduction to OpenGL

© 1995-2015 Josef Pelikán & Alexander Wilkie

CGG MFF UK Praha

[email protected]

http://cgg.mff.cuni.cz/~pepca/

Page 2: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

2 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Advances in Hardware

3D acceleration is a common feature in consumer devices

Focus on games, multimedia

Appearance – quality of results

Sophisticated texturing functionality

Combinations of many textures and processing

High performance

The best semi-conductor technology is used for GPUs (NVIDIA Kepler: 28 nm), massive parallelism

Very fast memory (multipath access, GDDR5, ..)

High performance bus between GPU and CPU (PCI-E)

Page 3: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

3 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Advances in Software

Two main libraries/APIs for 3D graphicsOpenGL (SGI, open standard) and Direct3D (Microsoft)Approach is similar, API is hardware-determined

Efficient transmission of data to the GPUMaximal use of shared data fields

Programmable shaders!Revolution in graphics programming„vertex-shader”: mesh vertex processing„geometry/tesselation-sh.“: generating geometry„fragment-shader” („pixel-shader”): executed for each pixel that is displayed

Page 4: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

4 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Development Tools

Can be used by programmers and artists

Higher languages for GPU programmingCg (NVIDIA), HLSL (DirectX), GLSL (OpenGL)

Cg and HLSL are very similar

Composition of graphical effectsCompact description of the entire effect (GPU programs, references to data) in one file

DirectX .FX format, NVIDIA CgFX format

Tools: Effect Browser (Microsoft), FX Composer (NVIDIA), RenderMonkey (ATI)

Page 5: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

5 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

OpenGL Schematics (FFP)

OpenGL Fixed Functionality Pipeline:

PrimitiveAssembly

ClipProject

ViewportCull

FragmentProcessing

Per-FragmentOperations

FrameBuffer

Operations

PixelTransfer

PixelUnpack

PixelPack

ReadControl

Pixel Groups

Textures

FragmentsVertices

Geom.

AppMemory

Pixels

TextureMemory

FrameBuffer

Per-VertexOperations Geom.

Rasterize

Pixels

Page 6: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

6 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

OpenGL: Geometric Primitives

Types of geometric primitives:

point, line, polyline, loop

polygon, triangle, triangle strip, triangle fan, quad, quad strip

Processing of individual vertices

glVertex, glColor, glNormal, glTexCoord, ...

inefficient (many calls to gl* functions)

Vertex Arrays

glDrawArrays, glMultiDrawArrays, glDrawElements, ...

glColorPointer, glVertexPointer, ... or interlaced

Page 7: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

7 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Geometric Primitives I

GL_POINTS

V0V1

V2

V3

V4

V5

GL_LINES

V0V1

V2

V3

V4

V5

GL_LINE_STRIP

V0V1

V2

V3

V4

V5

GL_LINE_LOOP

V0V1

V2

V3

V4

V5

Page 8: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

8 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Geometric Primitives II

GL_TRIANGLES

V0V1

V2

V3

V4

V5

GL_TRIANGLE_STRIP

V0V1

V2

V3

V5

V4

GL_TRIANGLE_FAN

V6

V0

V5

V4

V3

V2

V1

Page 9: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

9 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Geometric Primitives III

GL_QUADS

V0V3

V1V2

V6

V5

GL_QUAD_STRIP GL_POLYGON

V6

V0

V5

V4

V3

V2

V1

V4 V7

V1V0

V3V2

V6

V7

V5 V4

Page 10: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

10 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

OpenGL „macros“ (Display Lists)

DISPLAY_LIST_MODE instead of IMMEDIATE_MODE

Storage of a sequence of GL commands in memory:

glNewList, glEndList

The list can be pushed to the server (graphical HW)

Basic idea: „list = macro”

Execution of entire lists

glCallList, glCallLists

Potentially very efficient (the commands can be optimised by the graphics server)

Page 11: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

11 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Model coordinates(Object space)

Coordinate Systems

Modeling transform

View transform

xy

-z

xy

-zProjection transform

[x,y,z,w]

[x,y,z,w]

n f

-z

xy

xy

-z

World space

Camera coordinates(Eye space)

Clip space

Page 12: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

12 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Coordinate Systems II

Perspective divide

Viewport transform

xy

-z

[x,y,z][x,y,z,w]

[x,y,z]

[x,y] actual fragment size on screenz depth compatible with the z buffer

OpenGL: [-1,-1,-1] to [1,1,1]DirectX: [-1,-1, 0] to [1,1,1]

Clip space Normalized device space

Window space

Page 13: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

13 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Coordinate Systems III

Model coordinates

Database of objects that comprise the scene

Source: 3D modeling applications (3DS, Maya, ..)

World coordinates

Absolute coordinates of the 3D world

The relative coordinates of object instances are given there

Camera coordinates

3D world → relative camera coordinates

Projection center: origin, view direction: -z (or z)

Page 14: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

14 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Coordinate transforms

Model camera transforms

Combined transformation „modelview”

World coordinates are not directly used

Projection transformation

Defining the view frustum [ l, r, b, t, n, f ]

Near and far clipping plane: n, f

Result in homogeneous coordinates (before clipping)

Clip space

Output coordinates of vertex shaders!

Page 15: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

15 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Projective Transformation

The remote point f can be moved to infinity

[2nr−l

0 0 0

0 2nt−b

0 0

−rlr−l

−tbt−b

f nf −n

1

0 0 −2fnf −n

0 ]

n f

-z

xy

[2nr−l

0 0 0

0 2nt−b

0 0

−rlr−l

−tbt−b

1 1

0 0 −2n 0 ]

Page 16: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

16 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Coordinate Transformations

Perspective division/clip

Transforms homogeneous coordinates to cartesian

Normalised Device Coordinates („NDC”)

Unit sized cube

OpenGL: [-1,-1,-1] to [1,1,1]

DirectX: [-1,-1,0] to [1,1,1]

Window coordinates („window space”)

Result of screen and depth transformation

Used during rasterisation and when working with fragments

Page 17: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

17 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Rigid Body Transformations

Retain shape, only change orientation

Only consists of translation and rotation

Often used for coordinate system transforms (e.g. world coordinates, and viewer coordinates)

yx

z

(left handed = right winding system)

v (view)

u (up)r (right)

Page 18: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

18 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Transformation Between Systems

x

y

z

O

u

st

A coordinate system has its origin in Oand is given by three basis vectors

[ s, t, u ]

M stu xyz = [sx sy sz

tx t y t z

ux

uy

uz]

M xyz stu = M stu xyzT

[1, 0 , 0 ]⋅M stu xyz = s

[0, 1, 0 ]⋅M stu xyz = t

[0, 0 , 1 ]⋅M stu xyz = u

Page 19: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

19 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Geometric Data on the Server

VBO, VAO, starting with OpenGL 1.5

For .NET practically a requirement (client memory is not fixed)

The buffers on the graphics server contain geometric data

Buffer creation: glBindBuffer

Data entry: glBufferData, glBufferSubData

Mapping to application memory: glMapBuffer, glUnmap..

Work with client memory or with buffers

glColorPointer, glNormalPointer, glVertexPointer, ...

Page 20: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

20 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Vertex Processing

Vertex transformations via model and perspective matrices

glMatrixMode

glLoadIdentity, glLoadMatrix, glMultMatrix

glRotate, glScale, glTranslate, ...

Lighting attributesglLight, glLightModel, glMaterial

Page 21: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

21 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Assembly and Processing

AssemblyDetermination which vertices a primitive needs

Assembly of data packages and uploading

Processing of primitivesClipping

Projection to view frustum – removal of „w“

Projection and clipping to 2D viewport

Back face culling- Single vs. double sided primitives

Page 22: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

22 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Rasterisation, Fragments

Rasterisation = display of vector primitivesDecomposition of objects into fragmentsObjects: points, lines triangles, bitmaps

FragmentsRaster element, that potentially contributes to the colour of a pixelSize: pixel size, or smaller (anti-aliasing)Data packing in the raster unit of the GPU:

- In/Output: x, y, z (depth can be changed!)- Texture coordinates t0 to tn

- Specular and diffuse colour, fog, user data, ...- Output colour in RGB and α (frame-buffer op.)

Page 23: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

23 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Fragment Interpolation

Fragment attributes are automatically interpolated between vertex values for:

depth (z or w)

Texture coordinates

Colour (specular and diffuse)

User data, ...

Fast HW interpolators

Perspectivically correct interpolation

Only [ x, y ] change linearly

Other variables require one division for each fragment

Page 24: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

24 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Fragment Processing

Texture operations

Highly optimised operations

Colour selection from texture memory

Texel interpolation:- mip-mapping, anisotropic filtering, ...

Texture combination (many operations possible)

Specialised effects (bump-mapping, environment mapping)

Calculation of fog

Based on depth z

Combination of several colours (diff., spec.)

Page 25: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

25 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Raster Images in OpenGL

Per-VertexOperations

PrimitiveAssembly

ClipProject

ViewportCull

FragmentProcessing

Per-FragmentOperations

FrameBuffer

Operations

PixelTransfer

PixelUnpack

PixelPack

ReadControl

Pixel Groups

Textures

FragmentsVertices

Geom.

AppMemory

Pixels

TextureMemory

FrameBuffer

Geom.

Rasterize

Pixels

Page 26: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

26 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

OpenGL Programmable Pipeline

PrimitiveAssembly

ClipProject

ViewportCull

Per-FragmentOperations

FrameBuffer

Operations

PixelUnpack

PixelPack

Pixel Groups

Textures

Fragments

Vertices

FragmentProcessor

VertexProcessorGeom.

AppMemory

Pixels

TextureMemory

FrameBuffer

PixelTransfer

Geom.

Rasterize

Pixels

ReadControl

Page 27: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

27 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Vertex Processor

Replaces the vertex processing module:Transformation of vertices

Transformation/normalisation of normal vectors

Calculation of texture coordinates

Calculation of lighting vectors

Definition of material constants for vertices

What cannot be modified:The number of vertices! (no delete/add)

Type & topology of geometric primitives- Partial solution: degenerate primitives

Page 28: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

28 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Geometric Processor

„Tesselation shaders“

New in OpenGL 4.0HW support for subdivision surfaces (spline sheets, ...)Two shaders: „tesselation control“ and „tesselation evaluation“The first defines topology, the second generates geometry (coefficients)

„Geometry shader“

From OpenGL 3.0 onwardsJust before the rasterisation unit Possibility to generate/delete primitives and verticesMore powerful than TS, but slower (not easy to incude in schematics)

Page 29: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

29 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Fragment Processor

Replaces the module for fragment processing:

Arithmetic operations with interpolation

Reading of data and textures

Texture generation

Fog calculations

Computation of the final colour of the fragment colour

Possibility to modify fragment depth

What cannot be modified:

Number of fragments! (no addition or deletion)

Fragment position on screen [x,y]

Page 30: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

30 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

GPU Programming

„Vertex shader”

Code for the vertex processor

„Fragment shader”

Code for the fragment processors

Both can be programmed by the user!

HW independent programming languages

GPU micro-code compiled at run-time (hard to work with: many versions, specific to individual cards)

Low-level instructions (similar to assembler)

or higher languages Cg, HLSL, GLSL

Page 31: Introduction to OpenGL - Univerzita Karlovacgg.mff.cuni.cz/~pepca/lectures/pdf/pg1-25-opengl-en.pdf · (NVIDIA Kepler: 28 nm), massive parallelism Very fast memory (multipath access,

31 / 31OpenGL 2013 © Josef Pelikán, http://cgg.mff.cuni.cz/~pepca

Literature

Tomas Akenine-Möller, Eric Haines: Real-time rendering, 2nd edition, A K Peters, 2002, ISBN: 1568811829

OpenGL ARB: OpenGL Programming Guide, 4th edition, Addison-Wesley, 2004, ISBN: 0321173481

Randima Fernando, Mark J. Kilgard: The Cg Tutorial, Addison-Wesley, 2003, ISBN: 0321194969

Jack Hoxley: An Overview of Microsoft's Direct3D 10 API, 13.12.2005, www.gamedev.net