Version selection

From LWJGL
Jump to: navigation, search

Contents

Introduction

OpenGL is being used under many versions. At the time of writing we are currently at version 4.2. During the development a decision was made to drop support for legacy versions of OpenGL (1.X, 2.X) because they relied more on a fixed-pipeline. Instead the newer versions would use a programmable pipeline which would increase performance and forward compatibility with the current and next generation of GPU’s.

If you are completely new to OpenGL it is best to learn version 3.X or even 4.X instead of the older ones. Say we want to develop OpenGL in a specific "context" (meaning under a pre-defined version/API and set of specifications) we can do this by selecting the version which our Display should use.

Display.create()

To do this we can provide 2 extra configuration objects to our Display.create() method:

  • PixelFormat: This object controls the pixel configuration, here we can configure how many bits a pixel is made out of for instance.
  • ContextAttribs: This object controls the OpenGL version that will be used.

When setting the version we can also set other attributes such as selecting a "profile" and whether it should be forward or backward compatible. The compatibility flag was introduced in the 3.0 API, a number of items were flagged as "deprecated" and would be removed in a later version. So when using a "forward compatible" context any functionality flagged as deprecated will be removed.

The profiles were introduced in version 3.2. There are 2 versions: core and compatibility. Core is the new standard API while "compatibility" re-introduced the deprecated functionality. Settings both the forward compatibility and the core profile would do the same thing twice (with a few exceptions) but if you want to use OpenGL the way they want you to use it I suggest you use these configurations.

Also see: [1]

Code example

Now to actually do this in code, first we will see what the standard OpenGL version is. We let the Display object manage the context creation for us and see what version we get with the following line of code:

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

On my system the result was: OpenGL version: 4.2.0

Because I have access to version 4.2, I'll select a lower version to use namely 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);
}

Checking for the version in use: 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)

Personal tools
Namespaces

Variants
Actions
Navigation
Tools