Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question about switching shader programs (ES 2)

I'm trying to write a rather simple opengl es 2 program.

I have two different shaders, one renders a solid color rectangle to the screen, the other one renders a texture mapped rectangle.

I want to first draw the solid color rectangle, and then on top of it, draw the texture mapped rectangle.

    glUseProgram(program1);
  glUniform1f(shader1_uniforms[UNIFORM_SCREEN_WIDTH], (GLfloat)screenWidth);
  glUniform1f(shader1_uniforms[UNIFORM_SCREEN_HEIGHT], (GLfloat)screenHeight);

  glDisableVertexAttribArray(ATTRIB_TEX2);

  glVertexAttribPointer(ATTRIB_VERTEX2, 2, GL_FLOAT, 0, 0, squareVertices);
  glEnableVertexAttribArray(ATTRIB_VERTEX2);
  glVertexAttribPointer(ATTRIB_COLOR2, 4, GL_FLOAT, 1, 0, squareColors);
  glEnableVertexAttribArray(ATTRIB_COLOR2);


  glDrawArrays(GL_TRIANGLE_STRIP,0,4);


   glEnable(GL_BLEND);
   glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

   glBindTexture(GL_TEXTURE_2D,1);




  glUseProgram(program2);
  glUniform1f(shader2_uniforms[UNIFORM_SCREEN_WIDTH], (GLfloat)screenWidth );
  glUniform1f(shader2_uniforms[UNIFORM_SCREEN_HEIGHT], (GLfloat)screenHeight);
  glUniform1i(shader2_uniforms[UNIFORM_UIMAP], 0);



  glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
  glEnableVertexAttribArray(ATTRIB_VERTEX);
  glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, 1, 0, squareColors);
  glEnableVertexAttribArray(ATTRIB_COLOR);
  glVertexAttribPointer(ATTRIB_TEX, 2, GL_FLOAT, 0, 0, squareTexCoords);
  glEnableVertexAttribArray(ATTRIB_TEX);

  // Draw
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


   glDisable(GL_BLEND);

the two shaders work correctly, if executed separately (for example, comment the part using the first shader). but when i combine them together, like the above, the texture map will disappear. I spent quite awhile trying to figure out why...

can someone help me? thanks.

like image 878
haha Avatar asked Nov 15 '22 09:11

haha


1 Answers

You can't combine shaders that way, only the last program specified with glUseProgram will be used.

To combine shaders, you must do it manually generating a new shader which does both things at the same time.

like image 150
Dr. Snoopy Avatar answered Dec 19 '22 13:12

Dr. Snoopy