Session 8 (presentation)

NAIK - 'STELLATIONS' PROJECTION MAPPING TEST

How the physical object was coded in Processing

This Processing sketch uses the PeasyCam library to create a 3D camera, and the nervoussystem.obj library to export a 3D object. The setup() function sets the size of the window, initializes the PeasyCam object, and sets the background color, stroke color and fill color. The draw() function clears the background and draws the object. The keyPressed() function checks for key inputs and performs specific actions based on which key is pressed: 'r' for starting to record the obj file and 'n' for creating a new instance of the Shaper class.

This sketch requires:

  • The PeasyCam library for camera control

  • The OBJExport library from Nervous System

  • A custom class (see next code listing) that defines the geometry

Controls:

  • Mouse: Use PeasyCam's default controls for rotating/zooming

  • r' key: Export current geometry as OBJ file

  • 'n' key: Generate new geometry

// Import required libraries
import peasy.PeasyCam;          // PeasyCam library for 3D camera control
import nervoussystem.obj.*;     // Library for OBJ file export functionality

// Global variables
PeasyCam cam;    // Camera object for 3D navigation
Shaper o;        // Custom object that defines and draws the geometry

void setup() {
    // Initialize the sketch window
    size(800, 600, P3D);    // Create an 800x600 window with 3D rendering
    
    // Initialize the camera with a default distance of 400 units
    cam = new PeasyCam(this, 400);
    
    // Create a new instance of the custom Shaper class
    o = new Shaper();
    
    // Set up basic rendering properties
    background(0);          // Set black background
    stroke(255);           // Set white stroke color
    fill(255, 150);        // Set white fill color with 150/255 alpha (semi-transparent)
    //lights();            // Commented out lighting (can be uncommented to enable default lighting)
}

void draw() {
    // Clear the frame with a black background
    background(0);
    
    // Draw the current geometry defined in the Shaper class
    o.draw();
}

void keyPressed() {
    // Handle keyboard input
    
    if (key == 'r') {
        // Export the current geometry as an OBJ file
        // Filename includes "m60" prefix and a random number to avoid overwriting
        beginRecord("nervoussystem.obj.OBJExport", "m60" + (int)random(1000) + ".obj");
        o.draw();          // Draw the geometry to be exported
        endRecord();       // Complete the export process
    }
    
    if (key == 'n') {
        // Generate new geometry by creating a fresh instance of Shaper
        o = new Shaper();
    }
}

/**
 * Defines a 3D shape with a regular base and randomized top vertices
 */
class Shaper {
  // Scale determines the base size of the shape
  int scale = 50;
  // Maximum random offset that can be applied to top vertices
  int randomness = 100;
  
  // Vertices for the top face (randomized positions)
  PVector top1, top2, top3, top4;
  // Vertices for the bottom face (fixed positions)
  PVector bottom1, bottom2, bottom3, bottom4;
  
  Shaper() {
    // Initialize bottom vertices in a square pattern
    bottom1 = new PVector(-scale, 0, scale);    // Bottom front left
    bottom2 = new PVector(-scale, 0, -scale);   // Bottom back left
    bottom3 = new PVector(scale, 0, -scale);    // Bottom back right
    bottom4 = new PVector(scale, 0, scale);     // Bottom front right
    
    // Create top vertices with random offsets
    // Each coordinate gets a random offset between -randomness and +randomness
    // Y coordinate is set higher (-2*scale) to create height
    top1 = new PVector(-scale+random(-randomness,randomness), -2*scale+random(-randomness,randomness), scale+random(-randomness,randomness));    // Top front left
    top2 = new PVector(-scale+random(-randomness,randomness), -2*scale+random(-randomness,randomness), -scale+random(-randomness,randomness));   // Top back left
    top3 = new PVector(scale+random(-randomness,randomness), -2*scale+random(-randomness,randomness), -scale+random(-randomness,randomness));    // Top back right
    top4 = new PVector(scale+random(-randomness,randomness), -2*scale+random(-randomness,randomness), scale+random(-randomness,randomness));     // Top front right
  }
  
  void draw() {
    // Draw shape using QUADS (each face made of 4 vertices)
    beginShape(QUADS);
    
      // Bottom face
      vertex(bottom1.x, bottom1.y, bottom1.z);
      vertex(bottom2.x, bottom2.y, bottom2.z);
      vertex(bottom3.x, bottom3.y, bottom3.z);
      vertex(bottom4.x, bottom4.y, bottom4.z);
      
      // Top face
      vertex(top1.x, top1.y, top1.z);
      vertex(top2.x, top2.y, top2.z);
      vertex(top3.x, top3.y, top3.z);
      vertex(top4.x, top4.y, top4.z);
      
      // Front face
      vertex(top1.x, top1.y, top1.z);
      vertex(top4.x, top4.y, top4.z);
      vertex(bottom4.x, bottom4.y, bottom4.z);
      vertex(bottom1.x, bottom1.y, bottom1.z);
      
      // Right face
      vertex(top4.x, top4.y, top4.z);
      vertex(top3.x, top3.y, top3.z);
      vertex(bottom3.x, bottom3.y, bottom3.z);
      vertex(bottom4.x, bottom4.y, bottom4.z);
      
      // Back face
      vertex(top3.x, top3.y, top3.z);
      vertex(top2.x, top2.y, top2.z);
      vertex(bottom2.x, bottom2.y, bottom2.z);
      vertex(bottom3.x, bottom3.y, bottom3.z);
      
      // Left face
      vertex(top2.x, top2.y, top2.z);
      vertex(top1.x, top1.y, top1.z);
      vertex(bottom1.x, bottom1.y, bottom1.z);
      vertex(bottom2.x, bottom2.y, bottom2.z);
    
    endShape();
  }
}

Last updated