![]() |
Multimedia »
GDI+ »
General
Intermediate
License: The Code Project Open License (CPOL)
GDI+ 3D Demo using C#By Dom RositasAn article on 3-D graphics and C# |
C#, Windows, .NET 1.0, .NET 1.1, GDI+, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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.
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!
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Jun 2004 Editor: Nishant Sivakumar |
Copyright 2004 by Dom Rositas Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |