Code viewer for World: Cube World (Title test)
// Cloned by Daniel Marcu on 19 Sep 2023 from World "One Cube World (P5)" by Starter user 
// Please leave this clone trail here.

const objectsize = 100;      // size of object   
const anglechange = 0.001;   // how much the rotate angle changes each step
var angle = 0;                // rotate angle starts at 0
var dragX = 0;                // variable to track mouse drag X
var dragY = 0;                // variable to track mouse drag Y
var img;

// make an array of random (x,y,z) positions
const noboxes = 30;               // how many boxes to have
var a = new Array(noboxes);       // array of the box positions

for (var i = 0; i < noboxes; i++)   // set up the array
{
  a[i] = [random(-500, 500), random(-500, 500), random(-500, 500)];
}

function preload() {
  img = loadImage('path_to_your_image.png'); // Replace 'path_to_your_image.png' with the correct image path.
}

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
}

function drawSolidCube(size) {
  // Define the vertices of a solid cube
  let vertices = [
    [-size / 2, -size / 2, -size / 2],
    [size / 2, -size / 2, -size / 2],
    [size / 2, size / 2, -size / 2],
    [-size / 2, size / 2, -size / 2],
    [-size / 2, -size / 2, size / 2],
    [size / 2, -size / 2, size / 2],
    [size / 2, size / 2, size / 2],
    [-size / 2, size / 2, size / 2],
  ];

  // Define the faces (vertices indices) of the cube
  let faces = [
    [0, 1, 2, 3],
    [4, 5, 6, 7],
    [0, 3, 7, 4],
    [1, 2, 6, 5],
    [0, 1, 5, 4],
    [2, 3, 7, 6],
  ];

  // Draw the cube by connecting vertices with faces
  for (let i = 0; i < 6; i++) {
    beginShape();
    for (let j = 0; j < 4; j++) {
      let vIdx = faces[i][j];
      vertex(vertices[vIdx][0], vertices[vIdx][1], vertices[vIdx][2]);
    }
    endShape(CLOSE);
  }
}

function draw() {
  background("red");    // background color
  texture(img);

  // Apply the rotation from mouse dragging
  rotateX(angle + dragY);
  rotateY(angle + dragX);
  rotateZ(angle);

  for (var i = 0; i < noboxes; i++) {
    push(); // Save the current transformation matrix
    translate(a[i][0], a[i][1], a[i][2]);   // get box position i
    drawSolidCube(objectsize); // Draw the solid cube
    pop(); // Restore the previous transformation matrix
  }

  angle += anglechange;  // change angle each step to get rotate movement
}

// Mouse interaction to make the scene draggable
function mouseDragged() {
  dragX += radians(mouseX - pmouseX);
  dragY += radians(mouseY - pmouseY);
}