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?
Visual-Curve-Servlet/src/com/knotrenderer/view/KnotRenderer.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
204 lines (174 sloc)
5.4 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
package com.knotrenderer.view; | |
import java.sql.Date; | |
import java.text.SimpleDateFormat; | |
import com.knotrenderer.model.Curve; | |
import com.knotrenderer.util.CurveLoader; | |
import processing.core.PApplet; | |
import processing.event.KeyEvent; | |
import processing.event.MouseEvent; | |
public class KnotRenderer extends PApplet | |
{ | |
// private static final float CAMERA_START_Z = 40f; | |
// private static final float ZOOM_MULTIPLIER = 20.f; | |
private static final float CAMERA_START_Z = 40f; | |
private static final float ZOOM_MULTIPLIER = 5.f; | |
private static int WIDTH = 640; | |
private static int HEIGHT = 480; | |
private RenderCurve renderCurve; | |
private float cameraX = WIDTH / 2; | |
private float cameraY = HEIGHT / 2; | |
private float cameraZ = CAMERA_START_Z; | |
private float startMouseX; | |
private float startMouseY; | |
private float rotationX = 0; | |
private float rotationY = 0; | |
public static void main(String[] args) | |
{ | |
PApplet.main(new String[] {KnotRenderer.class.getName()}); | |
} | |
public void test() | |
{ | |
// getSurface().get | |
} | |
@Override | |
public void settings() | |
{ | |
size(WIDTH, HEIGHT, "processing.opengl.PGraphics3D"); | |
} | |
@Override | |
public void setup() | |
{ | |
new ControlP5Controller(this); | |
surface.setResizable(true); | |
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4th-C1-high-precision.curve"); | |
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-3.curve"); | |
Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot.curve"); | |
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-2.curve"); | |
// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot.curve"); | |
// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/test.curve"); | |
// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-3.curve"); | |
Date date = new Date(System.currentTimeMillis()); | |
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); | |
System.out.println("Program Started at " + formatter.format(date)); | |
renderCurve = new RenderCurve(this, curve); | |
} | |
@Override | |
public void mouseWheel(MouseEvent event) | |
{ | |
cameraZ -= ZOOM_MULTIPLIER * event.getCount(); | |
} | |
@Override | |
public void frameResized(int w, int h) { | |
super.frameResized(w, h); | |
WIDTH = w; | |
System.out.println(WIDTH); | |
HEIGHT = h; | |
} | |
@Override | |
public void keyPressed(KeyEvent event) { | |
super.keyPressed(event); | |
if(event.getKey() == CODED) | |
{ | |
int keyCode = event.getKeyCode(); | |
switch (keyCode) | |
{ | |
case UP: | |
rotationX += Math.PI / 6; | |
break; | |
case DOWN: | |
rotationX -= Math.PI / 6; | |
break; | |
case LEFT: | |
rotationY -= Math.PI / 6; | |
break; | |
case RIGHT: | |
rotationY += Math.PI / 6; | |
break; | |
} | |
} | |
else | |
{ | |
switch(event.getKey()) | |
{ | |
case ' ': | |
//PDZ- when the user presses space make sure that the WIDTH and HEIGHT are updated to the latest | |
WIDTH = width; | |
HEIGHT = height; | |
//TODO: PDZ- Doing it this way will apply the same transformation to every curve. Might want to make it so that you | |
//can control/rotate/translate each curve individually **SEE BELOW** | |
cameraX = WIDTH/2; | |
cameraY = HEIGHT/2; | |
//PDZ- Uncomment these if you want the space bar to also undo any rotations and scaling of the knot/curve | |
// cameraZ = CAMERA_START_Z; | |
// rotationX = 0; | |
// rotationY = 0; | |
break; | |
case 'w': | |
cameraY -= 20; | |
break; | |
case 's': | |
cameraY += 20; | |
break; | |
case 'a': | |
cameraX -=20; | |
break; | |
case 'd': | |
cameraX +=20; | |
break; | |
} | |
} | |
} | |
@Override | |
public void mouseDragged(MouseEvent event) | |
{ | |
//TODO: PDZ- Fix the issue where panning will slightly rotate the object (might be an issue with the camera) | |
if(mousePressed && (mouseButton == LEFT)) | |
{ | |
if(startMouseX < 1) | |
{ | |
startMouseX = mouseX; | |
} | |
float deltaX = mouseX - startMouseX; | |
cameraX += deltaX; | |
startMouseX = mouseX; | |
if(startMouseY < 1) | |
{ | |
startMouseY = mouseY; | |
} | |
float deltaY = mouseY - startMouseY; | |
cameraY += deltaY; | |
startMouseY = mouseY; | |
} | |
else if(mousePressed && (mouseButton == RIGHT)) | |
{ | |
} | |
} | |
@Override | |
public void mouseClicked(MouseEvent event) | |
{ | |
startMouseX = mouseX; | |
startMouseY = mouseY; | |
} | |
@Override | |
public void draw() | |
{ | |
background(0); | |
//PDZ- camera needs to be first since the translation messes up the coordinate | |
pushMatrix(); | |
camera(WIDTH / 2, HEIGHT / 2, 40f, WIDTH/2.0f, HEIGHT/2.0f, 0f, 0f, 1f, 0f); | |
popMatrix(); | |
pushMatrix(); | |
//TODO: PDZ- see comment above about separating the camera from the curves **SEE ABOVE** | |
// You would just need to apply the translation/rotation in the curve draw methods | |
translate(cameraX, cameraY, cameraZ); | |
stroke(255); | |
strokeWeight(0.2f); | |
rotateX(rotationX); | |
rotateY(rotationY); | |
scale(20); //PDZ- this needs to come after the translation | |
noFill(); | |
renderCurve.drawBezierCurve(); | |
renderCurve.drawStickKnot(); | |
popMatrix(); | |
} | |
} |