Java OpenGL版本选择程序

时间:2014-01-21 15:58:01

标签: java opengl lwjgl

我对我应该告诉LWJGL使用的OpenGL版本有点困惑。我有一个GPU支持4.4并且正在为此编码,但是我也想在一台支持3.3的GPU的旧PC上偶尔测试它。

实际上我对OpenGL版本中的几乎所有内容感到困惑,这是我现在使用的代码:

abstract public class Controller {
    //...
    private final List<Context> contextList = new ArrayList<>();
    //...

    private void initConstructor() {
        if (debug) {
            this.debugger = new Debugger();
            this.debuggerThread = new Thread(this.debugger);
            this.debuggerThread.start();
        }

        contextList.add(new Context(4, 4, true, true));
        contextList.add(new Context(4, 3, true, true));
        contextList.add(new Context(4, 2, true, true));
        contextList.add(new Context(4, 1, true, true));
        contextList.add(new Context(4, 0, true, true));
        contextList.add(new Context(3, 3, true, true));
        contextList.add(new Context(3, 2, true, true));
        contextList.add(new Context(3, 1, true, true));
        contextList.add(new Context(3, 0, true, true));
        contextList.add(new Context(2, 1, false, false));
        contextList.add(new Context(2, 0, false, false));
    }

    public void start() {
        LWJGLException exception = null;
        Context currentContext = contextList.get(0);
        try {
            for (Context context : contextList) {
                DisplayMode[] displayModes = Display.getAvailableDisplayModes();
                DisplayMode chosenDisplayMode = null;
                DisplayMode desktopDisplayMode = Display.getDesktopDisplayMode();
                for (DisplayMode displayMode : displayModes) {
                    if (desktopDisplayMode.getWidth() == displayMode.getWidth() && 
                            desktopDisplayMode.getHeight() == displayMode.getHeight() && 
                            desktopDisplayMode.getBitsPerPixel() == displayMode.getBitsPerPixel() && 
                            desktopDisplayMode.getFrequency() == displayMode.getFrequency()) {
                        chosenDisplayMode = displayMode;
                    }
                }
                if (chosenDisplayMode == null) {
                    throw new RuntimeException("No display mode has been found for desktop display mode: " + desktopDisplayMode);
                }
                screenWidth = chosenDisplayMode.getWidth();
                screenHeight = chosenDisplayMode.getHeight();
                System.out.println("Chosen display mode: " + chosenDisplayMode);
                Display.setDisplayMode(chosenDisplayMode);
                Display.setFullscreen(true);
                Display.setResizable(false);
                Display.setTitle("3D Game");
                Display.setVSyncEnabled(true);
                ContextAttribs contextAttribs = new ContextAttribs(context.major, context.minor);
                if (context.useForwardCompatible) {
                    contextAttribs = contextAttribs.withForwardCompatible(context.useForwardCompatible);
                }
                if (context.useProfileCore) {
                    contextAttribs = contextAttribs.withProfileCore(context.useProfileCore);
                }
                Display.create(new PixelFormat(), contextAttribs);
                exception = null;
                break;
            }
        } catch (LWJGLException ex) {
            exception = ex;
        }

        if (exception != null) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, exception);
            System.err.println("Display wasn't initialized correctly.");
            Display.destroy();
            System.exit(1);
        }
        System.out.println("Started OpenGL with the following parameters:");
        System.out.println("Major version: " + currentContext.major);
        System.out.println("Minor version: " + currentContext.minor);
        System.out.println("Forward compatible: " + currentContext.useForwardCompatible);
        System.out.println("Profile Core: " + currentContext.useProfileCore);

        try {
            init();
            initSuccesful = true;

            while (!Display.isCloseRequested()) {
                renderChecks();
                renderImpl();
                inputChecks();
            }
        } catch (RuntimeException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            shutdownImpl();
        }
    }

    //...

    private class Context {
        private final int major;
        private final int minor;
        private final boolean useForwardCompatible;
        private final boolean useProfileCore;

        Context(final int major, final int minor, final boolean useForwardCompatible, final boolean useProfileCore) {
            this.major = major;
            this.minor = minor;
            this.useForwardCompatible = useForwardCompatible;
            this.useProfileCore = useProfileCore;
        }
    }

它导致(相当简单)程序在我的旧PC上运行,但遗憾的是它包括在每次在旧PC上创建显示器时闪烁5次屏幕。

另一个奇怪的事情(或多或少是一个奖励问题),就是当我在我的电脑上启动它时,它很快就会闪烁一次屏幕(用黑屏闪烁),然后正常,然后切换到全屏。

0 个答案:

没有答案