Render Spheres, torus and other complex objects by algorithm?

Started by
23 comments, last by directNoob 17 years, 9 months ago
Hi everybody. I´m in big trouble. I can´t imagine how I can render, or more precisely, how to create vertex and index info of more complex objects. I mean, due to the sphere example, there is a formula for every point of the sphere, but how do I index this points and how do I evaluate the distance among two points? And this is not all. What about the torus, or this knot objects often seen in demos. I realy want to get deep into this topic. I need your help to start off, because I have absolutely no idea where to start!!! And if it needs complex numbers, no matter, I´m well introduced to it! Thanks Alex
Advertisement
I'm going to move this over to GP&T whilst you might be using Direct3D the basic algorithms/methods for creating primitives is independent of a particular API. Maybe the GP&T regulars can recommend some resources for you [smile]

Check out the D3DX shape drawing functions - several of the ones you mentioned are included "in box" already.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

or you can do it the noob way (ie: my way [grin]) and simply use a 3D model. Although that actually isn't too simple.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Thanks, but I don´t need the premade objects. Instead, I need to do it my self.

Alex
Quote:Original post by directNoob
Thanks, but I don´t need the premade objects. Instead, I need to do it my self.
There's certainly plenty of good info to be found on this subject (I can offer some tips/links), but just out of curiosity can you explain the context? Why do you need to generate these objects procedurally? And what shapes exactly are you interested in (aside from those that have already been mentioned)?
I don't want to hijack the people already helping directNoob, but I wanted to plant this in his mind to help the concept to sink in.

directNoob:
1. For procedural objects, you have to think about how to break it down into a triangluated surface.
2. If you can break it down into rectangular gridlike patches, it's easy to convert that to triangles (cut the quadlaterals in half - viola, triangles)
3. Look for planar, rotational and spherical symmetry to procedurally generate the object.

(just do it!)

Example - sphere
An easy way to create a sphere is to think of it in terms of latitude and longitude.
this image

shows this idea perfectly.
See if you can come up with a process to recreate the points. (hint, in this case, the sphere is basically a 2d grid, wrapped to the shape of a sphere)

-Michael g.
@Thr33d
Your sample image suffers from problem due to the singularity at the poles. For spheres, a sub-division approach seems better suitable, especially since mapping coordinates can be interpolated easily.

To subdivide a suitable geometric object (any platonic polyeder will do - start with a tetrahedron for simplicity), you apply a basic subdivision algorithm and simply normalise the vertices.

The following ASCII chart illustrates the idea:
                A               / \                         /                B-----C

To subdivide the triangle ABC, you create new points A', B' and C' by
cutting the edges into half.
__
A' = AB / 2
__
B' = BC / 2
__
C' = CA / 2
               A              /           A' +...+ C'            / . .            B---+---C               B'

This naturally forms four sub-triangles A'AC, BB'A', CC'B' and A'B'C' (dotted edges).

Repeat the subdivision on the newly created sub-triangles to refine the resulting mesh. A tetrahedron conviniently consists of four equilateral triangles so it's a nice start to test this subdivision out with.

Using an octahedron or even an icosahedron (20 sides) reduces the number of subdivisions (and hence resulting triangles) required to form a smooth sphere.

HTH,
Pat.
Hi and thanks.

@jyk:

I just want to get deeper into this material. I´m tired load x files for example.
I want to make my own objects plus, I want to extent my mathematical horizon.
When you have resources, please give it to me! I would be very grateful.
I want to experiment with this object and figure out how the guys in the demo scene are doing it. But the worst thing is, I can´t get behind it myself.
And this disappoints me very much.

@Thr33d:
Ok, that´s easy.
r^2 = x^2 + y^2 + z^2 =>z = +-sqrt(r^2 - (x^2 + y^2))


With this formula, I get every point on the sphere.
Did you ask for this?

Greetings
Alex
A good place to start is here, with the 'Platonic solids' .pdf. Having these shapes available can be a handy point of departure for more complex shapes.

For spheres, the aforementioned lat-long approach is quite easy to code, provided you're not concerned about the irregular distribution of triangles. Otherwise you'll want to take darookie's advice and apply recursive subdivision to an octa- or icosahedron.

A torus can be generated with simple trig; you just need to think about the geometry involved (an inner ring with circumscribing rings at regular intervals) and derive the code accordingly.

From there, procedural generation of meshes can get more or less arbitrary complex. There are subdivision surfaces, generalized cylinders using curves and various types of curve frames - the list goes on. For more advanced meshes you'll need to be comfortable with vector math, and will most likely need to develop as a foundation a mesh class with support for connectivity queries.
Or ... you can check the source to freeglut! [smile]

I did that when I wanted to procedurally generate a torus, but never actually got it done [wink].

This topic is closed to new replies.

Advertisement