📄 jogldisplaysystem.java
字号:
// Create the OpenGL canvas,
final JOGLAWTCanvas glCanvas = new JOGLAWTCanvas(caps);
// Put the window into orthographic projection mode with 1:1
// pixel
// ratio.
// We haven't used GLU here to do this to avoid an unnecessary
// dependency.
// TODO Same initialization should probably be used for all
// OpenGL
// surfaces (VBO, Pbuffer, etc.).
// final GL gl = glCanvas.getGL();
// gl.setSwapInterval(0);
// gl.glMatrixMode(GL.GL_PROJECTION);
// gl.glLoadIdentity();
// gl.glOrtho(0.0, glCanvas.getWidth(), 0.0, glCanvas.getHeight(),
// -1.0,
// 1.0);
// gl.glMatrixMode(GL.GL_MODELVIEW);
// gl.glLoadIdentity();
// XXX: Same as default JOGLCamer.doViewPortChange
// gl.glViewport(0, 0, glCanvas.getWidth(),
// glCanvas.getHeight());
//
// // Clear window to avoid the desktop "showing through"
// gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// // Enable automatic checking for OpenGL errors.
// // TODO Make this configurable, possibly from a system property.
// glCanvas.setGL(new DebugGL(glCanvas.getGL()));
// FIXME All of this is boilerplate which should be a part of the
// MouseInput, KeyInput, etc. classes if possible.
MouseInput.setProvider(MouseInput.INPUT_AWT);
((AWTMouseInput) MouseInput.get()).setDragOnly(true);
final MouseListener mouseListener = (MouseListener) MouseInput.get();
glCanvas.addMouseListener(mouseListener);
glCanvas.addMouseMotionListener((MouseMotionListener) MouseInput.get());
glCanvas.addMouseWheelListener((MouseWheelListener) MouseInput.get());
// Setting a custom cursor when hidden does not change its visibility
((AWTMouseInput) MouseInput.get()).setHardwareCursor(new Cursor(Cursor.HAND_CURSOR));
KeyInput.setProvider(KeyInput.INPUT_AWT);
final KeyListener keyListener = (KeyListener) KeyInput.get();
glCanvas.addKeyListener(keyListener);
// TODO Look into JInput.
// JoystickInput.setProvider(JoystickInput.INPUT_DUMMY);
glCanvas.setFocusable(true);
glCanvas.requestFocus();
glCanvas.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent event) {
((AWTMouseInput) MouseInput.get()).setEnabled(true);
((AWTKeyInput) KeyInput.get()).setEnabled(true);
}
@Override
public void focusLost(FocusEvent event) {
((AWTMouseInput) MouseInput.get()).setEnabled(false);
((AWTKeyInput) KeyInput.get()).setEnabled(false);
}
});
return glCanvas;
}
@Override
public JOGLRenderer getRenderer() {
return renderer;
}
@Override
public void setTitle(String title) {
if (frame != null)
frame.setTitle(title);
}
@Override
public String getAdapter() {
// TODO Auto-generated method stub
return null;
}
@Override
public RenderContext<GLContext> getCurrentContext() {
return currentContext;
}
@Override
public String getDisplayAPIVersion() {
try {
GL gl = GLU.getCurrentGL();
return gl.glGetString(GL.GL_VERSION);
} catch (Exception e) {
return "Unable to retrieve API version.";
}
}
@Override
public String getDisplayRenderer() {
try {
GL gl = GLU.getCurrentGL();
return gl.glGetString(GL.GL_RENDERER);
} catch (Exception e) {
return "Unable to retrieve adapter details.";
}
}
@Override
public String getDisplayVendor() {
try {
GL gl = GLU.getCurrentGL();
return gl.glGetString(GL.GL_VENDOR);
} catch (Exception e) {
return "Unable to retrieve vendor.";
}
}
@Override
public String getDriverVersion() {
// TODO Auto-generated method stub
return null;
}
/**
* Initializes the window for the DisplaySystem.
* <p>
* NOTE: The window will already have been made visible, meaning that the
* {@link java.awt.Component} will have been realized and the
* {@link GLContext} will already have been made current.
*
* @param width
* the width of the window's canvas.
* @param height
* the height of the window's canvas.
* @see #initForCanvas(int, int)
*/
private void initForWindow(final int width, final int height) {
// Enable automatic checking for OpenGL errors.
// TODO Make this configurable, possibly from a system property.
// autoDrawable.setGL(new DebugGL(autoDrawable.getGL()));
// TODO Move this into the canvas, instead of grabbing the current
// current GLAutoDrawable, since this precludes multiple canvases.
// TODO Can this be centralized in createGLCanvas?
JOGLContextCapabilities caps = new JOGLContextCapabilities(autoDrawable);
renderer = new JOGLRenderer(this, caps, width, height);
// TODO switching to GLAutoDrawable as the key would allow this to be
// centralized. The other option is to move the context into the surface
// where it belongs.
currentContext = new RenderContext<GLContext>(autoDrawable.getContext());
currentContext.setupRecords(renderer);
updateStates(renderer);
// XXX Currently assuming that VSync is disabled.
GL gl = autoDrawable.getGL();
gl.setSwapInterval(0);
}
/**
* This will be called from a GLEventListener, meaning that the
* {@link java.awt.Component} will have been realized and the
* {@link GLContext} will already have been made current.
*/
public void initForCanvas(final int width, final int height) {
// FIXME This seems turned around.
((Canvas) autoDrawable).setSize(width, height);
// Enable automatic checking for OpenGL errors.
// TODO Make this configurable, possibly from a system property.
// TODO Can this be centralized in createGLCanvas?
autoDrawable.setGL(new DebugGL(autoDrawable.getGL()));
// TODO Move this into the canvas, instead of grabbing the current
// current GLAutoDrawable, since this precludes multiple canvases.
JOGLContextCapabilities caps = new JOGLContextCapabilities(autoDrawable);
renderer = new JOGLRenderer(this, caps, width, height);
// XXX What does this mean? Copied from LWJGLDisplaySystem.
renderer.setHeadless(true);
// TODO switching to GLAutoDrawable as the key would allow this to be
// centralized. The other option is to move the context into the surface
// where it belongs.
currentContext = new RenderContext<GLContext>(autoDrawable.getContext());
currentContext.setupRecords(renderer);
updateStates(renderer);
// XXX Currently assuming that VSync is disabled.
GL gl = autoDrawable.getGL();
gl.setSwapInterval(0);
}
@Override
public boolean isActive() {
// TODO Auto-generated method stub return false;
// FIXME handle both frame and canvas implementations.
return frame.hasFocus();
}
@Override
public boolean isClosing() {
return isClosing;
}
@Override
public boolean isValidDisplayMode(int width, int height, int bpp, int freq) {
boolean isValid = false;
for(DisplayMode dm : availableDisplayModes) {
if( dm.getWidth() == width && dm.getHeight() == height &&
dm.getBitDepth() == bpp && dm.getRefreshRate() == freq ){
isValid = true;
break;
}
}
return( isValid );
}
@Override
public void moveWindowTo(int locX, int locY) {
frame.setLocation(locX,locY);
}
@Override
public void recreateWindow(int w, int h, int bpp, int frq, boolean fs) {
// TODO Auto-generated method stub
}
@Override
public void reset() {
// TODO Auto-generated method stub
}
@Override
public void setIcon(Image[] iconImages) {
// TODO Auto-generated method stub
}
@Override
public void setRenderer(final Renderer renderer) {
if (renderer instanceof JOGLRenderer) {
this.renderer = (JOGLRenderer) renderer;
} else {
logger.warning("Invalid Renderer type");
}
}
@Override
public void setVSyncEnabled(boolean enabled) {
// TODO Should this be immediate?
// FIXME What if the autoDrawable has not been allocated.
autoDrawable.getGL().setSwapInterval(enabled ? 1 : 0);
}
@Override
protected void updateDisplayBGC() {
// TODO Auto-generated method stub
}
@Override
public void close() {
// Dispose of any JOGL resources.
if (autoDrawable != null) {
try {
// Handle the case where jME is controlling the game loop.
if (GLContext.getCurrent() != null) {
// Release the OpenGL resources.
autoDrawable.getContext().release();
} else {
// Assume that the single threaded model is in effect, and
// request that the context be closed on that thread.
Threading.invokeOnOpenGLThread(new Runnable() {
public void run() {
// Make the context current if necessary
if (GLContext.getCurrent() == null) {
autoDrawable.getContext().makeCurrent();
}
// Release the OpenGL resources.
autoDrawable.getContext().release();
}
});
}
} catch (GLException releaseFailure) {
logger.log(Level.WARNING, "Failed to release OpenGL Context"
+ autoDrawable, releaseFailure);
}
}
// Dispose of any window resources.
if (frame != null) {
frame.dispose();
}
}
/**
* Switches to another RenderContext identified by the contextKey or to a
* new RenderContext if none is provided.
*
* @param contextKey
* key identifier
* @return RenderContext identified by the contextKey or new RenderContext
* if none provided
* @todo Move JOGL renderer and display system into the same package to
* allow for better permission control.
*/
public synchronized RenderContext<GLContext> switchContext(
final GLContext contextKey) {
// Since we are switching contexts, make the provided context the
// current context. Start by releasing any existing context.
if (currentContext != null) {
GLContext holder = currentContext.getContextHolder();
holder.release();
}
// Make the new context the current context, waiting if necessary as the
// context is initializing.
while (contextKey.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
try {
logger.info("Waiting for the GLContext to initialize...");
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
// Get the stored context state records for this GLContext.
currentContext = contextStore.get(contextKey);
if (currentContext == null) {
// Since the context has no known existing state records. Setup the
// records and add them to the store.
currentContext = new RenderContext<GLContext>(contextKey);
currentContext.setupRecords(renderer);
contextStore.put(contextKey, currentContext);
}
return currentContext;
}
public RenderContext<GLContext> removeContext(GLContext contextKey) {
if (contextKey != null) {
RenderContext<GLContext> context = contextStore.get(contextKey);
if (context != currentContext) {
return contextStore.remove(contextKey);
} else {
logger.warning("Can not remove current context.");
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -