2 Shaders using the same vertex data

Posted by Fonix on Stack Overflow See other posts from Stack Overflow or by Fonix
Published on 2012-09-04T21:23:41Z Indexed on 2012/09/04 21:38 UTC
Read the original article Hit count: 192

Filed under:
|
|
|

So im having problems rendering using 2 different shaders. Im currently rendering shapes that represent dice, what i want is if the dice is selected by the user, it draws an outline by drawing the dice completely red and slightly scaled up, then render the proper dice over it. At the moment some of the dice, for some reason, render the wrong dice for the outline, but the right one for the proper foreground dice.

Im wondering if they aren't getting their vertex data mixed up somehow. Im not sure if doing something like this is even allowed in openGL:

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(GLfloat), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(effect->vertCoord);        
glVertexAttribPointer(effect->vertCoord, 3, GL_FLOAT, GL_FALSE, 0, 0);

glEnableVertexAttribArray(effect->toon_vertCoord);        
glVertexAttribPointer(effect->toon_vertCoord, 3, GL_FLOAT, GL_FALSE, 0, 0);

im trying to bind the vertex data to 2 different shaders here when i load my first shader i have:

vertCoord = glGetAttribLocation(TexAndLighting, "position");

and the other shader has:

toon_vertCoord = glGetAttribLocation(Toon, "position");

if I use the shaders independently of each other they work fine, but when i try to render both one on top of the other they get the model mixed up some times. here is how my draw function looks:

- (void) draw {
[EAGLContext setCurrentContext:context];

glBindVertexArrayOES(_vertexArray); 

effect->modelViewMatrix = mvm;
effect->numberColour = GLKVector4Make(numbers[colorSelected].r, numbers[colorSelected].g, numbers[colorSelected].b, 1);
effect->faceColour = GLKVector4Make(faceColors[colorSelected].r, faceColors[colorSelected].g, faceColors[colorSelected].b, 1);

if(selected){
    [effect drawOutline]; //this function prepares the shader
    glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
}

[effect prepareToDraw]; //same with this one
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
}

this is what it looks like, as you can see most of the outlines are using the wrong dice, or none at all: picture of dice

links to full code:
http://pastebin.com/yDKb3wrD Dice.mm //rendering stuff
http://pastebin.com/eBK0pzrK Effects.mm //shader stuff
http://pastebin.com/5LtDAk8J //my shaders, shouldn't be anything to do with them though

TL;DR: trying to use 2 different shaders that use the same vertex data, but its getting the models mixed up when rendering using both at the same time, well thats what i think is going wrong, quite stumped actually.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios