Davbol- thanks, that was the rotation issue! Things now work 99% as expected- as for the x rotation- when the camera is looking straight down on the cube, it seems to spin a bit on the Y axis. There seems to be no graceful way to orbit 360 degrees on the X like you can on the Y.
John- not sure exactly what you mean. With the mouse drag I'm going for the distance dragged as a percentage of the screen width/height to determine the amount of rotation. If there is a simpler way do tell!
Code:import processing.opengl.*;
int canvasSize = 512;
int gridLines = 11;
float gridSize = 100;
float x = 0;
float y = 25;
float z = -75;
float x2,y2,z2;
float x3,y3,z3;
float rotX, rotY;
int mousePressedX, mousePressedY;
int dragLengthX, dragLengthY;
void setup() {
size(canvasSize, canvasSize, OPENGL);
noFill();
noStroke();
x3 = x2 = x;
y3 = y2 = y;
z3 = z2 = z;
}
void draw() {
background(255);
// camera
beginCamera();
if(mousePressed)
camera(x3, y3, z3, 0, 20, 0, 0, -1, 0);
else
camera(x, y, z, 0, 20, 0, 0, -1, 0);
perspective(1.235, 1, .1, 500);
endCamera();
// draw grid
pushMatrix();
translate(-gridSize/2, 0, -gridSize/2);
stroke(200, 100, 100, 50);
strokeWeight(1);
float gridSpacing = gridSize / (float)gridLines;
for (int i=0; i<gridLines+1; i++) {
line(0, 0, i*gridSpacing, gridSize, 0, i*gridSpacing);
line(i*gridSpacing, 0, 0, i*gridSpacing, 0, gridSize);
}
popMatrix();
// draw box
pushMatrix();
translate(0, 25, 0);
noFill();
stroke(0, 0, 0);
box(50, 50, 50);
popMatrix();
// draw one side red
pushMatrix();
noStroke();
fill(220, 5, 15, 50);
beginShape(QUADS);
vertex(-25,0,25);
vertex(-25,50,25);
vertex(25,50,25);
vertex(25,0,25);
endShape();
popMatrix();
}
void mousePressed() {
mousePressedX = mouseX;
mousePressedY = mouseY;
}
void mouseReleased() {
z=z3;
x=x3;
y=y3;
}
void mouseDragged() {
dragLengthX = mouseX - mousePressedX;
dragLengthY = mouseY - mousePressedY;
// rotate around Y axis
rotY = (float)dragLengthX / (float)canvasSize * PI;
z2 = z*cos(rotY) - x*sin(rotY);
x2 = z*sin(rotY) + x*cos(rotY);
y2 = y;
// rotate around X axis
rotX = (float)dragLengthY / (float)canvasSize * PI;
y3 = y2*cos(rotX) - z2*sin(rotX);
z3 = y2*sin(rotX) + z2*cos(rotX);
x3 = x2;
}