Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: How to lathe a 2D shape into 3D?

Tags:

opengl

delphi

I have an OpenGL program (written in Delphi) that lets user draw a polygon. I want to automatically revolve (lathe) it around an axis (say, Y asix) and get a 3D shape.

How can I do this?

like image 994
Mahm00d Avatar asked Nov 07 '10 09:11

Mahm00d


1 Answers

For simplicity, you could force at least one point to lie on the axis of rotation. You can do this easily by adding/subtracting the same value to all the x values, and the same value to all the y values, of the points in the polygon. It will retain the original shape.

The rest isn't really that hard. Pick an angle that is fairly small, say one or two degrees, and work out the coordinates of the polygon vertices as it spins around the axis. Then just join up the points with triangle fans and triangle strips.

To rotate a point around an axis is just basic Pythagoras. At 0 degrees rotation you have the points at their 2-d coordinates with a value of 0 in the third dimension.

Lets assume the points are in X and Y and we are rotating around Y. The original 'X' coordinate represents the hypotenuse. At 1 degree of rotation, we have:

sin(1) = z/hypotenuse
cos(1) = x/hypotenuse

(assuming degree-based trig functions)

To rotate a point (x, y) by angle T around the Y axis to produce a 3d point (x', y', z'):

y' = y
x' = x * cos(T)
z' = x * sin(T)

So for each point on the edge of your polygon you produce a circle of 360 points centered on the axis of rotation.

Now make a 3d shape like so:

  1. create a GL 'triangle fan' by using your center point and the first array of rotated points
  2. for each successive array, create a triangle strip using the points in the array and the points in the previous array
  3. finish by creating another triangle fan centered on the center point and using the points in the last array

One thing to note is that usually, the kinds of trig functions I've used measure angles in radians, and OpenGL uses degrees. To convert degrees to radians, the formula is:

degrees = radians / pi * 180
like image 163
sje397 Avatar answered Oct 05 '22 12:10

sje397