+ All Categories
Home > Documents > 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Date post: 05-Jan-2016
Category:
Upload: phillip-norris
View: 221 times
Download: 4 times
Share this document with a friend
Popular Tags:
77
1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel
Transcript
Page 1: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

1

Graphics, Day 2

Based on lecture by Ed Angel

© Ed Angel

Uncredited images from Ed Angel

Page 2: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

2

Objectives

Administrivia

Brief historical introduction

Meet our first Pioneer – Ivan Sutherland

Fundamental imaging notions

Physical basis for image formation

Light

Color

Perception

Synthetic camera model

Other models – Ray Tracing

Mathematics: meaning of Dot product, applications

Serpinski Gasket and the Chaos Game

Escape to 3D!

Page 3: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

3

Notifications

Page 4: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

4

Questions

Must we use shaders for all our projects?

I haven't converted my examples yet: I can't ask you to do what I haven't done

But if your purpose in taking the course is to learn GPU programming, it would be good practice…

Can we use Java?

In theory, yes. In practice, you have enough to worry about already

What can you do with shaders?

I'll give some examples tonight

Why must we link the Foundations when we #included OpenGL?

This is the difference between Circle.h and Circle.cpp

Page 5: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

5

Basic Graphics System

Input devices

Output device

Image formed in Frame Buffer

Page 6: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

6

CRT

Can be used either as a line-drawing device (calligraphic) or to display contents of frame buffer (raster mode)

Page 7: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

7

Computer Graphics: 1950-1960

Computer graphics goes back to the earliest days of computing

Strip charts

Pen plotters

Simple displays using A/D converters to go from computer to calligraphic CRT

Cost of refresh for CRT too high

Computers slow, expensive, unreliable

API very device specific

Page 8: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

8

Computer Graphics: 1960-1970

Wireframe graphics

Draw only lines

Ivan Sutherland's Sketchpad

Display Processors

Storage tube

wireframe representationof sun object

Page 9: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

9

Sketchpad

Ivan Sutherland’s PhD thesis at MIT (1963)

Recognized the potential of man-machine interaction

Loop

Display something

User moves light pen

Computer generates new display

Sutherland also created many of the now common algorithms for computer graphics

Alan Kay presents Sutherland's sketchpad

http://www.youtube.com/watch?v=mOZqRJzE8xg

Page 10: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

10

Display Processor

Rather than have the host computer try to refresh display use a special purpose computer called a display processor (DPU)

Graphics stored in display list (display file) on display processor

Host compiles display list and sends to DPU

Page 11: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

11

Direct View Storage Tube

Created by Tektronix

Did not require constant refresh

Standard interface to computers

Allowed for standard software

Plot3D in Fortran

Relatively inexpensive

Opened door to use of computer graphics for CAD community

Drew lines - vector graphics

Page 12: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

12

Computer Graphics: 1970-1980

Raster Graphics

Beginning of graphics standards

IFIPS

GKS: European effort

Becomes ISO 2D standard

Core: North American effort

3D but fails to become ISO standard

Workstations and PCs

Page 13: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

13

Raster Graphics

Image produced as an array (the raster) of picture elements (pixels) in the frame buffer

Page 14: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

14

Raster Graphics

Allows us to go from lines and wire frame images to filled polygons

Note different patches have different shading

Can see edges and vertices

Page 15: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

15

PCs and Workstations

Although we no longer make the distinction between workstations and PCs, historically they evolved from different roots

Early workstations characterized by

Networked connection: client-server model

High-level of interactivity

Early PCs included frame buffer as part of user memory

Easy to change contents of frame buffer and create images

Page 16: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

16

Computer Graphics: 1980-1990

Realism comes to computer graphics

smooth shading environment mapping

bump mapping

Page 17: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

17

Bump Map

Change surface plane

Normal vector

Page 18: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

18

Computer Graphics: 1980-1990

Special purpose hardware

Silicon Graphics geometry engine

VLSI implementation of graphics pipeline

Industry-based standards

PHIGS

Pixar's RenderMan API

Networked graphics: X Window System

Human-Computer Interface (HCI)

Page 19: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

19

Computer Graphics: 1990-2000

OpenGL API

Completely computer-generated feature-length movies are successful

New hardware capabilities

Texture mapping

Blending

Accumulation, stencil buffers

Pixar

Page 20: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

20

Computer Graphics: 2000-

Photorealism

Graphics cards for PCs dominate market

Nvidia, ATI, 3DLabs

Game boxes and game players determine direction of market

Computer graphics routine in movie industry: Maya, Lightwave

Programmable pipelines

Grand Theft Auto

Page 21: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

21

Image Formation

In computer graphics, we form images which are generally two dimensional using a process analogous to how images are formed by physical imaging systems

Cameras

Microscopes

Telescopes

Human visual system

Wikipedia

Page 22: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

22

Elements of Image Formation

Objects

Viewer

Light source(s)

How light interacts with material

Glossy or matt?

Note the independence of the objects, the viewer, and the light source(s)

Generating shadows is not easy – a theme we will return to

Page 23: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

23

Light

Light is the part of the electromagnetic spectrum that causes a reaction in our visual systems

Generally these are wavelengths in the range of about 350-750 nm (nanometers)

Long wavelengths appear as reds and short wavelengths as blues

Page 24: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

24

Luminance and Color Images

Luminance Image

Monochromatic

Values are gray levels

Analogous to working with black and white film or television

Color Image

Has perceptional attributes of hue, saturation, and lightness

Do we have to match every frequency in visible spectrum? No!

Page 25: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

25

Three-Color Theory

Human visual system has two types of sensors

Rods: monochromatic, night vision

Cones

Color sensitive

Three types of cones

Only three values (the tristimulus

values) are sent to the brain

Need only match these three values to fool eye

Need only three primary colors

Page 26: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

26

Shadow Mask CRT

Page 27: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

27

Additive and Subtractive Color

Additive colorForm a color by adding amounts of three primaries

CRTs, projection systems, positive filmPrimaries are Red (R), Green (G), Blue (B)

Subtractive colorForm a color by filtering white light with cyan (C), Magenta (M), and

Yellow (Y) filtersLight-material interactionsPrintingNegative film

Page 28: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

28

Synthetic Camera Model

center of projection

image plane

projector

p

projection of p

Page 29: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

29

Advantages

Separation of objects, viewer, light sources

Two-dimensional graphics is a special case of three-dimensional graphics

Leads to simple software API

Specify objects, lights, camera, attributes

Let implementation determine image

Leads to fast hardware implementation

Page 30: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

30

Global vs Local Lighting

Cannot compute color or shade of each object independently

Some objects are blocked from light

Light can reflect from object to object

Some objects might be translucent

Page 31: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

31

Alternatives

Ray Tracing and Ray Casting

Wikipedia

Page 32: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

32

Why not ray tracing?

Ray tracing is more physically based Simpler for objects such as polygons and quadrics.Can produce global lighting effects such as shadows and multiple reflections Why don’t we use it to design a graphics system?Ray tracing is slow and not well-suited for many interactive applications

Wikipedia

Page 33: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

33

Dot Product

C = A − B

C ⋅C = (A − B) ⋅(A − B)

C ⋅C = A ⋅A + B ⋅B − 2A ⋅B

| C |2=| A |2 + | B |2 −2A ⋅B

| C |2=| A |2 + | B |2 −2 | A | ⋅| B | cosθ

A ⋅B =| A | ⋅| B | cosθ

A ⋅B| A | ⋅| B |

= cosθ

But the Law of Cosines tells us that

Putting these together, we get

Page 34: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

34

More Vector MathematicsDot product – measures length and angle – can tease out one term

A•B = |A| |B| cos (theta)

cos(theata) = A•B/|A||B|

Perpendicular vectors have a dot product of 0

Given vector v1 = (a, b)

Build vector v2 = (-b, a)

v1 . v2 = -ab + ba = 0, so they are perpendicular

Could also use (b, -a) = -v2

Given vector v = (a, b, c) we have many perpendiculars

(-b, a, 0), (0, -c, b), (-c, 0, a) and combinations of these.

Page 35: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

35

Applications

If AB == 0 and |A| =/= 0 and |B| =/= 0, then A and B are perpendicular

We can define a line through the origin as the set of points that are the endpoints of vectors perpendicular to a fixed vector v

The vector is called the normal vector to the line

v = (a, b), then the line is ax + by = 0

All (x, y) such that (x, y)•(a, b) = ax + by = 0

We can translate the line from the origin to get - ax + by = c

A normal vector can also be used to define a plan in 3 D

Wolfram Mathworld

Page 36: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

36

Applications

Backface culling – We use the fact that AB < 0 means that the angle is > 90

When deciding if a triangle is facing the camera or is facing away, we compute the dot product of the normal vector to the polygon with the vector from the camera to one of the polygon's vertices.

Gives fast test to see if we need to draw the triangle: those that are facing away are culled (removed)

Page 37: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

37

Applications

We want to know the distance between a point x and a line L

Take a point y on the line L, and draw a vector A from to y to x

Project the vector A onto the vector B

Distance from x to the line is length of perpendicular A-P

P = (A ⋅B)B

| B |2

Page 38: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

38

Applications

Does a line intersect a circle?

What do you need to answer the question?

Page 39: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Vertex Shader for color

Page 40: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Vertex Shader for Rotation

chapter 3 example6 – vertex shader

attribute vec4 vPosition;

attribute vec4 vColor;

varying vec4 color;

uniform vec3 theta;

void main()

{

// Compute the sines and cosines of theta for each of

// the three axes in one computation.

vec3 angles = radians( theta );

vec3 c = cos( angles );

vec3 s = sin( angles );

Page 41: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Rotation (cont)

// Remember: thse matrices are column-major

mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0,

0.0, c.x, s.x, 0.0,

0.0, -s.x, c.x, 0.0,

0.0, 0.0, 0.0, 1.0 );

mat4 ry = mat4( c.y, 0.0, -s.y, 0.0,

0.0, 1.0, 0.0, 0.0,

s.y, 0.0, c.y, 0.0,

0.0, 0.0, 0.0, 1.0 );

// Workaround for bug in ATI driver

ry[1][0] = 0.0;

ry[1][1] = 1.0;

Page 42: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Rotation (cont)

mat4 rz = mat4( c.z, -s.z, 0.0, 0.0,

s.z, c.z, 0.0, 0.0,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0 );

// Workaround for bug in ATI driver

rz[2][2] = 1.0;

color = vColor;

gl_Position = rz * ry * rx * vPosition;

}

Page 43: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Vertex Shader: Rotation & Shading

// www.lighthouse3d.com

varying vec3 normal, lightDir;

void main()

{

lightDir = normalize(vec3(gl_LightSource[0].position));

normal = normalize(gl_NormalMatrix * gl_Normal);

gl_Position = ftransform();

}

Page 44: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Fragment Shader: NPR (Non Photo Realistic)varying vec3 normal, lightDir;

void main() {

float intensity;

vec3 n;

vec4 color;

n = normalize(normal);

intensity = max(dot(lightDir,n),0.0);

if (intensity > 0.98)

color = vec4(0.8,0.8,0.8,1.0);

else if (intensity > 0.5)

color = vec4(0.4,0.4,0.8,1.0);

else if (intensity > 0.25)

color = vec4(0.2,0.2,0.4,1.0);

else

color = vec4(0.1,0.1,0.1,1.0);

gl_FragColor = color;

}

Page 45: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Sierpinski Gasket

Discovered by Waclaw Sierpinski in 1915

Start with a triangle

In each step, remove 25% of the remaining area

We are left with a shape with area 0

Page 46: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Sierpinski Gasket

The odd numbers in Pascal’s Triangle are in bold

Can view trios of three bold numbers as a black triangle

See also S. Wolfram’s Cellular Automata rule 90

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

Page 47: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Drawing Triangleconst int NumTimesToSubdivide = 5;const int NumTriangles = 729; const int NumVertices = 3 * NumTriangles;

vec2 points[NumVertices];int Index = 0;

voidtriangle( const vec2& a, const vec2& b, const vec2& c ){ points[Index++] = a; points[Index++] = b; points[Index++] = c;}

Page 48: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Drawing Fractalvoid divide_triangle( const vec2& a, const vec2& b,

const vec2& c, int count ){ if ( count > 0 ) { vec2 v0 = ( a + b ) / 2.0; vec2 v1 = ( a + c ) / 2.0; vec2 v2 = ( b + c ) / 2.0; divide_triangle( a, v0, v1, count - 1 ); divide_triangle( c, v1, v2, count - 1 ); divide_triangle( b, v2, v0, count - 1 ); } else { triangle( a, b, c ); // end of recursion }}

We are representing points as ordered pairs

Find the midpoints of the three sides, and recurse

Page 49: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

Drawing Triangles

void display( void ){ glClear( GL_COLOR_BUFFER_BIT ); glDrawArrays( GL_TRIANGLES, 0, NumTriangles ); glFlush();} void display( void ) // Display from example1.cpp { glClear( GL_COLOR_BUFFER_BIT ); glDrawArrays( GL_POINTS, 0, NumPoints ); glFlush(); }

Page 50: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

50

OpenGL Primitives

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTS

GL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOP

GL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

Page 51: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

51

Polygon IssuesOpenGL will only display polygons correctly that are

Simple: edges cannot cross

Convex: All points on line segment between two points in a polygon are also in the polygon

Flat: all vertices are in the same plane

User program can check if above true

OpenGL will produce output if these conditions are violated but it may not be what is desired

Triangles satisfy all conditions: we can always use triangles

nonsimple polygonnonconvex polygon

Page 52: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

52

Chaos Gamehttp://math.bu.edu/DYSYS/applets/chaos-game.html

Page 53: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

53

Chaos Game Routine

vec2 points[NumPoints];...// Select an initial point inside of the trianglepoints[0] = vec2( 0.25, 0.50 );

// compute and store N-1 new pointsfor ( int i = 1; i < NumPoints; ++i ) { int j = rand() % 3; // pick a vertex at random

// Compute point halfway between the selected vertex // and the previous point points[i] = ( points[i - 1] + vertices[j] ) / 2.0;}

Page 54: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

54

Chaos Game Main Loop

// compute and store N-1 new pointsfor ( int i = 1; i < NumPoints; ++i ) { int j = rand() % 3; // pick a vertex at random

// Compute point halfway between the selected vertex // and the previous point points[i] = ( points[i - 1] + vertices[j] ) / 2.0;}

Starting point

Second point

Second Random Vertex

Page 55: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

55

Why does this work?

In my version the starting point is in the center of the middle triangle

Each time we move halfway towards a corner, we move into a blank triangle

We will never land in a shaded triangle

So why does the resulting set look the the Sierpinski Gasket?

Page 56: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

56

Why does this work?

Imagine our random vertex is on the north, so we map all points north

At each step, our point is in the center of a blank triangle

But at every step, the new triangle is half as big, so point is closer to curve

In the limit, point lies within epsilon of the Sierpinski curve

If the choice of next vertex is not random, we do not get the full setA

B C

Page 57: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

57

Attributes

Attributes are part of the OpenGL state and determine the appearance of objects

Color (points, lines, polygons)

Size and width (points, lines)

Stipple pattern (lines, polygons)

Polygon mode

Display as filled: solid color or stipple pattern

Display edges

Display vertices

Page 58: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

58

RGB color

Each color component is stored separately in the frame buffer

Usually 8 bits per component in buffer: 24 bit color

8 bits can store numbers from 0 to 255 as Unsigned Bytes

Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), whereas in glColor3ub the values range from 0 to 255

Page 59: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

59

Indexed ColorAn alternative is Index Color

Colors are indices into tables of RGB values (see next slide for GIF scheme)

Requires less memory for

indices usually 8 bits

not as important now

Memory inexpensive

Need more colors for shading

Page 60: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

60

GIF Color MapGIF, introduced by CompuServe, uses the idea of indirection

Rather than store the value, store a reference to the valueWe have an image with 24-bit colors for each pixelTo save storage, store index into a small table of colors (color map)Rather than store 24 bits, we typically pick 256 colors and store 8 bits

Lossy if original has more than 256 colorsWorks better for the Simpsons than the Sopranos

Store the color map first, then the array of references to map

0x00ED9D 0x00ED9D 0x00ED9D 0x00ED9D 0x00ED9D

0x00ED9D 0x00ED9D 0xFF69F0 0xFF69F0 0xFF69F0

0x00ED9D 0xFF69F0 0x00ED9D 0x00ED9D 0x00ED9D

0xFF69F0 0x00ED9D 0x00ED9D 0x00ED9D 0x00ED9D

0xFF69F0 0x00ED9D 0xFF0000 0x00ED9D 0x00ED9D

0xFF69F0 0x00ED9D 0x00ED9D 0x00ED9D 0x00ED9D

0xFF69F0 0x00ED9D 0x00ED9D 0x00ED9D 0x00ED9D

0x00ED9D

0xFF69F0

0xFF0000

0 0 0 0 0

0 0 2 2 2

0 2 0 0 0

2 0 0 0 0

2 0 1 0 0

2 0 0 0 0

2 0 0 0 00xFF69F0 0x00ED9D 0x00ED9D 0xFF0000 0xFF0000 2 0 0 1 1

Original Image Compressed Image

Page 61: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

61

Color and State

The color as set by glColor becomes part of the state and will be used until changed

Colors and other attributes are not part of the object but are assigned when the object is rendered

We can create conceptual vertex colors by code such asglColor(…)glVertex(…)glColor(…)glVertex(…)

We will see an example in example4 of chapter 2

Page 62: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

62

3D Example4

/* sierpinski gasket with vertex arrays */

#include "Angel.h"

const int NumTimesToSubdivide = 4;const int NumTetrahedrons = 256;const int NumTriangles = 4*NumTetrahedrons; const int NumVertices = 3*NumTriangles;

vec3 points[NumVertices];vec3 colors[NumVertices];

int Index = 0;

Page 63: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

63

3D Example

voidtriangle( const vec3& a, const vec3& b, const vec3& c, const int color ){ static vec3 base_colors[] = {

vec3( 1.0, 0.0, 0.0 ), vec3( 0.0, 1.0, 0.0 ),vec3( 0.0, 0.0, 1.0 ), vec3( 0.0, 0.0, 0.0 )

}; points[Index] = a; colors[Index] = base_colors[color]; Index++; points[Index] = b; colors[Index] = base_colors[color]; Index++; points[Index] = c; colors[Index] = base_colors[color]; Index++;}

Page 64: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

64

3D Example

voidtetra( const vec3& a, const vec3& b, const vec3& c, const vec3& d )

{ triangle( a, b, c, 0 ); triangle( a, c, d, 1 ); triangle( a, d, b, 2 ); triangle( b, d, c, 3 );}

Page 65: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

65

Position and Color

// First, we create an empty buffer of the size we need by passing// a NULL pointer for the data valuesglBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors),

NULL, GL_STATIC_DRAW );

// Next, we load the real data in parts. We need to specify the// correct byte offset for placing the color data after the point// data in the buffer. Conveniently, the byte offset we need is// the same as the size (in bytes) of the points array, which is// returned from "sizeof(points)".glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors );

Page 66: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

66

Position and Color

// Initialize the vertex position attribute from the vertex shader GLuint vPosition = glGetAttribLocation( program, "vPosition" );glEnableVertexAttribArray( vPosition );glVertexAttribPointer( vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

// Likewise, initialize the vertex color attribute. We// need to specify the starting offset (in bytes) for the color// data. Just like loading the array, we use "sizeof(points)"// to determine the correct value.GLuint vColor = glGetAttribLocation( program, "vColor" );glEnableVertexAttribArray( vColor );glVertexAttribPointer( vColor, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) );

Page 67: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

67

Vertex Shader

attribute vec3 vPosition;attribute vec3 vColor;varying vec4 color;

voidmain(){ gl_Position = vec4(vPosition, 1.0); color = vec4( vColor, 1.0 );}

Page 68: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

68

3D Examplevoiddisplay( void ){ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glDrawArrays( GL_TRIANGLES, 0, NumVertices ); glFlush(); }

There is added complexity handing the coloras well as the position

But switch from 2 to 3 dimensions is easy1) Example4 uses default Camera settings2) The Depth Buffer (z Buffer) is used for hidden line removal

Page 69: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

69

Depth Buffervoid init( void ){ ... glEnable( GL_DEPTH_TEST ); ...}void display( void ){ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glDrawArrays( GL_TRIANGLES, 0, NumVertices ); glFlush(); }int main( int argc, char **argv ){ glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );

Page 70: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

70

Coordinate Systems

The units in glVertex are determined by the application and are called object or problem coordinates

The viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the image

Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates

OpenGL also uses some internal representations that usually are not visible to the application

Page 71: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

71

OpenGL Camera

OpenGL places a camera at the origin in object space pointing in the negative z direction

The default viewing volume is a box centered at the origin with a side of length 2

Page 72: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

72

Orthographic Viewing

z=0

z=0

In the default orthographic view, points are projected forward along the z axis onto theplane z=0

Page 73: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

73

Transformations and Viewing

In OpenGL, projection is carried out by a projection matrix (transformation)

There is only one set of transformation functions so we must set the matrix mode first glMatrixMode (GL_PROJECTION)

Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume

glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

Page 74: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

74

glMatrixModel

void glMatrixMode(GLenum mode);

mode

Specifies which matrix stack is the target for subsequent matrix operations.

Three values are accepted: GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE. The initial value is GL_MODELVIEW.

Additionally, if the ARB_imaging extension is supported, GL_COLOR is also accepted.

Page 75: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

75

3- and 2-dimensional viewing

glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

In glOrtho(left, right, bottom, top, near, far)

The near and far distances are measured from the camera

Two-dimensional vertex commands place all vertices in the plane z=0

If the application is in two dimensions, we can use the function

gluOrtho2D(left, right, bottom, top)

In two dimensions, the view or clipping volume becomes a clipping window

Page 76: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

76

Viewports

Do not have use the entire window for the image: glViewport(x,y,w,h)

Values in pixels (screen coordinates)

Page 77: 1 Graphics, Day 2 Based on lecture by Ed Angel © Ed Angel Uncredited images from Ed Angel.

77

Summation

We can define a number of different attributes in OpenGL

Color is the simplest to see

We can define a number of different objects:

points, lines, triangles, …

We can move the objects, and move the camera independently

3D is a simple extension of 2D drawing

Currently, our camera uses projection

We will be able to define a perspective camera shortly

We can use Shaders to perform computations on the GPU


Recommended