12,239,797 members (69,665 online)
alternative version

93.8K views
37 bookmarked
Posted

# GDI+ 3D Demo using C#

, 24 Jun 2004 CPOL
 Rate this:
An article on 3-D graphics and C#

## Introduction

This small program was developed as part of my learning of C# with GDI+ features. It also uses code I have authored on a previous platform (Amiga) with C language. Being a newbie to C# and GDI+, I would very much appreciate helpful feedback, along with any demo animation suggestions.

## Using the code

Build and run the example as a Windows Form with your C# compiler. I used Visual Studio, and for rapid coding, I have found the free Snippet Compiler by Jeff Key at www.sliver.com is excellent. When the program is run, you should see a spiraled sphere shape in gradient colors and initially you are looking toward a sphere pole.

I'll go over some points in the program code and the procedures used.

First, a `Bitmap` object, `bmap`, is created, which is not really needed if you use the `System.Drawing` functions, but I used it to start exploring double buffering. Also, a bitmap object is necessary in order to use the `Bitmap.SetPixel` method. The other `Bitmap` object, `bm`, is not used for drawing. Its function is to clear the Form bitmap so that a new rotated view of the sphere will not blit from the hidden bitmap, `bmap`, onto the Windows Form and leave artifacts from previous views. GDI has blitter functions available specifically for this operation but GDI+ does not.

The `InitRot` function takes the angles of rotation for the three axis in degrees as input from the user controls and outputs values used in the sphere rotation calculations which are combined with calculations to construct the sphere itself. The sphere object calculations result in x,y,z coordinates to describe a 3-D sphere pixel by pixel, starting from an x coordinate at one polar end of the sphere object, calculating all associated circular y coordinates then incrementing x and repeat finding the associated y points etc, etc till the opposite pole is reached.

Comment out the line of code reading :

```if((j+i)%64 >= 0 && (j+i)%64 <=32)
```
and you will see this incremental plotting of points results in a plain sphere as output. What this line does is selectively filter x points to, in effect, increment x sooner than usual so that a spiral from pole to pole is formed. The mod division is for creating multiple spirals instead of just one. You can see this by substituting the commented out line :
```if(Math.Abs(j-i) <=12)
```
for the line using mod division. You will have to input an x or y rotation first, because the initial pole view of the single spiral does not clearly show this. Incidentally, I tried using a `Point` array and then index it with `unsafe` pointers to form the spirals, but the results were too cumbersome. One line of code worked faster and better here for me. An array with varying indexed access to the `Point` data would open up more graphics design possibilities. So I am still working on it, but if a more experienced C# programmer could suggest a way to use the `System.Array Class` I am willing to listen.

The next `if` block of code :

`if( z2 > 0 ) { ... `
checks the z coordinates to see if they output negative results. If they do, this means that the points in 3-D space are on the hidden side of the sphere. Usually these points are skipped and not drawn since the front surface is all we see. In this case, however, since the spirals have cutout portions that allow some of the back surface spirals to show through, the `SetPixel Color` parameter is set to a gray gradient for these. The front spirals are colored gradients. GDI+ has good color gradient methods available, but here again, it was easier to write it this way especially since I was dealing with 3-D and rotation changes to the graphics. A lot of code would be needed to define the boundaries or paths that GDI+ calls for.

## Conclusion

Recent news from Microsoft is that Avalon, the graphics package with Longhorn will provide 3-D drawing support. Have fun experimenting with the program and be sure to send me any interesting or wild and zany designs you come up with!

## Share

 Engineer United States
I have programming computers since before the first IBM PC was introduced in the Forth language on STD Bus systems and then Microsoft BASIC, DOS, QuickBasic and QuickC. Later, my most interesting and productive work was on Commodore64 and the Amiga computers. I recently started programming again and while recuperating from cancer surgery got deeply interested in C#.
I have been employed in various electronics occupations for 30 years and presently work for a company that manufactures and analyzes radiation dosimeters for professionals in nuclear power plants and research and also dosimeters for nuclear medicine personnel. Any spare time is spent in enjoying cigars, motorcycles and now, learning C# programming, GDI+ and OpenGL.

## You may also be interested in...

 First Prev Next
 draw xyz file of facee Member 919716920-Sep-12 3:18 Member 9197169 20-Sep-12 3:18
 Re: draw xyz file of facee Dom Rositas11-Oct-12 5:28 Dom Rositas 11-Oct-12 5:28
 Explain More. norm.net25-Jun-04 3:21 norm.net 25-Jun-04 3:21
 Re: Explain More. drositas25-Jun-04 19:16 drositas 25-Jun-04 19:16
 Slowness... Joel Holdsworth25-Jun-04 1:54 Joel Holdsworth 25-Jun-04 1:54
 Last Visit: 31-Dec-99 19:00     Last Update: 28-Apr-16 11:05 Refresh 1