JME2(JavaMonkeyEngine2):选择物体并且在缩略图中显示. 鼠标右键按下旋转视角

发现BlogJava没以前人气旺了...首页都没啥人更新了..那我在写一个吧.

一个小程序 ..支持鼠标左键点击  右键按下可旋转  缩略图可以展示被点击的物体.




功能:左边是个缩略图.白框外面是场景...要求点击场景中的任何一个物体并且将物体显示在缩略图中..并且鼠标要求左键无法移动视角只可点击..右键只有在按下时 才可旋转视角.

要实现这个效果..我们一步一步来..
首先是初始化 我们先将 摄象机 灯光 模型 等等都将在这里一次生成.

protected void simpleInitGame() {
display.setTitle(
"PickBox Demo");


// 安装摄象机
cam.setFrustumPerspective(45.0f, (float) display.getWidth()
/ (float) display.getHeight(), 15000);
cam.setLocation(
new Vector3f(200150200));
cam.lookAt(
new Vector3f(000), 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.0f1.0f1.0f1.0f));
light.setAmbient(
new ColorRGBA(0.5f0.5f0.5f1.0f));
light.setLocation(
new Vector3f(0300));
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,
1010));
textureBox.setLocalTranslation(
new Vector3f(0100));
textureBox.setRenderState(ts);
textureBox.setModelBound(
new BoundingBox());
textureBox.updateModelBound();
objects.attachChild(textureBox);


whiteBox 
= new Box("box2"new Vector3f(-5-5-5), new Vector3f(555));
whiteBox.setLocalTranslation(
new Vector3f(0300));
whiteBox.setModelBound(
new BoundingBox());
whiteBox.updateModelBound();
objects.attachChild(whiteBox);


shadowBox 
= new Box("sn"new Vector3f(-5-5-5), 555);
shadowBox.setLocalTranslation(
new Vector3f(01010));
shadowBox.setModelBound(
new BoundingBox());
shadowBox.updateModelBound();


return objects;
}

 下面将创建缩略图周围的白色框框
private void createDebugQuads() {
tRenderer 
= display.createTextureRenderer(256256,
TextureRenderer.Target.Texture2D);
tRenderer.getCamera().setAxes(
new Vector3f(-100),
new Vector3f(001), new Vector3f(010));
tRenderer.getCamera().setLocation(
new Vector3f(0-10020));


monitorNode 
= new Node("Monitor Node");
monitorNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
Quad quad 
= new Quad("Monitor");
quad.updateGeometry(
250250);
quad.setLocalTranslation(
new Vector3f(1502100));
quad.setZOrder(
1);
monitorNode.attachChild(quad);


Quad quad2 
= new Quad("Monitor Back");
quad2.updateGeometry(
270270);
quad2.setLocalTranslation(
new Vector3f(1502100));
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);
}

    }



源代码下载:点击下载

posted on 2011-03-24 17:52 相信 阅读(1975) 评论(0)  编辑  收藏 所属分类: JME 2/3


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

不显示applet

常用链接

留言簿(16)

我参与的团队

随笔档案

文章分类

文章档案

新闻档案

相册

swingchina 专业搞Swing的网站

搜索

最新评论

阅读排行榜

评论排行榜