c ++ opengl使用GL_LINE_LOOP绘制圆柱体

时间:2014-09-18 16:05:59

标签: c++ opengl

所以我试图通过以圆圈方式绘制一系列正方形(检查图像)来使用嵌套for循环来复制圆柱体。这是我需要实现的代码。我知道我应该重复地实现每个线路循环的四个坐标,直到它成为一个圆圈。我不知道如何计算:

-deltaZ

-xLeft

-yLeft

-xRight

-yRight

-thetaRight

我能得到一些帮助吗?谢谢! (P.S. numSides至少为3,numStacks开始于大约10

//Move everything to the z axis
glTranslatef(0.0, 0.0, 2.0);
//change its size so that way its so big
glScalef(0.5, 0.5, 0.0);

float radius = 1.0;
float zTop = 1.0;
float zBottom;
float xLeft;
float yLeft;
float xRight;
float yRight;
float thetaRight;

for (int m = 0; m <= numStacks; m++)
{

  zBottom = zTop-deltaZ;
  xLeft = 0;
  yLeft = 0;
  thetaRight = m*pi/numStacks;
  for (int i=0; i<numSides; i++)
  {
    xRight = 0;
    yRight = 0;
    glBegin(GL_LINE_LOOP);
    //top left
    glVertex3f(xLeft, yLeft, zTop);
    //bottom left
    glVertex3f(xLeft, yLeft, zBottom);
    //bottom right
    glVertex3f(xRight, yRight, zBottom);
    //top right
    glVertex3f(xRight, yRight, zBottom);
    glEnd();

    //exchanging these values moves the
    //line_loop to the right
    xLeft = xRight;
    yLeft = yRight;
    thetaRight = thetaRight + i*pi/numSides;
 }
  zTop = zBottom;
}

enter image description here

0 个答案:

没有答案