00001 package edu.tum.cs.vis;
00002
00003 import java.awt.Color;
00004 import java.awt.Dimension;
00005 import java.awt.Frame;
00006 import java.awt.GraphicsDevice;
00007 import java.awt.GraphicsEnvironment;
00008 import java.awt.Insets;
00009 import java.awt.Label;
00010 import java.awt.SystemColor;
00011 import java.awt.event.MouseAdapter;
00012 import java.awt.event.MouseEvent;
00013 import java.awt.event.MouseListener;
00014 import java.awt.event.MouseMotionListener;
00015 import java.awt.event.WindowAdapter;
00016 import java.awt.event.WindowEvent;
00017 import java.util.Vector;
00018 import javax.vecmath.Matrix4f;
00019
00020 import edu.tum.cs.util.math.Vector3f;
00021
00022 import processing.core.PApplet;
00023 import processing.core.PFont;
00024
00025
00026 public class Canvas extends PApplet implements MouseListener, MouseMotionListener {
00027 static final long serialVersionUID = 0;
00028
00032 protected int width = 800, height = 600;
00033
00037 protected boolean useCamera;
00038
00042 protected float leftMouseX = -1.0f, leftMouseY = -1.0f, rightMouseX = -1.0f, rightMouseY = -1.0f, centerMouseY = -1.0f;
00043
00047 protected float xRotDisplay, yRotDisplay, xShiftDisplay, yShiftDisplay, zShiftDisplay, zoomDisplay;
00048
00052 protected Vector3f eye, eyeTarget, eyeUp;
00053
00057 protected float sceneSize = 4000;
00058
00059 public boolean debugCamera;
00060
00064 Vector<Drawable> items = new Vector<Drawable>();
00065 Vector<Drawable> items2D = new Vector<Drawable>();
00066
00071 public void setWidth(int width) {
00072 this.width = width;
00073 }
00074
00079 public void setHeight(int height) {
00080 this.height = height;
00081 }
00082
00083 public int getHeight() {
00084 return height;
00085 }
00086
00087 public int getWidth() {
00088 return width;
00089 }
00090
00091 public void setSceneSize(float size) {
00092 this.sceneSize = size;
00093 this.zoomDisplay = 400 / sceneSize;
00094 }
00095
00096 public Canvas() {
00097 useCamera = false;
00098 debugCamera = false;
00099 eye = new Vector3f(50.0f,50f,20f);
00100 eyeUp = new Vector3f(0,0,500);
00101 eyeTarget = new Vector3f(0,0,0);
00102 setStandardViewParams();
00103 }
00104
00108 public void setStandardViewParams() {
00109 xRotDisplay = -106.25027f;
00110 yRotDisplay = 0.020062504f;
00111 xShiftDisplay = 103f;
00112 zShiftDisplay = 162f;
00113 setSceneSize(this.sceneSize);
00114 }
00115
00116 public void setEyeTarget(Vector3f v) {
00117 this.eyeTarget.set(v);
00118 }
00119
00120 public void setup() {
00121 size(width, height, P3D);
00122 lights();
00123
00124 PFont font = createFont("Verdana", 12);
00125 textFont(font);
00126
00127 ellipseMode(RADIUS);
00128 frameRate(20);
00129
00130 noLoop();
00131 draw();
00132 }
00133
00134 public void draw() {
00135
00136 background(60, 60, 60);
00137 cursor(CROSS);
00138
00139 if(!useCamera) {
00140 camera();
00141
00142 pushMatrix();
00143 translate(width / 4.0f, height / 1.5f, -400.0f);
00144
00145 lights();
00146
00147 rotateZ(PI / 2);
00148 rotateY(-PI / 2);
00149 translate(0.0f, zShiftDisplay, xShiftDisplay);
00150
00151 rotateZ(radians(xRotDisplay));
00152 rotateX(radians(yRotDisplay));
00153
00154 scale(zoomDisplay);
00155
00156
00157 }
00158 else {
00159 lights();
00160
00161 setCamera();
00162 }
00163
00164 preDrawItems();
00165
00166 drawItems();
00167
00168
00169 if(debugCamera) {
00170 float coordlength = sceneSize / 8;
00171 Vector3f up = new Vector3f(eyeUp);
00172 up.scale(coordlength);
00173 up.add(eyeTarget);
00174 this.drawLine(eyeTarget, up, 0xff0000ff);
00175 Vector3f dir = new Vector3f(eyeTarget);
00176 dir.subtract(eye);
00177 Vector3f third = new Vector3f();
00178 third.cross(dir, up);
00179 third.normalize();
00180 third.scale(coordlength);
00181 third.add(eyeTarget);
00182 this.drawLine(eyeTarget, third, 0xff00ff00);
00183 this.drawLine(eye, eyeTarget, 0xffff0000);
00184 }
00185
00186 if(!useCamera)
00187 popMatrix();
00188 else {
00189
00190 }
00191
00192 for(Drawable d : items2D)
00193 d.draw(this);
00194 }
00195
00196 protected void preDrawItems() {
00197
00198 }
00199
00200 protected void setCamera() {
00201
00202 camera(eye.x, eye.y, eye.z, eyeTarget.x, eyeTarget.y, eyeTarget.z, eyeUp.x, eyeUp.y, eyeUp.z);
00203
00204
00205
00206
00207 }
00208
00209 public synchronized void drawItems() {
00210 for(Drawable d : items)
00211 d.draw(this);
00212 }
00213
00214 public void drawLine(Vector3f p1, Vector3f p2, int color) {
00215
00216 drawLine(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, color);
00217
00218 }
00219
00220 public void drawLine(float x1, float y1, float z1, float x2, float y2, float z2, int color) {
00221 stroke(color);
00222 line(x1,y1,z1,x2,y2,z2);
00223 }
00224
00225 public synchronized void add(Drawable d) {
00226 this.items.add(d);
00227 }
00228
00229 public synchronized void add2D(Drawable d) {
00230 this.items2D.add(d);
00231 }
00232
00233 public void rotateAxis(double theta, Vector3f rotAxis){
00234 Matrix4f mat = Vector3f.getRotationMatrix(theta,rotAxis);
00235 applyMatrix(mat.m00, mat.m01, mat.m02, mat.m03,
00236 mat.m10, mat.m11, mat.m12, mat.m13,
00237 mat.m20, mat.m21, mat.m22, mat.m23,
00238 mat.m30, mat.m31, mat.m32, mat.m33);
00239 }
00240
00244
00245
00246
00247
00248 @Override
00249 public void keyPressed() {
00250 switch(keyCode) {
00251 case java.awt.event.KeyEvent.VK_C:
00252 useCamera = !useCamera;
00253
00254 redraw();
00255 System.out.println("Turned camera " + (useCamera ? "on" : "off"));
00256 break;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00403 public void runMain() {
00404
00405 if(platform == MACOSX) {
00406
00407
00408 System.setProperty("apple.awt.graphics.UseQuartz", "true");
00409 }
00410
00411 try {
00412 boolean external = false;
00413
00414 String name = null;
00415 boolean present = false;
00416 Color backgroundColor = Color.black;
00417 Color stopColor = Color.gray;
00418 GraphicsDevice displayDevice = null;
00419 boolean hideStop = false;
00420
00421
00422
00423
00424 String folder = null;
00425 try {
00426 folder = System.getProperty("user.dir");
00427 }
00428 catch (Exception e) {
00429 }
00430
00431
00432
00433
00434
00435
00436 if(displayDevice == null) {
00437 GraphicsEnvironment environment = GraphicsEnvironment
00438 .getLocalGraphicsEnvironment();
00439 displayDevice = environment.getDefaultScreenDevice();
00440 }
00441
00442 Frame frame = new Frame(displayDevice.getDefaultConfiguration());
00443
00444
00445
00446
00447 frame.setResizable(false);
00448
00449
00451
00452 frame.setTitle(name);
00453
00454
00455
00456
00457 PApplet applet = this;
00458
00459
00460 applet.frame = frame;
00461 applet.sketchPath = folder;
00462
00464
00465
00466
00467
00468
00469
00470
00471
00472 if(present) {
00473 frame.setUndecorated(true);
00474 frame.setBackground(backgroundColor);
00475 displayDevice.setFullScreenWindow(frame);
00476 }
00477 frame.setLayout(null);
00478 frame.add(applet);
00479 frame.pack();
00480
00481 applet.init();
00482
00483
00484
00485
00486
00487 while(applet.defaultSize && !applet.finished) {
00488
00489 try {
00490 Thread.sleep(5);
00491
00492 }
00493 catch (InterruptedException e) {
00494
00495 }
00496 }
00497
00498
00499
00500 if(present) {
00501
00502
00503
00504
00505
00506 Dimension fullscreen = frame.getSize();
00507 applet.setBounds((fullscreen.width - applet.width) / 2,
00508 (fullscreen.height - applet.height) / 2, applet.width,
00509 applet.height);
00510
00511 if(!hideStop) {
00512 Label label = new Label("stop");
00513 label.setForeground(stopColor);
00514 label.addMouseListener(new MouseAdapter() {
00515 public void mousePressed(MouseEvent e) {
00516 System.exit(0);
00517 }
00518 });
00519 frame.add(label);
00520
00521 Dimension labelSize = label.getPreferredSize();
00522
00523
00524 labelSize = new Dimension(100, labelSize.height);
00525 label.setSize(labelSize);
00526 label.setLocation(20, fullscreen.height - labelSize.height
00527 - 20);
00528 }
00529
00530
00531 if(external) {
00532 applet.setupExternalMessages();
00533 }
00534
00535 }
00536 else {
00537
00538
00539
00540 Insets insets = frame.getInsets();
00541
00542 int windowW = Math.max(applet.width, MIN_WINDOW_WIDTH)
00543 + insets.left + insets.right;
00544 int windowH = Math.max(applet.height, MIN_WINDOW_HEIGHT)
00545 + insets.top + insets.bottom;
00546
00547 frame.setSize(windowW, windowH);
00548
00549 if(external) {
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 }
00574 else {
00575 frame.setLocation((applet.screenWidth - applet.width) / 2,
00576 (applet.screenHeight - applet.height) / 2);
00577 }
00578
00579
00580
00581
00582 if(backgroundColor == Color.black) {
00583
00584 backgroundColor = SystemColor.control;
00585 }
00586 frame.setBackground(backgroundColor);
00587
00588 int usableWindowH = windowH - insets.top - insets.bottom;
00589 applet.setBounds((windowW - applet.width) / 2, insets.top
00590 + (usableWindowH - applet.height) / 2, applet.width,
00591 applet.height);
00592
00593 if(external) {
00594 applet.setupExternalMessages();
00595
00596 }
00597 else {
00598 frame.addWindowListener(new WindowAdapter() {
00599 public void windowClosing(WindowEvent e) {
00600 System.exit(0);
00601 }
00602 });
00603 }
00604
00605
00606 applet.setupFrameResizeListener();
00607
00608
00609 if(applet.displayable()) {
00610 frame.setVisible(true);
00611 }
00612 }
00613
00614
00615
00616 applet.requestFocus();
00617
00618
00619 }
00620 catch (Exception e) {
00621 e.printStackTrace();
00622 System.exit(1);
00623 }
00624 }
00625 }