Click here to Skip to main content
12,943,367 members (48,298 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 24 Jun 2004

GDI+ 3D Demo using C#

, 24 Jun 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on 3-D graphics and C#


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 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.


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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dom Rositas
United States 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...


Comments and Discussions

Questiondraw xyz file of facee Pin
Member 919716920-Sep-12 2:18
memberMember 919716920-Sep-12 2:18 
AnswerRe: draw xyz file of facee Pin
Dom Rositas11-Oct-12 4:28
memberDom Rositas11-Oct-12 4:28 
GeneralExplain More. Pin
norm.net25-Jun-04 2:21
membernorm.net25-Jun-04 2:21 
GeneralRe: Explain More. Pin
drositas25-Jun-04 18:16
memberdrositas25-Jun-04 18:16 
GeneralSlowness... Pin
Joel Holdsworth25-Jun-04 0:54
memberJoel Holdsworth25-Jun-04 0:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 25 Jun 2004
Article Copyright 2004 by Dom Rositas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid