Jeff Melton
Jeff Melton

Reputation: 357

GLSL Shaders not working on OSX Lion MacBook Pro

I have a MacBook Pro running OSX Lion with a CoreI5 with Intel HD 3000 Graphics and I can not get a basic shader working in Xcode 4. I am familiar with OpenGL and GLSL on Windows but am new to the Mac. I added the GLSL code into the 'Cocoa on the Mac'. Here is the code:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    GLuint vshader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fshader = glCreateShader(GL_FRAGMENT_SHADER);


    const GLchar *vShaderSource = "void main() { \
    gl_Position = ftransform; }";
    glShaderSource(vshader, 1, &vShaderSource, NULL);
    glCompileShader(vshader);

    GLint good;
    glGetShaderiv(vshader, GL_COMPILE_STATUS, &good);
    if (good == GL_FALSE)
    {
        GLint logLength=0;
        char *logMessage=NULL;
        glGetShaderiv(vshader, GL_INFO_LOG_LENGTH, &logLength);
        logMessage = malloc(logLength);
        glGetShaderInfoLog(vshader, logLength, NULL, logMessage);
        NSString *errorString = [[NSString alloc] initWithUTF8String:(logMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
        free(logMessage);
        return;
    }


    const GLchar *fShaderSource = "void main() { \
    vec4 color = vec4(1.0, 0.0, 0.0, 1.0); \
    gl_FragColor = color; \
    }";
    glShaderSource(fshader, 1, &fShaderSource, NULL);
    glCompileShader(fshader);
    glGetShaderiv(fshader, GL_COMPILE_STATUS, &good);
    if (good == GL_FALSE)
    {
        GLint logLength=0;
        char *logMessage=NULL;
        glGetShaderiv(fshader, GL_INFO_LOG_LENGTH, &logLength);
        logMessage = malloc(logLength);
        glGetShaderInfoLog(fshader, logLength, NULL, logMessage);
        NSString *errorString = [[NSString alloc] initWithUTF8String:(logMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
        free(logMessage);
        return;
    }



    program = glCreateProgram();
    glAttachShader(program, vshader);
    glAttachShader(program, fshader);
    glLinkProgram(program);
    // Check link status
    glGetProgramiv(program, GL_LINK_STATUS, &good);
    if (good == GL_FALSE)
    {                       
        int logLength=0;
        char *logMessage=NULL;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
        logMessage = malloc(logLength);
        glGetProgramInfoLog(program, logLength, NULL, logMessage);      
        NSString *errorString = [[NSString alloc] initWithUTF8String:(logMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
        free(logMessage);
        return;
    }

    // Check validate status
    glValidateProgram(program);
    glGetProgramiv(program, GL_VALIDATE_STATUS, &good);
    if (good == GL_FALSE)
    {                       
        int logLength=0;
        char *logMessage=NULL;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
        logMessage = malloc(logLength);
        glGetProgramInfoLog(program, logLength, NULL, logMessage);          
        NSString *errorString = [[NSString alloc] initWithUTF8String:(logMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
        free(logMessage);
        return;
    }


    glUseProgram(program);

    GLenum glError = glGetError();
    if (glError)
    {
        char *errorMessage = gluErrorString(glError);
        NSString *errorString = [[NSString alloc] initWithUTF8String:(errorMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
    }


}

Here is the render loop:

    static void drawAnObject ()
{
    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.6, 0.0);
        glVertex3f( -0.2, -0.3, 0.0);
        glVertex3f(  0.2, -0.3 ,0.0);
    }
    glEnd();
}
-(void)drawRect:(NSRect)bounds
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    drawAnObject();
    glFlush();
    GLenum glError = glGetError();
    if (glError)
    {
        char *errorMessage = gluErrorString(glError);
        NSString *errorString = [[NSString alloc] initWithUTF8String:(errorMessage)];
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setMessageText:errorString];
        [alert runModal];
    }

}

Upvotes: 0

Views: 1027

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

This:

gl_Position = ftransform;

Should be:

gl_Position = ftransform();

You should also specify the GLSL version in each shader with #version 120, at the beginning of each shader source (in the first line).

Upvotes: 1

Related Questions