samedi 27 juin 2015

GLSL using same layout locations with multiple programs

I can render with multiple programs with the following code:

snippet from near end of main loop in main.cpp

. . .

glUseProgram( programID1 );

glEnableVertexAttribArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[0] );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 20, (void*)0 );
    //only needed XY coordinates for the simple 2D I'm making
glEnableVertexAttribArray( 1 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[0] );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8 );
    //the third 'UV' coordinate chooses a tile from my 2d texture array
glDrawArrays( GL_TRIANGLES, 0, sizeof( vertuvbuffer0_data ) / 5 );
    //every vertex takes 5 floats, so to get total number I divide by 5
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );

glUseProgram( programID2 );
glEnableVertexAttribArray( 2 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[1] );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 20, (void*)0 );
glEnableVertexAttribArray( 3 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[1] );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8 );
glDrawArrays( GL_TRIANGLES, 0, sizeof( vertuvbuffer1_data ) / 5 );
...
glDisableVertexAttribArray( 2 );
glDisableVertexAttribArray( 3 );

. . .

This is obviously with separate layouts inside the different shaders, what I'm having trouble doing is trying to do this:

. . .

glUseProgram( programID1 );

glEnableVertexAttribArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[0] );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 20, (void*)0 );
glEnableVertexAttribArray( 1 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[0] );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8 );
glDrawArrays( GL_TRIANGLES, 0, sizeof( vertuvbuffer0_data ) / 5 );
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );

glUseProgram( programID2 );
glEnableVertexAttribArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[1] );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 20, (void*)0 );
glEnableVertexAttribArray( 1 );
glBindBuffer( GL_ARRAY_BUFFER, vertuvbuffer[1] );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8 );
glDrawArrays( GL_TRIANGLES, 0, sizeof( vertuvbuffer1_data ) / 5 );
...
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );

. . .

The vertex shaders have their respective layout locations.

I'm not sure about how to achieve something similar, if there's no way to do this I would have to write different vertex shaders for every object I want to draw, and I can't imagine that would be the case.

I hope this makes sense, so what can I possibly do at this point?

Aucun commentaire:

Enregistrer un commentaire