GLSL Shaders无法使用OSX Lion MacBook Pro

时间:2012-03-26 16:10:00

标签: macos opengl glsl

我有一台运行OSX Lion的MacBook Pro,配备带有Intel HD 3000图形的CoreI5,我无法在Xcode 4中使用基本着色器。我熟悉Windows上的OpenGL和GLSL,但我是Mac的新手。我将GLSL代码添加到了Mac上的' Cocoa中。这是代码:

    - (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];
    }


}

这是渲染循环:

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

}

1 个答案:

答案 0 :(得分:1)

此:

gl_Position = ftransform;

应该是:

gl_Position = ftransform();

您还应在每个着色器源(第一行)的开头用#version 120指定每个着色器中的GLSL版本。