- (BOOL)loadShaders方法对我不起作用

时间:2011-02-01 09:08:03

标签: ios opengl-es nsbundle

我正在尝试使用来自基于视图的应用程序的opengl es 2.0制作图形,但是 - (BOOL)loadShaders返回NO总是因此控制在renderer1中进行。知道发生了什么事吗?

- (BOOL)loadShaders {
        //return YES;
    GLuint vertShader, fragShader;
    NSString *vertShaderPathname, *fragShaderPathname;

        // create shader program
    program = glCreateProgram();

        // create and compile vertex shader

    vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"];  // this is returning nil always.

    if (!compileShader(&vertShader, GL_VERTEX_SHADER, 1, vertShaderPathname)) {
        destroyShaders(vertShader, fragShader, program);
        return NO;
    }

        // create and compile fragment shader
    fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"];
    if (!compileShader(&fragShader, GL_FRAGMENT_SHADER, 1, fragShaderPathname)) {
        destroyShaders(vertShader, fragShader, program);
        return NO;
    }

        // attach vertex shader to program
    glAttachShader(program, vertShader);

        // attach fragment shader to program
    glAttachShader(program, fragShader);

        // bind attribute locations
        // this needs to be done prior to linking
    glBindAttribLocation(program, ATTRIB_VERTEX, "position");
    glBindAttribLocation(program, ATTRIB_COLOR, "color");

        // link program
    if (!linkProgram(program)) {
        destroyShaders(vertShader, fragShader, program);
        return NO;
    }

        // get uniform locations
    uniforms[UNIFORM_MODELVIEW_PROJECTION_MATRIX] = glGetUniformLocation(program, "modelViewProjectionMatrix");

        // release vertex and fragment shaders
    if (vertShader) {
        glDeleteShader(vertShader);
        vertShader = 0;
    }
    if (fragShader) {
        glDeleteShader(fragShader);
        fragShader = 0;
    }

    return YES;
}

1 个答案:

答案 0 :(得分:4)

如果vertShaderPathname为空,则表示无法找到顶点着色器源。在这种情况下,请确保项目中存在着色器,并捆绑为资源。

要在xcode中执行此操作,请转到左侧的树,导航到“Targets->(projname) - >复制捆绑资源”并确保您有“Shader.vsh”。如果它出现在“编译源代码”中,则需要将其拖到“复制包资源”