Bounding box rotation
in
Programming Questions
•
1 year ago
How do I rotate a set of points in a bounding box? I made a ray marcher for a Mandelbulb that works correctly but I want the part of the object that is currently facing the viewer to point down instead (a rotation of -HALF_PI along the x-axis). Using rotateX won't do anything as the image is made using pixels[].
Here is the code:
- int compact = 2;
- int maxIterations = 3;
- int power = 8;
- int screenSize = 400;
- float xmin = -1.5;
- float ymin = -1.5;
- float zmin = -1.5;
- float wh = 3;
- float toAdd = wh/(screenSize*compact);
- void setup() {
- size(screenSize, screenSize, P2D);
- noLoop();
- }
- void draw() {
- loadPixels();
- float[] pixVals = new float[screenSize*screenSize];
- int[] pixDivs = new int[screenSize*screenSize];
- float x = xmin;
- for (int i = 0; i < screenSize*compact; i++) {
- float y = ymin;
- for (int j = 0; j < screenSize*compact; j++) {
- float z = zmin;
- for (int k = 255; k > 0 && z < -1*zmin; k--) {
- float dr = 1;
- float nx = x;
- float ny = y;
- float nz = z;
- float rad = sqrt(x*x+y*y+z*z);
- int n = 0;
- while (n < maxIterations) {
- float powRad = pow(rad, power);
- float theta = atan2(sqrt(nx*nx+ny*ny), nz)*power;
- float phi = atan2(ny, nx)*power;
- nx = sin(theta)*cos(phi)*powRad+x;
- ny = sin(theta)*sin(phi)*powRad+y;
- nz = cos(theta)*powRad+z;
- dr = dr*power*pow(rad, power-1)+1;
- rad = sqrt(nx*nx+ny*ny+nz*nz);
- if (rad > 4) break;
- n++;
- }
- if (0.5*log(rad)*rad/dr < 0.00001) {
- int pixX = i/compact;
- int pixY = j/compact;
- if (pixX > 0 && pixX < screenSize && pixY > 0 && pixY < screenSize) {
- pixVals[pixX+pixY*screenSize] += k;
- pixDivs[pixX+pixY*screenSize]++;
- }
- break;
- }
- else {
- k--;
- z += 0.5*log(rad)*rad/dr;
- }
- }
- y += toAdd;
- }
- x += toAdd;
- println((float)i/(screenSize*compact)*100+"%");
- }
- for (int i = 0; i < screenSize*screenSize; i++) {
- int val = (int)((float)pixVals[i]/pixDivs[i]);
- pixels[i] = (val<<16)+(val<<8)+val;
- }
- updatePixels();
- println("Time: "+millis());
- saveFrame("result.png");
- }
1