OpenGl用2点画一个圆柱体glRotated [英] OpenGl draw a cylinder with 2 points glRotated

查看:116
本文介绍了OpenGl用2点画一个圆柱体glRotated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示绘制一个从a点开始的圆柱体,该圆柱体指向该键,我认为该键位于第一个glRotated中,但这是我第一次使用openGL a和b是btVector3

I want to show draw a cylinder that starts at point a and that points to I think the key is in the first glRotated, but this is my first time working with openGL a and b are btVector3

glPushMatrix();
glTranslatef(a.x(), a.y(), a.z());
glRotated(0, b.x(), b.y(), b.z());
glutSolidCylinder(.01, .10 ,20,20);
glPopMatrix();

任何建议??

推荐答案

根据 glutsolidcylinder(3 )-Linux手册页:

glutSolidCylinder()绘制一个带阴影的圆柱体,其中心位于原点,并且其轴沿z轴正方向.

glutSolidCylinder() draws a shaded cylinder, the center of whose base is at the origin and whose axis is along the positive z axis.

因此,您必须分别准备转换:

Hence, you have to prepare the transformations respectively:

  • 将圆柱体的中心移至原点(即( a + b )/2)
  • 旋转圆柱的轴(即 b - a )成为z轴.
  • move the center of cylinder to origin (that's (a + b) / 2)
  • rotate that axis of cylinder (that's b - a) becomes z-axis.

使用 glRotatef() 似乎也被误解了:

The usage of glRotatef() seems to be mis-understood, also:

  • 第一个值是旋转角度,以度为单位
  • 第二,第三和第四值分别是旋转轴的x,y,z.

这将导致:

// center of cylinder
const btVector3 c = 0.5 * (a + b);
// axis of cylinder
const btVector3 axis = b - a;
// determine angle between axis of cylinder and z-axis
const btVector3 zAxis(0.0, 0.0, 1.0);
const btScalar angle = zAxis.angle(axis);
// determine rotation axis to turn axis of cylinder to z-axis
const btVector3 axisT = zAxis.cross(axis).normalize();
// do transformations
glTranslatef(c.x(), c.y(), c.z());
if (axisT.norm() > 1E-6) { // skip this if axis and z-axis are parallel
  const GLfloat radToDeg = 180.0f / 3.141593f; 
  glRotatef(angle * radToDeg, axisT.x(), axisT.y(), axisT.z());
}
glutSolidCylinder(0.1, axis.length(), 20, 20);

我记着写了这段代码(使用 btVector3 我以前从未使用过).因此,请带上一粒盐. (可能需要调试.)

I wrote this code out of mind (using the doc. of btVector3 which I've never used before). Thus, please, take this with a grain of salt. (Debugging might be necessary.)

因此,请记住以下几点:

So, please, keep the following in mind:

  1. 文档.没有提到 btVector3::angle() 返回的角度是度还是弧度?我假设是弧度.

  1. The doc. does not mention whether btVector3::angle() returns angle in degree or radians – I assumed radians.

在编写此类代码时,我经常不小心翻转东西(例如,朝相反方向旋转).这样的事情,我通常会在调试中解决,这对于上面的示例代码可能是必需的.

When writing such code, I often accidentally flip things (e.g. rotation into opposite direction). Such things, I usually fix in debugging, and this is probably necessary for the above sample code.

如果( b - a )已经沿z轴正向或负向移动,则( b -一个)× (0,0,1)将产生一个0向量.不幸的是,医生. btVector3::normalize() 没有提及将其应用于0矢量时发生的情况.当然,如果在这种情况下引发异常,则必须添加额外的检查.

If (b - a) is already along positive or negative z-axis, then (b - a) × (0, 0, 1) will yield a 0-vector. Unfortunately, the doc. of btVector3::normalize() does not mention what happens when applied to a 0-vector. If an exception is thrown in this case, extra checks have to be added, of course.

这篇关于OpenGl用2点画一个圆柱体glRotated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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