Linking and compiling shaders for OpenGL 3.3 and 2.1 using LWJGL

I am using LWJGL to create an OpenGL context. I can get it running on my machine (OpenGL 4.2 compatible) and with changes to the simple shaders also on OpenGL 2.1. I have to write code (the shaders, or rather the linking and compilation of them, seem to be the problem here) that is compatible with OpenGL 2.1. I assumed it would be easy to just write:

The Frag shader:

#version 120

in vec4 pass_Color;

out vec4 out_Color;

void main(void) {
    out_Color = pass_Color;
}

The Vert shader:

#version 120

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;

void main(void) {
    gl_Position = Projection * View * Model * in_Position;

    pass_Color = in_Color;
}

as the first line of my shaders and call it a day. However, on one machine with a OpenGL 3.3 supporting graphics card, those shaders would not link and compile. When changed to

#version 330
// - or even -
#version 150

they do link and compile. I have read about compatibility, core and forward compatibility and as I understand it, the problem could be that GLSL 1.20 is not supported on the OpenGL 3.3 machine unless it is instructed in the code to run a compatibility context (I may be phrasing it wrongly, please correct me). But I worry, since 2.1 does not know about OpenGL profiles, surely I will need to do some checks before attempting to setup the context with one.

Even if this is the case, I am not entirely sure how to do the correct logic to ensure that both machines with graphics cards limited to OpenGL 2.1 (and corresponding GLSL version) and machines with much later versions (3.3, 4.2, etc.) will accept it as well (using the LWJGL).

Please note that I would like to keep the code as "modern OpenGL" as possible.


Try specifying ogl version like this:

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

        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setTitle(WINDOW_TITLE);
        Display.create(pixelFormat, contextAtrributes);

...since macs by default support a maximum version of OpenGL 2.1. On OS X 10.7+ you can specifically request the core profile which will give you access to OpenGL 3.2 however unlike Windows and Linux you can't mix OpenGL 3+ features in the standard OpenGL profile.

From here.

链接地址: http://www.djcxy.com/p/38516.html

上一篇: 如何在Ubuntu上使用Mesa 10.1启用OpenGL 3.3

下一篇: 使用LWJGL为OpenGL 3.3和2.1链接和编译着色器