JME2 (JavaMonkeyEngine2): select an object and display it in the thumbnail. Right-click the object and press the rotation angle.

Source: Internet
Author: User

I found that BlogJava is not popular before... no one has updated the homepage... so I am writing one.

A small program... supports right-clicking with the left mouse button and pressing the rotated thumbnail to display the clicked objects.


Function: a thumbnail is displayed on the left. the scene is displayed outside the white box... you need to click any object in the scene and display the object in the thumbnail .. in addition, the left mouse button is required to move the angle of view only .. you can right-click to rotate the angle of view only when you press it.

To achieve this effect, let's proceed step by step ..
First, we will first generate all the camera lighting models here.

Protected void simpleInitGame (){
Display. setTitle ("PickBox Demo ");


// Install the camera
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 );


// Install the light system
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 );


// All optional Objects will be added to the Node rootNode. attachChild (createObjects ());


// Setup renderpasses
RenderPass rootPass = new RenderPass ();
RootPass. add (rootNode );
PManager. add (rootPass );


CreateDebugQuads ();
StatNode. attachChild (debugQuadsNode );
// To enable the left mouse button to automatically rotate the angle without the mouse, release the mouse first.
MouseInput. get (). setCursorVisible (true );
}
// Objects all objects that can be selected by the mouse should be loaded to this Node
// Because we will use BoundingPickResults in the Update method to find this 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 );
ShadowBox. setLocalTranslation (new Vector3f (0, 10, 10 ));
ShadowBox. setModelBound (new BoundingBox ());
ShadowBox. updateModelBound ();


Return objects;
}
The white box around the thumbnail will be created below
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 );
}
// We will use Texture2D to place the box in the interface, which will be a Quad quadrilateral.


// Next is our core method
Protected void simpleUpdate () {// We first determine whether it is left-click. 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 ();
// To enable BoundingPickResults to click all models in objects
Objects. findPick (mouseRay, pr );
Try {
// We will get the current 1st nodes through pickResults, and get the Nmae of the Node. Then we will judge and copy the clicked Box to the Box in the thumbnail. He entered String name =
Pr. getPickData (0). getTargetMesh (). getName (); if
(Name. inclusignorecase ("box1") {shadowBox = textureBox;} else if
(Name. inclusignorecase ("box2") {shadowBox = whiteBox;} catch
(IndexOutOfBoundsException ex) {ex. printStackTrace ();}}
}


Next we are processing the mouse... because it is required to right-click the mouse and drag the mouse to rotate the Angle of View OK
Then, we can add some conditions in MouseLook's condition maction to achieve this effect.

Public void upload maction (InputActionEvent evt ){
Float time = 0.01f * speed;
// First, right-click the mouse and press it. If you press. setCursorVisible (false), hide the cursor and move the angle of view.
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. Invalid maction (event );
} Else if (mouse. getLocalTranslation (). x <0 ){
Event. setTime (time * mouse. getLocalTranslation (). x *-1 );
RotateLeft. Role maction (event );
}
If (mouse. getLocalTranslation (). y> 0 ){
Event. setTime (time * mouse. getLocalTranslation (). y );
LookUp. Specify maction (event );
} Else if (mouse. getLocalTranslation (). y <0 ){
Event. setTime (time * mouse. getLocalTranslation (). y *-1 );
LookDown. Reverse maction (event );
}
}
}
// When the right mouse button is raised, the cursor is displayed.
If (! Mou

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.