|
sickfar
|
 |
« on: 11. May 2012, 07:54:28 pm » |
|
Hi all, it's again me : ) Now I have new trouble: direct camera movement. What does it means: I'm developing CAD system using Xith3D (don't laugh, I'm using Java so Xith is the best I found). So I need pan & zoom viewport. Not by FPSListener - how can I do it? I need pan & zoom using mouse movements with pressed button (ok, this I'm understanding - onMouseButtonPressed). But moving camera... I looked at some test & Xin but understood nothing. Please, help me!
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #1 on: 11. May 2012, 09:15:07 pm » |
|
Let's first stake off some basics.
Do you know, how to capture relative mouse movement using a JAGaToo mouse listener? You'll find this in XIN. Do you know how to define and update the viewport? This should be in XIN, too. At least you'll find this in one of the xith-tk tests.
If you know these basics, then it should be pretty straight foreward.
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #2 on: 11. May 2012, 09:18:14 pm » |
|
Ok, I'll look through again and try again...
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #3 on: 12. May 2012, 06:56:44 pm » |
|
I'm trying do this, but got a trouble... Here is my code: private boolean isMoving = false;
@Override public void onMouseButtonPressed(MouseButtonPressedEvent e, MouseButton button) { if (button == MouseButtons.LEFT_BUTTON) { isMoving = true; System.out.println("Start moving..."); } }
@Override public void onMouseButtonReleased(MouseButtonReleasedEvent e, MouseButton button) { if (button == MouseButtons.LEFT_BUTTON) { isMoving = false; System.out.println("Stop moving..."); } }
@Override public void onMouseMoved(MouseMovedEvent e, int x, int y, int dx, int dy) { if (isMoving) { viewFocus = viewFocus.add(e.getDX(), e.getDY(), 0); eyePosition = eyePosition.add(e.getDX(), e.getDY(), 0); env.getView().lookAt(eyePosition, viewFocus, vecUp); System.out.println("Moved mouse " + e.getDX() + ", " + e.getDY()); } } And here is my exception: Start moving... java.lang.ArrayIndexOutOfBoundsException: -2147483644 at org.openmali.vecmath2.TupleNf.addValue(TupleNf.java:298) at org.openmali.vecmath2.Tuple3f.add(Tuple3f.java:266) at test.NewMain.onMouseMoved(NewMain.java:89) at org.jagatoo.input.devices.Mouse.fireOnMouseMoved(Mouse.java:608) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.collectOrFireEvents(LWJGLMouse.java:180) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.updateMouse(LWJGLMouse.java:277) at org.jagatoo.input.devices.Mouse.update(Mouse.java:787) at org.jagatoo.input.InputSystem.updateMouses(InputSystem.java:1135) at org.jagatoo.input.InputSystem.update(InputSystem.java:1178) at org.xith3d.base.Xith3DEnvironment.updateInputSystem(Xith3DEnvironment.java:211) at org.xith3d.loop.RenderLoop.prepareNextFrame(RenderLoop.java:597) at org.xith3d.loop.RenderLoop.loopIteration(RenderLoop.java:642) at org.xith3d.loop.RenderLoop.update(RenderLoop.java:702) at org.xith3d.loop.UpdatingThread.nextIteration(UpdatingThread.java:487) at org.xith3d.loop.RenderLoop.nextIteration(RenderLoop.java:713) at org.xith3d.loop.RenderLoop.loop(RenderLoop.java:766) at org.xith3d.loop.UpdatingThread.run(UpdatingThread.java:540) at org.xith3d.loop.RenderLoop.run(RenderLoop.java:789) at org.xith3d.loop.RenderLoop.begin(RenderLoop.java:845) at org.xith3d.loop.RenderLoop.begin(RenderLoop.java:875) at test.NewMain.main(NewMain.java:101) Why is it so?
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #4 on: 12. May 2012, 07:36:32 pm » |
|
OK, I've put some changes to openmali - and it works now, question is over. I did not understood what is camera and view, but now that seems okay.
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #5 on: 13. May 2012, 10:08:49 am » |
|
- The camera is called View in Xith3D.
- You could use onMouseButtonStateChanged() instead of onMouseButtonPressed() and onMouseButtonReleased().
- You could even get the mouse button state from the MouseMovedEvent, if you're no interested in the pressed and released events.
- dx and dy are pseed in as parameters to onMouseMoved(). So you don't need to get them from the event object. Well, they're the same values. It's just simpler to use the parameter ints.
- You really don't need to change anything in OpenMaLi here. I know, what the problem is. You're using some Point3f instances from the pool (Point3f.fromPool()), and give them back to the pool through Point3f.toPool(), but you stil use them. This leads to exactly the exception, that you get. Unfortunately I had to use this dirty trick with the ArrayIndexOutOfBoundsException. Otherwise I would have lost a lot of performance for nothing.
Just do one of the following two things.
1. (preferred) private final Point3f viewFocus = new Point3f( 0f, 0f, 0f ); private final Point3f eyePosition = new Point3f( 0f, 0f, 5f ); private final Vector3f vecUp = Vector3f.POSITIVE_Y_AXIS; // Read-only vector. So don't try to modify.
@Override public void onMouseMoved( MouseMovedEvent e, int x, int y, int dx, int dy ) { if ( e.getMouse().getButtonState( MouseButtons.LEFT_BUTTON ).getBooleanValue() ) // Is left mouse button pressed? { viewFocus.add( dx, dy, 0f ); eyePosition.add( dx, dy, 0f ); env.getView().lookAt( eyePosition, viewFocus, vecUp ); System.out.println( "Mouse moved: " + dx + ", " + dy ); } }
2. private final Point3f viewFocus = new Point3f( 0f, 0f, 0f ); private final Point3f eyePosition = new Point3f( 0f, 0f, 5f ); private final Vector3f vecUp = Vector3f.POSITIVE_Y_AXIS; // Read-only vector. So don't try to modify. private boolean isMoving = false;
@Override public void onMouseButtonStateChanged( MouseButtonEvent e, MouseButton button, boolean state ) { isMoving = ( ( button == MouseButtons.LEFT_BUTTON ) && state ); }
@Override public void onMouseMoved( MouseMovedEvent e, int x, int y, int dx, int dy ) { if ( isMoving ) // Is left mouse button pressed? { Point3f viewFocus2 = Point3f.fromPool( this.viewFocus ); // New point from pool initialized with the values of our viewFocus Point3f eyePosition2 = Point3f.fromPool( this.eyePosition ); // New point from pool initialized with the values of our eyePosition viewFocus2.add( dx, dy, 0f ); eyePosition2.add( dx, dy, 0f ); env.getView().lookAt( eyePosition2, viewFocus2, vecUp ); System.out.println( "Mouse moved: " + dx + ", " + dy ); // Copy the new values back into our instances. this.eyePosition.set( eyePosition2 ); this.viewFocus.set( viewFocus2 ); Point3f.toPool( eyePosition2 ); Point3f.toPool( viewFocus2 ); } }
As you can see, the first variant works without pooled instances and is a lot simpler.
|
|
|
|
« Last Edit: 13. May 2012, 10:11:06 am by Marvin Fröhlich »
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #6 on: 13. May 2012, 07:04:32 pm » |
|
Ok, I've did it and it works. But now I have another problem. I need rotate camera around viewFocus with radius (eyePosition - viewFocus) and I don't understand how to do this using Transform3D (or not?). And what is Transform3D and how does it work? Please help me : )
|
|
|
|
« Last Edit: 13. May 2012, 07:12:29 pm by sickfar »
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #7 on: 14. May 2012, 07:06:37 am » |
|
Transform3D simply encapsulates a Matrix4f (transformation matrix). You can use regular linear algebra to rotate around a point. IIRC there is even a method on Matrix4f to do that. And IIRC there is a class called TransformationUntils or something like that. One of them should help you out here.
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #8 on: 14. May 2012, 10:15:01 am » |
|
OK, I'll try to understand vector algebra later : )
The next problem: I'm integrating Xith with Swing, using Canvas3DJPanel. That's OK, it work's, but Canvas does not recieves mouse events. Should I do it as InputAdapterRenderLoop or as Swing's event listener?
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #9 on: 14. May 2012, 02:09:42 pm » |
|
Neither. Use Xith/JAGaToo Input listener. InputAdapterRenderLoop is for testing only.
I guess, you'll have to request focus on the Canvas3DJPanel.
Are you sure, you want to use Canvas3DJPanel? Normally Canvas3DPanel is enough for a Swing integration. And it is a whole lot faster.
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #10 on: 14. May 2012, 08:00:15 pm » |
|
But Swing+AWT in my practice is not good solution, cause AWT components suppress Swing components... But if you advice, I'll try, thanks!
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #11 on: 14. May 2012, 10:06:36 pm » |
|
My advice is: Use Canvas3DPanel, if it works. If it doesn't use J.
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #12 on: 16. May 2012, 11:53:49 am » |
|
Yes, it completely works! Thank you! I will have lots of questions - I'm writing my diploma with Xith : ))
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Offline
Posts: 4403
May the 4th, be with you...
|
 |
« Reply #13 on: 16. May 2012, 02:25:38 pm » |
|
Ah, very interesting. Don't hesitate to ask your questions. And I would be interested in the final script.
|
|
|
|
|
Logged
|
|
|
|
|
sickfar
|
 |
« Reply #14 on: 21. May 2012, 08:29:24 pm » |
|
Ye, I have new question! : ))) Look: for camera rotation I need to get vector, whiсh will be planned in plane parallel for view plane and which will be perpendicular for mouse movement vector : )) For me it's rather difficult, any ideas for this? Simply, I need to get rotation axis for view : )
I transform view with Transform3D without matrices
|
|
|
|
|
Logged
|
|
|
|
|