Loading...
Logo
Processing Forum
I tried running an old sketch I made in an earlier version of Processing and it doesn't work anymore. If I replace the line highlighted in red with:
Copy code
  1. pixels[i+j*width] = color(steps);
Then it works, but it used to work how it is in the code below. What changed?
Copy code
  1. float minThreshold = 0.0001;
  2. int maxDepth = 800;
  3. float angle = 0;

  4. void setup() {
  5.   size(400, 300, P2D);
  6.   //noLoop();
  7. }

  8. void draw() {
  9.   float x = cos(angle+HALF_PI)*50;
  10.   float z = sin(angle+HALF_PI)*50;
  11.   background(0);
  12.   loadPixels();
  13.   for (int i = 0; i < width; i++) {
  14.     for (int j = 0; j < height; j++) {
  15.       float dx = (float)i/width-0.5;
  16.       float dy = ((float)j/height-0.5)*(float)height/width;
  17.       float dz = 1.0;
  18.       float normVec = 1.0/sqrt(dx*dx+dy*dy+dz*dz);
  19.       dx *= normVec;
  20.       dy *= normVec;
  21.       dz *= normVec;
  22.       float rayX = 0;
  23.       float rayY = 0;
  24.       float rayZ = 0;
  25.       float rayLength = 0;
  26.       int steps = 255;
  27.       while (steps > 0 && rayLength < maxDepth) {
  28.         // Rotation Y-axis
  29.         float rotX = rayX*cos(angle)-rayZ*sin(angle);
  30.         float rotZ = rayZ*cos(angle)+rayX*sin(angle);

  31.         // Sphere
  32.         float sphereDist = sqrt(sq(rayX)+sq(rayY)+sq(rayZ-50))-5;

  33.         // Cube
  34.         //float cubeDist = max(abs(rayX)-5, max(abs(rayY-10)-5, abs(rayZ-50)-5));
  35.         float cubeDist = max(abs(rotX-x)-5, max(abs(rayY-10)-5, abs(rotZ-z)-5));

  36.         // Torus
  37.         //float q = sqrt(sq(rayX)+sq(rayY+10))-3.33;
  38.         //float torusDist = sqrt(sq(q)+sq(rayZ-50))-1.67;
  39.         float q = sqrt(sq(rotX-x)+sq(rayY+10))-3.33;
  40.         float torusDist = sqrt(sq(q)+sq(rotZ-z))-1.67;

  41.         float smallestDist = min(sphereDist, min(cubeDist, torusDist))*0.5;
  42.         if (smallestDist < minThreshold) {
  43.           pixels[i+j*width] = (steps<<16)+(steps<<8)+steps;
  44.           break;
  45.         }
  46.         else {
  47.           rayLength += smallestDist;
  48.           rayX = dx*rayLength;
  49.           rayY = dy*rayLength;
  50.           rayZ = dz*rayLength;
  51.           steps--;
  52.         }
  53.       }
  54.     }
  55.   }
  56.   updatePixels();
  57.   angle += TWO_PI/36;
  58. }

Replies(6)

The bitshift method you use creates transparent colors, since you only set the rgb.

Apparantly in older versions of Processing only the rgb information was used and the colors still showed. Processing 2.0 has apparantly improved and uses all the argb information (which is more accurate). Because of that the 'flaw' in your method is exposed.

To solve this you can use the following line instead:
Copy code
  1.           pixels[i+j*width] = (255<<24)+(steps<<16)+(steps<<8)+steps;
Thanks. Kind of surprised the default changed but it is nice to have transparency
I believe this ORed version works as well:
Copy code
  1. pixels[i + j*width] = 0xFF<<24 | steps<<16 | steps<<8 | steps;
    
Or even an exotic one using octal literals:
Copy code
  1. pixels[i + j*width] = 0xFF<<030 | steps<<020 | steps<<010 | steps;
    
That first one is quite nice, I didn't realize I could OR instead of adding. That second one just looks bizarre (I've never used octals), is it just for fun or is there a good reason to use octals?
Just for fun! 

But I don't get it why you would think the octal sequence:
030, 020, 010.

was more bizarre than:
24, 16, 8.

For me, the octal one is easier to memorize! 

P.S.: The same as a hexadecimal sequence would be:
0x18, 0x10, 0x8.
Indeed, it is an original usage of octal numbers, and one of the rare useful ones... (they are nearly never used, except by mistake...).