diff options
Diffstat (limited to 'src/jake2/render/opengl')
-rw-r--r-- | src/jake2/render/opengl/GLDriver.java | 6 | ||||
-rw-r--r-- | src/jake2/render/opengl/JoglDriver.java | 450 | ||||
-rw-r--r-- | src/jake2/render/opengl/JoglES2.java (renamed from src/jake2/render/opengl/JoglGL.java) | 4 | ||||
-rw-r--r-- | src/jake2/render/opengl/JoglES2Driver.java | 135 | ||||
-rw-r--r-- | src/jake2/render/opengl/JoglGL2.java | 317 | ||||
-rw-r--r-- | src/jake2/render/opengl/JoglGL2Driver.java | 135 | ||||
-rw-r--r-- | src/jake2/render/opengl/NEWTWin.java | 218 |
7 files changed, 811 insertions, 454 deletions
diff --git a/src/jake2/render/opengl/GLDriver.java b/src/jake2/render/opengl/GLDriver.java index 8a8eb4c..6fda8cf 100644 --- a/src/jake2/render/opengl/GLDriver.java +++ b/src/jake2/render/opengl/GLDriver.java @@ -1,8 +1,10 @@ package jake2.render.opengl; +import javax.media.nativewindow.util.Dimension; + import jake2.qcommon.xcommand_t; -import java.awt.Dimension; +import com.jogamp.newt.ScreenMode; public interface GLDriver { @@ -22,7 +24,7 @@ public interface GLDriver { void logNewFrame(); - java.awt.DisplayMode[] getModeList(); + ScreenMode[] getModeList(); void updateScreen(xcommand_t callback); diff --git a/src/jake2/render/opengl/JoglDriver.java b/src/jake2/render/opengl/JoglDriver.java deleted file mode 100644 index 8a1818a..0000000 --- a/src/jake2/render/opengl/JoglDriver.java +++ /dev/null @@ -1,450 +0,0 @@ -/* - * JoglDriver.java - * Copyright (C) 2004 - * - */ -/* -Copyright (C) 1997-2001 Id Software, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ - -package jake2.render.opengl; - -import jake2.Defines; -import jake2.Globals; -import jake2.SizeChangeListener; -import jake2.client.VID; -import jake2.qcommon.Cbuf; -import jake2.qcommon.xcommand_t; -import jake2.render.Base; -import jake2.sys.JOGLKBD; - -import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.util.LinkedList; - -import javax.media.opengl.*; -import javax.media.opengl.awt.GLCanvas; -import javax.swing.ImageIcon; - -import jogamp.opengl.FPSCounterImpl; - -/** - * JoglCommon - */ -public abstract class JoglDriver extends JoglGL implements GLDriver { - - protected JoglDriver() { - // singleton - } - - private GraphicsDevice device; - private DisplayMode oldDisplayMode; - private volatile Display display; - private volatile Frame window; - - // This is either the above Window reference or the global - // applet if we're running in applet mode - private volatile Container container; - - // window position on the screen - int window_xpos, window_ypos; - - public DisplayMode[] getModeList() { - DisplayMode[] modes = device.getDisplayModes(); - LinkedList<DisplayMode> l = new LinkedList<DisplayMode>(); - l.add(oldDisplayMode); - - for (int i = 0; i < modes.length; i++) { - DisplayMode m = modes[i]; - - if (m.getBitDepth() != oldDisplayMode.getBitDepth()) continue; - if (m.getRefreshRate() > oldDisplayMode.getRefreshRate()) continue; - if (m.getHeight() < 240 || m.getWidth() < 320) continue; - - int j = 0; - DisplayMode ml = null; - for (j = 0; j < l.size(); j++) { - ml = (DisplayMode)l.get(j); - if (ml.getWidth() > m.getWidth()) break; - if (ml.getWidth() == m.getWidth() && ml.getHeight() >= m.getHeight()) break; - } - if (j == l.size()) { - l.addLast(m); - } else if (ml.getWidth() > m.getWidth() || ml.getHeight() > m.getHeight()) { - l.add(j, m); - } else if (m.getRefreshRate() > ml.getRefreshRate()){ - l.remove(j); - l.add(j, m); - } - } - DisplayMode[] ma = new DisplayMode[l.size()]; - l.toArray(ma); - return ma; - } - - DisplayMode findDisplayMode(Dimension dim) { - DisplayMode mode = null; - DisplayMode m = null; - DisplayMode[] modes = getModeList(); - int w = dim.width; - int h = dim.height; - - for (int i = 0; i < modes.length; i++) { - m = modes[i]; - if (m.getWidth() == w && m.getHeight() == h) { - mode = m; - break; - } - } - if (mode == null) mode = oldDisplayMode; - return mode; - } - - String getModeString(DisplayMode m) { - StringBuffer sb = new StringBuffer(); - sb.append(m.getWidth()); - sb.append('x'); - sb.append(m.getHeight()); - sb.append('x'); - sb.append(m.getBitDepth()); - sb.append('@'); - sb.append(m.getRefreshRate()); - sb.append("Hz"); - return sb.toString(); - } - - /** - * @param dim - * @param mode - * @param fullscreen - * @return enum Base.rserr_t - */ - public int setMode(Dimension dim, int mode, boolean fullscreen) { - - final Dimension newDim = new Dimension(); - - VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display\n"); - - VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ":"); - - if (Globals.appletMode && container == null) { - container = (Container) Globals.applet; - } - - /* - * full screen handling - */ - if (device == null) { - GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - device = env.getDefaultScreenDevice(); - } - - if (oldDisplayMode == null) { - oldDisplayMode = device.getDisplayMode(); - } - - if (!VID.GetModeInfo(newDim, mode)) { - VID.Printf(Defines.PRINT_ALL, " invalid mode\n"); - return Base.rserr_invalid_mode; - } - - VID.Printf(Defines.PRINT_ALL, " " + newDim.width + " " + newDim.height + '\n'); - - if (!Globals.appletMode) { - // destroy the existing window - if (window != null) shutdown(); - - window = new Frame("Jake2 (jogl2)"); - container = window; - ImageIcon icon = new ImageIcon(getClass().getResource("/icon-small.png")); - window.setIconImage(icon.getImage()); - window.setLayout(new GridBagLayout()); - // register event listener - window.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { - Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit"); - } - }); - } - - if (Globals.appletMode) { - // Destroy the previous display if there is one - shutdown(); - - // We don't support full-screen mode - fullscreen = false; - - // We need to feed the container to the JOGL - // keyboard class manually because we'll never get - // a component shown event for it - JOGLKBD.Init(container); - } - - Display canvas = new Display(new GLCapabilities(GLProfile.get(GLProfile.GL2))); - // we want keypressed events for TAB key - canvas.setFocusTraversalKeysEnabled(false); - canvas.setSize(newDim.width, newDim.height); - - // the OpenGL canvas grows and shrinks with the window - final GridBagConstraints gbc = new GridBagConstraints(); - gbc.fill = GridBagConstraints.BOTH; - gbc.weightx = gbc.weighty = 1; - - // D I F F E R E N T J A K E 2 E V E N T P R O C E S S I N G - container.addComponentListener(JOGLKBD.listener); - canvas.addKeyListener(JOGLKBD.listener); - canvas.addMouseListener(JOGLKBD.listener); - canvas.addMouseMotionListener(JOGLKBD.listener); - canvas.addMouseWheelListener(JOGLKBD.listener); - - if (fullscreen) { - - container.add(canvas, gbc); - - DisplayMode displayMode = findDisplayMode(newDim); - - newDim.width = displayMode.getWidth(); - newDim.height = displayMode.getHeight(); - window.setUndecorated(true); - window.setResizable(false); - - device.setFullScreenWindow(window); - - if (device.isFullScreenSupported()) - device.setDisplayMode(displayMode); - - window.setLocation(0, 0); - window.setSize(displayMode.getWidth(), displayMode.getHeight()); - canvas.setSize(displayMode.getWidth(), displayMode.getHeight()); - - VID.Printf(Defines.PRINT_ALL, "...setting fullscreen " + getModeString(displayMode) + '\n'); - - } else { - if (!Globals.appletMode) { - container.add(canvas, gbc); - final Frame f2 = window; - try { - EventQueue.invokeAndWait(new Runnable() { - public void run() { - //f2.setLocation(window_xpos, window_ypos); - f2.pack(); - f2.setResizable(false); - f2.setVisible(true); - } - }); - } catch (Exception e) { - e.printStackTrace(); - } - } else { - final Display fd = canvas; - try { - EventQueue.invokeAndWait(new Runnable() { - public void run() { - container.add(fd, BorderLayout.CENTER); - // Notify the size listener about the change - SizeChangeListener listener = Globals.sizeChangeListener; - if (listener != null) { - listener.sizeChanged(newDim.width, newDim.height); - } - fd.setSize(newDim.width, newDim.height); - } - }); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - if (!Globals.appletMode) { - while (!canvas.isDisplayable() || !window.isDisplayable()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) {} - } - } - canvas.requestFocus(); - canvas.display(); // force GL resource validation - this.display = canvas; - - setGL(display.getGL()); - init(0, 0); - - return Base.rserr_ok; - } - - public void shutdown() { - if (!Globals.appletMode) { - try { - EventQueue.invokeAndWait(new Runnable() { - public void run() { - if (oldDisplayMode != null - && device.getFullScreenWindow() != null) { - try { - if (device.isFullScreenSupported()) { - if (!device.getDisplayMode().equals(oldDisplayMode)) - device.setDisplayMode(oldDisplayMode); - - } - device.setFullScreenWindow(null); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - }); - } catch (Exception e) { - e.printStackTrace(); - } - - if (window != null) { - if (display != null) display.destroy(); - window.dispose(); - while (window.isDisplayable()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - } - - } - } - } else { - if (display != null) { - display.destroy(); - // Remove the old display if there is one - container.remove(display); - } - } - display = null; - } - - /** - * @return true - */ - public boolean init(int xpos, int ypos) { - // set window position - window_xpos = xpos; - window_ypos = ypos; - // clear the screen - // first buffer - beginFrame(0.0f); - glViewport(0, 0, display.getWidth(), display.getHeight()); - glClearColor(0, 0, 0, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - endFrame(); - // second buffer - beginFrame(0.0f); - glViewport(0, 0, display.getWidth(), display.getHeight()); - glClearColor(0, 0, 0, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - endFrame(); - return true; - } - - public void beginFrame(float camera_separation) { - display.activate(); - } - - public void endFrame() { - display.update(); - } - - public void appActivate(boolean activate) { - // do nothing - } - - public void enableLogging(boolean enable) { - // do nothing - } - - public void logNewFrame() { - // do nothing - } - - /* - * @see jake2.client.refexport_t#updateScreen() - */ - - public void updateScreen(xcommand_t callback) { - callback.execute(); - } - - protected void activate() { - display.activate(); - } - - // -------------------------------------------------------------------------- - - @SuppressWarnings("serial") - private static class Display extends GLCanvas { - final FPSCounterImpl fpsCounter; - - public Display(GLCapabilities capabilities) { - super(capabilities); - setAutoSwapBufferMode(false); - fpsCounter = new FPSCounterImpl(); - fpsCounter.setUpdateFPSFrames(60*5, System.err); // all 5s in 60hz mode - } - - @Override - public GL2 getGL() { - activate(); - return super.getGL().getGL2(); - } - - - /** - * @see java.awt.Component#setBounds(int, int, int, int) - */ - @Override - public void setBounds(int x, int y, int width, int height) { - final int mask = ~0x03; - if ((width & 0x03) != 0) { - width &= mask; - width += 4; - } - -// System.out.println("display bounds: " + x + ", " + y + ", " + width + ", " + height); - super.setBounds(x, y, width, height); - Base.setVid(width, height); - // let the sound and input subsystems know about the new window - VID.NewWindow(width, height); - } - - void activate() { - final GLContext ctx = this.getContext(); - if ( null != ctx && GLContext.getCurrent() != ctx ) { - ctx.makeCurrent(); - } - } - - private void release() { - final GLContext ctx = this.getContext(); - if ( null != ctx && GLContext.getCurrent() == ctx) { - ctx.release(); - } - } - - void update() { - release(); - swapBuffers(); - fpsCounter.tickFPS(); - } - } -} diff --git a/src/jake2/render/opengl/JoglGL.java b/src/jake2/render/opengl/JoglES2.java index c009a40..1967619 100644 --- a/src/jake2/render/opengl/JoglGL.java +++ b/src/jake2/render/opengl/JoglES2.java @@ -5,11 +5,11 @@ import java.nio.*; import javax.media.opengl.GL2; -public class JoglGL implements QGL { +public class JoglES2 implements QGL { private GL2 gl; - JoglGL() { + JoglES2() { // singleton } diff --git a/src/jake2/render/opengl/JoglES2Driver.java b/src/jake2/render/opengl/JoglES2Driver.java new file mode 100644 index 0000000..7319324 --- /dev/null +++ b/src/jake2/render/opengl/JoglES2Driver.java @@ -0,0 +1,135 @@ +/* + * JoglDriver.java + * Copyright (C) 2004 + * + */ +/* +Copyright (C) 1997-2001 Id Software, Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + */ + +package jake2.render.opengl; + +import jake2.qcommon.xcommand_t; +import jake2.render.Base; + +import javax.media.nativewindow.util.Dimension; +import javax.media.opengl.*; + +import com.jogamp.newt.ScreenMode; + +/** + * JoglCommon + */ +public abstract class JoglES2Driver extends JoglES2 implements GLDriver { + + protected JoglES2Driver() { + // singleton + } + + private NEWTWin newtWin = null; + + public ScreenMode[] getModeList() { + if(null == newtWin) { + throw new RuntimeException("NEWTWin not yet initialized."); + } + return newtWin.getModeList(); + } + + public int setMode(Dimension dim, int mode, boolean fullscreen) { + if(null == newtWin) { + newtWin = new NEWTWin(); + } + int res = newtWin.setMode(dim, mode, fullscreen); + if( Base.rserr_ok == res ) { + + setGL(newtWin.window.getGL().getGL2()); + init(0, 0); + + return Base.rserr_ok; + } + return res; + } + + public void shutdown() { + if(null != newtWin) { + newtWin.shutdown(); + } + } + + /** + * @return true + */ + public boolean init(int xpos, int ypos) { + // clear the screen + // first buffer + beginFrame(0.0f); + glViewport(0, 0, newtWin.window.getWidth(), newtWin.window.getHeight()); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + endFrame(); + // second buffer + beginFrame(0.0f); + glViewport(0, 0, newtWin.window.getWidth(), newtWin.window.getHeight()); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + endFrame(); + return true; + } + + public void beginFrame(float camera_separation) { + activate(); + } + + public void endFrame() { + final GLContext ctx = newtWin.window.getContext(); + if ( null != ctx && GLContext.getCurrent() == ctx) { + ctx.release(); + } + newtWin.window.swapBuffers(); + newtWin.fpsCounter.tickFPS(); + } + + public void appActivate(boolean activate) { + // do nothing + } + + public void enableLogging(boolean enable) { + // do nothing + } + + public void logNewFrame() { + // do nothing + } + + /* + * @see jake2.client.refexport_t#updateScreen() + */ + public void updateScreen(xcommand_t callback) { + callback.execute(); + } + + protected void activate() { + final GLContext ctx = newtWin.window.getContext(); + if ( null != ctx && GLContext.getCurrent() != ctx ) { + ctx.makeCurrent(); + } + } + + // -------------------------------------------------------------------------- +} diff --git a/src/jake2/render/opengl/JoglGL2.java b/src/jake2/render/opengl/JoglGL2.java new file mode 100644 index 0000000..a1d1b17 --- /dev/null +++ b/src/jake2/render/opengl/JoglGL2.java @@ -0,0 +1,317 @@ +package jake2.render.opengl; + + +import java.nio.*; + +import javax.media.opengl.GL2; + +public class JoglGL2 implements QGL { + + private GL2 gl; + + JoglGL2() { + // singleton + } + + void setGL(GL2 gl) { + this.gl = gl; + } + + public void glAlphaFunc(int func, float ref) { + gl.glAlphaFunc(func, ref); + } + + public void glBegin(int mode) { + gl.glBegin(mode); + } + + public void glBindTexture(int target, int texture) { + gl.glBindTexture(target, texture); + } + + public void glBlendFunc(int sfactor, int dfactor) { + gl.glBlendFunc(sfactor, dfactor); + } + + public void glClear(int mask) { + gl.glClear(mask); + } + + public void glClearColor(float red, float green, float blue, float alpha) { + gl.glClearColor(red, green, blue, alpha); + } + + public void glColor3f(float red, float green, float blue) { + gl.glColor3f(red, green, blue); + } + + public void glColor3ub(byte red, byte green, byte blue) { + gl.glColor3ub(red, green, blue); + } + + public void glColor4f(float red, float green, float blue, float alpha) { + gl.glColor4f(red, green, blue, alpha); + } + + public void glColor4ub(byte red, byte green, byte blue, byte alpha) { + gl.glColor4ub(red, green, blue, alpha); + } + + public void glColorPointer(int size, boolean unsigned, int stride, + ByteBuffer pointer) { + gl.glColorPointer(size, GL_UNSIGNED_BYTE, stride, pointer); + } + + public void glColorPointer(int size, int stride, FloatBuffer pointer) { + gl.glColorPointer(size, GL_FLOAT, stride, pointer); + } + + public void glCullFace(int mode) { + gl.glCullFace(mode); + } + + public void glDeleteTextures(IntBuffer textures) { + gl.glDeleteTextures(textures.limit(), textures); + } + + public void glDepthFunc(int func) { + gl.glDepthFunc(func); + } + + public void glDepthMask(boolean flag) { + gl.glDepthMask(flag); + } + + public void glDepthRange(double zNear, double zFar) { + gl.glDepthRange(zNear, zFar); + } + + public void glDisable(int cap) { + gl.glDisable(cap); + } + + public void glDisableClientState(int cap) { + gl.glDisableClientState(cap); + } + + public void glDrawArrays(int mode, int first, int count) { + gl.glDrawArrays(mode, first, count); + } + + public void glDrawBuffer(int mode) { + gl.glDrawBuffer(mode); + } + + public void glDrawElements(int mode, IntBuffer indices) { + gl.glDrawElements(mode, indices.limit(), GL_UNSIGNED_INT, indices); + } + + public void glEnable(int cap) { + gl.glEnable(cap); + } + + public void glEnableClientState(int cap) { + gl.glEnableClientState(cap); + } + + public void glEnd() { + gl.glEnd(); + } + + public void glFinish() { + gl.glFinish(); + } + + public void glFlush() { + gl.glFlush(); + } + + public void glFrustum(double left, double right, double bottom, + double top, double zNear, double zFar) { + gl.glFrustum(left, right, bottom, top, zNear, zFar); + } + + public int glGetError() { + return gl.glGetError(); + } + + public void glGetFloat(int pname, FloatBuffer params) { + gl.glGetFloatv(pname, params); + } + + public String glGetString(int name) { + return gl.glGetString(name); + } + + public void glHint(int target, int mode) { + gl.glHint(target, mode); + } + + public void glInterleavedArrays(int format, int stride, + FloatBuffer pointer) { + gl.glInterleavedArrays(format, stride, pointer); + } + + public void glLoadIdentity() { + gl.glLoadIdentity(); + } + + public void glLoadMatrix(FloatBuffer m) { + gl.glLoadMatrixf(m); + } + + public void glMatrixMode(int mode) { + gl.glMatrixMode(mode); + } + + public void glOrtho(double left, double right, double bottom, + double top, double zNear, double zFar) { + gl.glOrtho(left, right, bottom, top, zNear, zFar); + } + + public void glPixelStorei(int pname, int param) { + gl.glPixelStorei(pname, param); + } + + public void glPointSize(float size) { + gl.glPointSize(size); + } + + public void glPolygonMode(int face, int mode) { + gl.glPolygonMode(face, mode); + } + + public void glPopMatrix() { + gl.glPopMatrix(); + } + + public void glPushMatrix() { + gl.glPushMatrix(); + } + + public void glReadPixels(int x, int y, int width, int height, + int format, int type, ByteBuffer pixels) { + gl.glReadPixels(x, y, width, height, format, type, pixels); + } + + public void glRotatef(float angle, float x, float y, float z) { + gl.glRotatef(angle, x, y, z); + } + + public void glScalef(float x, float y, float z) { + gl.glScalef(x, y, z); + } + + public void glScissor(int x, int y, int width, int height) { + gl.glScissor(x, y, width, height); + } + + public void glShadeModel(int mode) { + gl.glShadeModel(mode); + } + + public void glTexCoord2f(float s, float t) { + gl.glTexCoord2f(s, t); + } + + public void glTexCoordPointer(int size, int stride, FloatBuffer pointer) { + gl.glTexCoordPointer(size, GL_FLOAT, stride, pointer); + } + + public void glTexEnvi(int target, int pname, int param) { + gl.glTexEnvi(target, pname, param); + } + + public void glTexImage2D(int target, int level, int internalformat, + int width, int height, int border, int format, int type, + ByteBuffer pixels) { + gl.glTexImage2D(target, level, internalformat, width, height, border, + format, type, pixels); + } + + public void glTexImage2D(int target, int level, int internalformat, + int width, int height, int border, int format, int type, + IntBuffer pixels) { + gl.glTexImage2D(target, level, internalformat, width, height, border, + format, type, pixels); + } + + public void glTexParameterf(int target, int pname, float param) { + gl.glTexParameterf(target, pname, param); + } + + public void glTexParameteri(int target, int pname, int param) { + gl.glTexParameteri(target, pname, param); + } + + public void glTexSubImage2D(int target, int level, int xoffset, + int yoffset, int width, int height, int format, int type, + IntBuffer pixels) { + gl.glTexSubImage2D(target, level, xoffset, yoffset, width, height, + format, type, pixels); + } + + public void glTranslatef(float x, float y, float z) { + gl.glTranslatef(x, y, z); + } + + public void glVertex2f(float x, float y) { + gl.glVertex2f(x, y); + } + + public void glVertex3f(float x, float y, float z) { + gl.glVertex3f(x, y, z); + } + + public void glVertexPointer(int size, int stride, FloatBuffer pointer) { + gl.glVertexPointer(size, GL_FLOAT, stride, pointer); + } + + public void glViewport(int x, int y, int width, int height) { + gl.glViewport(x, y, width, height); + } + + public void glColorTable(int target, int internalFormat, int width, + int format, int type, ByteBuffer data) { + gl.glColorTable(target, internalFormat, width, format, type, data); + } + + public void glActiveTextureARB(int texture) { + gl.glActiveTexture(texture); + } + + public void glClientActiveTextureARB(int texture) { + gl.glClientActiveTexture(texture); + } + + public void glPointParameterEXT(int pname, FloatBuffer pfParams) { + gl.glPointParameterfv(pname, pfParams); + } + + public void glPointParameterfEXT(int pname, float param) { + gl.glPointParameterf(pname, param); + } + public void glLockArraysEXT(int first, int count) { + gl.glLockArraysEXT(first, count); + } + + public void glArrayElement(int index) { + gl.glArrayElement(index); + } + + public void glUnlockArraysEXT() { + gl.glUnlockArraysEXT(); + } + + public void glMultiTexCoord2f(int target, float s, float t) { + gl.glMultiTexCoord2f(target, s, t); + } + + /* + * util extensions + */ + public void setSwapInterval(int interval) { + gl.setSwapInterval(interval); + } + +} diff --git a/src/jake2/render/opengl/JoglGL2Driver.java b/src/jake2/render/opengl/JoglGL2Driver.java new file mode 100644 index 0000000..46b7f97 --- /dev/null +++ b/src/jake2/render/opengl/JoglGL2Driver.java @@ -0,0 +1,135 @@ +/* + * JoglDriver.java + * Copyright (C) 2004 + * + */ +/* +Copyright (C) 1997-2001 Id Software, Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + */ + +package jake2.render.opengl; + +import jake2.qcommon.xcommand_t; +import jake2.render.Base; + +import javax.media.nativewindow.util.Dimension; +import javax.media.opengl.*; + +import com.jogamp.newt.ScreenMode; + +/** + * JoglCommon + */ +public abstract class JoglGL2Driver extends JoglGL2 implements GLDriver { + + protected JoglGL2Driver() { + // singleton + } + + private NEWTWin newtWin = null; + + public ScreenMode[] getModeList() { + if(null == newtWin) { + throw new RuntimeException("NEWTWin not yet initialized."); + } + return newtWin.getModeList(); + } + + public int setMode(Dimension dim, int mode, boolean fullscreen) { + if(null == newtWin) { + newtWin = new NEWTWin(); + } + int res = newtWin.setMode(dim, mode, fullscreen); + if( Base.rserr_ok == res ) { + + setGL(newtWin.window.getGL().getGL2()); + init(0, 0); + + return Base.rserr_ok; + } + return res; + } + + public void shutdown() { + if(null != newtWin) { + newtWin.shutdown(); + } + } + + /** + * @return true + */ + public boolean init(int xpos, int ypos) { + // clear the screen + // first buffer + beginFrame(0.0f); + glViewport(0, 0, newtWin.window.getWidth(), newtWin.window.getHeight()); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + endFrame(); + // second buffer + beginFrame(0.0f); + glViewport(0, 0, newtWin.window.getWidth(), newtWin.window.getHeight()); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + endFrame(); + return true; + } + + public void beginFrame(float camera_separation) { + activate(); + } + + public void endFrame() { + final GLContext ctx = newtWin.window.getContext(); + if ( null != ctx && GLContext.getCurrent() == ctx) { + ctx.release(); + } + newtWin.window.swapBuffers(); + newtWin.fpsCounter.tickFPS(); + } + + public void appActivate(boolean activate) { + // do nothing + } + + public void enableLogging(boolean enable) { + // do nothing + } + + public void logNewFrame() { + // do nothing + } + + /* + * @see jake2.client.refexport_t#updateScreen() + */ + public void updateScreen(xcommand_t callback) { + callback.execute(); + } + + protected void activate() { + final GLContext ctx = newtWin.window.getContext(); + if ( null != ctx && GLContext.getCurrent() != ctx ) { + ctx.makeCurrent(); + } + } + + // -------------------------------------------------------------------------- +} diff --git a/src/jake2/render/opengl/NEWTWin.java b/src/jake2/render/opengl/NEWTWin.java new file mode 100644 index 0000000..662dbc7 --- /dev/null +++ b/src/jake2/render/opengl/NEWTWin.java @@ -0,0 +1,218 @@ +/* + * NEWTWin.java + * Copyright (C) 2004 + * + */ +package jake2.render.opengl; + +import jake2.Defines; +import jake2.Globals; +import jake2.SizeChangeListener; +import jake2.client.VID; +import jake2.qcommon.Cbuf; +import jake2.render.Base; +import jake2.sys.NEWTKBD; + +import java.util.List; + +import javax.media.nativewindow.util.Dimension; +import javax.media.nativewindow.util.DimensionImmutable; +import javax.media.nativewindow.util.SurfaceSize; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; + +import jogamp.opengl.FPSCounterImpl; + +import com.jogamp.newt.NewtFactory; +import com.jogamp.newt.Screen; +import com.jogamp.newt.ScreenMode; +import com.jogamp.newt.event.WindowAdapter; +import com.jogamp.newt.event.WindowEvent; +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.newt.util.MonitorMode; +import com.jogamp.newt.util.ScreenModeUtil; + +public class NEWTWin { + ScreenMode oldDisplayMode = null; + volatile Screen screen = null; + volatile GLWindow window = null; + final FPSCounterImpl fpsCounter = new FPSCounterImpl(); + + public ScreenMode[] getModeList() { + final List<ScreenMode> sml = screen.getScreenModes(); + ScreenMode[] sma = new ScreenMode[sml.size()]; + sml.toArray(sma); + return sma; + } + + public ScreenMode findDisplayMode(DimensionImmutable dim) { + final List<ScreenMode> sml = ScreenModeUtil.filterByResolution(screen.getScreenModes(), dim); + if(sml.size() == 0) { + return oldDisplayMode; + } + return sml.get(0); + } + + public String getModeString(ScreenMode sm) { + final MonitorMode mm = sm.getMonitorMode(); + final SurfaceSize ss = mm.getSurfaceSize(); + final DimensionImmutable m = ss.getResolution(); + final StringBuffer sb = new StringBuffer(); + sb.append(m.getWidth()); + sb.append('x'); + sb.append(m.getHeight()); + sb.append('x'); + sb.append(ss.getBitsPerPixel()); + sb.append('@'); + sb.append(mm.getRefreshRate()); + sb.append("Hz"); + return sb.toString(); + } + + /** + * @param dim + * @param mode + * @param fullscreen + * @return enum Base.rserr_t + */ + public int setMode(Dimension dim, int mode, boolean fullscreen) { + + final Dimension newDim = new Dimension(); + + VID.Printf(Defines.PRINT_ALL, "Initializing OpenGL display\n"); + + VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ":"); + + /** + if (Globals.appletMode && container == null) { + container = (Container) Globals.applet; + } */ + + final boolean screenRemRef; + if(null == screen) { + screen = NewtFactory.createScreen(NewtFactory.createDisplay(null), 0); + screen.addReference(); // trigger native creation + screenRemRef = true; + } else { + screenRemRef = false; + } + + if (!VID.GetModeInfo(newDim, mode)) { + VID.Printf(Defines.PRINT_ALL, " invalid mode\n"); + return Base.rserr_invalid_mode; + } + + VID.Printf(Defines.PRINT_ALL, " " + newDim.getWidth() + " " + newDim.getHeight() + '\n'); + + if (!Globals.appletMode) { + // destroy the existing window + if (window != null) shutdown(); + } + + if(null == window) { + window = GLWindow.create(screen, new GLCapabilities(GLProfile.get(GLProfile.GL2))); + window.setTitle("Jake2 (jogl-gl2-newt)"); + } + + if (oldDisplayMode == null) { + oldDisplayMode = window.getScreen().getCurrentScreenMode(); + } + + window.addWindowListener(new WindowAdapter() { + public void windowDestroyNotify(WindowEvent e) { + if (!Globals.appletMode) { + Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit"); + } + } + + public void windowResized(WindowEvent e) { + int width = window.getWidth(); + int height = window.getHeight(); + final int mask = ~0x03; + if ((width & 0x03) != 0) { + width &= mask; + width += 4; + } + + Base.setVid(width, height); + // let the sound and input subsystems know about the new window + VID.NewWindow(width, height); + } + }); + + if (Globals.appletMode) { + // Destroy the previous display if there is one + shutdown(); + + // We don't support full-screen mode + fullscreen = false; + } + + // We need to feed the NEWT Window to the NEWTKBD + NEWTKBD.Init(window); + + window.setSize(newDim.getWidth(), newDim.getHeight()); + + // D I F F E R E N T J A K E 2 E V E N T P R O C E S S I N G + window.addWindowListener(NEWTKBD.listener); + window.addKeyListener(NEWTKBD.listener); + window.addMouseListener(NEWTKBD.listener); + + if (fullscreen) { + window.setFullscreen(true); + + ScreenMode sm = findDisplayMode(newDim); + final DimensionImmutable smDim = sm.getMonitorMode().getSurfaceSize().getResolution(); + newDim.setWidth( smDim.getWidth() ); + newDim.setHeight( smDim.getHeight() ); + window.getScreen().setCurrentScreenMode(sm); + window.setFullscreen(true); + window.setVisible(true); + + VID.Printf(Defines.PRINT_ALL, "...setting fullscreen " + sm.toString() + '\n'); + + } else { + if (!Globals.appletMode) { + window.setVisible(true); + } else { + // Notify the size listener about the change + final SizeChangeListener listener = Globals.sizeChangeListener; + if (listener != null) { + listener.sizeChanged(newDim.getWidth(), newDim.getHeight()); + } + } + } + + if (!Globals.appletMode) { + while ( !window.isNativeValid()|| !window.isRealized() ) { + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + } + } + window.requestFocus(); + window.display(); // force GL resource validation + + if(screenRemRef) { + screen.removeReference(); + } + + fpsCounter.setUpdateFPSFrames(5*60, System.err); + + return Base.rserr_ok; + } + + void shutdown() { + if (!Globals.appletMode) { + if ( null != window ) { + window.destroy(); + } + } else { + if ( null != window ) { + window.destroy(); // same thing + } + } + window = null; + } + +} |