如何在opengl中绘制y轴或x轴的圆柱体

时间:2011-12-25 18:59:25

标签: c++ opengl cylindrical

我只想在opengl中绘制一个圆柱体。我发现了很多样本​​,但是所有样本都在z轴上绘制了圆柱体。我希望它们在x或y轴上。我怎样才能做到这一点。下面的代码是代码在z方向绘制圆柱体,我不想要它

  GLUquadricObj *quadratic;
  quadratic = gluNewQuadric();
  gluCylinder(quadratic,0.1f,0.1f,3.0f,32,32);

2 个答案:

答案 0 :(得分:8)

您可以使用glRotate(angle, x, y, z)旋转坐标系:

GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
gluCylinder(quadratic,0.1f,0.1f,3.0f,32,32);

http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml

答案 1 :(得分:4)

在每次渲染时,使用glPushMatrix glRotatef绘制圆柱体并使用glPopMatrix完成绘图。

例:glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate your object around the y axis on yRotationAngle radians

例:OnRender()功能示例

void OnRender() {
  glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background
  glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer
  glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations

  glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis on yRotationAngle radians

  // here *render* your cylinder (create and delete it in the other place. Not while rendering)
  gluCylinder(quadratic,0.1f,0.1f,3.0f,32,32);

  glFlush(); // Flush the OpenGL buffers to the window  
}