# Curses! Artifacts!

• 04-27-2013, 07:26 PM
jrandom
Curses! Artifacts!
I've verified this isn't a result of my coordinate transforms, so the simplex noise generator itself must be messed up. I'll be darned if I know how.

*grumble* *grumble*

Attachment 113967

Attachment 113968
• 04-27-2013, 09:31 PM
jrandom
Just crawled over every single line of code in the noise generator. Looks solid. Argh.
• 04-27-2013, 10:00 PM
jrandom
If anyone is curious: the code for the noise generator (updated to include the complete fix I finally stumbled across -- see a few posts down).

I've only been working with the 3D noise generator. While 1D and 2D have been coded, neither have been tested.
• 04-27-2013, 10:44 PM
jrandom
Interesting... I think the artifacts are limited to areas where at least one of the texture coordinates is negative...

(don't mind me, I'm pretty much just talking to myself at this point)
• 04-27-2013, 10:58 PM
jrandom
SOLVED! And it's a bug in the reference implementation. Not my fault! Whee!

Once I knew that negative coordinates were involved in the problem, a google search turned up the answer -- There's a section of code that gets array indexes:

Code:

```            const int ii = i % 256;             const int jj = j % 256;             const int kk = k % 256;```
But modulus preserves sign! This results in negative numbers being used to access the arrays, grabbing whatever memory happens to be there. Using a simple inline abs() function (so the compiler can optimize away branching) fixes the problem:

Code:

```static inline int Fast_Abs( int value ) {     return ( value < 0 ) ? -value : value; }```
Code:

```            const int ii = Fast_Abs(i) % 256;             const int jj = Fast_Abs(j) % 256;             const int kk = Fast_Abs(k) % 256;```
Well that was a waste of a day. Grr. At least I took a break and got some good solid hours put into Fallout: New Vegas. Shooting mutant radioactive wasteland monsters can be cathartic.
• 04-27-2013, 11:06 PM
jrandom
... which brings up a whole new artifact problem. ARGH. Yeargh. Gagh. Mrgh.

Attachment 113971
• 04-27-2013, 11:43 PM
jrandom
FINALLY!

Simply using the absolute value gets you all sorts of discontinuities as seen above. You have to invert if the integer value is less than zero, but you still get discontinuities at -1 -- a fun sort of off-by-one error. This finally did it:

Code:

```            auto int_process = [](int v) -> int             {                 return (v >= 0) ? v % 256                                 : 255 - ((-(v+1)) % 256);             };                         const int ii = int_process(i);             const int jj = int_process(j);             const int kk = int_process(k);```
• 04-28-2013, 08:56 AM
jrandom
... or simply use the obvious solution:

Code:

`const int ii = i & 0xFF;`