3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6...

46
P1

Transcript of 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6...

Page 1: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P1

Page 2: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

2

Overview

• Display Lists• Beyond Display Lists: Vertex Arrays• Principles of Shading• P1 – Colors

– Overview of data structure– Normals – Shading– HSV and RGB Colorspaces

Page 3: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

3

OpenGL revisited –Simple Rendering Example

glBegin(GL_TRIANGLES);

glColor3fv(color);

glNormal3fv(normal);

glVertex3f(0.0, 0.0, 1.0)

glVertex3f(0.0, 1.0, 0.0)

glVertex3f(1.0, 0.0, 0.0)

glEnd()

Page 4: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

4

Immediate Mode

• Immediate Mode– Primitives are sent to pipeline and displayed right

away– No memory of graphical entities– Primitive data lost after drawing– New images created by regenerating primitives

Page 5: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

5

Retained Mode

• Retained Mode Graphics– Primitives stored in display lists

(in “compiled” form)– Display lists kept on graphics server– Images recreated by “executing” the display list– More efficient– Can be redisplayed with different state– Can be shared among OpenGL

graphics contexts

Page 6: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

6

Display Lists I

• steps: create it, then call it

GLuint id;void init( void ){

id = glGenLists( 1 );glNewList( id, GL_COMPILE );// other OpenGL routinesglEndList();

}void display( void ){

glCallList( id );}

Page 7: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

7

Display Lists II – glNewList()

• glNewList accepts the constants:– GL_COMPILE, which creates a display list– GL_COMPILE_AND_EXECUTE, which both creates and

executes a display list• Lists can be overwritten

– Creation of a new list with the same identifying number as an existing display list

– No error occurs.

Page 8: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

8

Display Lists III – Modeling

glNewList( BOX, GL_COMPILE )glBegin( GL_QUADS );

glVertex3d( … );glVertex3d( … );…

glEnd();glEndList();

mybox

Page 9: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

9

Display Lists IV – Advanced

• Not all OpenGL routines can be stored in displaylists

• State changes persist, even after a display list isfinished

• Display lists can call other display lists• Display lists are not editable, but you can fake it

– make a list (A) which calls other lists (B, C, and D)– delete and replace B, C, and D, as needed

Page 10: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

10

A Step Beyond –Vertex Arrays

• Edges and Vertices are often processed multiple times– Box: 6 faces, 8 vertices, each vertex is processed 3

times, thus 24 points processed!• OpenGL has vertex array functions

– Enables collective access to data– Minimization of function calls– Dramatic Performance gain

Page 11: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

11

Vertex Arrays –How to use them

• 3 steps:– Activate up to 6 vertex arrays

• Coordinate, Color, Normal, etc.– Fill the arrays with data– Render geometry with array data

• Vertex data can be dynamically changed• OpenGL 1.1+ needed

Page 12: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

12

Vertex Arrays – Activation

• glEnableClientState(GLenum array)

– Activates certain array– Allowed arrays:

GL_VERTEX_ARRAYGL_COLOR_ARRAYGL_NORMAL_ARRAYGL_INDEX_ARRAYGL_TEXTURE_COORD_ARRAYGL_EDGE_FLAG_ARRAY

• glDisableClientState(GLenum array)

– Deactivates certain array

Page 13: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

13

Vertex Arrays – Data

• 6 different functions for specifying array dataglVertexPointer(Glint size, GLenum type,

GLsizei stride, const GLvoid * pointer)– size: number of channels (2, 3 or 4)– type: data type (GL_SHORT, GL_INT, GL_FLOAT, etc.)– stride: byte offset between consecutive data– pointer: pointer to first data item

• More…glColorPointer(…)glNormalPointer(…)

Page 14: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

14

Vertex Arrays – Rendering

• Single array elementglArrayElement(Glint ith)

– Gets values from every activated array– Renders element– Stands in a glBegin() – glEnd() pair

• More ElementsglDrawElements(GLenum mode,

GLsizei count, GLenum type, void *indices)– Access to data elements over indices array– Other possibility:

glDrawArrays(GLenum mode, GLint first, GLsizei count)

Page 15: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

15

Principles of Lighting & Shading

Page 16: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

16

Page 17: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

17

Page 18: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

18

Page 19: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

19

Principles of Lighting & Shading

• Lighting simulates how objects reflect light– material composition of object– light’s color and position– global lighting parameters

• ambient light• two sided lighting

– available in both color index and RGBA mode

Page 20: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

20

Light Simulation in OpenGL

• Constant Shading– One color (and one normal) per primitive – Flat

Shading• Gouraud Shading

– Computed at vertices– Linear interpolation of vertex intensities

• Phong Shading– Linear interpolation of vertex normals

• More to come in Lighting and Shading module

Page 21: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

21

Page 22: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

22

Vertex Color Computation

• Lighting contributors– Ambient is color of the object from all the

undirected light in a scene.– Diffuse is the base color of the object under current

lighting. There must be a light shining on the object to get a diffuse contribution.

– Specular is the contribution of the shiny highlights on the object.

– Emission is the contribution added in if the object emits light (i.e. glows)

Page 23: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

23

ambient

ambient + diffuse

ambient + diffuse + specular (phong)

Page 24: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

24

Surface Normals

• Normals define how a surface reflects lightglNormal3f( x, y, z );

• Current normal is used to compute vertex’s color

• Use unit normals for proper lighting scaling affects a normal’s length

glEnable( GL_NORMALIZE );

orglEnable( GL_RESCALE_NORMAL );

Page 25: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

25

Page 26: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

26

Material Properties

• Define the surfaces properties of a primitiveglMaterialfv(face, property, value);

• Separate materials for front and backGL_DIFFUSE Base colorGL_SPECULAR Highlight ColorGL_AMBIENT Low-light ColorGL_EMISSION Glow ColorGL_SHININESS Surface Smoothness

• Lights and their properties in another exercise

Page 27: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

27

P1

• Goals– Color Spaces– Vertex Normals– Lighting and Shading in OpenGL– Display Lists

Page 28: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

28

P1 – Data Structure

• First Array– Number of Nodes with corresponding 3D

coordinates– “the vertices”

• Second Array– Number of Triangles with references to their

vertices in first list– “the indices”

Page 29: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

29

P1 – Normals

• Per-Triangle Normal– How to determine a normal of a triangle?– Vector algebra

• Per-Vertex Normal– Average of face normals

Page 30: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

30

Vertex Normals

Page 31: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

31

Vertex Normals

Page 32: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

32

Vertex Normals

Page 33: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

33

Vertex Normals

Page 34: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

34

Vertex Normals

Better to weightnormals by incidentangle.

Page 35: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

35

Page 36: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

36

Page 37: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

37

Page 38: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

38

P1 – Color Spaces revisited

• RGB– Red , green and blue– Normalized to [0,1]

• HSV– Hue (dt. Farbton)– Saturation (dt. Sättigung) – Value (dt. Helligkeit)– Advantages of HSV

• HSV to RGB?– Hint: See script

RGB

HSV

Page 39: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

39

Page 40: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

40

0

360

Page 41: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

41

0

360

Hue mapped to height range 0 – 360.

Page 42: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

42

0

360

250

Hue mapped to height range 0 – 250.

Page 43: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

43

0

360

250

Interesting point.

Page 44: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

44

0

360

250

Saturation mapped to distance range 0 – max.

Page 45: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

45

0

360

250

Saturation mapped to distance range 0 – 1/2 max.

Page 46: 3 P2 - Colors · P2 - Colors 11 Vertex Arrays – How to use them •3:pes st – Activate up to 6 vertex arrays • Coordinate, Color, Normal, etc. – Fill the arrays with data

P2 - Colors

46

0

360

250

Saturation mapped to distance range 0 – 1/4 max.