/* Interactive3D is an experiment to try to parse info from a text field in a HTML Page rather than a text file, as was the case from the sample from Sun entitled "Wireframe" This allows for an interactive way of coming up with the .obj files to be used with "Wireframe" This implementation makes a different class Model3D that contructs the object so that the existing "Wireframe" sample code will work. Chris Thiel version 1 finished Dec 5, 1998 * Some code Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */ /* A set of classes to parse, represent and display 3D wireframe models represented in Wavefront .obj format. */ import java.lang.*; import java.awt.*; import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Event; import java.util.*; class Model3D { float vert[]; int tvert[]; int nvert, maxvert; int con[]; int ncon, maxcon; boolean transformed; Matrix3D mat; float xmin, xmax, ymin, ymax, zmin, zmax; Float x, y, z; int n; String s=null; Model3D () { mat = new Matrix3D (); mat.xrot(20); mat.yrot(30); } /** Create a 3D model by parsing the user's input String from an HTML document */ Model3D (String userInput) { this(); //convert new line chars from unix/win userInput.trim(); while (userInput.indexOf("\n") >=0) { userInput= userInput.substring(0, userInput.indexOf("\n")) + " ; " + userInput.substring(userInput.indexOf("\n")+1); } //convert return chars from macs while (userInput.indexOf("\r") >=0) { userInput= userInput.substring(0, userInput.indexOf("\r")) + " ; " + userInput.substring(userInput.indexOf("\r")+1); } if ( !(userInput.endsWith(" ; "))) userInput = userInput+" ; "; StringTokenizer t= new StringTokenizer(userInput, " ", false); while (t.hasMoreTokens() ) { s = t.nextToken(); //charsRead++; if ((s.charAt(0)=='f') || (s.charAt(0)=='l')) { //fCount=0; int start = -1; int prev = -1; int n = -1; s=t.nextToken();//the first vertex number while ( s.charAt(0)!=';' ) { //fCount++; n = Integer.parseInt(s); if (prev >= 0) add(prev - 1, n - 1); if (start < 0) start = n; prev = n; if (t.hasMoreTokens() ) s=t.nextToken(); } if (start >= 0) add(start - 1, prev - 1); } else if (s.charAt(0)=='v') { x=Float.valueOf(t.nextToken()); y=Float.valueOf(t.nextToken()); z=Float.valueOf(t.nextToken()); //vCount++; addVert(x.floatValue(), y.floatValue(), z.floatValue()); if (t.hasMoreTokens() ) s=t.nextToken();// the ; separator } }// of parsing the userInput string }//of constructor /** Add a vertex to this model */ int addVert(float x, float y, float z) { int i = nvert; if (i >= maxvert) if (vert == null) { maxvert = 100; vert = new float[maxvert * 3]; } else { maxvert *= 2; float nv[] = new float[maxvert * 3]; System.arraycopy(vert, 0, nv, 0, vert.length); vert = nv; } i *= 3; vert[i] = x; vert[i + 1] = y; vert[i + 2] = z; return nvert++; } /** Add a line from vertex p1 to vertex p2 */ void add(int p1, int p2) { int i = ncon; if (p1 >= nvert || p2 >= nvert) return; if (i >= maxcon) if (con == null) { maxcon = 100; con = new int[maxcon]; } else { maxcon *= 2; int nv[] = new int[maxcon]; System.arraycopy(con, 0, nv, 0, con.length); con = nv; } if (p1 > p2) { int t = p1; p1 = p2; p2 = t; } con[i] = (p1 << 16) | p2; ncon = i + 1; } /** Transform all the points in this model */ void transform() { if (transformed || nvert <= 0) return; if (tvert == null || tvert.length < nvert * 3) tvert = new int[nvert*3]; mat.transform(vert, tvert, nvert); transformed = true; } /* Quick Sort implementation */ private void quickSort(int a[], int left, int right) { int leftIndex = left; int rightIndex = right; int partionElement; if ( right > left) { /* Arbitrarily establishing partition element as the midpoint of * the array. */ partionElement = a[ ( left + right ) / 2 ]; // loop through the array until indices cross while( leftIndex <= rightIndex ) { /* find the first element that is greater than or equal to * the partionElement starting from the leftIndex. */ while( ( leftIndex < right ) && ( a[leftIndex] < partionElement ) ) ++leftIndex; /* find an element that is smaller than or equal to * the partionElement starting from the rightIndex. */ while( ( rightIndex > left ) && ( a[rightIndex] > partionElement ) ) --rightIndex; // if the indexes have not crossed, swap if( leftIndex <= rightIndex ) { swap(a, leftIndex, rightIndex); ++leftIndex; --rightIndex; } } /* If the right index has not reached the left side of array * must now sort the left partition. */ if( left < rightIndex ) quickSort( a, left, rightIndex ); /* If the left index has not reached the right side of array * must now sort the right partition. */ if( leftIndex < right ) quickSort( a, leftIndex, right ); } } private void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T; } /** eliminate duplicate lines */ void compress() { int limit = ncon; int c[] = con; quickSort(con, 0, ncon - 1); int d = 0; int pp1 = -1; for (int i = 0; i < limit; i++) { int p1 = c[i]; if (pp1 != p1) { c[d] = p1; d++; } pp1 = p1; } ncon = d; } static Color gr[]; /** Paint this model to a graphics context. It uses the matrix associated with this model to map from model space to screen space. The next version of the browser should have double buffering, which will make this *much* nicer */ void paint(Graphics g) { if (vert == null || nvert <= 0) return; transform(); if (gr == null) { gr = new Color[16]; for (int i = 0; i < 16; i++) { int grey = (int) (170*(1-Math.pow(i/15.0, 2.3))); gr[i] = new Color(grey, grey, grey); } } int lg = 0; int lim = ncon; int c[] = con; int v[] = tvert; if (lim <= 0 || nvert <= 0) return; for (int i = 0; i < lim; i++) { int T = c[i]; int p1 = ((T >> 16) & 0xFFFF) * 3; int p2 = (T & 0xFFFF) * 3; int grey = v[p1 + 2] + v[p2 + 2]; if (grey < 0) grey = 0; if (grey > 15) grey = 15; if (grey != lg) { lg = grey; g.setColor(gr[grey]); } g.drawLine(v[p1], v[p1 + 1], v[p2], v[p2 + 1]); } } /** Find the bounding box of this model */ void findBB() { if (nvert <= 0) return; float v[] = vert; float xmin = v[0], xmax = xmin; float ymin = v[1], ymax = ymin; float zmin = v[2], zmax = zmin; for (int i = nvert * 3; (i -= 3) > 0;) { float x = v[i]; if (x < xmin) xmin = x; if (x > xmax) xmax = x; float y = v[i + 1]; if (y < ymin) ymin = y; if (y > ymax) ymax = y; float z = v[i + 2]; if (z < zmin) zmin = z; if (z > zmax) zmax = z; } this.xmax = xmax; this.xmin = xmin; this.ymax = ymax; this.ymin = ymin; this.zmax = zmax; this.zmin = zmin; } } public class Interactive3D extends Applet implements Runnable { String userInput=null; String s=null; int i=0; Model3D md; boolean painted = true; float xfac; int prevx, prevy; float xtheta, ytheta; float scalefudge = 1; Matrix3D amat = new Matrix3D(), tmat = new Matrix3D(); String mdname = null; String message = null; public void setString(String aString) { userInput = aString; //digest(); //md.transformed = true; run(); //repaint(); } public void init() { userInput = "v 0 0 0 ; v 1 0 0 ; v 1 1 0 ; v 0 1 0 ; v 0 0 1 ; v 1 0 1 ; v 1 1 1 ; v 0 1 1 ; f 1 2 3 4 ; f 5 6 7 8 ; l 1 5 ; l 2 6 ; l 3 7 ; l 4 8"; amat.yrot(20); amat.xrot(20); resize(size().width <= 20 ? 400 : size().width, size().height <= 20 ? 400 : size().height); } public void run() { //get userString try { Model3D m = new Model3D (userInput); md = m; m.findBB(); m.compress(); float xw = m.xmax - m.xmin; float yw = m.ymax - m.ymin; float zw = m.zmax - m.zmin; if (yw > xw) xw = yw; if (zw > xw) xw = zw; float f1 = size().width / xw; float f2 = size().height / xw; xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge; } catch(Exception e) { md = null; message = e.toString(); } repaint(); } public void start() { if (md == null && message == null) new Thread(this).start(); } public void stop() { } public boolean mouseDown(Event e, int x, int y) { prevx = x; prevy = y; return true; } public boolean mouseDrag(Event e, int x, int y) { tmat.unit(); float xtheta = (prevy - y) * 360.0f / size().width; float ytheta = (x - prevx) * 360.0f / size().height; tmat.xrot(xtheta); tmat.yrot(ytheta); amat.mult(tmat); if (painted) { painted = false; repaint(); } prevx = x; prevy = y; return true; } public void paint( Graphics g ) { if (md != null) { md.mat.unit(); md.mat.translate(-(md.xmin + md.xmax) / 2, -(md.ymin + md.ymax) / 2, -(md.zmin + md.zmax) / 2); md.mat.mult(amat); //md.mat.scale(xfac, -xfac, 8 * xfac / size().width); md.mat.scale(xfac, -xfac, 16 * xfac / size().width); md.mat.translate(size().width / 2, size().height / 2, 8); md.transformed = false; md.paint(g); setPainted(); } else if (message != null) { g.drawString("Error in model:", 3, 20); g.drawString(message, 10, 40); } } // of paint private synchronized void setPainted() { painted = true; notifyAll(); }// of setPainted }// of class