Processing and Virtual Reality Headset

edited June 2017 in Android Mode

Hi everybody,

I'm currently working on a design project which represent a virtual world. My processing is a land with a camera in movement, giving the impression of surfing. I would like to make it in virtual reality, how I can do virtual reality with a processing ? Is it easy ? How it works ?

Many thanks !!

Here screen shots :

Capture d’écran 2017-06-15 à 14.57.59

. Capture d’écran 2017-06-15 à 14.58.05

Here my code :

/**
 Terrain
 Demonstrating the Shapes3D library.

 Click and drag mouse to change direction

 W or P  = faster
 S or L  = slower

 Click on artefacts to change color/texture

 created by Peter Lager
 */

import shapes3d.utils.*;
import shapes3d.animation.*;
import shapes3d.*;

Terrain terrain;
Box skybox;
Box[] droids;
Tube tube;
Ellipsoid obelisk;

PVector[] droidDirs;
int nbrDroids;

TerrainCam cam;
int camHoversAt = 8;

float terrainSize = 1000;
float horizon = 400;

long time;
float camSpeed;
int count;
boolean clicked;

// List of image files for texture
String[] textures = new String[] {
  "grid01.png", "dada.png", "rouge.jpg", 
  "globe.jpg", "avast.png"
};

void setup() {
  size(1300, 900, P3D);
  cursor(CROSS);

  terrain = new Terrain(this, 60, terrainSize, horizon);
  terrain.usePerlinNoiseMap(0, 40, 0.15f, 0.15f);
  terrain.setTexture("grid01.png", 4);
  terrain.tag = "Ground";
  terrain.tagNo = -1;
  terrain.drawMode(S3D.TEXTURE);

/**
  skybox = new Box(this, 0, 0, 500);
  skybox.setSize(100, 100, 1);
  skybox.setTexture("dada.png", Box.FRONT);
  //skybox.setTexture("Black.png", Box.BACK);
  //skybox.setTexture("Black.png", Box.LEFT);
  //skybox.setTexture("Black.png", Box.RIGHT);
  //skybox.setTexture("Black.png", Box.TOP);
  //skybox.visible(true, Box.BOTTOM);
  skybox.drawMode(S3D.TEXTURE);
  skybox.tag = "Skybox";
  skybox.tagNo = -1;
  */

  nbrDroids = 4;
  droids = new Box[nbrDroids];
  droidDirs = new PVector[nbrDroids];
  for (int i = 0; i < nbrDroids; i++) {
    droids[i] = new Box(this, 10);
    droids[i].setSize(10, 10, 1);
    droids[i].moveTo(getRandomPosOnTerrain(terrain, terrainSize, 50));
    droids[i].tagNo = i;
      droids[i].tag = "cheval " + i;

    //droids[i].fill(color(random(128, 255), random(128, 255), random(128, 255)));    
    //droids[i].drawMode(S3D.SOLID);

    // Rajoute une texture cheval et annule la couleur
    droids[i].setTexture(textures[1]);
    droids[i].drawMode(S3D.TEXTURE);

    droidDirs[i] = getRandomVelocity(random(15, 25));
    terrain.addShape(droids[i]);
  }

  // 1 obélisque avec texture cheval
  obelisk = new Ellipsoid(this, 4, 20);
  obelisk.setRadius(5, 30, 5);
  obelisk.moveTo(getRandomPosOnTerrain(terrain, terrainSize, 28));
  obelisk.fill(color(0, 255, 0));
  obelisk.tag = "dada";
  obelisk.tagNo = 99999;
  obelisk.setTexture(textures[4]);
  obelisk.drawMode(S3D.TEXTURE);
  terrain.addShape(obelisk);

  // 1 tube aux fonds rouge et vert et au corps en texture cheval
  tube = new Tube(this, 10, 30);
  tube.setSize(3, 10, 6, 6, 20);
  tube.moveTo(getRandomPosOnTerrain(terrain, terrainSize, 15.5));
  tube.tagNo = 0;
  tube.setTexture(textures[1]);
  tube.drawMode(S3D.TEXTURE);
  tube.fill(color(255, 0, 0), Tube.S_CAP);
  tube.fill(color(0, 255, 0), Tube.E_CAP);
  tube.drawMode(S3D.SOLID, Tube.BOTH_CAP);

  camSpeed = 10;
  cam = new TerrainCam(this);
  cam.adjustToTerrain(terrain, Terrain.WRAP, camHoversAt);
  cam.camera();
  cam.speed(camSpeed);
  cam.forward.set(cam.lookDir());

  // Tell the terrain what camera to use
  terrain.cam = cam;

  time = millis();
}

void draw() {
  background(35);
  // Get elapsed time
  long t = millis() - time;
  time = millis();

  // Update shapes on terrain
  update(t/1000.0);

  // Update camera speed and direction
  if (mousePressed) 
    processMousePressed();
  if (keyPressed) 
    processKeyPressed(t);
    // Clicked on artefact?
  if (clicked)
    processMouseClick();

  // Calculate amount of movement based on velocity and time
  cam.move(t/1000.0);
  // Adjust the cameras position so we are over the terrain
  // at the given height.
  cam.adjustToTerrain(terrain, Terrain.WRAP, camHoversAt);
  // Set the camera view before drawing
  cam.camera();

  //obelisk.draw();
  //tube.draw();
  terrain.draw();

  // Get rid of directional lights so skybox is evenly lit.
  //skybox.moveTo(cam.eye().x, 0, cam.eye().z);
  //skybox.draw();
}

void processMousePressed() {
  float achange = (mouseX - pmouseX) * PI / width;
  // Keep view and move directions the same
  cam.rotateViewBy(achange);
  cam.turnBy(achange);
}

void processKeyPressed(long t) {
  if (key == 'W' || key =='w' || key == 'P' || key == 'p') {
    camSpeed += (t/100.0);
    cam.speed(camSpeed);
  } else if (key == 'S' || key =='s' || key == 'L' || key == 'l') {
    camSpeed -= (t/100.0);
    cam.speed(camSpeed);
  } else if (key == ' ') {
    camSpeed = 0;
    cam.speed(camSpeed);
  }
}


void processMouseClick() {
  clicked = false; // reset
  Shape3D selected = Shape3D.pickShape(this, mouseX, mouseY);
  println("hello" + selected);
  if (selected != null) {

     camSpeed = 0;
    cam.speed(camSpeed);


    if (selected.tagNo > textures.length)
      selected .fill(color(random(128, 255), random(128, 255), random(128, 255)));
    else if (selected.tagNo >= 0) {
      selected.tagNo = (selected.tagNo + 1) % textures.length;
      selected.setTexture(textures[selected.tagNo]);
    }
  }
}


/**
 * Update artefacts and seekers
 */
public void update(float time) {
  PVector np;
  obelisk.rotateBy(0, time*radians(16.9f), 0);
  tube.rotateBy(time*radians(25.6f), time*radians(6.871f), time*radians(17.3179f));
  for (int i = 0; i < nbrDroids; i++) {
    np = PVector.add(droids[i].getPosVec(), PVector.mult(droidDirs[i], time));
    droids[i].moveTo(np);
    droids[i].adjustToTerrain(terrain, Terrain.WRAP, 15);
  }
}

/**
 * Get a random position on the terrain avoiding the edges
 * @param t the terrain
 * @param tsize the size of the terrain
 * @param height height above terrain
 * @return
 */
public PVector getRandomPosOnTerrain(Terrain t, float tsize, float height) {
  PVector p = new PVector(random(-tsize/2.1f, tsize/2.1f), 0, random(-tsize/2.1f, tsize/2.1f));
  p.y = t.getHeight(p.x, p.z) - height;
  return p;
}

/**
 * Get random direction for seekers.
 * @param speed
 */
public PVector getRandomVelocity(float speed) {
  PVector v = new PVector(random(-10000, 10000), 0, random(-10000, 10000));
  v.normalize();
  v.mult(speed);
  return v;
}

/**
 * next texture or change color
 */
public void mouseClicked() {
  clicked = true;
}
Tagged:

Answers

Sign In or Register to comment.