package jhpro.engine3d;
import java.awt.Dimension;
import java.awt.Graphics;
public class Object3d
{
Object3d()
{
amat = new Matrix3d();
tmat = new Matrix3d();
omat = new Matrix3d();
wmat = new Matrix3d();
painted = true;
scaleFactor = 1.0F;
modelName = null;
message = null;
}
Object3d(Dimension p_winSize)
{
amat = new Matrix3d();
tmat = new Matrix3d();
omat = new Matrix3d();
wmat = new Matrix3d();
painted = true;
scaleFactor = 1.0F;
modelName = null;
message = null;
winSize = p_winSize;
scaleFactor = 0.8F;
}
public void doInitialRotation()
{
amat.unit();
tmat.unit();
tmat.xrot(70D);
amat.mult(tmat);
omat.mult(amat);
}
public void setMatrixRotation(float xtheta, float ytheta)
{
tmat.unit();
tmat.xrot(xtheta);
tmat.yrot(ytheta);
amat.mult(tmat);
}
private float calc_xfac(Volume v)
{
float xw = v.getXLen();
float yw = v.getYLen();
float zw = v.getZLen();
if(yw > xw)
xw = yw;
if(zw > xw)
xw = zw;
float f1 = (float)winSize.width / xw;
float f2 = (float)winSize.height / xw;
float x_fac = 0.7F * (f1 >= f2 ? f2 : f1) * scaleFactor;
return x_fac;
}
public Drawable loadModel(String modelName)
{
scaleFactor = modelName.indexOf("cube.obj") < 0 ? 1.0F : 0.8F;
if(modelName.indexOf("teapot.off") >= 0)
scaleFactor = 1.2F;
try
{
md = MeshObject.getModel(modelName);
Volume v = md.determineBoundingBox();
// System.out.println("Read done: Volume is: \n " + v.toString());
// System.out.println("Model: " + md.toString());
xfac = calc_xfac(v);
doInitialRotation();
if(modelName.indexOf("teapot.off") >= 0)
{
amat.unit();
tmat.unit();
tmat.xrot(-244D);
tmat.yrot(-6D);
amat.mult(tmat);
omat.mult(amat);
}
}
catch(Exception e)
{
md = null;
message = e.toString();
System.out.println("Error ( loadModel ) " + e);
System.out.println(" " + message);
}
return md;
}
public Drawable loadModel(FacetedObject ob)
{
scaleFactor = 1;
try
{
md = MeshObject.getModel(ob);
Volume v = md.determineBoundingBox();
// System.out.println("Read done: Volume is: \n " + v.toString());
// System.out.println("Model: " + md.toString());
xfac = calc_xfac(v);
doInitialRotation();
/*
if(modelName.indexOf("teapot.off") >= 0)
{
amat.unit();
tmat.unit();
tmat.xrot(-244D);
tmat.yrot(-6D);
amat.mult(tmat);
omat.mult(amat);
}
*/
}
catch(Exception e)
{
md = null;
message = e.toString();
System.out.println("Error ( loadModel ) " + e);
System.out.println(" " + message);
}
return md;
}
public synchronized void spin()
{
double spinRateY = 2.5D;
if(isFirstTime)
{
isFirstTime = false;
spinMatrix.unit();
spinMatrix.yrot(spinRateY);
// System.out.println("Spin matrix : \n" + spinMatrix);
}
amat.mult(spinMatrix);
}
public synchronized void setPainted()
{
painted = true;
}
public synchronized void paintOffscreen(Graphics g, int w, int h)
{
if(md != null)
{
omat.unit();
Volume v = md.getVolume();
omat.translate(-(v.x_min + v.x_max) / 2.0F, -(v.y_min + v.y_max) / 2.0F, -(v.z_min + v.z_max) / 2.0F);
omat.mult(amat);
omat.scale(xfac, -xfac, (16D * xfac) / (double)winSize.width);
omat.translate(winSize.width / 2, winSize.height / 2, 8D);
md.transform(omat);
OF_off.tranformNormal(amat);
md.Paint(g);
setPainted();
} else
if(message != null)
{
g.drawString("Error in model:", 4, 20);
g.drawString(message, 10, 40);
System.out.println("Error in model : " + message);
}
}
Drawable md;
Matrix3d amat;
Matrix3d tmat;
Matrix3d omat;
Matrix3d wmat;
public boolean painted;
double xfac;
float scaleFactor;
String modelName;
String message;
Dimension winSize;
static Matrix3d spinMatrix = new Matrix3d();
static boolean isFirstTime = true;
static int timesSinceRecalibration = 0;
}
Ads help maintain this website.