发现BlogJava没以前人气旺了...首页都没啥人更新了..那我在写一个吧.
一个小程序 ..支持鼠标左键点击
右键按下可旋转 缩略图可以展示被点击的物体.
功能:左边是个缩略图.白框外面是场景...要求点击场景中的任何一个物体并且将物体显示在缩略图中..并且鼠标要求左键无法移动视角只可点击..右键只有在按下时 才可旋转视角.
要实现这个效果..我们一步一步来..
首先是初始化 我们先将 摄象机 灯光 模型 等等都将在这里一次生成.
protected void simpleInitGame() {
display.setTitle("PickBox Demo");
// 安装摄象机
cam.setFrustumPerspective(45.0f, (float) display.getWidth()
/ (float) display.getHeight(), 1, 5000);
cam.setLocation(new Vector3f(200, 150, 200));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
cam.update();
pr = new BoundingPickResults();
am = new AbsoluteMouse("The Mouse", display.getWidth(), display
.getHeight());
am.registerWithInputHandler(input);
// 安装灯光系统
PointLight light = new PointLight();
light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
light.setLocation(new Vector3f(0, 30, 0));
light.setEnabled(true);
lightState.attach(light);
// Objects 所有可选的物体都将加到这个Node里 rootNode.attachChild(createObjects());
// Setup renderpasses
RenderPass rootPass = new RenderPass();
rootPass.add(rootNode);
pManager.add(rootPass);
createDebugQuads();
statNode.attachChild(debugQuadsNode);
//为了让鼠标左键不随鼠标自动旋转视角 我们首先要将鼠标释放出来
MouseInput.get().setCursorVisible(true);
}
//objects 所有可以让鼠标选择的物体都应该加载到这个Node上面
//因为我们会在Update方法中 使用BoundingPickResults 来寻找这个Node
private Node createObjects() {
objects = new Node("objects");
ts = display.getRenderer().createTextureState();
Texture t0 = TextureManager.loadTexture(PickBoxDemo.class.getClassLoader()
.getResource("jmetest/data/texture/wall.jpg"),
Texture.MinificationFilter.Trilinear,
Texture.MagnificationFilter.Bilinear);
t0.setWrap(Texture.WrapMode.Repeat);
ts.setTexture(t0);
textureBox = new Box("box1", new Vector3f(-10, -10, -10), new Vector3f(10,
10, 10));
textureBox.setLocalTranslation(new Vector3f(0, 10, 0));
textureBox.setRenderState(ts);
textureBox.setModelBound(new BoundingBox());
textureBox.updateModelBound();
objects.attachChild(textureBox);
whiteBox = new Box("box2", new Vector3f(-5, -5, -5), new Vector3f(5, 5, 5));
whiteBox.setLocalTranslation(new Vector3f(0, 30, 0));
whiteBox.setModelBound(new BoundingBox());
whiteBox.updateModelBound();
objects.attachChild(whiteBox);
shadowBox = new Box("sn", new Vector3f(-5, -5, -5), 5, 5, 5);
shadowBox.setLocalTranslation(new Vector3f(0, 10, 10));
shadowBox.setModelBound(new BoundingBox());
shadowBox.updateModelBound();
return objects;
}
下面将创建缩略图周围的白色框框
private void createDebugQuads() {
tRenderer = display.createTextureRenderer(256, 256,
TextureRenderer.Target.Texture2D);
tRenderer.getCamera().setAxes(new Vector3f(-1, 0, 0),
new Vector3f(0, 0, 1), new Vector3f(0, 1, 0));
tRenderer.getCamera().setLocation(new Vector3f(0, -100, 20));
monitorNode = new Node("Monitor Node");
monitorNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
Quad quad = new Quad("Monitor");
quad.updateGeometry(250, 250);
quad.setLocalTranslation(new Vector3f(150, 210, 0));
quad.setZOrder(1);
monitorNode.attachChild(quad);
Quad quad2 = new Quad("Monitor Back");
quad2.updateGeometry(270, 270);
quad2.setLocalTranslation(new Vector3f(150, 210, 0));
quad2.setZOrder(2);
monitorNode.attachChild(quad2);
ZBufferState buf = display.getRenderer().createZBufferState();
buf.setEnabled(false);
monitorNode.setRenderState(buf);
tRenderer.setBackgroundColor(new ColorRGBA(0f, 0f, 0f, 1f));
fakeTex = new Texture2D();
fakeTex.setRenderToTextureType(Texture.RenderToTextureType.RGBA);
tRenderer.setupTexture(fakeTex);
TextureState screen = display.getRenderer().createTextureState();
screen.setTexture(fakeTex);
screen.setEnabled(true);
quad.setRenderState(screen);
monitorNode.setLightCombineMode(Spatial.LightCombineMode.Off);
rootNode.attachChild(monitorNode);
}
//我们将用Texture2D的方式将框框放到界面里 那将是一个Quad 四边形 .
//接下来是我们最核心的方法
protected void simpleUpdate(){ //我们首先来判断 是否是左键点击 这里的isButtonDown(0) 中的 0 代表的就是左键 if
(MouseInput.get().isButtonDown(0)) { Vector2f screenPos = new
Vector2f(); screenPos.set(am.getHotSpotPosition().x,
am.getHotSpotPosition().y); Vector3f worldCoords =
display.getWorldCoordinates(screenPos, 0); Vector3f worldCoords2 =
display.getWorldCoordinates(screenPos, 1); Ray mouseRay = new
Ray(worldCoords, worldCoords2.subtractLocal(
worldCoords).normalizeLocal()); pr.clear();
//为了让 BoundingPickResults 可以点击 objects内的所有模型
objects.findPick(mouseRay, pr);
try {
//我们会通过pickResults拿到我们点击当前的第1个Node.并且拿到Node的Nmae.接着我们判断并且将点击的Box 复制给缩略图里的Box..这样。他就进去了 String name =
pr.getPickData(0).getTargetMesh().getName(); if
(name.equalsIgnoreCase("box1")) { shadowBox = textureBox; } else if
(name.equalsIgnoreCase("box2")) { shadowBox = whiteBox; } } catch
(IndexOutOfBoundsException ex) { ex.printStackTrace(); } }
}
接下来我们是鼠标的处理...由于要求是鼠标右键按下时并且拖动鼠标 才可以旋转视角 OK
那么我们可以在MouseLook的performAction里 增加一些条件 来实现这个效果
public void performAction(InputActionEvent evt) {
float time = 0.01f * speed;
//首先看鼠标是否右键按下 如果按下 .setCursorVisible(false); 让光标隐藏掉并且移动视角
if(MouseInput.get().isButtonDown(1)){
MouseInput.get().setCursorVisible(false);
if(!buttonPressRequired || MouseInput.get().isButtonDown(mouseButtonForRequired)) {
if (mouse.getLocalTranslation().x > 0) {
event.setTime(time * mouse.getLocalTranslation().x);
rotateRight.performAction(event);
} else if (mouse.getLocalTranslation().x < 0) {
event.setTime(time * mouse.getLocalTranslation().x * -1);
rotateLeft.performAction(event);
}
if (mouse.getLocalTranslation().y > 0) {
event.setTime(time * mouse.getLocalTranslation().y);
lookUp.performAction(event);
} else if (mouse.getLocalTranslation().y < 0) {
event.setTime(time * mouse.getLocalTranslation().y * -1);
lookDown.performAction(event);
}
}
}
//当鼠标右键抬起后 我们将在光标显示出来.
if(!MouseInput.get().isButtonDown(1)){
MouseInput.get().setCursorVisible(true);
}
}
源代码下载:点击下载