3d

Methods

inner Clip3D(type, min_z, max_z, v) → {Array.<V3D>}

Clips the polygon given in `v'. The frustum (viewing volume) is defined by -z<x<z, -z<y<z, 0<min_z<z<max_z. If max_z<=min_z, the z<max_z clipping is not done. As you can see, clipping is done in the camera space, with perspective in mind, so this routine should be called after you apply the camera matrix, but before the perspective projection. The routine will correctly interpolate u, v, and c in the vertex structure. However, no provision is made for high/truecolor GCOL.
Parameters:
Name Type Description
type POLYTYPE one of POLYTYPE.
min_z number minimum z value.
max_z number maximum z value.
v Array.<V3D> an array of vertices.
Returns:
Array.<V3D> - an array of vertices.

inner Polygon3D(type, texture, v)

Draw 3d polygons using the specified rendering mode. Unlike the regular polygon() function, these routines don't support concave or self-intersecting shapes. The width and height of the texture bitmap must be powers of two, but can be different, eg. a 64x16 texture is fine How the vertex data is used depends on the rendering mode:

The `x' and `y' values specify the position of the vertex in 2d screen coordinates.

The `z' value is only required when doing perspective correct texture mapping, and specifies the depth of the point in 3d world coordinates.

The `u' and `v' coordinates are only required when doing texture mapping, and specify a point on the texture plane to be mapped on to this vertex. The texture plane is an infinite plane with the texture bitmap tiled across it. Each vertex in the polygon has a corresponding vertex on the texture plane, and the image of the resulting polygon in the texture plane will be mapped on to the polygon on the screen.

We refer to pixels in the texture plane as texels. Each texel is a block, not just a point, and whole numbers for u and v refer to the top-left corner of a texel. This has a few implications. If you want to draw a rectangular polygon and map a texture sized 32x32 on to it, you would use the texture coordinates (0,0), (0,32), (32,32) and (32,0), assuming the vertices are specified in anticlockwise order. The texture will then be mapped perfectly on to the polygon. However, note that when we set u=32, the last column of texels seen on the screen is the one at u=31, and the same goes for v. This is because the coordinates refer to the top-left corner of the texels. In effect, texture coordinates at the right and bottom on the texture plane are exclusive.

There is another interesting point here. If you have two polygons side by side sharing two vertices (like the two parts of folded piece of cardboard), and you want to map a texture across them seamlessly, the values of u and v on the vertices at the join will be the same for both polygons. For example, if they are both rectangular, one polygon may use (0,0), (0,32), (32,32) and (32,0), and the other may use (32,0), (32,32), (64,32), (64,0). This would create a seamless join. Of course you can specify fractional numbers for u and v to indicate a point part-way across a texel. In addition, since the texture plane is infinite, you can specify larger values than the size of the texture. This can be used to tile the texture several times across the polygon.
Parameters:
Name Type Description
type POLYTYPE one of POLYTYPE.
texture Bitmap texture Bitmap.
v Array.<V3D> an array of vertices.

inner Quad3D(type, texture, v1, v2, v3, v4)

Draw 3d quads using vertex.
Parameters:
Name Type Description
type POLYTYPE one of POLYTYPE.
texture Bitmap texture Bitmap.
v1 V3D a vertex.
v2 V3D a vertex.
v3 V3D a vertex.
v4 V3D a vertex.

inner SetSceneGap(gap)

This number (default value = 100.0) controls the behaviour of the z-sorting algorithm. When an edge is very close to another's polygon plane, there is an interval of uncertainty in which you cannot tell which object is visible (which z is smaller). This is due to cumulative numerical errors for edges that have undergone a lot of transformations and interpolations.
The default value means that if the 1/z values (in projected space) differ by only 1/100 (one percent), they are considered to be equal and the x-slopes of the planes are used to find out which plane is getting closer when we move to the right.
Larger values means narrower margins, and increasing the chance of missing true adjacent edges/planes. Smaller values means larger margins, and increasing the chance of mistaking close polygons for adjacent ones. The value of 100 is close to the optimum. However, the optimum shifts slightly with resolution, and may be application-dependent. It is here for you to fine-tune.
Parameters:
Name Type Description
gap number gap value.

inner Triangle3D(type, texture, v1, v2, v3)

Draw 3d triangles, using vertices.
Parameters:
Name Type Description
type POLYTYPE one of POLYTYPE.
texture Bitmap texture Bitmap.
v1 V3D a vertex.
v2 V3D a vertex.
v3 V3D a vertex.