if() condition not working inside hint(DISABLE_DEPTH_TEST) using OPENGL for 3D

edited April 2014 in JavaScript Mode

Hi, Is there any way to get an if() condition to work between hint(DISABLE_DEPTH_TEST) and hint(ENABLE_DEPTH_TEST)? See lines 86 and 99.

import processing.opengl.*;
float dx = 0.0, dy = 0.0; // distance dragged in x and y directions when mouse is pressed
float dxTemp, dyTemp;
float camX = 0.0, camY = 0.0, camZ = 0.0;
float R = 150; // radius of sphere camera moves on
float dragx1 = 0.0, dragy1 = 0.0; // values of x and y when mouse is first pressed
boolean overRegion = false;
boolean pressed = false;

float x = 0, z = 0;
int A = 5; //amplitude
int s = 100; //spacing between sources
int n = 5; //number of wavelengths from source o central maximum
int lambda = 25; // wavelength
float y1, y2, angle = 0.0;
float valueX;
float incr = 2*PI/90; //speed of wave
float d = sqrt(n*n*lambda*lambda-((s*s)/4)); //width of viewable area and also half the length of the viewable length
boolean button1 = true; //wave 1 on by default
boolean button2 = false;
boolean button3 = false;

void setup() {
  size(400, 400, OPENGL);
}

void draw() {
  background (255, 255, 255);
  if (mouseX > 10.0 && mouseY > 10.0) { //later this will be the region inside the border controls
    overRegion = true;
  }
  else {
    overRegion = false;
  }
  camX = map(dx, 0, 200, 0, R);
  camY = map(dy, 0, 200, 0, R);
  camZ = sqrt(pow(R, 2)-pow(camX, 2)-pow(camY, 2));
  //camera(100, -100, 400, //camera location fixed
  camera(camX, camY, camZ, //camera location dynamic
  0, 0, 0, // camera target
  0, 1, 0); // camera orientation

  stroke(200, 0, 0);//red
  line(-200, 0, 0, 200, 0, 0);//x-axis
  stroke(0, 200, 0);//green
  line(0, -200, 0, 0, 200, 0);//y-axis
  stroke(0, 0, 200);//blue
  line(0, 0, -200, 0, 0, 200);//z-axis
  stroke(0, 0, 0);
  //sphere(30);//main sphere at origin

  pushMatrix();//draws spheres to indicate positive side of axes
  translate(150, 0, 0);
  sphere(5);//positive x-axis
  translate(-150, 150, 0);
  sphere(5);//positive y-axis
  translate(0, -150, 150);
  sphere(5);//positive z-axis
  popMatrix();

  for (x = 0; x < d; x += 2) {
    for (z = -d; z < d; z += 2) {
      y1=A*sin(angle+(sqrt(x*x+(z-s/2)*(z-s/2))/lambda)*2*PI);
      y2=A*sin(angle+(sqrt(x*x+(z+s/2)*(z+s/2))/lambda)*2*PI);
      if (button1) {
        stroke(#0D1EB4);//blue
        point(x, y1, z);//wave1
      } 
      if (button2) {
        stroke(#F20A25);//red
        point(x, y2, z);//wave2
      }
      if (button3) {
        stroke(#050505);//grey
        point(x, y1+y2, z);//interference
      }
    }
  }
  angle = angle - incr;
  stroke(#2598CB);//green
  translate(0, 0, -s/2);
  sphere(5);
  translate(0, 0, s);//translation is accumulative each time it is used
  sphere(5);

  hint(DISABLE_DEPTH_TEST);// Disable Depth Test
  //camera();
  camera(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
  stroke(0, 0, 0);//rect edge colour
  fill(#919293);
  rect(0, 0, width, 30);//grey horizontal top border
  rect(0, 30, 30, height);//grey vertical left border
  textSize(12);
  fill(0, 0, 0);//text colour
  text("Wave1", 10, 20);
  text("Wave2", 65, 20);
  text("Interference", 115, 20); 

  if (button1) {
    fill(#BFE1F0);//rect colour
    rect(0, 0, 55, 30, 7);
  }
  else {
    fill(#6FCBF5);//rect colour
    rect(0, 0, 55, 30, 7);
  }
/*
  if (button2) {
    fill(#BFE1F0);//rect colour
    rect(55, 0, 55, 30, 7);
  } 
  else {
    fill(#6FCBF5);//rect colour
    rect(55, 0, 55, 30, 7);
  }
  if (button3) {
    fill(#BFE1F0);//rect colour
    rect(110, 0, 80, 30, 7);
  } 
  else {
    fill(#6FCBF5);//rect colour
    rect(110, 0, 80, 30, 7);
  }
*/
  hint(ENABLE_DEPTH_TEST);// Enable Depth Test
}//end of draw()

void mousePressed() {
  if (overRegion) {
    pressed = true;
  } 
  else {
    pressed = false;
  }
  dragx1 = mouseX;
  dragy1 = mouseY;
  if (mouseButton == LEFT) {
    if (mouseX > 0 && mouseX < 54.9 && mouseY > 0 && mouseY < 30) {
      button1 = !button1;
    }
    if (mouseX > 55 && mouseX < 110 && mouseY > 0 && mouseY < 30) {
      button2 = !button2;
    }
    if (mouseX > 110 && mouseX < 190 && mouseY > 0 && mouseY < 30) {
      button3 = !button3;
    }
  }
}

void mouseDragged() {
  if (pressed) {
    dx = mouseX - dragx1 + dxTemp;
    dy = mouseY - dragy1 + dyTemp;
  }
}

void mouseReleased() {
  dxTemp = dx;
  dyTemp = dy;
  pressed = false;
}

Thanks, Shane

Answers

  • As far as I can see the if statements work just fine - there is no syntax error and they don't produce a runtime error.

    The buttons are fixed in the XY plane so rotate when dragging with the mouse but then that means the coordinates used for the button tests in the mousePressed() method are invalid.

    You need to be more specific about what you want to to happen.

  • Thanks, but why does it work on my computer but not when I put it online?

    Shane

  • Perhaps hint(DISABLE_DEPTH_TEST) and hint(ENABLE_DEPTH_TEST) are not supported in JavaScript mode. :-?

  • Hi, Hint() is mentioned on the reference pages for processing.js http://processingjs.org/reference/hint_/

  • Answer ✓

    They copied the Processing reference, but I am not sure they support everything described there...

Sign In or Register to comment.