【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)

Java中使用LWJGL(Lightweight Java Game Library)库操作OpenGL进行三角形平移,首先你需要设置好OpenGL环境并加载必要的库。以下是一个基本步骤: 1. **初始化GL**: ```java import org.lwjgl.opengl.GL11; // ... public static void initGL() { GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glClearColor(0f, 0f, 0f, 1f); // 设置背景颜色 } ``` 2. **创建顶点数组对象(VAO)**: ```java VAO vao = glGenVertexArrays(); glBindVertexArray(vao); ``` 3. **准备顶点数据**: 通常会有一个包含顶点坐标、颜色和纹理坐标的`glm::vec3`数组。这里假设你已经有了一个`float[] vertices`。 4. **创建缓冲区对象(Buffer Object, BO)**: ```java int vertexBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); ByteBuffer bb = BufferUtils.createByteBuffer(vertices.length * 3 * Float.BYTES); bb.put(vertices); bb.flip(); // 数据从内存翻转到缓冲区 glBufferData(GL_ARRAY_BUFFER, bb, GL_STATIC_DRAW); ``` 5. **设置顶点属性**: ```java int vertexLocation = glGetAttribLocation(glProgram, "position"); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(vertexLocation); // 对于其他需要的属性如颜色、纹理坐标,重复以上过程 ``` 6. **平移变换**: 在绘制前,可以使用`glTranslatef(x, y, z)`函数进行模型空间平移,例如: ```java float translationX = 1.0f; // 要平移的距离 float translationY = 0.0f; float translationZ = 0.0f; GL11.glTranslatef(translationX, translationY, translationZ); ``` 7. **绘制三角形**: ```java glDrawArrays(GL_TRIANGLES, 0, vertices.length / 3); // 根据实际顶点数绘制 ``` 8. **清理**: ```java glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); ``` 记得最后别忘了关闭资源: ```java GL11.glDeleteVertexArrays(vao); GL11.glDeleteBuffers(vertexBuffer); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值