加载顶点着色器时出错

时间:2013-01-25 17:19:44

标签: macos opengl graphics xcode4.5 shader

我正在OpenGl和计算机图形学的第一步。 我发现this book并且我试图遵循示例和理论,但是当我尝试编译项目时出现以下错误

vertex.glsl failed to compile:
ERROR: 0:1: '' :  version '150' is not supported

顶点文件包含:

#version 150

varying vec4 vPosition;

void main(){
    gl_Position = vPosition;
}

我正在尝试执行的代码是:

#include "Angel.h"
#include <iostream>

const int numPoints = 5000;
typedef vec2 point2;

void init(){

    point2 points[numPoints];
    point2 vertices[3] = {
        point2(-1.0, -1.0), point2(0.0, 1.0), point2(1.0, -1.0)
    };

    points[0] = point2(0.25, 0.5);

    for (int k = 1; k < numPoints; k++) {
        int j = rand()%3;
        points[k] = (points[k-1]+vertices[j])/2.0;
    }

    GLuint program = InitShader("vertex.glsl", "fragment.glsl");
    glUseProgram(program);

    GLuint abuffer;
    glGenVertexArraysAPPLE(1, &abuffer);
    glBindVertexArrayAPPLE(abuffer);

    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

    GLuint location = glGetAttribLocation(program, "vPosition");
    glEnableVertexAttribArray(location);
    glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

    glClearColor(1.0, 1.0, 1.0, 1.0);

}


void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_POINTS, 0, numPoints);
    glFlush();
}

int main(int argc, char** argv){

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA);

    glutInitWindowSize(640, 480);

    glutCreateWindow("Sierpinski Gasket");

    init();

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;
}

我发现问题出在函数InitShader("vertex.glsl", "fragment.glsl");中,这就是:

#include "Angel.h"

namespace Angel {

    // Create a NULL-terminated string by reading the provided file
    static char*
    readShaderSource(const char* shaderFile)
    {
        FILE* fp = fopen(shaderFile, "r");

        if ( fp == NULL ) { return NULL; }

        fseek(fp, 0L, SEEK_END);
        long size = ftell(fp);

        fseek(fp, 0L, SEEK_SET);
        char* buf = new char[size + 1];
        fread(buf, 1, size, fp);

        buf[size] = '\0';
        fclose(fp);

        return buf;
    }


    // Create a GLSL program object from vertex and fragment shader files
    GLuint
    InitShader(const char* vShaderFile, const char* fShaderFile)
    {
        struct Shader {
            const char*  filename;
            GLenum       type;
            GLchar*      source;
        }  shaders[2] = {
            { vShaderFile, GL_VERTEX_SHADER, NULL },
            { fShaderFile, GL_FRAGMENT_SHADER, NULL }
        };

        GLuint program = glCreateProgram();

        for ( int i = 0; i < 2; ++i ) {
            Shader& s = shaders[i];
            s.source = readShaderSource( s.filename );
            if ( shaders[i].source == NULL ) {
                std::cerr << "Failed to read " << s.filename << std::endl;
                exit( EXIT_FAILURE );
            }

            GLuint shader = glCreateShader( s.type );
            glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
            glCompileShader( shader );

            GLint  compiled;
            glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
            if ( !compiled ) {
                std::cerr << s.filename << " failed to compile:" << std::endl;
                GLint  logSize;
                glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
                char* logMsg = new char[logSize];
                glGetShaderInfoLog( shader, logSize, NULL, logMsg );
                std::cerr << logMsg << std::endl;
                delete [] logMsg;

                exit( EXIT_FAILURE );
            }

            delete [] s.source;

            glAttachShader( program, shader );
        }

        /* link  and error check */
        glLinkProgram(program);

        GLint  linked;
        glGetProgramiv( program, GL_LINK_STATUS, &linked );
        if ( !linked ) {
            std::cerr << "Shader program failed to link" << std::endl;
            GLint  logSize;
            glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
            char* logMsg = new char[logSize];
            glGetProgramInfoLog( program, logSize, NULL, logMsg );
            std::cerr << logMsg << std::endl;
            delete [] logMsg;

            exit( EXIT_FAILURE );
        }

        /* use program object */
        glUseProgram(program);

        return program;
    }

}  // Close namespace Angel block

整个源代码可以在本书的链接中找到。 这个问题是什么?我对着色器一无所知,我只是想让这个应用程序工作。 我在使用OS X 10.8,Xcode 4.5

的Mac上执行此操作

2 个答案:

答案 0 :(得分:4)

仔细检查您的实施GL_VERSIONGL_SHADING_LANGUAGE_VERSION是否符合预期。

OSX可能不会/可能不会给您带有不合格glutInitDisplayMode(GLUT_RGBA)的GL 3.2核心上下文。在那里尝试slappin'a GLUT_3_2_CORE_PROFILE

glutInitDisplayMode(GLUT_RGBA | GLUT_3_2_CORE_PROFILE);

答案 1 :(得分:1)

如果不支持版本150,您要么使用非常旧的硬件,要么显卡驱动程序不是最新的。