Hallo everybody,
I am new to JME and have the following issue:
My actual work is to integrate a LWJGLCanvas into a Netbeans RCP application. That means the LWJGLCanvas is added into a TopComponent.
The problem is that keyboard and mouse events are not occurring in the glCanvas. Does anybody have an idea why ?
CODE:
public class ThreeDPanel extends JPanel {
private LWJGLCanvas glCanvas;
private JMECanvasImplementor impl;
private DisplaySystem display;
private int width;
private int height;
private static boolean instance = false;
public ThreeDPanel() {
this(new Dimension(320,240));
}
ThreeDPanel(Dimension dimension) {
super();
height = dimension.height;
width = dimension.width;
setLayout(new BorderLayout());
display = DisplaySystem.getDisplaySystem();
display.setVSyncEnabled(true);
display.registerCanvasConstructor("AWT", LWJGLAWTCanvasConstructor.class);
glCanvas = (LWJGLCanvas) display.createCanvas(
width, height);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
doResize();
}
});
new Thread() {
{
setDaemon(true);
}
@Override
public void run() {
while (true) {
glCanvas.repaint();
yield();
}
}
}.start();
//addMouseListerns();
glCanvas.setUpdateInput(true);
if (instance == false) {
KeyInput.setProvider(InputSystem.INPUT_SYSTEM_AWT);
instance = true;
}
((AWTKeyInput) KeyInput.get()).setEnabled(true);
KeyListener kl = (KeyListener) KeyInput.get();
glCanvas.addKeyListener(kl);
AWTMouseInput.setup(glCanvas, false);
//glCanvas.setDrawWhenDirty(false);
impl= new SceneImpl(width, height);
//impl = SceneImpl.getInstance(width, height);
glCanvas.setImplementor(impl);
MouseListener[] m=getMouseListeners();
for (int i = 0; i < m.length; i++) {
MouseListener mouseListener = m[i];
glCanvas.addMouseListener(mouseListener);
}
add(glCanvas);
}
public LWJGLCanvas getGLCanvas() {
return glCanvas;
}
private void addMouseListerns() {
glCanvas.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse");
}
});
}
protected void doResize() {
height = glCanvas.getParent().getHeight();
width = glCanvas.getParent().getWidth();
glCanvas.setBounds(0, 0, width, height);
impl.resizeCanvas(width, height);
glCanvas.makeDirty();
}
public void forceUpdateToSize() {
// force a resize to ensure proper canvas size.
glCanvas.setSize(glCanvas.getParent().getWidth(), glCanvas.getParent().getHeight() + 1);
glCanvas.setSize(glCanvas.getParent().getWidth(), glCanvas.getParent().getHeight() - 1);
}
public JMECanvasImplementor getImpl() {
return impl;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package at.itarchitects.accidentsimulation.gui.view2d;
import com.jme.bounding.BoundingBox;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.input.InputHandler;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.MouseInputAction;
import com.jme.light.DirectionalLight;
import com.jme.light.PointLight;
import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.system.canvas.SimpleCanvasImpl;
import com.jme.util.GameTaskQueue;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.collada.ColladaImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.TerrainPage;
import com.jmex.terrain.util.FaultFractalHeightMap;
import com.jmex.terrain.util.HillHeightMap;
import com.jmex.terrain.util.MidPointHeightMap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.Callable;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
public class SceneImpl extends SimpleCanvasImpl {
private Box box;
private Sphere s;
long startTime = 0;
long fps = 0;
private InputHandler input;
private TerrainPage terrain;
private static SceneImpl instance;
public static SceneImpl getInstance(int x, int y) {
if (instance == null) {
instance = new SceneImpl(x, y);
}
return instance;
}
public SceneImpl(int width, int height) {
super(width, height);
instance = this;
}
@Override
public void simpleSetup() {
ZBufferState buf = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
buf.setEnabled(true);
buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
rootNode.setRenderState(buf);
CullState cs = DisplaySystem.getDisplaySystem().getRenderer().createCullState();
cs.setCullFace(CullState.Face.Back);
cs.setEnabled(true);
rootNode.setRenderState(cs);
setupCamera();
setupLight();
setupScene();
setupInputHandler();
rootNode.updateWorldBound();
rootNode.updateRenderState();
}
@Override
public void simpleUpdate() {
if (box != null) {
cam.setFrustumPerspective(45.0f, (float) DisplaySystem.getDisplaySystem().getRenderer().getWidth()
/ (float) DisplaySystem.getDisplaySystem().getRenderer().getHeight(), 1, 1000);
// place box on terrain
float characterMinHeight = terrain.getHeight(box.getLocalTranslation()) + ((BoundingBox) box.getWorldBound()).yExtent;
if (!Float.isInfinite(characterMinHeight) && !Float.isNaN(characterMinHeight)) {
box.getLocalTranslation().y = characterMinHeight;
}
rootNode.updateRenderState();
}
}
private void setupInputHandler() {
input = new InputHandler();
System.out.println("
>>>>>>>>>> added");
input.addAction(new InputAction() {
@Override
public void performAction(InputActionEvent evt) {
System.out.println("
Trigger added");
System.out.println(evt.getTriggerName());
}
}, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL, InputHandler.AXIS_ALL, false);
input.addAction(new InputAction() {
@Override
public void performAction(InputActionEvent evt) {
System.out.println(evt.getTriggerName());
}
}, InputHandler.DEVICE_KEYBOARD, InputHandler.BUTTON_ALL, InputHandler.AXIS_NONE, false);
}
private void setupCamera() {
cam.setFrustumPerspective(45.0f, (float) DisplaySystem.getDisplaySystem().getRenderer().getWidth() / (float) DisplaySystem.getDisplaySystem().getRenderer().getHeight(), 1, 1000);
cam.setLocation(new Vector3f(100, 100, 100));
cam.lookAt(new Vector3f(0, 50, 0), Vector3f.UNIT_XYZ);
cam.update();
}
protected void setupLight() {
LightState ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
DirectionalLight light = new DirectionalLight();
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.setDirection(new Vector3f(1, -1, 0));
light.setEnabled(true);
ls.setEnabled(true);
ls.attach(light);
rootNode.setRenderState(ls);
}
protected void setupScene() {
rootNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
createSky();
createTerrain();
Vector3f max = new Vector3f(5, 5, 5);
Vector3f min = new Vector3f(-5, -5, -5);
// Make a box
box = new Box("Mybox", min, max);
box.setModelBound(new BoundingBox());
box.updateModelBound();
//box.setLocalRotation(new Matrix3f(0.5f, 0f, 0f, 0f, 0.70f, -0.7f, 0.5f, 0.7f, 0.7f));
box.setLocalTranslation(new Vector3f(0, 0, 0));
rootNode.attachChild(box);
/*Callable<?> exeObjects = new Callable() {
@Override
public Object call() {
rootNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
createSky();
createTerrain();
Vector3f max = new Vector3f(5, 5, 5);
Vector3f min = new Vector3f(-5, -5, -5);
// Make a box
box = new Box("Mybox", min, max);
box.setModelBound(new BoundingBox());
box.updateModelBound();
//box.setLocalRotation(new Matrix3f(0.5f, 0f, 0f, 0f, 0.70f, -0.7f, 0.5f, 0.7f, 0.7f));
box.setLocalTranslation(new Vector3f(0, 0, 0));
rootNode.attachChild(box);
//rootNode.attachChild(SceneImpl.this.loadMyColladaModel());
//rootNode.attachChild(SceneImpl.this.loadMyOBJModel());
return null;
}
};
GameTaskQueueManager.getManager().render(exeObjects);*/
}
private void createTerrain() {
HillHeightMap heightMap = new HillHeightMap(129, 2000, 5.0f, 20.0f,
(byte) 2);
heightMap.setHeightScale(0.1f);
Vector3f terrainScale = new Vector3f(20, 1, 20);
terrain = new TerrainPage("Terrain", 33, heightMap.getSize(), terrainScale,
heightMap.getHeightMap());
terrain.setDetailTexture(1, 256);
terrain.updateRenderState();
rootNode.attachChild(terrain);
}
private void createSky() {
Skybox skybox = new Skybox("skybox", 300, 300, 300);
String dir = "/at/itarchitects/accidentsimulation/gui/view2d/data/";
Texture north = TextureManager.loadTexture(this.getClass().getResource(dir + "1.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
Texture south = TextureManager.loadTexture(this.getClass().getResource(dir + "3.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
Texture east = TextureManager.loadTexture(this.getClass().getResource(dir + "2.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
Texture west = TextureManager.loadTexture(this.getClass().getResource(dir + "4.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
Texture up = TextureManager.loadTexture(this.getClass().getResource(dir + "6.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
Texture down = TextureManager.loadTexture(this.getClass().getResource(dir + "5.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap,
Texture.MagnificationFilter.Bilinear);
skybox.setTexture(Skybox.Face.North, north);
skybox.setTexture(Skybox.Face.West, west);
skybox.setTexture(Skybox.Face.South, south);
skybox.setTexture(Skybox.Face.East, east);
skybox.setTexture(Skybox.Face.Up, up);
skybox.setTexture(Skybox.Face.Down, down);
skybox.preloadTextures();
Vector3f camLoc = cam.getLocation();
camLoc.setY(camLoc.getY() - 50);
skybox.setLocalTranslation(camLoc);
skybox.updateRenderState();
rootNode.attachChild(skybox);
}
public Node loadMyColladaModel() {
InputStream source = this.getClass().getResourceAsStream("/at/itarchitects/accidentsimulation/gui/view2d/data/probe.dae");
if (source == null) {
}
ColladaImporter.load(source, "probe.dae");
return ColladaImporter.getModel();
}
public Node loadMyMD5Model() {
URL modelURL = this.getClass().getResource("/at/itarchitects/accidentsimulation/gui/view2d/data/probe.3dm");
Node gamemap = new Node();
//BinaryImporter importer = BinaryImporter.getInstance();
FormatConverter converter = new MaxToJme(); // com.jmex.model.XMLparser.Converters.*
ByteArrayOutputStream BO = new ByteArrayOutputStream();
try {
converter.convert(modelURL.openStream(), BO);
gamemap = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
//gamemap.setCullMode(SceneBase.CULL_NEVER );
gamemap.setModelBound(new BoundingBox());
gamemap.updateModelBound();
} catch (IOException e) {
e.printStackTrace();
}
return gamemap;
}
public TriMesh loadMyOBJModel() {
URL modelURL = this.getClass().getResource("/at/itarchitects/accidentsimulation/gui/view2d/data/probe.obj");
TriMesh gamemap = new TriMesh();
//BinaryImporter importer = BinaryImporter.getInstance();
FormatConverter converter = new ObjToJme(); // com.jmex.model.XMLparser.Converters.*
ByteArrayOutputStream BO = new ByteArrayOutputStream();
try {
converter.convert(modelURL.openStream(), BO);
gamemap = (TriMesh) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
//gamemap.setCullMode(SceneBase.CULL_NEVER );
gamemap.setModelBound(new BoundingBox());
gamemap.updateModelBound();
} catch (IOException e) {
e.printStackTrace();
}
return gamemap;
}
}