在Mac上启用GLSL 3.3

时间:2019-01-03 02:10:01

标签: c++ opengl glfw glew

我尝试使用GLSL 3.3,但系统指示支持的版本为1.2。

我尝试使用SDL_GL_SetAttribute设置主要版本和次要版本,但是glGetString(GL_SHADING_LANGUAGE_VERSION)仍然是1.2。

我不确定我的代码有什么问题。任何帮助将不胜感激!

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>

unsigned int CompileShader(unsigned int type, const std::string& source)
{
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result != GL_TRUE)
    {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "Fail to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<
            " shader!" << std::endl;
        printf("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
        std::cout << message << std::endl;
        return 0;
    }

    return id;
}


static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{...}


int main(void)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    // init window, context
    // balabala

    std::string vertexShader = 
        "#version 330 core\n"
        "\n"
        "layout (location = 0) in vec4 position;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   gl_position = position;\n"
        "}\n";

    std::string fragmentShader = 
        "#version 330 core\n"
        "\n"
        "layout (location = 0) out vec4 color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   color = vec4(1.0,0.0,0.0,1.0);\n"
        "}\n";


    unsigned int shader = CreateShader(vertexShader, fragmentShader);
    glUseProgram(shader);

    // Loop until the user closes the window
    // balabala

    SDL_Quit();
    return 0;
}

下面是输出的错误消息:

@"Fail to compile vertex shader!\r\n"
@"Supported GLSL version is 1.20.\r\n"
@"ERROR: 0:1: '' :  version '330' is not supported\r\n"
@"ERROR: 0:1: '' : syntax error: #version\r\n"
@"ERROR: 0:3: 'layout' : syntax error: syntax error\r\n"
@"\r\n"
@"Fail to compile fragment shader!\r\n"
@"Supported GLSL version is 1.20.\r\n"
@"ERROR: 0:1: '' :  version '330' is not supported\r\n"
@"ERROR: 0:1: '' : syntax error: #version\r\n"
@"ERROR: 0:3: 'layout' : syntax error: syntax error\r\n"
@"\r\n"

谢谢!

0 个答案:

没有答案