【LWJGL2 WIKI】【现代OpenGL篇】版本选择

原文:http://wiki.lwjgl.org/wiki/Version_selection

Introduction 介绍

OpenGL有许多版本可以用。写教程的时候,最新版是4.2,开发期间决定逐渐弃用传统OpenGL(1.X和2.X版),因为它们依赖于固定管线。新版使用的是可编程管线,性能更好,对目前和未来的GPU来说,向前兼容性更好。
如果你刚开始接触OpenGL,最好就学3.X或4.X版本,不用学旧版了。也就是说,开发以特定的“上下文”来开发OpenGL(即在预定义版本/API和规格集下)。我们可以给我们的Display选择一个版本来区分此。

Display.create()

为此,我们提供了两个设置对象在Display.create()方法里:
- PixelFormat: 此对象控制象素配置,比如可以指定每象素由几位组成。
- ContextAttribs: 此对象控制使用的OpenGL版本。
设定版本时,我们也可以设定其他的参数,比如选择一个profile以及是否需要向前向后兼容。兼容性Flag在3.0的API里被引入,一些项目已被标记为“弃用”,之后的版本可能会被移除。所以当使用向前兼容上下文时,一切标记弃用的功能都将被移除。
profile概念在3.2版里被引入,它有两个版本:核心和兼容性。核心是指新标准的API,兼容性则用来重新引入被弃用的功能。设置向前兼容和核心profile将做两次相同的事情,但是如果你想以正常的姿势使用OpenGL,我建议就这样配置。
也可以见:http://www.opengl.org/wiki/Core_And_Compatibility_in_Contexts

Code example 代码范例

现在写代码,首先看看标准OpenGL是怎样的。让Display对象管理上下文的创建,然后看看用下面的代码会得到哪个版本。

System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));

在我的系统里,返回结果是: OpenGL version: 4.2.0
因为我可以访问到4.2,所以我选择了一个低版本3.2。

PixelFormat pixelFormat = new PixelFormat();
ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
    .withForwardCompatible(true)
    .withProfileCore(true);

try {
    Display.setDisplayMode(new DisplayMode(320, 240));
    Display.setTitle("Version selection");
    Display.create(pixelFormat, contextAtrributes);
} catch (LWJGLException e) {
    e.printStackTrace();
    System.exit(-1);
}

检查使用的版本,OpenGL version: 3.2.0

Complete source code 完整源代码

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

public class VersionSelection {
    public static void main(String[] args) {
        new VersionSelection();
    }

    public VersionSelection() {
        boolean select = true;

        if (select) this.specifiedCreate();
        else this.normalCreate();

        System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));

        while (!Display.isCloseRequested()) {
            Display.sync(60);
            Display.update();
        }

        Display.destroy();
    }

    // Let Display manage our OpenGL version
    private void normalCreate() {      
        try {
            Display.setDisplayMode(new DisplayMode(320, 240));
            Display.setTitle("Version selection");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    // Use a specified version of OpenGL - namely version 3.2
    private void specifiedCreate() {
        PixelFormat pixelFormat = new PixelFormat();
        ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
            .withForwardCompatible(true)
            .withProfileCore(true);

        try {
            Display.setDisplayMode(new DisplayMode(320, 240));
            Display.setTitle("Version selection");
            Display.create(pixelFormat, contextAtrributes);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

Credit

Mathias Verboven (moci)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值