C++ 如何在opengl中在y或x轴上绘制圆柱体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8631009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw cylinder in y or x axis in opengl
提问by brtb
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
我只想在opengl中画一个圆柱体。我发现了很多样本,但所有样本都在 z 轴上绘制圆柱体。我希望它们在 x 或 y 轴上。我怎样才能做到这一点。下面的代码是在 z 方向绘制圆柱体的代码,我不想要它
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
gluCylinder(quadratic,0.1f,0.1f,3.0f,32,32);
采纳答案by micha
You can use glRotate(angle, x, y, z)
to rotate your coordinate system:
您可以使用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);
回答by nickolay
On every render use glPushMatrix
glRotatef
draw the cylinder and finish your drawing with glPopMatrix
.
在每次渲染时使用glPushMatrix
glRotatef
绘制圆柱体并使用glPopMatrix
.
Ex.: glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate your object around the y axis on yRotationAngle radians
前任。: glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate your object around the y axis on yRotationAngle radians
Ex.: OnRender()
function example
例如: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
}