ThreeD.java revision 3847
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* A set of classes to parse, represent and display 3D wireframe models
represented in Wavefront .obj format. */
@SuppressWarnings("serial")
class FileFormatException extends Exception {
public FileFormatException(String s) {
super(s);
}
}
/** The representation of a 3D model */
final class Model3D {
float vert[];
int tvert[];
int con[];
boolean transformed;
Model3D() {
}
/** Create a 3D model by parsing an input stream */
this();
st.eolIsSignificant(true);
scan:
while (true) {
default:
break scan;
case StreamTokenizer.TT_EOL:
break;
case StreamTokenizer.TT_WORD:
double x = 0, y = 0, z = 0;
}
}
}
addVert((float) x, (float) y, (float) z);
!= StreamTokenizer.TT_EOF) {
}
int start = -1;
int prev = -1;
int n = -1;
while (true) {
if (prev >= 0) {
}
if (start < 0) {
start = n;
}
prev = n;
} else {
break;
}
}
if (start >= 0) {
}
break scan;
}
} else {
// no-op
}
}
}
}
}
}
/** Add a vertex to this model */
int addVert(float x, float y, float z) {
int i = nvert;
if (i >= maxvert) {
maxvert = 100;
} else {
maxvert *= 2;
}
}
i *= 3;
vert[i] = x;
vert[i + 1] = y;
vert[i + 2] = z;
return nvert++;
}
/** Add a line from vertex p1 to vertex p2 */
int i = ncon;
return;
}
if (i >= maxcon) {
maxcon = 100;
} else {
maxcon *= 2;
}
}
int t = p1;
p2 = t;
}
ncon = i + 1;
}
/** Transform all the points in this model */
void transform() {
return;
}
}
transformed = true;
}
/* Quick Sort implementation
*/
int rightIndex = right;
int partionElement;
/* Arbitrarily establishing partition element as the midpoint of
* the array.
*/
// 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.
*/
++leftIndex;
}
/* find an element that is smaller than or equal to
* the partionElement starting from the rightIndex.
*/
--rightIndex;
}
// if the indexes have not crossed, swap
if (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) {
}
/* If the left index has not reached the right side of array
* must now sort the right partition.
*/
}
}
}
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 c[] = con;
int d = 0;
int pp1 = -1;
for (int i = 0; i < limit; i++) {
int p1 = c[i];
c[d] = p1;
d++;
}
}
ncon = d;
}
/** 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 */
return;
}
transform();
for (int i = 0; i < 16; i++) {
}
}
int lg = 0;
int c[] = con;
int v[] = tvert;
return;
}
for (int i = 0; i < lim; i++) {
int T = c[i];
if (grey < 0) {
grey = 0;
}
if (grey > 15) {
grey = 15;
}
}
}
}
/** Find the bounding box of this model */
void findBB() {
if (nvert <= 0) {
return;
}
float v[] = vert;
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;
}
}
}
}
/** An applet to put a 3D model into a page */
@SuppressWarnings("serial")
boolean painted = true;
float xfac;
float scalefudge = 1;
public void init() {
try {
// fall back to default scalefudge = 1
}
mdname = "model.obj";
}
addMouseListener(this);
addMouseMotionListener(this);
}
public void destroy() {
removeMouseListener(this);
removeMouseMotionListener(this);
}
public void run() {
try {
md = m;
m.findBB();
m.compress();
}
}
} catch (Exception e) {
}
try {
}
} catch (Exception e) {
}
repaint();
}
public void start() {
}
}
public void stop() {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
e.consume();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (painted) {
painted = false;
repaint();
}
prevx = x;
prevy = y;
e.consume();
}
public void mouseMoved(MouseEvent e) {
}
md.transformed = false;
setPainted();
}
}
private synchronized void setPainted() {
painted = true;
notifyAll();
}
public String getAppletInfo() {
return "Title: ThreeD \nAuthor: James Gosling? \n"
+ "An applet to put a 3D model into a page.";
}
public String[][] getParameterInfo() {
{ "model", "path string", "The path to the model to be displayed." },
{ "scale", "float", "The scale of the model. Default is 1." }
};
return info;
}
}