开发者

- (BOOL)loadShaders method is not working for me

I am trying to make a graph using opengl es 2.0 from a view based app but - (BOOL)loadShaders is returning NO always so control is going in renderer1. any idea what is happening?

- (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
    glBind开发者_运维问答AttribLocation(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;
}


If vertShaderPathname is empty, that means the vertex shader source couldn't be found. In this case, make sure there the shader exists in your project, and is bundled as a resource.

To do this in xcode, go to the tree on the left, navigate to "Targets->(projname)->Copy Bundle Resources" and make sure you have a "Shader.vsh". If it appears in "Compile Sources" instead, then you need to drag it into "Copy Bundle Resources"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜