3D Graphics Project Including Hidden Surface Removal and Phong Shading

This was an assignment I did when I was at University and I think it turned out reasonably well. Somehow I managed (with much help of the Windows 98 API) to write it all in Visual Basic 6 – before I realised how dreadful it was. All of the graphics code is written from scratch, so I imagine these days it would be a simple task to rewrite using a 3D library.

[You can download my example code here][1]. It is a VB6 application, so you may struggle to get it to work under Windows but I managed with Wine.

Assignment Info

Part 1 of the assignment was completed and for the 2nd part the depth sort hidden surface removal and lighting calculations were added.

High-level Design

The user is presented with a single window, in order to access all functionality of the project. This window accesses an object model used to provide 3D vector and matrix manipulation as well as shape and quad classes to allow easy construction and drawing of shapes. A special environment class controls the entire object model.

The user is initially able only to plot points on white canvas, and once finished must enter a number of corners before clicking the sweep button. This button initialises the environment class with certain default vales, such as the focal length, and creates a new shape object. Each point the user plotted and a quad created by finding the point immediately beneath it and the two points to its right. These points are found by rotating the initial two points a number of degrees around the Y-axis. This process is repeated for each point until the entire corner has been converted into quads. The rest of the quads are then generated in a similar manor through the entire 360 degrees. The quads are all added to the shape and the shape can then be drawn.

Drawing the shape is a simple task of looping through each quad and invoking the quads Draw method. This method uses a simple perspective translation to position itself and uses the Windows Polygon API to draw onto the canvas.

The Redraw button on the form reads the various settings on the window, such as X, Y and Z positions and rotations. These settings are then applied to the environment, or shape where appropriate and the environments DrawShape method called again. The settings available include translation, scale, rotation, hidden surface removal and Phong shading.

Components

Scroll Window – visual basic project

This is a simple custom-written OCX, which allows a standard visual basic PictureBox to be scrolled around with scrollbars. This is a relatively simple control who”s functionality is not really part of the graphics project.

basAPI – visual basic module

Contains the API declarations that visual basic uses to draw on the canvas. These are primarily the Polygon function, the POINTAPI structure and several functions to create and assign drawing pens.

cls3DVector – visual basic class

Contains a 4 element array representing the X, Y, Z and magnitude of a vector. The class also provides easy access to this array through properties and provides methods to calculate the dot product, cross product and to normalise the vector.

cls3DMatrix – visual basic class

Contains a 4 by 4 array of singles to represent a transformation matrix. The class provides functionality to set this internal array as a translation, rotation, simple perspective or scaling matrix using parameters to set the rotation angle, scale factors, translation amounts etc. Methods are also available to set the array by passing in two other matrices, which will be multiplied together; and to apply the matrix to a cls3DVector object.

cls3DQuad – visual basic class

Contains a 4 element array of cls3Dvectors to represent the four corners of the quad. It provides easy access to each corner through named properties such as TopLeft and provides information such as Minimum, maximum and average X, Y and Z coordinates. Methods are also provided to return the normal vector (a perpendicular vector pointing outward from the face of the quad), to draw the quad using a specified perspective matrix and to transform the quad using a specified matrix.

cls3DShape – visual basic class

The shape class manages an array of quads, allowing the client program to add, clear or depth sort the quads. These add and clear methods are simple but the depth sort is more complicated, using a quick sort algorithm to order the quads from furthest way to nearest (along the Z axis). The class also provides a number of methods to transform the shape in a number of different ways such as rotation, translation and scaling. All of these functions simply loop through the array of quads and transforms each quad using the quads own transformation methods. In addition to these methods the centre, minimum and maximum X, Y and Z coordinates as well as the lighting coefficients are made available so that the shape can be centred and moved more appropriately.

cls3DEnvironment – visual basic class

The environment object controls many of the overall settings such as focal length, shading style and the light intensity. These settings must all be set-up correctly before methods such as DrawShape can be called. The object also contains a collection of objects (although only one is ever used in this project) and provides access to these objects. In addition it controls the drawing of the shapes on the canvas and makes the appropriate lighting calculations in order to do this.

Example Graphics

[1]: /files/James-Rowe-3DGraphics.zip