📄 model3d.java
字号:
/*
* Generic 3D Model for Java Virtual Cube
* --------------------------------------
*
* Copyright 1996, Song Li
* URL: http://www.cs.umbc.edu/~sli2
*
* Portion of Program by David W. Liu
* URL: http://reality.sgi.com/employees/davidliu_mti
*
*
* You can use the code for any nonprofittable use. But remember to mention
* the authors' names in your revised program. You are also encouraged to
* improve the program or give your comments and bug reports. If there are
* further questions, please contact me and I'll be glad to help. My E-Mail
* address is: sli2@gl.umbc.edu.
*
*/
import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
class Model3D {
Matrix3D ViewMat ;
Hashtable Objects ;
Vector Faces ;
public Model3D () {
ViewMat = null ;
Objects = new Hashtable () ;
Faces = new Vector () ;
}
public Model3D (Vertex viewpoint) {
Objects = new Hashtable () ;
Faces = new Vector () ;
SetView (viewpoint) ;
}
public Model3D (Matrix3D viewmat) {
Objects = new Hashtable () ;
Faces = new Vector () ;
ViewMat = viewmat ;
}
public synchronized void AddObject (String name, Object3D object) {
Objects.put (name, object) ;
for (int i=0; i<object.FaceNum; i++)
Faces.addElement (object.Faces [i]) ;
return ;
}
private Object3D GetObject (String name) {
return (Object3D) (Objects.get (name)) ;
}
public synchronized void DelObject (String name) {
Object3D object = (Object3D) Objects.remove (name) ;
for (int i=0; i<object.FaceNum; i++)
Faces.removeElement (object.Faces [i]) ;
return ;
}
public synchronized void ClearObject () {
Objects.clear () ;
Faces.removeAllElements () ;
}
public void SetView (Vertex viewpoint) {
float x = viewpoint.x ;
float y = viewpoint.y ;
float z = viewpoint.z ;
float ro = (float) Math.sqrt (x*x + y*y + z*z) ;
float sintheta = (float) (y / Math.sqrt (x*x + y*y)) ;
float costheta = (float) (x / Math.sqrt (x*x + y*y)) ;
float sinphi = (float) (Math.sqrt (x*x+y*y) / ro) ;
float cosphi = z / ro ;
float transmat [] [] = {
{-sintheta, costheta, 0, 0} ,
{-costheta*cosphi, -sintheta*cosphi, sinphi, 0} ,
{-costheta*sinphi, -sintheta*sinphi, -cosphi, 0} ,
{ 0, 0, 0, 1} } ;
ViewMat = new Matrix3D (transmat) ;
}
public void SetView (Matrix3D viewmat) {
ViewMat = viewmat ;
}
public Matrix3D GetView () {
return ViewMat ;
}
public void XRotateView (double theta) {ViewMat.ProXRotate (theta) ;}
public void YRotateView (double theta) {ViewMat.ProYRotate (theta) ;}
public void ZRotateView (double theta) {ViewMat.ProZRotate (theta) ;}
public void XRotate (String name, double theta)
{ GetObject (name).XRotate (theta) ;}
public void YRotate (String name, double theta)
{ GetObject (name).YRotate (theta) ;}
public void ZRotate (String name, double theta)
{ GetObject (name).ZRotate (theta) ;}
public void Unify (String name)
{ GetObject (name).Unify () ;}
public void Transform (String name) {
GetObject (name).Transform () ;
}
public void Transform () {
for (Enumeration enum = Objects.elements () ;
enum.hasMoreElements () ;) {
Object3D obj = (Object3D) enum.nextElement () ;
obj.Transform () ;
}
}
protected void Sort () {
for (int i=0; i<Faces.size(); i++) {
Face face = (Face) Faces.elementAt (i) ;
face.CalcParam (ViewMat) ;
}
QuickSort (0, Faces.size() - 1) ;
}
public synchronized void Paint (Graphics graph, int width, int height) {
if (Faces.isEmpty ())
return ;
Sort () ;
for (int i=0; i<Faces.size(); i++) {
Face face = (Face) Faces.elementAt (i) ;
face.Paint (graph, width, height) ;
}
}
void DefineBound (int width, int height) {
float xmin = 10000, xmax = -10000;
float ymin = 10000, ymax = -10000;
float zmin = 10000, zmax = -10000;
for (Enumeration enum = Objects.elements (); enum.hasMoreElements () ;) {
Object3D obj = (Object3D) enum.nextElement () ;
for (int i=0; i<obj.VerNum; i++) {
xmin = Math.min(xmin, obj.Vertices[i].x);
ymin = Math.min(ymin, obj.Vertices[i].y);
zmin = Math.min(zmin, obj.Vertices[i].z);
xmax = Math.max(xmax, obj.Vertices[i].x);
ymax = Math.max(ymax, obj.Vertices[i].y);
zmax = Math.max(zmax, obj.Vertices[i].z);
}
}
float ratio = 0.6f * Math.min (width, height) /
Math.max(Math.max(xmax - xmin, ymax - ymin), zmax - zmin);
ViewMat.ProScale (ratio, ratio, ratio) ;
Transform () ;
}
protected void QuickSort (int left, int right) {
int i, j;
int mid;
Face tmp;
i = left;
j = right;
mid = (left+right) / 2;
do {
while (((Face) Faces.elementAt (mid)).CloserThan (
(Face) Faces.elementAt (i)) && i < right)
i ++ ;
while (((Face) Faces.elementAt (j)).CloserThan (
(Face) Faces.elementAt (mid)) && j > left)
j -- ;
if (i <= j) {
Face tempface ;
tempface = (Face) Faces.elementAt (i) ;
Faces.setElementAt ((Face) Faces.elementAt (j), i) ;
Faces.setElementAt (tempface, j) ;
i ++ ;
j -- ;
}
} while (i <= j);
if (left < j)
QuickSort (left, j);
if (i < right)
QuickSort (i, right);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -