glUseProgram weirdness

A77e71b962cd6c7c3b885f0488452f1f
0
tobeythorn 101 Sep 10, 2008 at 14:16

I’m trying to replace openGL fixed functionality with shaders. When I glUseProgram(myProgram) and then try to draw my vbo, nothing seems to be drawn. However, when I put a glUseProgram(0) after drawing, I get rendering via fixed functionality. This makes absolutely no sense to me. How can it even draw retroactively? I’m stumped for what is wrong. My shaders are copied from a “hello-world” tutorial and I’ve verified they work using OpenGlShaderBuilder program that comes with xcode. Does which pixel format I choose affect this? Here’s basically what my code looks like:

//prepare shader program
    ZFile* vsFile = [[ZFile alloc] initWithFile: vsName];
    ZFile* fsFile = [[ZFile alloc] initWithFile: fsName];
    
    const char * vs = [vsFile buffer];
    const char * fs = [fsFile buffer];
    
    //setup shaders
    GLuint myVertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(myVertexShader, 1, &vs, NULL);
    glCompileShader(myVertexShader);
    GLuint myFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(myFragmentShader, 1, &fs, NULL);
    glCompileShader(myFragmentShader);
    
    GLuint myProgram = glCreateProgram();
    glAttachShader(myProgram, myVertexShader);
    glAttachShader(myProgram, myVertexShader);
    glLinkProgram(myProgram);
    
    glUseProgram(myProgram);

//setup VBO
    // generate a new VBO and get the associated ID
    glGenBuffers(1, &vboId1);
    // bind VBO in order to use
    glBindBuffer(GL_ARRAY_BUFFER, vboId1);
    // upload data to VBO
    glBufferData(GL_ARRAY_BUFFER, 3*vCount*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
    // it is safe to delete after copying data to VBO
    // generate a new VBO and get the associated ID
    glGenBuffers(1, &vboId2);
    // bind VBO in order to use
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId2);
    // upload data to VBO
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*pCount*sizeof(GLuint), polygons, GL_STATIC_DRAW);
    [ leaf setVboIDv: vboId1 ];
    [ leaf setNumberOfVertices: vCount ];
    [ leaf setVboIDp: vboId2 ];
    [ leaf setNumberOfPolygons: pCount ];

//draw
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, [ leaf vboIDv ]);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, [ leaf vboIDp ]);
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
        glDisableClientState(GL_VERTEX_ARRAY);
        
        // bind with 0, so, switch back to normal pointer operation
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glUseProgram(0); //with out this line: black screen, with: fixed function rendering

I seriously appreciate your help,
-Tobey

2 Replies

Please log in or register to post a reply.

B7568a7d781a2ebebe3fa176215ae667
0
Wernaeh 101 Sep 10, 2008 at 15:25
    GLuint myProgram = glCreateProgram();
    glAttachShader(myProgram, myVertexShader);
    glAttachShader(myProgram, myVertexShader);
    glLinkProgram(myProgram);

Why do you attach the vertex shader twice ?
Shouldn’t the latter rather be like the fragment shader ?

Cheers,
- Wernaeh

A77e71b962cd6c7c3b885f0488452f1f
0
tobeythorn 101 Sep 10, 2008 at 15:34

Because I’m lexdystic! I reread that block of code so many times to make sure it was right but somehow didn’t notice the mistake! Thanks, and sorry for such stupidity. My shader now works, but glUseProgram(0) at the end still makes everything draw with fixed functionality, which doesn’t make any sense to me.

Thanks Wernaeh!

EDIT: Ok, I’ve noticed that it now draws the first frame with the shader, but subsequent glUseProgram(myProgram) calls seem to not work for some reason.

EDIT: Another dumb mistake, everything is working now