-2

I am writing some OpenGL app, whatever. In one place I've decided to use structured bindings, cuz it would save me a lot of typing. Here it is:

for (auto row = 0; row < N - 1; ++row) {
            for (auto col = 0; col < N - 1; ++col) {
                //glColor3f(colors[row][col].R, colors[row][col].G, colors[row][col].B);
                auto[xu, xv, yu, yv, zu, zv] = normal_points[row][col];
                factor = std::sqrt((yu * zv - zu * yv) + (zu * xv - xu * zv) + (xu * yv - yu * xv));
                glNormal3f((yu * zv - zu * yv) / factor, (zu * xv - xu * zv) / factor, (xu * yv - yu * xv) / factor);
                glVertex3f(points[row][col].x, points[row][col].y, points[row][col].z);
                //glColor3f(colors[row + 1][col].R, colors[row + 1][col].G, colors[row + 1][col].B);
                auto[xu, xv, yu, yv, zu, zv] = normal_points[row+1][col];
                factor = std::sqrt((yu * zv - zu * yv) + (zu * xv - xu * zv) + (xu * yv - yu * xv));
                glNormal3f((yu * zv - zu * yv) / factor, (zu * xv - xu * zv) / factor, (xu * yv - yu * xv) / factor);
                glVertex3f(points[row + 1][col].x, points[row + 1][col].y, points[row + 1][col].z);
                //glColor3f(colors[row][col + 1].R, colors[row][col + 1].G, colors[row][col + 1].B);
                auto[xu, xv, yu, yv, zu, zv] = normal_points[row][col+1];
                factor = std::sqrt((yu * zv - zu * yv) + (zu * xv - xu * zv) + (xu * yv - yu * xv));
                glNormal3f((yu * zv - zu * yv) / factor, (zu * xv - xu * zv) / factor, (xu * yv - yu * xv) / factor);
                glVertex3f(points[row][col + 1].x, points[row][col + 1].y, points[row][col + 1].z);
            }
        }

Now the problem is that with first binding (using normal_points[row][col]) it works perfectly. But when I do this for normal_points[row+1][col] the numbers that I get are the same. But in normal_points 2D array they are not. Proof:

enter image description here

enter image description here

So in the Watch 1 tab you can see that in the array those values differ, but when I jumped from the first breakpoint to the second one, the values of xu, xv etc. didn't change. How can I fix that? Is it because I use the same names? And if so, can't I use the same names? (cuz I've got second loop just like this one, and it would really save a lot of typing and provide readability)

W.F.
  • 13,888
  • 2
  • 34
  • 81
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57

1 Answers1

2

Sorry but I'm surprised that your code compile.

You write three times, in the same scope,

auto[xu, xv, yu, yv, zu, zv] = <something>;

So you're defining three times, in the same scope, the same variables.

Suggestion: use different names

auto [xu1, xv1, yu1, yv1, zu1, zv1] = normal_points[row][col];
// ...
auto [xu2, xv2, yu2, yv2, zu2, zv2] = normal_points[row+1][col];
// ...
auto [xu3, xv3, yu3, yv3, zu3, zv3] = normal_points[row][col+1];

or use three different scopes

for (auto col = 0; col < N - 1; ++col)
 {
    {
      auto [xu, xv, yu, yv, zu, zv] = normal_points[row][col];
      // ...
    }
    {
      auto [xu, xv, yu, yv, zu, zv] = normal_points[row+1][col];
      // ...
    }
    {
      auto [xu, xv, yu, yv, zu, zv] = normal_points[row][col+1];
      // ...
    }
 }
max66
  • 65,235
  • 10
  • 71
  • 111