Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
finalproject_geomatry/final_dragging.pde
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
202 lines (161 sloc)
4.91 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
click/drag to rotate entire object | |
click/ drag onto vertices | |
*/ | |
PGraphics3D p3d; | |
PMatrix3D proj = new PMatrix3D(); | |
PMatrix3D cam = new PMatrix3D(); | |
PMatrix3D modvw = new PMatrix3D(); | |
PMatrix3D modvwInv = new PMatrix3D(); | |
PMatrix3D screen2Model = new PMatrix3D(); | |
// rotation | |
float rotX, rotY; | |
// arrays hold the vertices of 3d object | |
float[] vertices3D = new float[0]; | |
int[] vertices2D = new int[0]; | |
// index of current mouseover / clicked vertex | |
int vertexMouseOver = -1; | |
int vertexKlicked= -1; | |
// z value in model/world space of current vertex | |
float zModelMouseOver; | |
float zModelKlick; | |
void setup() { | |
size(800, 600, P3D); | |
p3d = (PGraphics3D)g; | |
frameRate(50); | |
//generate 6 vertices randomly | |
for(int i=0;i<18;i++) { | |
vertices3D = append(vertices3D, random(-200, 200)); | |
} | |
for(int i=0;i<=5;i++) | |
{ | |
vertices3D[3*i+2]=0; | |
} | |
} | |
void draw() { | |
background(255); | |
// 3d object space begin | |
pushMatrix(); | |
//apply mouse rotation and translation to center of screen | |
translations(); | |
//get 3d matrices | |
proj = p3d.projection.get(); | |
cam = p3d.camera.get(); | |
modvw = p3d.modelview.get(); | |
modvwInv = p3d.modelviewInv.get(); | |
//visualize 3d axes for orientation | |
drawAxes3D(); | |
//visualize vertices | |
drawVertSphere(); | |
// drawVert(); | |
hitDetect(); | |
// calculate z value of picked vertex in model(world) space | |
if (vertexMouseOver > -1) { | |
zModelMouseOver = modelZ(vertices3D[vertexMouseOver], vertices3D[vertexMouseOver+1], vertices3D[vertexMouseOver+2]); | |
} | |
// 3d object space end | |
popMatrix(); | |
drawHitDetect(); | |
} | |
void mousePressed(){ | |
if (mouseButton==LEFT && vertexMouseOver>-1) | |
{ | |
vertexKlicked = vertexMouseOver; | |
zModelKlick = zModelMouseOver; | |
// calculate transformation matrix for projecting mouse coordsx | |
screen2Model = modvwInv; | |
screen2Model.apply(cam); | |
} | |
} | |
void mouseReleased(){ | |
vertexKlicked = -1; | |
} | |
void mouseDragged() { | |
if (mouseButton==LEFT && vertexKlicked>-1) { | |
float scrn[] = {mouseX, mouseY, 0}; | |
float model[] = new float[3]; | |
// apply transformation matrices to mouse coords | |
screen2Model.mult(scrn, model); | |
vertices3D[vertexKlicked] = model[0]; | |
vertices3D[vertexKlicked+1] = model[1]; | |
vertices3D[vertexKlicked+2] = 0; | |
} | |
else { | |
// mouse controlled rotation | |
float x1 = mouseX-pmouseX; | |
float y1 = mouseY-pmouseY; | |
rotX += -y1 * 0.01; | |
rotY += x1 * 0.01; | |
} | |
} | |
void translations() { | |
translate(width/2, height/2); | |
//mouse rotate | |
rotateX(rotX); | |
rotateY(rotY); | |
} | |
void drawAxes3D() { | |
for (int i=-500; i<500; i += 20){ | |
stroke(230); | |
line(i, 0, 0, i, 500, 0); | |
line(0, i, 0, 500, i, 0); | |
line(i, 0, 0, i, -500, 0); | |
line(0, i, 0, -500, i, 0); | |
} | |
for(int i=0;i<500;i=i+10) | |
{ | |
for (int j=0;j<500;j=j+10) | |
{ | |
stroke(50); | |
curve((i+0), (j+0),((i+0)*(i+0)+(j+0)*(j+0))/250,(i+1), (j+1),((i+1)*(i+1)+(j+1)*(j+1))/250, (i+2), (j+2),((i+2)*(i+2)+(j+2)*(j+2))/250,(i+3), (j+3),((i+3)*(i+3)+(j+3)*(j+3))/250); | |
curve(-(i+0), (j+0),((i+0)*(i+0)+(j+0)*(j+0))/250,-(i+1), (j+1),((i+1)*(i+1)+(j+1)*(j+1))/250, -(i+2), (j+2),((i+2)*(i+2)+(j+2)*(j+2))/250,-(i+3), (j+3),((i+3)*(i+3)+(j+3)*(j+3))/250); | |
curve((i+0), -(j+0),((i+0)*(i+0)+(j+0)*(j+0))/250,(i+1), -(j+1),((i+1)*(i+1)+(j+1)*(j+1))/250, (i+2), -(j+2),((i+2)*(i+2)+(j+2)*(j+2))/250,(i+3), -(j+3),((i+3)*(i+3)+(j+3)*(j+3))/250); | |
curve(-(i+0), -(j+0),((i+0)*(i+0)+(j+0)*(j+0))/250,-(i+1), -(j+1),((i+1)*(i+1)+(j+1)*(j+1))/250, -(i+2), -(j+2),((i+2)*(i+2)+(j+2)*(j+2))/250,-(i+3), -(j+3),((i+3)*(i+3)+(j+3)*(j+3))/250); | |
} | |
} | |
} | |
void drawVertSphere() { | |
//3d vertices3D as spheres | |
noStroke(); | |
fill(100); | |
sphereDetail(4); | |
for(int i=0; i<vertices3D.length; i=i+3) | |
{float a= vertices3D[i]; | |
float b= vertices3D[i+1]; | |
pushMatrix(); | |
translate(vertices3D[i], vertices3D[i+1], vertices3D[i+2]); | |
sphere(3); | |
popMatrix(); | |
pushMatrix(); | |
translate(vertices3D[i], vertices3D[i+1],(a*a+b*b)/250); | |
stroke(50); | |
sphere(3); | |
popMatrix(); | |
line(a,b,0,a,b,(a*a+b*b)/250); | |
} | |
} | |
void hitDetect() { | |
// mouse hit detection using screnX, screenY | |
vertices2D = new int[0]; | |
vertexMouseOver = -1; | |
for(int i=0; i<vertices3D.length; i=i+3) { | |
int x = int(screenX(vertices3D[i], vertices3D[i+1], vertices3D[i+2])); | |
int y = int(screenY(vertices3D[i], vertices3D[i+1], vertices3D[i+2])); | |
vertices2D = append(vertices2D, x); | |
vertices2D = append(vertices2D, y); | |
if (x > mouseX-3 && x < mouseX+3 && y > mouseY-3 && y < mouseY+3) { | |
vertexMouseOver = i; | |
} | |
} | |
} | |
void drawHitDetect() { | |
if (vertexKlicked > -1) { | |
stroke(255, 0, 0); | |
noFill(); | |
ellipse(vertices2D[vertexKlicked/3*2], vertices2D[vertexKlicked/3*2+1], 30, 30); | |
} else if (vertexMouseOver > -1) { | |
stroke(100, 100, 100); | |
noFill(); | |
ellipse(vertices2D[vertexMouseOver/3*2], vertices2D[vertexMouseOver/3*2+1], 20, 20); | |
} | |
} | |