By KACI Lounes. 2009

Overview:

Learn how to create realistic images from 3-Dimensional scenes designed by computer, using pure

VB code in a flexible and well-optimised ray-tracer, over many combinations of complex 3D algorithms.

An important note, is that is HIGHTLY recommended to compile the source-code files to .EXE format

before running the program, using a powerful PC as possible, and finally, closing all unneeded applications.

With the first publication on the Internet (v 1.00), this work takes more than one year to be achieved,

resuming all my knowledge (so far) in the domain of 3D computer-graphics.

Contents

1- Modelling:

1.1 Modelling primitives

1.2 Importing geometry from .3DO files (internal file format)

1.3 Importing geometry from .X files (Microsoft® DirectX® file format)

1.4 Managing 3D objects

1.4.1 Attaching geometry

1.4.2 Perform a copy operation

1.4.3 User-defined world/view matrices

1.4.4 Adjusting world coordinates (Translation, Rotations & Scales)

1.4.5 Materials assignment

1.4.5.1 Alpha mapping channel (cut mapping)

1.4.5.2 Specular highlighting

1.4.5.3 Including Color maps

1.4.5.4 Including Reflections, Refractions (with the index of refraction)

2- Lighting:

2.1 Integrating lighting

3- 3D Viewing:

3.1 Adding cameras

3.2 Viewports (previewing windows)

4- 3D Settings:

4.1 Memory allocations

4.2 Adjusting the stochastic Path-Tracer options

4.3 Global illumination technique: Photon mapping

4.4 Adjusting the texture-mapping filters (anti-minification & anti-magnification)

4.5 Selecting the shadows mode

4.6 Adding a Fog effect

4.7 Adding a background image

1. Modelling

The first step in the construction of a scene is the modelling process, basically, we model a scene by

first modelling the objects that the scene contains, then we assemble those objects at once.

A 3D object is composed by a certain number of vertices and faces, a 'Vertex', is just a 3D location

(X,Y,Z point from the Origin 0,0,0) in space, a cube has 8 vertices.. a rectangle has 4..and so on...

The faces are the areas between vertices, the minimum unit to define a 3D area (or surface) is a

triangle (3 vertices). In 3DDRE, only triangular surfaces are taken into account.

An 3D object uses also many texture channels, a texture is just a 2D image (bitmap) used to add

high level of details on the faces, instead using just a single value at the entire surface of a triangle.

Some texture-channels are in 8-Bits format (alpha, reflection...) others are in 24-Bites format (color maps).

Complicated objects use many vertices and faces to be correctly defined, but also large textures,

its easy to see that complex objects uses a lot of memory and rendering time, so we must take care

of the computational and storage costs in the modelling process.

In 3DDRE, many choices are in your disposition, you can create parametrically-defined objects

(Primitives), or import objects from .3DO files (internal file format) or importing MS® DirectX®

files (.X).

1.1 Creating Primitives

Primitives are parametrically-defined objects, for example, we can define a sphere just by the

location in 3D space (center) and the radius, in a second step, those objects are converted to triangular

objects format by defining a maximum resolution (faces count).

3DDRE include the following Primitives:

Box, Capsule, Cone, Cylinder, Disk, Grid, Hemisphere, Octahedron

Pyramid, Rectangle, Sphere, Tetrahedron, Torus, Tube, Flat-Tube

But also some specials:

Landscape (a grid modified by a displacement mapping)

Cornell® box

1.2 Importation from .3DO files

Like many 3D softwares, 3DDRE uses an internal file format to import 3D objects directly

to the current work, you can model an object, export that object into .3DO file for later uses,

then just re-import the file, and we done, in the way of ‘Prêt à porter’ !

Note that all the files in 3DDRE use internal textures, so all the things are in a single file.

1.3 Importation from Microsoft® DirectX® files (.X)

If is not enough, you can import .X files, pre-modelled by your favourite 3D software, note

well that is the only reason that we use the Microsoft® DirectX® 7 library file (DX7VB.DLL).

You must respect some conditions for loading the .X files:

- The modelled objects must have triangular faces.

- The texture coordinates must be real positives value, from 0.00 to 1.00, or they just

are ignored (with your confirmation, of course).

1.4 Managing 3D objects

3DDER include some functions to help you in your 3D editing, like attachments, making copies,

performing TRS operations (Translate, Rotate & Scale)…etc

1.4.1 Attaching geometry

An attachment process, or call it ‘Assemblement’ process, is to combine two 3D objects as

a single one, for example, you can assemble objects called: Torso, Arms, Legs, Head in

a single object called Human Being !!!!

The attachment procedure affect only the geometry, the textures are ignored.

1.4.2 Copy operation

Of course, you don’t have to model an object one million times… just use the copy function

(including the textures).

1.4.3 User-defined World/View matrices

I guess that is rarely i use (or you) this option (even stupid..!), the math used in the engine to

transform the object in the World/View spaces are the vectors/matrix operations, the format

of the matrices in 3DDRE is 4 rows vs 4 column (4x4 matrix), you can adjust the user defined

transformations by directly set-up the matrix.

1.4.4 Adjusting world coordinates

We can modify the location, orientations and size of a 3D object by modifying his world-coordinates,

Use the Scene Manager window to directly updating the the TSR values.

1.4.5 Materials assignment

In real life, objects are so different in their physical/chemical properties, resulting many

different ways of the light-scattering operations over theme, we use so materials to describes

how the light react when hits on the surfaces, for example, some materials reflect the light,

where others only refract it, others both reflect and refract the light, others absorb the light

(to a heat form).

1.4.5.1 Alpha mapping channel (cut mapping)

We uses the alpha mapping (or the cut mapping), to determine the clipped area in a polygon,

like indicated, this is a 2D monochromatic map (the engine consider as 8-Bits bitmap format)

containing the visibility information (white for visible, black for not), instead of modelling millions

of polygons, we just use this mapping (useful in Billboards, to model trees for example).

Some notes about Alpha mapping:

- The accuracy of the Alpha mapping depends largely on the size of the alpha maps.

- The Alpha mapping depends on the current texel filter, the visibility decision is done after the

filtering operation.

- The textures filter (anti minification) is not taken into account in Alpha mapping.

Cut-mapped box Cut-mapped plane

1.4.5.2 Specular highlight

The specular highlight, is a form of reflection of the light source on the surfaces to the lens (camera).

The specular highlight depends on the reflectivity of the surface, a non-shiny surface can’t emit

any specular reflection.

You can adjust the viewed secularity shape by adjusting the specular parameters K & N in the

Materials manager, where K is main-density parameter, and N is the the exponent factor parameter

of the specular highlight.

Lambertian (perfectly diffuse) surface Reflective surface (specular hightlight)

1.4.5.3 Color maps

We use Color maps to add high level of details on the objects, otherwise, objects looks just

flat, texture mapping is the most important part in the 3D graphics, is just a ‘paste’ operation

of a 2D image on a 3D objects (using texture-coordinates) to improve details, we can for

example use brick picture taken from the real world to apply on a 3d wall, for example.

We have two choices :

- Use an uniform color to apply at the entire object (no mapping option)

- Or using Color maps, The Color map format is 24-Bits RGB bitmap,

we can assign a color map to an object in the Materials manager.

1.4.5.4 Reflections & Refractions

An important integrations in this ray-tracer are the reflections and refractions, in the real world,

some objects reflect light, other objects can refract the light with a constant (even variant)

refraction index, or both, reflect and refract.

In 3DDRE, you can choose (Materials Manager window):

- Use a constants values for the entire object (percent), an positive integer values between 0 to 255

for the reflection and refraction text-boxes, and a real positive value in the refractionN text-box.

- Or use mappings instead, 8-Bits bitmap format for all reflection, refraction & refractionN (index).

Reflection with 10 bounces + Alpha(Cut) mapping Index of refraction (N) mapping

2. Lighting

In the section 1, we have discussed about the scene-modelling process, in this section, we talk about

the most important part in any 3D software: Lighting.

2.1 Integrating lighting:

The goal of the lighting is to illuminate a scene, we can’t say that an Human eye can see objects,

but the resulting light of sophisticated operations (absorptions, reflections…) and unlimited bounces

of light paths across the objects, a proof, is simply that we can’t view anything without a light source…

Basically, in the Real World, light is everywhere, and unlimited phenomenon(s) resulting those

we call theme ‘effects’ (!) the computer solutions like soft edges of the shadows, refractions,

caustics, focal blur, light bleeding, diffractions…

In 3DDRE, we can integrate the light by using light-sources units, light-sources are objects

too (in the World coordinates system) that can be visible (Glows or Billboards), 2 types of

point-defined light-sources are available in the current version:

- Omni light-sources (sphere): a type of light-source that emit light form a defined point (3D vector)

in all directions in a limit of a specific range (distant attenuation).

- Spot light-source (cone): in the same way of an Omni light-source, except that a Spot light is

limited by a maximum light-beam angle, and oriented by a direction vector, a second interior angle

allows us to define the angular attenuation.

Omni light-source Spot light-source

In the addition of their way of the lighting, those 2 types of light-sources describes the way of the

photons distribution over the scene, in the case of enabling the Photon mapping solution.

4.1 Memory allocations

Its possible to know the memory costs for your current work, just open the Render Settings window,

and you see the statistics of the current memory usage. Allocating memory is a necessary task before

you start to model a scene, you have to respect the maximum count of allocations.

Warning: If you re-allocate the memory, your current work will destroyed (a Reset operation).

4.2 Adjusting the stochastic Path-Tracer options

In this version of 3DDRE, the path-tracer is stochastic, means that the engine shots random view

rays to approximate the correct view, you can adjust the view paths count, and the bounces count

per a view-path in the Render Settings window. Howover, many paths per pixel are required just

in the scenes with objects with some reflection and some refraction, increasing the paths count

per pixel makes the rendering process extremely slow. With scenes with perfectly reflective/

refractive/diffusible objects, just 1 view path per pixel is sufficient.

The Hyrarchical path-tracer technique, in the other hand, sill coming in the future versions.

A quick note, is that the engine uses the bounding-boxes technique to improve the ray-trace speed.

4.3 Global illumination technique : Photon Mapping

One of the major challenges of rendering images realistically is the achieving the full global

illumination, the challenge is to get the lighting right. Lighting an image is a complicated process

because light is reflected, refracted, and absorbed by objects.

Images produced by global illumination methods can look very convincing indeed, But, and it's

a big 'but': 'BUT!' they are slower. Just as once you may have left the ray-tracer all day...

The main reason that global illumination techniques are too slow, is that they require sampling (pachs

in Radiosity, light paths in Monte Carlo techniques, and photons in Photon mapping).

Global illumination can be divided into two categories: Direct illumination and indirect illumination.

Direct illumination is the light which goes directly to an object from a light source whereas indirect

illumination is the light which an object receives, but has bounced at least once before it hits the

object. A special case of indirect illumination is called a 'Caustic', caustics represent the light that

has been reflected from or refracted through a specular object. A caustic results from light rays

bending toward a particular area and therefore, concentrating the light in that area.

In 3DDRE, my choice has been to integrate the Photon mapping algorithm, because the other

solutions are *Very* time-consuming specially in Microsoft® Visual Basic®..so…

The Photon mapping technique was developed by Dr Henrik Wan Jensen from the 1990s, the idea of

Photon mapping is to emit light from a light source. light is represented as photons, which are given

a specific power and are shot out from the light source in random directions (stay respecting the light

shape factor), These photons are stored in a photon map when they hit a diffuse surface, or they are

bounced from a specular or diffuse surface and stored when a diffuse surface is hit. Henrik provides

C++ code for a Photon mapping class in his book on the Photon mapping solution, the class uses a

kD−tree to keep track of stored photons (the integration of kD-tree search will come in the future version

of the engine, currently, the program uses the naive linear search of photons on the entire photon map,

this explain why this approach of the algorithm is too slow). he provides a function which gives the

irradiance estimate for a specified position in the scene. in 3DDRE, my choice has been that this function

searches all the photons in collision with the specified search radius from the given position, the estimate

produced by this function is a result of combining the power of all of the founded photons in the search process.

It’s truly important to know, that making good caustics needs an important number of photons, as a

caustic is a focused light in a particular area, Jensen suggest focusing the photons on the important

areas using projection maps, my choice has been, instead of projection maps, is just using Spot-light

sources to focus the photons.

Photon mapping at different levels of photons (accuracy of the color-bleeding)

Caustic generated by reflection Caustic generated by refraction

In the most cases, Photon mapping needs a carefully parameterization to gives better results,

(specially to render sharpe edges of a caustic), in every 3D scene. Generally, the resulting

blur effect of the irradiance estimate is very plausible to render soft color-bleeding for example,

instead of the noise effect in the Monte Carlo methods.

4.4 Adjusting the texture-mapping filters (anti-minification/magnification)

The classical way to set-up a mesh’s property is to update a single value, the program use then this

value to the entire area (triangles), a given example is the visibility option (Boolean). instead, we can

replace the single value with a mapping, a mapping (or texture-mapping) is a process of extracting

the information from a 2D image’s data (the visibility option is decided then with a ‘cut mapping’).

To use a mapping, its necessary to define textures-coordinates, in the case of this program, the

textures-coordinates are defined in the triangles data-structure.

Some conditions when using texture-mapping:

- The maps may be in the 8-Bits or 24-Bits bitmap format.

- Some properties are floats values, but even with filtering the mapping gives always byte values

(the 24-Bits is just 8-Bits x 3), the possible solution to fix this ‘bug’, will be simply to use large

textures.

- The textures may have different sizes, the engine uses parametric format (0…1) to define a

particular position on the map, this is will explain, why, the texture-coordinates (floats) must

be positives real values in the range of 0…1 when importing an .X file.

Defining texture-coordinates from the texture-map to the polygon (triangle)

The texture-mapping process convert the texture’s data from the texture-space to the screen (viewed)

space, and since the size of the texture is limited, but also the screen size (pixels), the conversion

between the two different spaces will show-up as a pixelization effect (magnification), in the case the

texel (a texture pixel) is bigger than the screen pixel, and a aliasing effect (minification) is shown in

the case of the corresponding texel in this position is smaller than the screen pixel.

In the goal of removing the resulting undesired pixelization effect, we can use an anti-magnification

filters, those filters tries to remove (reducing at least) the pixelization effect by using the fractional

part of the texture-coordinates, the most used filter is the simple Bilinear, this filter just makes an

interpolations of texels, over both U and V textures-axis. generally, all the anti-magnification filters

gives a blurry output, but also gives different shapes of the magnified output-texel.

In 3DDRE, the available anti-magnification filters are (some must be parameterised):

Bilinear, Bell, Gaussian, Cubic B-spline, Cubic BC-spline, Cubic cardinal-spline

Pixalization effect (magnification) Gaussian filter

As an anti-minification filter, my choise has been the popular Mip-Mapping, the Mip-Mapping,

which the MIP term means: MultumIn Parvo (many things in a small place), is a technique firstly

introduced by Lance Williams in 1983. the idea is to reducing the minification effect by decreasing

the texture’s resolution at the large distances viewed from the eye, practically, the texture is

pre-processed by resizing it at multiple resolutions in a descending way, the program choose

the largest Mip-map in the closer viewed distances, and the smallest Mip-map at the far viewed zones.

The jump from a Mip-map to another, can be avoided (or not) by linearly interpolating across the

Mip-maps. In the case of combining the Mips-interpolation, and a texels filter, we got the ‘full’

filtering solution, that some people call ‘Trilinear interpolation’.

# Nearest (no filters) Filtered_Mip_Linear (Trilinear)

In my opinion, The Mip-Mapping can be a ‘virtual’ Depth of Field version in some way… when this

last, stay coming in the future versions (Focal distance & Dispersion properties of the cameras)…

You can make the filters combination in the Texture-filter combo-box (Rendering settings window):

Nearest

Nearest_Mip_ Nearest

Nearest_Mip_Linear

Filtered

Filtered_Mip_Nearest

Filtered_Mip_Linear

(read Nearest = no texel filter, Mip_Nearest = no Mip-maps interpolation)

4.5 Selecting the shadows mode

Shadows are the result, of ‘stopping’ the direct light reception by the occulted objects. Howover, in the

Real World shadows are never black, because the unlimited bounces of light paths (even with the

invisible light spectrum). The observed diffraction effect gives to shadows ‘soft’ edges, we can integrate

an approach solution using ray-tracing (stochastic visibility), to represent the diffraction effect,

called ‘Area shadows’.

In 3DDRE, you can choose between two modes of shadow-casting

techniques (Materials Manager window):

- Flat Shadows : Cast the shadows using just 1 shadow-ray, looks like ‘flat’, but fast.

- Area shadows : Uses many (specified) shadow-rays, in a specified range.

Flat shadows vs Area shadows

4.6 Adding a Fog effect

3DDRE allows you to add some cool atmospheric effects. You can activate the fog effect in the

Render Settings window, and just adjust the parameters…

Without Fog Added Fog effect

The Fog effect is not volumetric in this version of the engine, may coming in the future versions…

4.7 Adding a background image

A background image can be inserted (24-Bits bitmap format) to the rendered image, in this case,

the image is resized with the current texel filter to fit the output image size, or just a 24-Bits color is used

instead (Render Settings window).

3. 3D Viewing

3.1 Adding cameras

To achieve a good construction af a 3D scenery, its necessary to visualize it from different views, in

any direction, at any angle. 3DDRE allows you to add Camera objects as many as you want (in the limit of the

allocation of course…), cameras are in the World coordinates system (like the light-sources), but a

current camera must be selected to view to scene from the cameras list, just click on the button

‘Select a camera…’ in the main form (or from the menu).

3.2 Viewports (previewing windows)

Viewports are the pre-visualization windows, the previewing mode is the fast wireframe, flat/gouraud/

texture-mapped visualizations (using MS-DirectX®) still coming in the future version of the engine.

The main window contains 4 viewports:

- Parallel views (Front, Top, Size) : Viewport controls are just translation and zoom.

- Perspective view : The view from the current camera, the render process takes renders

only from this view, Controls are : Z (Zoom) : moves back or forward; FOV (Field Of View)

control the view angle (lens aperture), an important note is that this FOV angle is doesn’t

change the FOV angle of the current camera (have to do this manually), so also to the

rendered images; T (Translation, or Pan view) : moves the camera on the current view-plane;

R (Rotation) : change the camera’s angle (direction vector); Rz (Roll over Z) : The Z rotation

angle over the view ray.

To change a view, just click on the red button on the desired view.

The red button of the Perspective viewport

EOF.