OpenGL:如何将2D形状加工成3D? [英] OpenGL: How to lathe a 2D shape into 3D?

查看:277
本文介绍了OpenGL:如何将2D形状加工成3D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenGL程序(用Delphi编写),可以让用户画一个多边形。我想自动围绕一个轴旋转(车床)(如Y asix),并获得3D形状。



我该怎么做?

解决方案

为简单起见,您可以强制至少一个点位于旋转轴上。您可以通过将所有x值中相同的值相加/减去相同的值,并将其与所有y值相同的值添加到多边形中。它将保留原来的形状。



其余的并不是那么难。选择一个相当小的角度,说一到两度,并在多边形顶点绕轴旋转时计算出坐标。然后只需加上三角形风扇和三角形条纹的点。



绕轴旋转点只是基本的毕达哥拉斯。在0度旋转时,您可以在第二维坐标中的点为2坐标,值为0。



假设点在X和Y中,我们在Y周围旋转。原始的X坐标代表斜边。在1度旋转时,我们有:

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

(假设基于度数的触发功能)



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

所以对于多边形边缘上的每个点,都会产生以旋转轴为中心的360点圆。



现在,制作如下形状的3D形状:


  1. 使用您的中心创建一个GL三角形风扇点和第一个旋转点数组

  2. 为每个连续数组创建一个三角形条,使用数组中的点和上一个数组中的点

  3. 通过创建以中心点为中心的另一个三角形风扇,并使用最后一个数组中的点来完成

有一件事不要e通常,我使用的trig函数的种类以弧度来测量角度,OpenGL使用度数。要将度数转换为弧度,公式为:

  degrees =弧度/ pi * 180 


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?

解决方案

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

这篇关于OpenGL:如何将2D形状加工成3D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆