use of com.jme3.app.Application in project jmonkeyengine by jMonkeyEngine.
the class VRApplication method start.
/**
* Starts the application.
* Creating a rendering context and executing the main loop in a separate thread.
* @param contextType the {@link com.jme3.system.JmeContext.Type type} of the context to create.
* @param waitFor if <code>true</code>, the method will wait until the application is started.
* @throws IllegalArgumentException if the context type is not supported.
*/
public void start(JmeContext.Type contextType, boolean waitFor) {
if (context != null && context.isCreated()) {
logger.warning("start() called when application already created!");
return;
}
if (settings == null) {
settings = new AppSettings(true);
}
logger.log(Level.FINE, "Starting application: {0}", getClass().getName());
// Create VR decicated context
if (contextType == Type.Display) {
context = new LwjglDisplayVR();
context.setSettings(settings);
} else if (contextType == Type.OffscreenSurface) {
context = new LwjglOffscreenBufferVR();
context.setSettings(settings);
} else {
logger.severe("Unsupported context type \"" + contextType + "\". Supported are \"Display\" and \"OffscreenSurface\"");
throw new IllegalArgumentException("Unsupported context type \"" + contextType + "\". Supported are \"Display\" and \"OffscreenSurface\"");
}
context.setSystemListener(this);
context.create(waitFor);
}
use of com.jme3.app.Application in project jmonkeyengine by jMonkeyEngine.
the class VRApplication method start.
@Override
public void start() {
logger.config("Starting application...");
// set some default settings in-case
// settings dialog is not shown
boolean loadSettings = false;
if (settings == null) {
setSettings(new AppSettings(true));
loadSettings = true;
}
GraphicsDevice defDev = null;
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
defDev = ge.getDefaultScreenDevice();
} catch (Throwable e1) {
logger.log(Level.SEVERE, "Cannot access default screen device: " + e1.getMessage(), e1);
}
if (isInVR() && !compositorAllowed()) {
logger.warning("VR Composition is not allowed.");
// "easy extended" mode
// TO-DO: JFrame was removed in LWJGL 3, need to use new GLFW library to pick "monitor" display of VR device
// first, find the VR device
GraphicsDevice VRdev = null;
GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
// pick the display that isn't the default one
for (GraphicsDevice gd : devs) {
if (gd != defDev) {
VRdev = gd;
break;
}
}
// did we get the VR device?
if (VRdev != null) {
// set properties for VR acceleration
try {
java.awt.DisplayMode useDM = null;
int max = 0;
for (java.awt.DisplayMode dm : VRdev.getDisplayModes()) {
int check = dm.getHeight() + dm.getWidth() + dm.getRefreshRate() + dm.getBitDepth();
if (check > max) {
max = check;
useDM = dm;
}
}
// create a window for the VR device
settings.setWidth(useDM.getWidth());
settings.setHeight(useDM.getHeight());
settings.setBitsPerPixel(useDM.getBitDepth());
settings.setFrequency(useDM.getRefreshRate());
settings.setSwapBuffers(true);
// allow vsync on this display
settings.setVSync(true);
setSettings(settings);
// make sure we are in the right display mode
if (VRdev.getDisplayMode().equals(useDM) == false) {
VRdev.setDisplayMode(useDM);
}
// make a blank cursor to hide it
//BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
//Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
//VRwindow.setCursor(blankCursor);
//jmeCanvas.getCanvas().setCursor(blankCursor);
//VRwindow.pack();
//VRwindow.setVisible(true);
//startCanvas();
logger.config("Starting application [SUCCESS]");
return;
} catch (Exception e) {
logger.log(Level.SEVERE, "Error during application start: " + e.getMessage(), e);
}
}
}
if (!isInVR()) {
logger.config("VR mode disabled.");
// not in VR, show settings dialog
if (Platform.get() != Platform.MACOSX) {
if (!JmeSystem.showSettingsDialog(settings, loadSettings)) {
logger.config("Starting application [SUCCESS]");
return;
}
} else {
// GLFW workaround on macs
settings.setFrequency(defDev.getDisplayMode().getRefreshRate());
settings.setDepthBits(24);
settings.setVSync(true);
// try and read resolution from file in local dir
File resfile = new File("resolution.txt");
if (resfile.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(resfile));
settings.setWidth(Integer.parseInt(br.readLine()));
settings.setHeight(Integer.parseInt(br.readLine()));
try {
settings.setFullscreen(br.readLine().toLowerCase(Locale.ENGLISH).contains("full"));
} catch (Exception e) {
settings.setFullscreen(false);
}
br.close();
} catch (Exception e) {
settings.setWidth(1280);
settings.setHeight(720);
}
} else {
settings.setWidth(1280);
settings.setHeight(720);
settings.setFullscreen(false);
}
settings.setResizable(false);
}
settings.setSwapBuffers(true);
} else {
logger.config("VR mode enabled.");
// use basic mirroring window, skip settings window
settings.setWidth(xWin);
settings.setHeight(yWin);
settings.setBitsPerPixel(24);
// never sleep in main loop
settings.setFrameRate(0);
settings.setFrequency(VRhardware.getDisplayFrequency());
settings.setFullscreen(false);
// stop vsyncing on primary monitor!
settings.setVSync(false);
settings.setSwapBuffers(!disableSwapBuffers || VRhardware instanceof OSVR);
settings.setTitle("Put Headset On Now: " + settings.getTitle());
settings.setResizable(true);
}
if (forceDisableMSAA) {
logger.config("Disabling multisampling.");
// disable multisampling, which is more likely to break things than be useful
settings.setSamples(1);
}
// set opengl mode
if (tryOpenGL3) {
logger.config("Using LWJGL OpenGL 3 renderer.");
settings.setRenderer(AppSettings.LWJGL_OPENGL3);
} else {
logger.config("Using LWJGL OpenGL 2 renderer.");
settings.setRenderer(AppSettings.LWJGL_OPENGL2);
}
setSettings(settings);
start(JmeContext.Type.Display, false);
// disable annoying warnings about GUI stuff being updated, which is normal behavior
// for late GUI placement for VR purposes
Logger.getLogger("com.jme3").setLevel(Level.SEVERE);
}
use of com.jme3.app.Application in project jmonkeyengine by jMonkeyEngine.
the class VRGuiManager method getCanvasSize.
/**
* Get the GUI canvas size. This method return the size in pixels of the GUI available area within the VR view.
* @return the GUI canvas size. This method return the size in pixels of the GUI available area within the VR view.
*/
public Vector2f getCanvasSize() {
if (environment != null) {
if (environment.getApplication() != null) {
if (screenSize == null) {
if (environment.isInVR() && environment.getVRHardware() != null) {
screenSize = new Vector2f();
environment.getVRHardware().getRenderSize(screenSize);
screenSize.multLocal(environment.getVRViewManager().getResolutionMuliplier());
} else {
AppSettings as = environment.getApplication().getContext().getSettings();
screenSize = new Vector2f(as.getWidth(), as.getHeight());
}
}
return screenSize;
} else {
throw new IllegalStateException("VR GUI manager underlying environment is not attached to any application.");
}
} else {
throw new IllegalStateException("VR GUI manager is not attached to any environment.");
}
}
use of com.jme3.app.Application in project jmonkeyengine by jMonkeyEngine.
the class VRGuiManager method getCanvasToWindowRatio.
/**
* Get the ratio between the {@link #getCanvasSize() GUI canvas size} and the application main windows (if available) or the screen size.
* @return the ratio between the {@link #getCanvasSize() GUI canvas size} and the application main windows (if available).
* @see #getCanvasSize()
*/
public Vector2f getCanvasToWindowRatio() {
if (environment != null) {
if (environment.getApplication() != null) {
if (ratio == null) {
ratio = new Vector2f();
Vector2f canvas = getCanvasSize();
int width = Integer.min(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth(), environment.getApplication().getContext().getSettings().getWidth());
int height = Integer.min(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight(), environment.getApplication().getContext().getSettings().getHeight());
ratio.x = Float.max(1f, canvas.x / width);
ratio.y = Float.max(1f, canvas.y / height);
}
return ratio;
} else {
throw new IllegalStateException("VR GUI manager underlying environment is not attached to any application.");
}
} else {
throw new IllegalStateException("VR GUI manager is not attached to any environment.");
}
}
use of com.jme3.app.Application in project jmonkeyengine by jMonkeyEngine.
the class VRGuiManager method getGuiQuad.
/**
* Create a GUI quad for the given camera.
* @param sourceCam the camera
* @return a GUI quad for the given camera.
*/
private Spatial getGuiQuad(Camera sourceCam) {
if (environment != null) {
if (environment.getApplication() != null) {
if (guiQuadNode == null) {
Vector2f guiCanvasSize = getCanvasSize();
Camera offCamera = sourceCam.clone();
offCamera.setParallelProjection(true);
offCamera.setLocation(Vector3f.ZERO);
offCamera.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
offView = environment.getApplication().getRenderManager().createPreView("GUI View", offCamera);
offView.setClearFlags(true, true, true);
offView.setBackgroundColor(ColorRGBA.BlackNoAlpha);
// create offscreen framebuffer
FrameBuffer offBuffer = new FrameBuffer((int) guiCanvasSize.x, (int) guiCanvasSize.y, 1);
//setup framebuffer's texture
guiTexture = new Texture2D((int) guiCanvasSize.x, (int) guiCanvasSize.y, Format.RGBA8);
guiTexture.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
guiTexture.setMagFilter(Texture.MagFilter.Bilinear);
//setup framebuffer to use texture
offBuffer.setDepthBuffer(Format.Depth);
offBuffer.setColorTexture(guiTexture);
//set viewport to render to offscreen framebuffer
offView.setOutputFrameBuffer(offBuffer);
// setup framebuffer's scene
Iterator<Spatial> spatialIter = environment.getApplication().getGuiViewPort().getScenes().iterator();
while (spatialIter.hasNext()) {
offView.attachScene(spatialIter.next());
}
if (useCurvedSurface) {
guiQuad = (Geometry) environment.getApplication().getAssetManager().loadModel("Common/Util/gui_mesh.j3o");
} else {
guiQuad = new Geometry("guiQuad", new CenterQuad(1f, 1f));
}
Material mat = new Material(environment.getApplication().getAssetManager(), "Common/MatDefs/VR/GuiOverlay.j3md");
mat.getAdditionalRenderState().setDepthTest(!overdraw);
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
mat.getAdditionalRenderState().setDepthWrite(false);
mat.setTexture("ColorMap", guiTexture);
guiQuad.setQueueBucket(Bucket.Translucent);
guiQuad.setMaterial(mat);
guiQuadNode = new Node("gui-quad-node");
guiQuadNode.setQueueBucket(Bucket.Translucent);
guiQuadNode.attachChild(guiQuad);
}
return guiQuadNode;
} else {
throw new IllegalStateException("VR GUI manager underlying environment is not attached to any application.");
}
} else {
throw new IllegalStateException("VR GUI manager is not attached to any environment.");
}
}
Aggregations