How to draw cylinder in y or x axis in opengl

安稳与你 提交于 2019-11-30 03:55:46

问题


I just want to draw a cylinder in opengl. I found lots of samples but all of them draws cylinders in z axis. I want them be in x or y axis. How can i do this. The code below is the code draw the cylinder in z direction and i dont want it

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

回答1:


You can use glRotate(angle, x, y, z) to rotate your coordinate system:

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




回答2:


On every render use glPushMatrix glRotatef draw the cylinder and finish your drawing with glPopMatrix.

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

Ex.: OnRender() function example

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  
}  


来源:https://stackoverflow.com/questions/8631009/how-to-draw-cylinder-in-y-or-x-axis-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!