Building Models. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Similar documents
CS452/552; EE465/505. Models & Viewing

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Introduction to Computer Graphics with WebGL

Classical and Computer Viewing. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Overview. Viewing and perspectives. Planar Geometric Projections. Classical Viewing. Classical views Computer viewing Perspective normalization

One or more objects A viewer with a projection surface Projectors that go from the object(s) to the projection surface

Three-Dimensional Graphics III. Guoying Zhao 1 / 67

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Review & Examples

3D Viewing. With acknowledge to: Ed Angel. Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Announcement. Project 1 has been posted online and in dropbox. Due: 11:59:59 pm, Friday, October 14

3D Viewing. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

3D Viewing. Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

CS452/552; EE465/505. Transformations

Computer Viewing. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Introduction to Computer Graphics 4. Viewing in 3D

Computer Graphics. Jeng-Sheng Yeh 葉正聖 Ming Chuan University (modified from Bing-Yu Chen s slides)

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Computer Viewing Computer Graphics I, Fall 2008

Computer Viewing. CITS3003 Graphics & Animation. E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

Viewing with Computers (OpenGL)

Chapter 5. Projections and Rendering

Announcements. Submitting Programs Upload source and executable(s) (Windows or Mac) to digital dropbox on Blackboard

CS452/552; EE465/505. Intro to Lighting

CS 432 Interactive Computer Graphics

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

Computer Viewing. Prof. George Wolberg Dept. of Computer Science City College of New York

Transformations. Rotation and Scaling

Computer Viewing and Projection. Overview. Computer Viewing. David Carr Fundamentals of Computer Graphics Spring 2004 Based on Slides by E.

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Programming with OpenGL Complete Programs Objectives Build a complete first program

CS452/552; EE465/505. Image Formation

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

2D and 3D Viewing Basics

Fundamental Types of Viewing

CS559 Computer Graphics Fall 2015

CS354 Computer Graphics Viewing and Modeling

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

Lecture 4. Viewing, Projection and Viewport Transformations

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Computer Graphics. P05 Viewing in 3D. Part 1. Aleksandra Pizurica Ghent University

Three Main Themes of Computer Graphics

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

3D Polygon Rendering. Many applications use rendering of 3D polygons with direct illumination

Overview of Projections: From a 3D world to a 2D screen.

So we have been talking about 3D viewing, the transformations pertaining to 3D viewing. Today we will continue on it. (Refer Slide Time: 1:15)

CS 418: Interactive Computer Graphics. Projection

Virtual Cameras and The Transformation Pipeline

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Transformations. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

QUESTION BANK 10CS65 : COMPUTER GRAPHICS AND VISUALIZATION

Overview. By end of the week:

Viewing and Projection

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

Viewing. Cliff Lindsay, Ph.D. WPI

Building Models. Objectives. Introduce simple data structures for building polygonal models. OpenGL vertex arrays. Vertex lists Edge lists

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

COMP Computer Graphics and Image Processing. a6: Projections. In part 2 of our study of Viewing, we ll look at. COMP27112 Toby Howard

CHAPTER 1 Graphics Systems and Models 3

CS 4204 Computer Graphics

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Computer Graphics - Treasure Hunter

Transformation, perspective projection, and LookAT in. Ming Ouhyoung, September 2018

Chapter 8 Three-Dimensional Viewing Operations

CS 4620 Midterm, October 23, 2018 SOLUTION

1 Transformations. Chapter 1. Transformations. Department of Computer Science and Engineering 1-1

For each question, indicate whether the statement is true or false by circling T or F, respectively.

3.1 Viewing and Projection

Viewing and Projection

Building Models. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CSC 470 Computer Graphics. Three Dimensional Viewing

CS452/552; EE465/505. Lighting & Shading

Chap 7, 2008 Spring Yeong Gil Shin

CS 130 Final. Fall 2015

GEOMETRIC TRANSFORMATIONS AND VIEWING

Introduction to Computer Graphics with WebGL

Introduce the basic input devices Input, Interaction and Animation

MAE : Lecture #12 - Projection and Perspective. Lecture Overview:

Geometry: Outline. Projections. Orthographic Perspective

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

We assume that you are familiar with the following:

COMP3421. Introduction to 3D Graphics

Display Lists. Conceptually similar to a graphics file. In client-server environment, display list is placed on server

COMP3421. Introduction to 3D Graphics

CSC 470 Computer Graphics

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Virtual Environments

Computer Graphics (CS 543) Lecture 6a: Viewing & Camera Control. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

Introduction to Computer Graphics with WebGL

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Game Architecture. 2/19/16: Rasterization

SUMMARY. CS380: Introduction to Computer Graphics Projection Chapter 10. Min H. Kim KAIST School of Computing 18/04/12. Smooth Interpolation

GEOMETRIC OBJECTS AND TRANSFORMATIONS I

Transcription:

Building Models 1

Objectives Introduce simple data structures for building polygonal models Vertex lists Edge lists 2

Representing a Mesh Consider a mesh v 5 v 6 e e e 3 v 9 8 8 v e 4 1 e 11 e v v 7 7 1 e v e 2 12 6 There are 8 nodes and 12 edges 5 interior polygons 6 interior (shared) edges Each vertex has a location v i = (x i y i z i ) e 2 e 5 e 10 v 3 e 4 3

Simple Representation Define each polygon by the geometric locations of its vertices Leads to WebGL code such as vertex.push(vec3(x1, y1, z1)); vertex.push(vec3(x6, y6, z6)); vertex.push(vec3(x7, y7, z7)); Inefficient and unstructured Consider moving a vertex to a new location Must search for all occurrences 4

Inward and Outward Facing Polygons The order {v 1, v 6, v 7 } and {v 6, v 7, v 1 } are equivalent in that the same polygon will be rendered by OpenGL but the order {v 1, v 7, v 6 } is different The first two describe outwardly facing polygons Use the right-hand rule = counter-clockwise encirclement of outward-pointing normal OpenGL can treat inward and outward facing polygons differently 5

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 polygon is an ordered list of vertices with an edge connecting successive pairs of vertices and the last to the first Topology holds even if geometry changes 6

Vertex Lists Put the geometry in an array Use pointers from the vertices into this array Introduce a polygon list P1 P2 P3 P4 P5 topology v 1 v 7 v 6 v 8 v 5 v 6 geometry x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 x 8 y 8 z 8 7

Shared Edges Vertex lists will draw filled polygons correctly but if we draw the polygon by its edges, shared edges are drawn twice Can store mesh by edge list 8

Edge List e1 e2 e3 e4 e5 e6 e7 e8 e9 v1 v6 x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 x 8 y 8 z 8 v 6 v 8 v 5 e 1 e 8 e 3 e 11 v e v 7 7 1 e v e 2 12 6 e 2 e 9 e 5 e 10 Note polygons are not represented v 3 e 4 9

Draw cube from faces var colorcube( ) { quad(0,3,2,1); quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(0,1,5,4); } 1 0 5 6 2 4 7 3 10

The Rotating Square 11

Objectives Put everything together to display rotating cube Two methods of display by arrays by elements 12

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 ) ]; 13

Colors Define global array for colors var 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 ]; 14

Draw cube from faces function colorcube( ) { quad(0,3,2,1); quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(0,1,5,4); } 1 5 6 2 4 7 0 Note that vertices are ordered so that we obtain correct outward facing normals Each quad generates two triangles 3 15

Initialization var canvas, gl; var numvertices = 36; var points = []; var colors = []; window.onload = function init(){ canvas = document.getelementbyid( "gl-canvas" ); gl = WebGLUtils.setupWebGL( canvas ); colorcube(); gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearcolor( 1.0, 1.0, 1.0, 1.0 ); gl.enable(gl.depth_test); 16 // rest of initialization and html file // same as previous examples

The quad Function Put position and color data for two triangles from a list of indices into the array vertices var quad(a, b, c, d) { var indices = [ a, b, c, a, c, d ]; for ( var i = 0; i < indices.length; ++i ) { points.push( vertices[indices[i]]); colors.push( vertexcolors[indices[i]] ); // for solid colored faces use //colors.push(vertexcolors[a]); 17 } }

Render Function function render(){ gl.clear( gl.color_buffer_bit gl.depth_buffer_bit); gl.drawarrays( gl.triangles, 0, numvertices ); requestanimframe( render ); } 18

Mapping indices to faces 19 var 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 indices to GPU var ibuffer = gl.createbuffer(); gl.bindbuffer(gl.element_array_buffer, ibuffer); gl.bufferdata(gl.element_array_buffer, new Uint8Array(indices), gl.static_draw); Render by elements gl.drawelements( gl.triangles, numvertices, gl.unsigned_byte, 0 ); Even more efficient if we use triangle strips or triangle fans 20

Adding Buttons for Rotation var xaxis = 0; var yaxis = 1; var zaxis = 2; var axis = 0; var theta = [ 0, 0, 0 ]; var thetaloc; document.getelementbyid( "xbutton" ).onclick = function () { axis = xaxis; }; document.getelementbyid( "ybutton" ).onclick = function () { axis = yaxis; }; document.getelementbyid( "zbutton" ).onclick = function () { axis = zaxis; }; 21

Render Function function render(){ gl.clear( gl.color_buffer_bit gl.depth_buffer_bit); theta[axis] += 2.0; gl.uniform3fv(thetaloc, theta); gl.drawarrays( gl.triangles, 0, numvertices ); requestanimframe( render ); } 22

Classical Viewing 23

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 24

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 25

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 26

Classical Projections 27

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 28

Taxonomy of Planar Geometric Projections planar geometric projections parallel perspective multiview axonometric orthographic 1 point 2 point 3 point oblique isometric dimetric trimetric 29

Perspective Projection 30

Parallel Projection 31

Orthographic Projection Projectors are orthogonal to projection surface 32

Multiview Orthographic Projection Projection plane parallel to principal face Usually form front, top, side views isometric (not multiview orthographic view) front in CAD and architecture, we often display three multiviews plus isometric top side 33

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 34

Axonometric Projections Allow projection plane to move relative to object classify by how many angles of a corner of a projected cube are the same none: trimetric two: dimetric three: isometric θ 2 θ 1 θ 3 35

Types of Axonometric Projections 36

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 37

Oblique Projection Arbitrary relationship between projectors and projection plane 38

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) 39

Perspective Projection Projectors coverge at center of projection 40

Vanishing Points Parallel lines (not parallel to the projection plan) 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 41

Three-Point Perspective No principal face parallel to projection plane Three vanishing points for cube 42

Two-Point Perspective On principal direction parallel to projection plane Two vanishing points for cube 43

One-Point Perspective One principal face parallel to projection plane One vanishing point for cube 44

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) 45

Computer Viewing Positioning the Camera 46

Objectives Introduce the mathematics of projection Introduce WebGL viewing functions in MV.js Look at alternate viewing APIs 47

From the Beginning In the beginning: fixed function pipeline Model-View and Projection Transformation Predefined frames: model, object, camera, clip, ndc, window After deprecation pipeline with programmable shaders no transformations clip, ndc window frames MV.js reintroduces original capabilities 48

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 49

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 50

Default Projection Default projection is orthogonal clipped out 2 z=0 51

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 Want a translation (translate(0.0,0.0,-d);) d > 0 52

Moving Camera back from Origin frames after translation by d d > 0 default frames 53

Moving the Camera We can move the camera to any desired position by a sequence of rotations and translations Example: side view Rotate the camera Move it away from origin Model-view matrix C = TR 54

WebGL code Remember that last transformation specified is first to be applied // Using MV.js var t = translate (0.0, 0.0, -d); var ry = rotatey(90.0); var m = mult(t, ry); or var m = mult(translate (0.0, 0.0, -d), rotatey(90.0);); 55

lookat LookAt(eye, at, up) 56

The lookat Function The GLU library contained the function glulookat to form the required modelview matrix through a simple interface Note the need for setting an up direction Replaced by lookat() in MV.js 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); 57 var mv = LookAt(eye, at, up);

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 58

Computer Viewing Projection 59

Objectives Introduce the mathematics of projection Add WebGL projection functions in MV.js 60

Projections and Normalization The default projection in the eye (camera) frame is orthogonal For points within the default view volume x p = x y p = y z p = 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 61

Homogeneous Coordinate Representation x p = x y p = y z p = 0 w p = 1 default orthographic projection p p = Mp M = 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 In practice, we can let M = I and set the z term to zero later 62

Simple Perspective Center of projection at the origin Projection plane z = d, d < 0 63

Perspective Equations Consider top and side views x z / d x p = z x / d y p = z y / d z p = d 64

65 Homogeneous Coordinate Form M = 0 1/ 0 0 0 1 0 0 0 0 1 0 0 0 0 1 d consider q = Mp where 1 z y x d z z y x / q = p =

Perspective Division However w 1, so we must divide by w to return from homogeneous coordinates This perspective division yields x p = z x / d y p = z y / d z p = d the desired perspective equations We will consider the corresponding clipping volume with mat.h functions that are equivalent to deprecated OpenGL functions 66

WebGL Orthogonal Viewing ortho(left,right,bottom,top,near,far) near and far measured from camera 67

WebGL Perspective frustum(left,right,bottom,top,near,far) 68

Using Field of View With frustum it is often difficult to get the desired view perpective(fovy, aspect, near, far) often provides a better interface front plane aspect = w/h 69

Computing Matrices Compute in JS file, send to vertex shader with gl.uniformmatrix4fv Dynamic: update in render() or shader 70

persepctive2.js 71 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); }

vertex shader attribute vec4 vposition; attribute vec4 vcolor; varying vec4 fcolor; uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; void main() { gl_position = projectionmatrix*modelviewmatrix*vposition; fcolor = vcolor; } 72

The Virtual Trackball 73

Objectives This is an optional lecture that Introduces the use of graphical (virtual) devices that can be created using WebGL Reinforce the benefit of not using direction angles and Euler angles Makes use of transformations Leads to reusable code that will be helpful later 74

Physical Trackball The trackball is an upside down mouse If there is little friction between the ball and the rollers, we can give the ball a push and it will keep rolling yielding continuous changes Two possible modes of operation Continuous pushing or tracking hand motion Spinning 75

A Trackball from a Mouse Problem: we want to get the two behavior modes from a mouse We would also like the mouse to emulate a frictionless (ideal) trackball Solve in two steps Map trackball position to mouse position Use event listeners to handle the proper modes 76

Using Quaternions Quaternion arithmetic works well for representing rotations around the origin Can use directly avoiding rotation matrices in the virtual trackball Code was made available long ago (pre shader) by SGI Quaternion shaders are simple 77

Trackball Frame origin at center of ball 78

Projection of Trackball Position We can relate position on trackball to position on a normalized mouse pad by projecting orthogonally onto pad 79

Reversing Projection Because both the pad and the upper hemisphere of the ball are twodimensional surfaces, we can reverse the projection A point (x,z) on the mouse pad corresponds to the point (x,y,z) on the upper hemisphere where y = 2 2 2 r x z if r x 0, r z 0 80

Computing Rotations Suppose that we have two points that were obtained from the mouse. We can project them up to the hemisphere to points p 1 and p 2 These points determine a great circle on the sphere We can rotate from p 1 to p 2 by finding the proper axis of rotation and the angle between the points 81

Using the cross product The axis of rotation is given by the normal to the plane determined by the origin, p 1, and p 2 n = p 1 p 2 82

Obtaining the angle The angle between p 1 and p 2 is given by sin θ = n p 1 p 2 If we move the mouse slowly or sample its position frequently, then θ will be small and we can use the approximation sin θ θ 83

Implementing with WebGL Define actions in terms of three booleans trackingmouse: if true update trackball position redrawcontinue: if true, idle function posts a redisplay trackballmove: if true, update rotation matrix 84

Vertex Shader I in vec4 vposition; in vec4 vcolor; out vec4 color; uniform vec4 rquat; // rotation quaternion // quaternion multiplier vec4 multq(vec4 a, vec4 b) { return(vec4(a.x*b.x - dot(a.yzw, b.yzw), a.x*b.yzw+b.x*a.yzw+cross(b.yzw, a.yzw))); } 85

Vertex Shader II // inverse quaternion vec4 invq(vec4 a) { return(vec4(a.x, -a.yzw)/dot(a,a)); } 86 void main() { vec3 axis = rquat.yxw; float theta = rquat.x; vec4 r, p; p = vec4(0.0, vposition.xyz); // input point quaternion p = multq(rquat, multq(p, invq(rquat))); // rotated point quaternion gl_position = vec4( p.yzw, 1.0); // back to homogeneous coordinates color = vcolor; }