贝塞尔曲线的OpenGL坐标 [英] OpenGL Coordinates from Bezier Curves
问题描述
基本上,我需要从OpenGL中的Bezier曲线实现中获取所有坐标.具体来说,我需要坐标以沿弯曲的轨迹路径移动场景中的球体对象(棒球).这就是我用来绘制曲线的方式:
Basically, I need to get all the coordinates drawn from a Bezier curve implementation in OpenGL. Specifically, I need the coordinates to move a sphere object (baseball) in my scene along a curved trajectory path. This is what I use to draw my curve:
GL2 gl = drawable.getGL().getGL2();
float ctrlpoints[][] = new float[][]{
{0.0f, 0.0f, 60f},
{0.0f, 3.0f, 45.0f},
{0.0f, 2.0f, 15.0f},
{0.0f, 1.0f, 0f}};
FloatBuffer ctrlpointBuf = FloatBuffer.allocate(ctrlpoints[0].length * ctrlpoints.length);
for (int i = 0; i < ctrlpoints.length; i++) {
for (int j = 0; j < 3; j++) {
ctrlpointBuf.put(ctrlpoints[i][j]);
}
}
ctrlpointBuf.rewind();
gl.glMap1f(GL2.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, numControlPoints, ctrlpointBuf);
gl.glEnable(GL2.GL_MAP1_VERTEX_3);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBegin(GL2.GL_LINE_STRIP);
for (int i = 0; i <= 30; i++) {
gl.glEvalCoord1f((float) i / (float) 30.0);
}
gl.glEnd();
有人知道如何从该实现中获得要点吗?
Does any one have any idea how to get the points out of this implementation?
推荐答案
贝塞尔曲线很容易计算.首先是可分离的,这意味着您可以一次计算一个坐标(首先是x,然后是y,然后是z ...).对于给定的坐标,以下是使用该定义的函数:
A Bezier curve is quite easy to compute. First of all is separable, that means that you can compute it one coordinate at a time (first x, then y, then z...). For a given coordinate the following is a function that uses the definition:
double bezier(double A, // Start value
double B, // First control value
double C, // Second control value
double D, // Ending value
double t) // Parameter 0 <= t <= 1
{
double s = 1 - t;
double AB = A*s + B*t;
double BC = B*s + C*t;
double CD = C*s + D*t;
double ABC = AB*s + BC*t;
double BCD = BC*s + CD*t;
return ABC*s + BCD*t;
}
请注意,在上面的函数中,参数t
不是曲线的弧长参数,而是从t=0
(点位于曲线起点)到t=1
的通用参数. (该点位于曲线的末端).
Note that in the above function the parameter t
is not the arc-length parameter for the curve but a generic parameter that goes from t=0
(where the point is at the beginning of the curve) to t=1
(where the point is at the end of the curve).
上面图片的交互式版本,您可以在其中拖动A,B,C,D和AB点.在此处可用.它使用html/js/canvas实现,并且仅在Chrome,Firefox,Safari上进行了测试.
An interactive version of the above picture where you can drag A, B, C, D and AB points is available here. It's implemented with html/js/canvas and tested only on Chrome, Firefox, Safari.
如果需要在XYZ中以受控的特定速度移动对象,一种简单的方法是计算近似的折线(例如,通过对100个t
值的曲线进行采样),然后以恒定的速度在生成的结果上移动折线.
If you need to move your objects at a controlled specific speed in XYZ an easy way is to compute an approximated polyline (for example by sampling the curve for 100 values of t
) and then walk at constant speed on the resulting polyline.
贝塞尔三次方的真实弧长参数化(即使用沿曲线测得的长度作为参数)相当烦人(IIRC没有积分的封闭形式解).
The true arc-length parametrization for a Bezier cubic (i.e. using a parameter that is the length measured along the curve) is rather annoying to compute (IIRC there is no closed form solution for the integral).
这篇关于贝塞尔曲线的OpenGL坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!