tags:

views:

574

answers:

3

does someone know how to draw 3D surfaces and hide the invisible lines?

I remember reading some code from an Apple II book, and it was only about 25 lines in BASIC, and it could plot some 3D surfaces, such as a mountain or a shape that looks like water ripples. The invisible lines were at first shown, but after adding a few lines of code, the invisible lines are gone too.

Does someone know how to do it or know of resources on the web for doing this?

+3  A: 

I remember that book. If I recall correctly, these were surface plots, and the trick used was to plot the lines from front to back, keeping track of the min and max y plotted for each x. Probably the line drawer was a Bresenham, modified to skip the points drawn unless they were greater than max or less than min.

What's nice about the algorithm is you can do curves, too. Not just line segments. Probably the ripple picture was some sort of combination of trig functions with x and y as parameters.

I can still see those plots in my head. Would love to know what book that was from.

Nosredna
+5  A: 

This depends a little on how you are drawing the polygons. I would recommend reading a little about either OpenGL or DirectX as they are designed to solve many of these problems for you. If you don't want to use them directly there are many great engines built on top of these APIs already.

Assuming that you are drawing filled polygons (textured, solid etc) here are a couple of common algorithms you could use:

  • Sort the polygons by distance from the camera and paint those furtherest away first, otherwise known as the Painters Algorithm. This method was used by many old 3D games like the classic Doom which also used a BSP Tree for efficient sorting.

  • Store the closest distance from the camera to each pizel on the screen and don't draw anything further away, otherwise known as Z-Buffering. This method is well supported by modern 3D video cards, OpenGL and DirectX.

  • Use a ray tracing algorithm which naturally has hidden surface removal.

Generic Error
Yeah. Makes sense to use the hardware we have. The code the questioner is talking about was drawing lines, though, not filled polys.
Nosredna
The question starts with "does someone know how to draw 3D surfaces". Either way using black filled polygons with white borders to fake the lines will be far easier than trying to determine visibility of each line.
Generic Error
Oh yeah, I agree 100%. But if he's just looking to do what the Apple II did (and what modern graphing calculators do), it's pretty easy to whip that out in a couple dozen lines.
Nosredna
+2  A: 

If you plot a triangle mesh, then you should do back-face culling of each triangle, and don't draw the edges of the triangles that have a back-facing normal. You calculate the normal as the cross product of two successive edges in the triangle.

If you want to plot only the outline, you plot only edges that go between a triangle with a front-facing normal, and a triangle with a back-facing normal.

Or you use OpenGL, turn on the depth buffer, and forget about all the underlying math :-)