📄 b04e4667e0c5001d1cd1e38dc83e3a87
字号:
package JavaTerrain;
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.net.MalformedURLException;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.JButton;
public class Tevi extends Applet {
//=== VARIABLES =====================================================================================
private String[][] AppletInfo = { { "Texture", "String", "Terrain texture file" },
{ "HeightField", "String", "height values, grayscale" },
{ "SelectionMap", "String", "map to select from" },
{ "NumTilesX", "int", "nr. of Tiles (horizontal)" },
{ "NumTilesY", "int", "nr. of Tiles (vertical)" },
{ "ROIWidth", "int", "width of ROI selected map" } };
private Image LoadImage = null; // image containing height values
private Texture2D Texture = null; // texture
private int[] HeightMap; // array of height values
private int Width; // width and height of height map
private static int NumTilesX = -1; // for region of interest selection
private static int NumTilesY = -1; // num. of tiles in x,y direction
private static int ROIWidth = 0; // w/h of ROI height map
private static String TextureFile = null; // texture file name
private static String HeightFile = null; // height values file name
private static String MapFile = null; // map of region file name
private SimpleUniverse SimpleU = null; // a Convenience Utility class
private BranchGroup ObjRoot = null; // top BG in SimpleU's geom. branch
private BranchGroup NavRoot = null; // BG for Key-/MouseBehavior
private BoundingLeaf AlwaysOnBoundingLeaf = null; // to make naviagtion always active
private JButton Abutton;
private TMouseBehavior MouseBeh; // mouse navigation
//=== METHODS =======================================================================================
public void init() {
//--- configure 3D canvas -----------------------------------------------------------------
setLayout(new BorderLayout());
GraphicsConfigTemplate3D gctTmpl = new GraphicsConfigTemplate3D();
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gDevice = gEnv.getDefaultScreenDevice();
GraphicsConfiguration gConfig = gDevice.getBestConfiguration(gctTmpl);
Canvas3D canvas3D = new Canvas3D(gConfig);
canvas3D.setSize(getWidth(), getHeight());
canvas3D.setStereoEnable(true);
add("Center", canvas3D);
//--- setup 2 BranchGroups ----------------------------------------------------------------
SimpleU = new SimpleUniverse(canvas3D);
ObjRoot = new BranchGroup();
NavRoot = new BranchGroup();
ObjRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
ObjRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
NavRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
NavRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
SimpleU.addBranchGraph(ObjRoot);
SimpleU.addBranchGraph(NavRoot);
//--- create an "always on"-bounding leaf -------------------------------------------------
AlwaysOnBoundingLeaf = new BoundingLeaf(new BoundingSphere(new Point3d(),
100000));
PlatformGeometry platformGeom = new PlatformGeometry();
platformGeom.addChild(AlwaysOnBoundingLeaf);
platformGeom.compile();
SimpleU.getViewingPlatform().setPlatformGeometry(platformGeom);
//--- add basic mouse navigation ----------------------------------------------------------
BranchGroup mbg = new BranchGroup();
MouseBeh = new TMouseBehavior();
MouseBeh.setSchedulingBoundingLeaf(AlwaysOnBoundingLeaf);
mbg.addChild(MouseBeh);
NavRoot.addChild(mbg);
Abutton.setText("Option");
add(Abutton);
//--- region selection? -------------------------------------------------------------------
if ((MapFile == null) && (NumTilesX == -1)) { MapFile = getParameter("SelectionMap"); }
if (MapFile != null) { selectROI(); }
else { initTerrain(); }
}
private void selectROI() {
//--- set up map plane geometry ----------------------------------------------------
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES
| GeometryArray.TEXTURE_COORDINATE_2);
float aspectRatio = (float) getWidth() / (float) getHeight();
Point3f p = new Point3f();
p.set(-1.0f, 1.0f / aspectRatio, 0.0f); plane.setCoordinate(0, p);
p.set(-1.0f, -1.0f / aspectRatio, 0.0f); plane.setCoordinate(1, p);
p.set( 1.0f, -1.0f / aspectRatio, 0.0f); plane.setCoordinate(2, p);
p.set( 1.0f, 1.0f / aspectRatio, 0.0f); plane.setCoordinate(3, p);
TexCoord2f q = new TexCoord2f();
q.set(0.0f, 1.0f); plane.setTextureCoordinate(0, 0, q);
q.set(0.0f, 0.0f); plane.setTextureCoordinate(0, 1, q);
q.set(1.0f, 0.0f); plane.setTextureCoordinate(0, 2, q);
q.set(1.0f, 1.0f); plane.setTextureCoordinate(0, 3, q);
//--- load world map texture ------------------------------------------------------
LoadImage = getImage(getCodeBase(), MapFile);
TextureLoader texLoader = new TextureLoader(LoadImage, this);
ImageComponent2D ic2d = texLoader.getImage();
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB,
ic2d.getWidth(), ic2d.getHeight());
texture.setImage(0, ic2d);
texture.setEnable(true);
texture.setMinFilter(Texture.NICEST);
texture.setMagFilter(Texture.NICEST);
//--- set up map plane appearance --------------------------------------------------
Appearance appear = new Appearance();
appear.setTexture(texture);
//--- display the map --------------------------------------------------------------
Shape3D planeObj = new Shape3D(plane, appear);
BranchGroup mapRoot = new BranchGroup();
mapRoot.setCapability(BranchGroup.ALLOW_DETACH);
mapRoot.addChild(planeObj);
mapRoot.compile();
ObjRoot.addChild(mapRoot);
SimpleU.getViewingPlatform().setNominalViewingTransform();
//--- start ROI selection mode -----------------------------------------------------
MouseBeh.setSelectROIMode(this);
}
public void selectROI2(int selX, int selY) {
//--- determine maine file to be loaded ----------------------------------------------
if (NumTilesX == -1) { NumTilesX = Integer.parseInt(getParameter("NumTilesX")); }
if (NumTilesY == -1) { NumTilesY = Integer.parseInt(getParameter("NumTilesY")); }
int tileWidth = getWidth() / NumTilesX;
int tileHeight = getHeight() / NumTilesY;
int tileX = Math.min((selX / tileWidth ) + 1, NumTilesX);
int tileY = Math.min((selY / tileHeight) + 1, NumTilesY);
HeightFile = createFileName(tileX, tileY);
//--- load selected height file ------------------------------------------------------
loadHeightData(HeightFile);
int picW = Width;
int picH = Width;
//--- determine width/height of COMPLETE height data set -----------------------------
int complW = picW * NumTilesX;
int complH = picH * NumTilesY;
if (ROIWidth == 0) {
ROIWidth = checkWidth(Integer.parseInt(getParameter("ROIWidth")));
}
if (ROIWidth > Math.min(complW, complH)) {
ROIWidth = checkWidth(Math.min(complW, complH));
}
//--- determine center pixel coordinates ---------------------------------------------
int centerX = complW * selX / getWidth();
int centerY = complH * selY / getHeight();
int rw2 = ROIWidth / 2;
if (centerX < rw2) { centerX = rw2; }
if (centerY < rw2) { centerY = rw2; }
if (centerX > complW - 1 - rw2) { centerX = complW - 1 - rw2; }
if (centerY > complH - 1 - rw2) { centerY = complH - 1 - rw2; }
//--- determine TL and BR tile to be loaded ------------------------------------------
int left = (centerX - rw2) / picW + 1;
int right = (centerX + rw2) / picW + 1;
int top = (centerY - rw2) / picH + 1;
int bottom = (centerY + rw2) / picH + 1;
//--- load all files -----------------------------------------------------------------
HeightMap = new int[ROIWidth * ROIWidth];
int pixY = centerY - rw2;
int nextY = picH * top - 1;
for (int y = top; y <= bottom; y++) {
int pixX = centerX - rw2;
int nextX = picW * left - 1;
for (int x = left; x <= right; x++) {
loadHeightData(createFileName(x, y));
if (nextX > centerX + rw2) { nextX = centerX + rw2; }
if (nextY > centerY + rw2) { nextY = centerY + rw2; }
grabPixels(LoadImage, pixX % picW, pixY % picH,
nextX - pixX + 1, nextY - pixY + 1,
HeightMap,
(pixY - (centerY - rw2)) * ROIWidth
+ (pixX - (centerX - rw2)),
ROIWidth);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -