Linking and compiling shaders for OpenGL 3.3 and 2.1 using LWJGL

徘徊边缘 提交于 2020-01-05 05:50:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19870053/linking-and-compiling-shaders-for-opengl-3-3-and-2-1-using-lwjgl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!