CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: How to draw simple 3d shapes?

1. Member
Join Date
Mar 2009
Posts
29

How to draw simple 3d shapes?

Good Day,

I am a novice programmer in 3D graphics and i am using C++ and DirectX 9.
I would like to know how to draw Rectangles in 3d space when i now the coordinates of each point in 3d space.
For example i need to draw a shape whose bases are random rectangles.
(Coordinates given in x,y,z)
The top base(y=0) could be for example:
point1=(-1,0,1)
point2=(0,0,3)
point3=(1,0,2)
point4=(1,0,1)
point5(0,0,0)

And the bottom base (y=-1) not symmetrical to the top base
point1y=(0,-1,1)
point2y=(0,-1,3)
point3y=(1,-1,2)
point4y=(1,-1,1)
point5y(0,-1,-2)

Is there a given function where i can give those coordinates and the shape is drawn automatically?
If not, what other option do i have for accomplishing this task?

Stakon.

2. Senior Member
Join Date
Nov 2006
Posts
1,611

Re: How to draw simple 3d shapes?

DirectX (or what used to be called Direct3D) has what you're asking for. The specifics of using it are beyond a simple post; you'll need to search for tutorials on the subject, and there are lots of books and free C++ libraries for doing this.

For an indepth discussion of 3D, David Eberly's texts regarding his 3D engine WildMagic is thorough, perhaps dense, with discussion on game engines that incorporate special effects and physics, but the basics are covered as well.

I'm not familiar with more gentle introductions, but I know there are many. If you're interest is going to remain limited to a few simple shapes then perhaps DirectX is appropriate, but if your interest is going to expand beyond that even slightly, you'll want to consider some kind of 3D engine.

One reason for that is the fact that the most widely used "alternate" standard (but by alternate I mean non-Microsoft specific; many, including me, consider DirectX the alternate) is OpenGL. OpenGL and DirectX are positioned similarly. That is, there is a C level interface that provides reasonably direct access to the hardware's 3D capability. OpenGL is available on devices ranging from 'supercomputers' to handheld game devices, whereas DirectX is only available on Microsoft devices and PC's.

What most engines, like Eberly's WildMagic, do is to encapsulate 3D drawing (and the representation of 3D objects) in such a way as to implement rendering independently of application code, so you can elect to use DirectX or OpenGL from one body of code - it is even possible to choose at runtime (though not in Eberly's implementation - others do).

There is some difference between a graphics engine and a game engine, which may affect your selection. A game engine includes components to handle physics, sound, input and graphics, while a graphics engine will only address graphics. By "engine" I mean a C++ library (and, there are C libraries also) that serve this purpose.

It is not, however, quite as simple as handing a list of points to a drawing function.

If your interest is strictly the display of a 3D graph, where the results are always going to be a rather simple solid shaded/wireframe outlined shape, you could choose a more old fashioned approach by drawing the shapes using 2D drawing primitives - especially if the output is not going to be animated. This involves the use of linear algebra, perhaps some 3D trig, to position the points as a projection onto a 2d surface, which then give the 2d points of simple convex polygons. This approach would seem simpler than invoking OpenGL or DirectX if, and I think only if, you're comfortable with the math. If you are, it turns out to be a relatively simple set of functions that perform the work, and the output is basic 3D solids (a pastel colored cartoon sketch, if you will). I'm sure many would cry foul for me saying that, but frankly the use of OpenGL or DirectX isn't all that simple if the math is familiar to you, and your expectations of the output are simple.

3. Elite Member
Join Date
Apr 1999
Posts
3,585

Re: How to draw simple 3d shapes?

I would suggest you take a look at the samples browser that installs with DirectX. It contains many examples and source code for you to look at.

4. Member
Join Date
Mar 2009
Posts
29

Re: How to draw simple 3d shapes?

Of what i read, i think i will not need the complexness of advanced 3d graphics, just some basic functions like drawing 3d polygons, scaling them , rotating them, and moving the camera around.

I am currently trying to find information in the DirectX Samples, but unfortunately most topics are either too advanced or irrelevant to what i need.

I have managed so far to create my own functions for creating and drawing my own 3d polygons, as long as the are Convex polygons.(Simply by using DrawPrimitive(D3DPT_TRIANGLEFAN....) )
I think i will not be able to make code able to manage Concave Polygons
If there is any library or article you know, which covers the above case, i would be delighted to learn about it.

(For a hint of what polygons are: http://en.wikipedia.org/wiki/Convex_polygon )

Stakon.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•