To display an object comprised of faces, we must define the faces and their calculated properties in to an array or list.
The properties required to define each face are:
[fromeye] - the distance between the observer/eye and the origin of the 3D space, where both lie on the y-axis
[fov] - the field of view, defined as an angle in radians
[onscreen] - the pixel radius the screen will use to define the [fov]
P, Q, R and S are the points that define a co-planar face. 3 or more points can be used to define a face.
C is the object centre point. This is used to ensure that the face normal is pointing out of the object, which allows us to not bother displaying it, if it is on the other side of an opaque object. C is derived from the average of all the points in the object. This will working for fairly primitive objects. It may have to be manually defined with the object, if it is more complex.
f is the centre point of the face. As the face is co-planar, f can be derived from the average of the points that define the face:
( Px + Qx + Rx + Sx ) / 4
f =
( Py + Qy + Ry + Sy ) / 4
( Pz + Qz + Rz + Sz ) / 4
Each point on the face must have the 2D position and the distance from the eye calculated. This will produce a 2D coordinate that represents the 3D point relative to an onscreen origin.
The normal unit vector, n, of the face is then calculated. This is represented as a 3D vector, which defines the direction the face faces. If the object is to be opaque, the normal direction must point away from the centre of the object, allowing us to determine whether to display it or not.
To ensure that the normal vector is pointing away from the objects centre point, we must calculate the angle between the vectors:
Now we need to determine whether the face is on the rear of the object, and therefore does not need to be displayed. To do this we must calculate the angle between the vectors:
If α < π/2 or α > -π/2 then the face must be displayed
Now to calculate the proportion of the light from a light source, we must derive the vector from the light source b to the face centre point f - fb - and calculate the angle between fb and n:
Now α can be used to determine the tinting of the light source:
We will use a [tint] value that will be between -1 (darker) & 1 (lighter), where 0 defines no tinting to the colour of the face.
α must be positive number, so if (α<0) then α=-α
The relation of α to [tint] is given by:
[tint] = ( ( π/2 ) - α ) / ( π/2 )
Usually colours are represented by hex: 0xffffff or #ffffff
This is: red( ff = 255/255) , green( ff = 255/255) , blue( ff = 255/255)
Therefore, using the [tint] value we can tint the colour of the face depending on the angle to the light source.
We must now determine which faces are in front of others.
This must be performed for each face on the object in relation every other face on the same object.
For each point P on face A, build a vector from the eye to the point:
Px - eyex
eyeToPoint =
Py - eyey
Pz - eyez
eyeToPoint does not need to converted to a unit vector.
We then need to temporarily translate the points of face B to effectively move the origin to the eye location. This is very simple, just subtract [fromeye] from each points y component.
if d is between 0 and 1 (0<d<1) then the point P is infront of face B
Now repeat this process for the points P of face B, with repect to face A.
If A has points in front of B and B does not have any points in front of A then A is infront of B
If B has points in front of A and A does not have any points in front of B then B is infront of A
If A has points behind B and B does not have any points behind A then B is infront of A
If B has points behind A and A does not have any points behind B then A is infront of B
Now, sort the faces in the object, with furthest face first. If A in front of B or B in front of A could not determined (usually because of a complete intersection), then sort by the distance from the eye of the nearest points.
No conversation on this topic You must be logged in to add to the conversation