Click here to Skip to main content
15,867,453 members
Articles / Multimedia / GDI+
Article

GDI+ 3D Demo using C#

Rate me:
Please Sign up or sign in to vote.
2.12/5 (12 votes)
24 Jun 2004CPOL4 min read 113.5K   5.1K   37   5
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 :

C#
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 :
C#
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 :

C#
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!

License

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


Written By
Engineer
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.

Comments and Discussions

 
Questiondraw xyz file of facee Pin
Member 919716920-Sep-12 2:18
Member 919716920-Sep-12 2:18 
AnswerRe: draw xyz file of facee Pin
Dom Rositas11-Oct-12 4:28
Dom Rositas11-Oct-12 4:28 
GeneralExplain More. Pin
NormDroid25-Jun-04 2:21
professionalNormDroid25-Jun-04 2:21 
GeneralRe: Explain More. Pin
Dom Rositas25-Jun-04 18:16
Dom Rositas25-Jun-04 18:16 
GeneralSlowness... Pin
Joel Holdsworth25-Jun-04 0:54
Joel 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.