문제

I'm trying to create a Menger Sponge for an assignment,

Meger sponge image from Wikipedia

and at this point I have a 3*3*3 cube of cubes. I'm now trying to remove the correct blocks, to form the first iteration of the fractal. Each of the 27 blocks I have given an index, starting at 0 (blockNumber in my code). The block I have to remove are contained in my skipHere[] array. 4 is the first one removed, because it is the middle block of the first row.

This code however is still printing out the full 3*3*3 structure, not skipping any block i'm trying to skip. So I think I'm looping over the skipHere[] array incorrectly, but I'm having trouble finding my logic error.

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    UpdateTransform(0,0,0);

    int skipHere[7] = {4, 10, 12, 13, 14, 16, 22};
    int blockNumber = 0;
    bool skip = false;
    int x = 0;
    int y = 0;
    int z = 0;
    float Xres = 0;
    float Yres = 0;
    float Zres = 0;
    for(x = 0;x < 3; ++x)
    {

      for(y = 0; y < 3; ++y)
      {

      for(z = 0; z < 3; ++z)
      {
        for(int i = 0; i < 7; i++)
        {

          if(blockNumber == skipHere[i])
          {
            skip = true;
          }

          if(skip == false)
          {
            glPushMatrix();
            UpdateTransform(Xres,Yres,Zres);
            drawOneCube();
            glPopMatrix();

          }
          skip = false;


      }

      Zres -= 1.1;

      blockNumber += 1;
    }   

    Yres += 1.1;
    Zres = 0;

      }

      Xres += 1.1;
      Yres = 0;


    }


    glutSwapBuffers();

}
도움이 되었습니까?

해결책

For each block (x,y,z) you iterate over all elements of skipHere in the most inner loop. At maximum one of these iterations could lead blockNumber == skipHere[i] and set skip to true.

However you also set skip = false afterwards in each loop iteration. So practically you will print each block at least 6 times (because it will only be skipped at most once of the seven iterations for i).

You should move the part

if(skip == false)
{
  glPushMatrix();
  UpdateTransform(Xres,Yres,Zres);
  drawOneCube();
  glPopMatrix();

}
skip = false;

outside the most inner loop, so there is at most one printing of each block and only if neither of the seven most inner loop iterations led to skip = true.

for(int i = 0; i < 7; i++)
{
    if(blockNumber == skipHere[i])
    {
        skip = true;
    }
}

if(!skip)
{
    glPushMatrix();
    UpdateTransform(Xres,Yres,Zres);
    drawOneCube();
    glPopMatrix();
}

skip = false;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top