Hello, I’m following this tutorial about height maps. My code works perfectly but I have a problem with understanding it’s main part. So
starting from the beginning:
-
Where does this formula come from?
unsigned char* texel = data + (j + width * i) * nChannels;
Why is it like that? -
To get x and z vertices we divide proper negated value by 2 and add i or j. Why is this height or width of an image if the coordinates should be in range between 0-1? For instance let’s say that I have an 800x600 image so -800/2=-400, (i = 0) -400 + i = -400.
-
Finally why y = texel[0]? Does it store the ‘grayness’ value?
for(unsigned int i = 0; i < height; i++)
{
for(unsigned int j = 0; j < width; j++)
{
// retrieve texel for (i,j) tex coord
unsigned char* texel = data + (j + width * i) * nChannels;
// raw height at coordinate
unsigned char y = texel[0];
// vertex
vertices.push_back( -height/2.0f + i ); // v.x
vertices.push_back( (int)y * yScale - yShift); // v.y
vertices.push_back( -width/2.0f + j/ ); // v.z
}
}