Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
CISC 3620 Lecture 5Modeling and Viewing
Topics:Building modelsRotating cube
Classical viewingComputer viewing: positioning the camera
Computer viewing: projection
Building models
3
Building models: Objectives● Introduce simple data structures for building
polygonal models– Vertex lists
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
4
Representing a Mesh● Consider a mesh
● How many nodes and edges?
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
5
Representing a Mesh● Consider a mesh
● There are 8 nodes and 12 edges– 5 interior polygons– 6 interior (shared) edges
● Each vertex has a location vi = (xi yi zi)
v1 v2
v7
v6v8
v5
v4
v3
e1
e8
e3
e2
e11
e6
e7
e10
e5
e4
e9
e12
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
6
Simple Representation● Define each polygon by the geometric locations of its
vertices● Leads to WebGL code such as this for each polygon
● Inefficient and unstructured– Consider moving a vertex to a new location– Must search all polygons for all occurrences
vertex.push(vec3(x1, y1, z1));vertex.push(vec3(x6, y6, z6));vertex.push(vec3(x7, y7, z7));
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Consider a more complicated mesh● WebGL renders triangles● But many triangles share
vertices and edges● gl.TRIANGLE_STRIP
and gl.TRIANGLE_FAN help
● Vertex lists are more general
https://commons.wikimedia.org/w/index.php?curid=10626667
https://commons.wikimedia.org/w/index.php?curid=10626667
9
Vertex Lists● Put vertex positions in an array● Use pointers from the polygons into this array● Introduce a polygon list
x1 y1 z1x2 y2 z2x3 y3 z3x4 y4 z4x5 y5 z5.x6 y6 z6x7 y7 z7x8 y8 z8
P1P2P3P4P5
v1v7v6
v8v5v6
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
10
Geometry vs Topology● Generally it is a good idea to look for data structures
that separate the geometry from the topology– Geometry: locations of the vertices– Topology: organization of the vertices and edges– Example: a heptagon (7-sided polygon)
● Topology: a heptagon is an ordered list of 7 vertices with an edge connecting successive pairs of vertices and the last to the first
● Geometry: a heptagon with its vertices at particular positions– Topology holds even if geometry changes
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
11
Vertex Lists● Put the geometry in an array● Use pointers from the vertices into this array● Introduce a polygon list
x1 y1 z1x2 y2 z2x3 y3 z3x4 y4 z4x5 y5 z5.x6 y6 z6x7 y7 z7x8 y8 z8
P1P2P3P4P5
v1v7v6
v8v5v6topology geometry
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
12
Draw cube from faces● Imagine we had a function
makeCubeFace(v1, v2, v3, v4)var makeCube( ){ // What are the faces? makeCubeFace( , , , );
}0
5 6
2
47
1
3
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
13
Draw cube from faces● Imagine we had a function
makeCubeFace(v1, v2, v3, v4)var makeCube( ){ makeCubeFace(0,3,2,1); makeCubeFace(2,3,7,6); makeCubeFace(3,0,4,7); makeCubeFace(1,2,6,5); makeCubeFace(4,5,6,7); makeCubeFace(5,4,0,1);}
0
5 6
2
47
1
3
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Rotating cube
15
Rotating cube: Objectives● Put everything together to display rotating cube● Two methods of display
– by arrays– by elements
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
16
Modeling a Cube● Define global array for vertices:var vertices = [ vec3( -0.5, -0.5, 0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( 0.5, 0.5, 0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( -0.5, -0.5, -0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( 0.5, -0.5, -0.5 ) ];
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
17
Colors
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
● Define global array for colorsvar vertexColors = [ [ 0.0, 0.0, 0.0, 1.0 ], // black [ 1.0, 0.0, 0.0, 1.0 ], // red [ 1.0, 1.0, 0.0, 1.0 ], // yellow [ 0.0, 1.0, 0.0, 1.0 ], // green [ 0.0, 0.0, 1.0, 1.0 ], // blue [ 1.0, 0.0, 1.0, 1.0 ], // magenta [ 0.0, 1.0, 1.0, 1.0 ], // cyan [ 1.0, 1.0, 1.0, 1.0 ] // white ];
18
Draw cube from facesvar makeCube( ){ makeCubeFace(0,3,2,1); makeCubeFace(2,3,7,6); makeCubeFace(3,0,4,7); makeCubeFace(1,2,6,5); makeCubeFace(4,5,6,7); makeCubeFace(5,4,0,1);}
0
5 6
2
4 7
1
3
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Note that vertices are ordered so that we obtain correct outward facing normalsEach quad generates two triangles
Do: implement makeCubeFace
20
The makeCubeFace FunctionThe less good way: draw by array
● Put position and color data for two triangles from a list of indices into the array vertices
function makeCubeFace(a, b, c, d){ var inds = [ a, b, c, a, c, d ]; for ( var i = 0; i < inds.length; ++i ) { points.push( vertices[inds[i]]); // Color by vertex colors.push( vertexColors[inds[i]] ); // Color by face //colors.push(vertexColors[a]); }}
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/gyvkxz07/2/
21
The good way:Map indices to triangles
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
var indices = [];// …function makeCubeFace(a, b, c, d){ var inds = [ a, b, c, a, c, d ]; for ( var i = 0; i < inds.length; ++i ) { indices.push(inds[i]); }}
22
The good way:Map indices to triangles
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Console.log(indices)
[1,0,3,3,2,1,2,3,7,7,6,2, 3,0,4,4,7,3,6,5,1,1,2,6,4,5,6,6,7,4,5,4,0,0,1,5];
Rendering by Elements● Send vertices and colors to GPU directlygl.bufferData(gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW);// ...gl.bufferData(gl.ARRAY_BUFFER, flatten(vertexColors), gl.STATIC_DRAW);
● Send indices of triangles to GPUvar iBuffer = gl.createBuffer();gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, iBuffer);gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
new Uint8Array(indices), gl.STATIC_DRAW);
23Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Render Function: drawElements() instead of drawArray()
24Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
function render(){ gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); theta[1] += 1; gl.uniform3fv(thetaLoc, theta); gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_BYTE, 0 ); requestAnimFrame( render );}
● Even more compact to combine with gl.TRIANGLE_STRIP or gl.TRIANGLE_FAN
Do: update to use gl.drawElements
Classical viewing
https://jsfiddle.net/asterix77/gyvkxz07/2/
27
Classical viewing: Objectives● Introduce the classical views● Compare and contrast image formation by
computer with how images have been formed by architects, artists, and engineers
● Learn the benefits and drawbacks of each type of view
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
28
Classical Viewing● Viewing requires three basic elements
– One or more objects– A viewer with a projection surface– Projectors that go from the object(s) to the projection surface
● Classical views are based on the relationship among these elements– The viewer picks up the object and orients it how she would like to see it
● Each object is assumed to constructed from flat principal faces – Buildings, polyhedra, manufactured objects
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
29
Planar Geometric Projections● Standard projections project onto a plane● Projectors are lines that either
– converge at a center of projection– are parallel
● Such projections preserve lines– but not necessarily angles
● Nonplanar projections are needed for applications such as map construction (flattening a sphere)
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
30
Classical Projections
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Do: Viewing a cube
32
Perspective vs Parallel● Computer graphics treats all projections the same
and implements them with a single pipeline● Classical viewing developed different techniques for
drawing each type of projection● Fundamental distinction is between parallel and
perspective viewing even though mathematically parallel viewing is the limit of perspective viewing
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/v4ecdt9b/50/
33
Taxonomy of Planar Geometric Projections
parallel perspective
axonometric multivieworthographic
oblique
isometric dimetric trimetric
2 point1 point 3 point
planar geometric projections
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
34
Perspective Projection
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
35
Parallel Projection
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
36
Orthographic Projection
Projectors are orthogonal to projection surface
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
37
Multiview Orthographic Projection● Projection plane parallel to principal face● Usually form front, top, side views
isometric (for context, not multivieworthographic view)
front
sidetop
in CAD and architecture, we often display three multiviews plus isometric
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Do: multiview orthographic projections in cube viewer
39
Advantages and Disadvantages● Preserves both distances and angles
– Shapes preserved– Can be used for measurements
● Building plans● Manuals
● Cannot see what object really looks like because many surfaces hidden from view– Often we add the isometric
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/v4ecdt9b/50/
40
Axonometric Projections
Allow projection plane to move relative to object
classify by how many angles ofa corner of a projected cube are the same
none: trimetrictwo: dimetricthree: isometric
1
32
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
41
Types of Axonometric Projections
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Do: Axonometric projections in cube viewer
43
Advantages and Disadvantages● Lines are scaled (foreshortened) but can find scaling factors● Lines preserved but angles are not
– Projection of a circle in a plane not parallel to the projection plane is an ellipse● Can see three principal faces of a box-like object● Some optical illusions possible
– Parallel lines appear to diverge● Does not look real because far objects are scaled the same as near
objects● Used in CAD applications
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/v4ecdt9b/50/
Axonometric optical illusions
https://en.wikipedia.org/wiki/Axonometric_projection
45
Oblique Projection
Arbitrary relationship between projectors and projection plane
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
46
Advantages and Disadvantages● Can pick the angles to emphasize a particular face
– Architecture: plan oblique, elevation oblique● Angles in faces parallel to projection plane are preserved while
we can still see “around” side
● In physical world, cannot create with simple camera; possible with bellows camera or special lens (architectural)
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Oblique projection in physical architectural photography
Original Perspective corrected
https://en.wikipedia.org/wiki/Perspective_control
48
Perspective Projection
Projectors converge at center of projection
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
49
Vanishing Points● Parallel lines (not parallel to the projection plane) on the
object converge at a single point in the projection (the vanishing point)
● Drawing simple perspectives by hand uses these vanishing point(s)
vanishing point
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
http://mathworld.wolfram.com/VanishingPoint.html
50
Three-Point Perspective● No principal face parallel to projection plane● Three vanishing points for cube
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
51
Two-Point Perspective● On principal direction parallel to projection plane● Two vanishing points for cube
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
52
One-Point Perspective ● One principal face parallel to projection plane● One vanishing point for cube
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Do: 1-, 2-, 3-point perspectivein cube viewer
54
Advantages and Disadvantages● Objects further from viewer are projected smaller than the
same sized objects closer to the viewer (diminution)– Looks realistic
● Equal distances along a line are not projected into equal distances (nonuniform foreshortening)
● Angles preserved only in planes parallel to the projection plane
● More difficult to construct by hand than parallel projections (but not more difficult by computer)
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/v4ecdt9b/50/
Computer viewing: Positioning the camera
56
Computer viewing: Objectives● Introduce the mathematics of projection● Introduce WebGL viewing functions in MV.js● Look at alternate viewing APIs
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
57
Computer Viewing● There are three aspects of the viewing process, all of
which are implemented in the pipeline,– Positioning the camera
● Setting the model-view matrix– Selecting a lens
● Setting the projection matrix– Clipping
● Setting the view volume
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
58
Default WebgL Projection
Default projection is orthogonal
clipped out
z=0
2
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
59
The WebGL Camera
● In WebGL, initially the object and camera frames are the same– Default model-view matrix is an identity
● The camera is located at origin and points in the negative z direction
● WebGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin – Default projection matrix is an identity
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
60
Moving the Camera Frame● If we want to visualize objects with both positive and negative z
values we can either– Move the camera in the positive z direction
● Translate the camera frame– Move the objects in the negative z direction
● Translate the world frame
● Both of these views are equivalent and are determined by the model-view matrix, which is applied to objects– Want a translation: translate(0.0,0.0,-d);– d > 0
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
61
Moving Camera back from Origin
default frames
frames after translation by –d d > 0
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
62
Moving the Camera
● We can move the camera to any desired position by a sequence of rotations and translations
● Example: side view
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
63
Moving the Camera
● We can move the camera to any desired position by a sequence of rotations and translations
● Example: side view– Move the camera away from the origin– Rotate it around the origin– Model-view matrix C = TR
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
64
WebGL code● Remember that last transformation specified is first to be
applied
// Using MV.jsvar t = translate (0.0, 0.0, -d);var ry = rotateY(90.0);var m = mult(t, ry);orvar m = mult(translate (0.0, 0.0, -d), rotateY(90.0););
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
65
lookAt
LookAt(eye, at, up)
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
66
The lookAt Function
● MV.js contains the function lookAt to form the required modelview matrix through a simple interface
● Note the need for setting an up direction● Can concatenate with modeling transformations● Example: isometric view of cube aligned with axes
var eye = vec3(1.0, 1.0, 1.0);var at = vec3(0.0, 0.0, 0.0);var up = vec3(0.0, 1.0, 0.0);
var mv = LookAt(eye, at, up);
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
67
Other Viewing APIs
● The LookAt function is only one possible API for positioning the camera
● Others include– View reference point, view plane normal, view up
(PHIGS, GKS-3D)– Yaw, pitch, roll– Elevation, azimuth, twist– Direction angles
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Computer viewing: Projection
69
Projection: Objectives● Introduce the mathematics of projection● Add WebGL projection functions in MV.js
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
70
Projections and Normalization
● The default projection in the eye (camera) frame is orthogonal ● For points within the default view volume
xp = xyp = yzp = 0
● Most graphics systems use view normalization– All other views are converted to the default view by transformations
that determine the projection matrix– Allows use of the same pipeline for all views
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
71
Homogeneous Coordinate Representation
xp = xyp = yzp = 0wp = 1
pp = Mp
M =
default orthographic projection
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
72
Homogeneous Coordinate Representation
xp = xyp = yzp = 0wp = 1
pp = Mp
M =
1000000000100001
In practice, we can let M = I and set the z term to zero later
default orthographic projection
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
73
Simple Perspective
● Center of projection at the origin● Projection plane z = d, d < 0
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
74
Perspective Equations
Consider top and side views
xp =
dzx/
dzx/
yp =dz
y/
zp = d
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
75
Homogeneous Coordinate Form
M = consider p = Mq where
1zyx
dzzyx
/
q = p =
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
76
Homogeneous Coordinate Form
M =
0/100010000100001
d
consider p = Mq where
1zyx
dzzyx
/
q = p =
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
77
Perspective Division
● However w 1, so we must divide by w to return from homogeneous coordinates
● This perspective division yields
the desired perspective equations ● We will consider the corresponding clipping volume with
MV.js functions that are equivalent to deprecated OpenGL functions
xp =
dzx/
yp =
dzy/
zp = d
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
78
WebGL Orthogonal Viewing
ortho(left,right,bottom,top,near,far)
near and far measured from camera
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
79
WebGL Perspective
frustum(left,right,bottom,top,near,far)
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
“View frustum”
https://commons.wikimedia.org/wiki/File:ViewFrustum.svg
80
Using Field of View
● With frustum it is often difficult to get the desired view
● perspective(fovy, aspect, near, far) often provides a better interface
aspect = w/h
front plane
Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
Look at code of cube viewer
Computing Matrices: perspective2.js
● Compute in JS file, send to vertex shader with gl.uniformMatrix4fv
● Dynamic: update in render() or shader
82Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
https://jsfiddle.net/asterix77/v4ecdt9b/50/
perspective2.js
83Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
var render = function(){ gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); eye = vec3(radius*Math.sin(theta)*Math.cos(phi), radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta)); modelViewMatrix = lookAt(eye, at , up); projectionMatrix = perspective(fovy, aspect, near, far); gl.uniformMatrix4fv( modelViewMatrixLoc, false, flatten(modelViewMatrix) ); gl.uniformMatrix4fv( projectionMatrixLoc, false, flatten(projectionMatrix) ); gl.drawArrays( gl.TRIANGLES, 0, NumVertices ); requestAnimFrame(render);
}
https://www.cs.unm.edu/~angel/WebGL/7E/05/perspective2.html
vertex shader
84Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015
attribute vec4 vPosition;attribute vec4 vColor;varying vec4 fColor;uniform mat4 modelViewMatrix;uniform mat4 projectionMatrix;
void main() { gl_Position = projectionMatrix*modelViewMatrix*vPosition; fColor = vColor;
}
https://www.cs.unm.edu/~angel/WebGL/7E/05/perspective2.html
Summary● Building models● Rotating cube● Classical viewing● Computer viewing: positioning the camera● Computer viewing: projection
Slide 1Slide 2ObjectivesRepresenting a MeshSlide 5Simple RepresentationSlide 7Vertex ListsGeometry vs TopologySlide 11Draw cube from facesSlide 13Slide 14Slide 15Modeling a CubeColorsSlide 18Slide 19The quad FunctionMapping indices to facesSlide 22Rendering by ElementsSlide 24Slide 25Slide 26Slide 27Slide 28Planar Geometric ProjectionsClassical ProjectionsSlide 31Perspective vs ParallelTaxonomy of Planar Geometric ProjectionsPerspective ProjectionParallel ProjectionOrthographic ProjectionMultiview Orthographic ProjectionSlide 38Advantages and DisadvantagesAxonometric ProjectionsTypes of Axonometric ProjectionsSlide 42Slide 43Slide 44Oblique ProjectionSlide 46Slide 47Slide 48Vanishing PointsThree-Point PerspectiveTwo-Point PerspectiveOne-Point PerspectiveSlide 53Slide 54Slide 55Slide 56Computer ViewingDefault ProjectionThe WebGL CameraMoving the Camera FrameMoving Camera back from OriginMoving the CameraSlide 63WebGL codelookAtThe lookAt FunctionOther Viewing APIsSlide 68Slide 69Projections and NormalizationHomogeneous Coordinate RepresentationSlide 72Simple PerspectivePerspective EquationsHomogeneous Coordinate FormSlide 76Perspective DivisionWebGL Orthogonal ViewingWebGL PerspectiveUsing Field of ViewSlide 81Computing Matricespersepctive2.jsvertex shaderSlide 85