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

GLuint myProgram = glCreateProgram();

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);
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);
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


-Tobey

2 Replies

0
101 Sep 10, 2008 at 15:25
    GLuint myProgram = glCreateProgram();


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

Cheers,
- Wernaeh

0
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