Docsity

Surface Normals - E-Commerce - Lecture Slides, Slides of Fundamentals of E-Commerce

Partial preview of the text

Download Surface Normals - E-Commerce - Lecture Slides and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

Surface Normals and Lighting VectorsNormalsLightingShading modelsBackface culling The world is curved...

Vector Math ^ Vector: A point in space.

Space can be one-dimensional...Two-dimensional...Three-dimensional...Or four--or more!

(but only at 8:14AMexactly!)

Vector Math: Sample code class^ Vec{ private:float

m_x,^

m_^ y,^

m_^ z;

public:Vector(void)

{^ x^

=^ y^ =^

z^ =^ 0;

}

Vector(float

x,^ float

y,^ float

z)

{^ m_x^

=^ x;^ m_y

=^ y;^

m_z^ =^

z;^ }

Vector(const

Vector

&V) {^ m_x^

=^ V.x();

m_y^ =

V.y();

m_z^ =

V.z();

}

float^

x()^ {

return

m_x;^

}

float^

y()^ {

return

m_y;^

}

float^

z()^ {

return

m_z;^

}

};

Vector Math ^ The

length

of a vector is found using the

Pythagorean Theorem.^ ^ P = [x, y, z]^ ^ length(P) = sqrt(x

222 +y+z

^ Putting that in code:^ public:float

length(void){ return^

sqrt(m_x*m_x

+^ m_y*m_y

+^ m_z*m_z);

} Docsity.com

Vector Math ^ If you subtract one point from another, you getthe vector which goes between them.^ ^ If you have two points in space, A and B:^ ^ then the vector V = B-A is the vector from A to B:

A

B

A

B

Vector Math: Sample code ^ More handy code: public:Vec operator +(const Vec &V){ return Vec(m_x+V.x(), m_y+V.y(), m_z+V.z()); }Vec operator -(const Vec &V){ return Vec(m_x-V.x(), m_y-V.y(), m_z-V.z()); }Vec operator (float f){ return Vec(m_xf, m_yf, m_zf); }Vec operator /(float f){ return (f!=0)? (this)(1.0/f) : (this)/0.000001; } ^ Using length() to normalize: public:Vec normalized(void) { return (this)/length(); }

Vector Math ^ Dot Products^ ^ A dot product takes two vectors and returns a scalar.^ ^ In one-dimensional numbers, a dot product /is/ multiplicationas we know it; x*y=z, all scalars. ^ A few of the properties of a dot product:^ ^ The dot product of two unit vectors is equal to the cos() of theangle between them.^ ^ If the dot product of two unit vectors is positive, the anglebetween them is

less than ninety degrees. ^ If the dot product is zero, the angle is

exactly ninety

^ If the dot product is less than zero, the angle between the twovectors is

greater than ninety degrees

C

A B

θ°

Vector Math ^ A cross product finds a vector which is

exactly

perpendicular

to the two vectors being crossed.

^ A single vector lies on a line. Three points definea^ plane

. The cross product of two vectors is a

vector parallel to the normal of their plane.^ ^ (And oddly enough, the length of the resulting vector isexactly the area of the parallelogram whose two sides areformed by the two vectors.)