gl-terrain-demo icon indicating copy to clipboard operation
gl-terrain-demo copied to clipboard

Wring scale calculation?

Open urosidoki opened this issue 3 years ago • 1 comments

https://github.com/itoral/gl-terrain-demo/blob/efbe5e77268f1c111ab6bc52a45a86d8ebad9655/src/ter-terrain.cpp#L106

I am not sure this line (and the one below is correct, you might cause a segmentation fault later on when you call float h = pixels[img_y * image->pitch + img_x * 4];

Am I mistaken?

urosidoki avatar Jan 09 '22 13:01 urosidoki

Yes, you're right. Not a segfault necessarily, but we may be accessing the pixels array out of bounds.

I think we want to divide by t->width and t->height to compute the scale:

float scale_x = ((float) image->w) / t->width; 
float scale_y = ((float) image->h) / t->height; 

And then we probably want to do:

int img_x =  MIN2(roundf(x * scale_x), image->w  - 1);
int img_y =  MIN2(roundf(y * scale_y), image->h - 1);

itoral avatar Jan 10 '22 06:01 itoral