Error loading vertex shader

I am taking my first steps in OpenGl and computer graphics. I found this book and I am trying to follow examples and theory, but when I try to compile the project I got the following error

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

      

The vertex file has the following:

#version 150

varying vec4 vPosition;

void main(){
    gl_Position = vPosition;
}

      

The code I am trying to follow is:

#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;
}

      

I found that the problem is with the function InitShader("vertex.glsl", "fragment.glsl");

, which is this:

#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

      

All source code can be found in the book link. What is this problem about? I don't know anything about shaders and I'm just trying to get this app to work. I am doing this on Mac with OS X 10.8, Xcode 4.5

+3


source to share


2 answers


Double check that your implementation is GL_VERSION

and GL_SHADING_LANGUAGE_VERSION

is what you expect.

OSX cannot / cannot give you a basic GL 3.2 context with unqualified glutInitDisplayMode(GLUT_RGBA)

. Try slappin 'a GLUT_3_2_CORE_PROFILE

there:



glutInitDisplayMode(GLUT_RGBA | GLUT_3_2_CORE_PROFILE);

      

+4


source


If version 150 is not supported, you are either running on really old hardware or your graphics card drivers are not up to date.



+1


source







All Articles