如何在OpenGL中绘制连接两个点的圆柱体

时间:2009-12-10 02:18:22

标签: c++ opengl 3d

我有两个点,每个点都有自己的X和Y值,它们具有相同的Z值。

我想要一个函数在这两点之间绘制Cylinder。

5 个答案:

答案 0 :(得分:2)

要构建具有两个给定点的圆柱体,需要进行矢量分析。您正在构建两个垂直向量,这些向量将添加到每个点并使用sin / cos与半径相乘进行缩放。 它接受所有点(旧代码有一个bug,因为它错过了sqrt()的长度)。 现在它正常运行并用gl例程绘制圆柱体;我在JOGL中测试过它。 为了更快地绘制,将firstPerpVector / secondPerpVector / points变量移动到私有最终数组字段并在开头初始化它们。

Java代码:

  public float[] getFirstPerpVector(float x, float y, float z) {
  float[] result = {0.0f,0.0f,0.0f};
  // That's easy.
  if (x == 0.0f || y == 0.0f || z == 0.0f) {
    if (x == 0.0f)
      result[0] = 1.0f;
    else if (y == 0.0f)
      result[1] = 1.0f;
    else
      result[2] = 1.0f;
  }
  else {
    // If xyz is all set, we set the z coordinate as first and second argument .
    // As the scalar product must be zero, we add the negated sum of x and y as third argument
    result[0] = z;      //scalp = z*x
    result[1] = z;      //scalp = z*(x+y)
    result[2] = -(x+y); //scalp = z*(x+y)-z*(x+y) = 0
    // Normalize vector
    float length = 0.0f;
    for (float f : result)
      length += f*f;
    length = (float) Math.sqrt(length);
    for (int i=0; i<3; i++)
      result[i] /= length;
  }
  return result;
}

public void drawCylinder(GL gl, float x1, float y1, float z1, float x2, float y2, float z2) {
  final int X = 0,
            Y = 1,
            Z = 2;
  // Get components of difference vector
  float x = x1-x2,
        y = y1-y2,
        z = z1-z2;
  float[] firstPerp = getFirstPerpVector(x,y,z);
  // Get the second perp vector by cross product
  float[] secondPerp = new float[3];
  secondPerp[X] = y*firstPerp[Z]-z*firstPerp[Y];
  secondPerp[Y] = z*firstPerp[X]-x*firstPerp[Z];
  secondPerp[Z] = x*firstPerp[Y]-y*firstPerp[X];
  // Normalize vector
  float length = 0.0f;
  for (float f : secondPerp)
    length += f*f;
  length = (float) Math.sqrt(length);
  for (int i=0; i<3; i++)
    secondPerp[i] /= length;

  // Having now our vectors, here we go:
  // First points; you can have a cone if you change the radius R1

  final int ANZ = 32;  // number of vertices
  final float FULL = (float) (2.0f*Math.PI),
              R1   = 4.0f; // radius
  float[][] points = new float[ANZ+1][3];
  for (int i=0; i<ANZ; i++) {
    float angle = FULL*(i/(float) ANZ);

    points[i][X] = (float) (R1*(Math.cos(angle)*firstPerp[X]+Math.sin(angle)*secondPerp[X]));
    points[i][Y] = (float) (R1*(Math.cos(angle)*firstPerp[Y]+Math.sin(angle)*secondPerp[Y]));
    points[i][Z] = (float) (R1*(Math.cos(angle)*firstPerp[Z]+Math.sin(angle)*secondPerp[Z]));
  }
  // Set last to first
  System.arraycopy(points[0],0,points[ANZ],0,3);

  gl.glColor3f(1.0f,0.0f,0.0f);
  gl.glBegin(GL.GL_TRIANGLE_FAN);
  gl.glVertex3f(x1,y1,z1);
  for (int i=0; i<=ANZ; i++) {
    gl.glVertex3f(x1+points[i][X],
                  y1+points[i][Y],
                  z1+points[i][Z]);
  }
  gl.glEnd();

  gl.glBegin(GL.GL_TRIANGLE_FAN);
  gl.glVertex3f(x2,y2,z2);
  for (int i=0; i<=ANZ; i++) {
    gl.glVertex3f(x2+points[i][X],
                  y2+points[i][Y],
                  z2+points[i][Z]);
  }
  gl.glEnd();

  gl.glBegin(GL.GL_QUAD_STRIP);
  for (int i=0; i<=ANZ; i++) {
    gl.glVertex3f(x1+points[i][X],
                  y1+points[i][Y],
                  z1+points[i][Z]);
    gl.glVertex3f(x2+points[i][X],
                  y2+points[i][Y],
                  z2+points[i][Z]);
  }
  gl.glEnd();      
}

答案 1 :(得分:0)

答案 2 :(得分:0)

如果您不能使用GLU库中的gluCylinder()(例如,如果您使用OpenGL-ES)

或者你必须从小的扁平段中制作圆柱的两侧,
How do you draw a cylinder with OpenGLES?

答案 3 :(得分:0)

此解决方案假设您有一些通用的Camera代码,它将3D矢量和4D四元数组合到一个实用程序类中。因此,对于世界空间中的两个3D点A和B,从A到B绘制圆柱体的一般解决方案是:

void draw_cylinder
(
     const vector3d& A
    ,const vector3d& B
    ,const double radius
)
{

    // STEP 1: calculate the distance between the points A and B

    double height = (B-A).length();

    // STEP 2: put a camera object at point A

    camera C(A); // quat rotation assumed to be facing -Z axis

    // STEP 3: tell the camera to look directly at point B

    C.look_at(B);

    // STEP 4: save the current OpenGl matrix state

    glPushMatrix();

    // STEP 5: give the camera transform to OpenGL 

    C.apply_transform_model_to_world();

    // STEP 6: draw your cylinder from (0,0,0) to (0,0,-height) 

    gluCylinder
    (
         pqobj      //GLUquadric*
        ,radius     //GLdouble base 
        ,radius     //GLdouble top
        ,height     //GLdouble 
        ,8          //GLint slices
        ,8          //GLint stacks
    );

    // STEP 7: restore the OpenGl matrix stack

    glPopMatrix();

}

答案 4 :(得分:-1)

使用当前接受的答案发现错误。结束了我自己的想法。你可以用一些简单的数学来做到这一点:

http://www.thjsmith.com/40/cylinder-between-two-points-opengl-c