Search code examples
androidc++opengl-esshadow-mapping

Android NDK SDL2 OpenGL ES 2 shadow mapping (directional)- is it possible?


Trying to utilize directional shadow mapping on OpenGL. Need to generate a depth texture that isn't a renderbuffer (need to be able to read from it) it future passes. Keep getting GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT (as result of glCheckFramebufferStatus) when running on Android (don't get error when running on OSX).

Here's my code:

 //Shadow
  //gen tex
  gl_shadow_bogus_texture_active_n = 7;
  glActiveTexture(GL_TEXTURE0+gl_shadow_bogus_texture_active_n);
  glGenTextures(1, &gl_shadow_bogus_texture_buff_id);
  glBindTexture(GL_TEXTURE_2D, gl_shadow_bogus_texture_buff_id);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,tex_dim.x,tex_dim.y,0,GL_RGB,GL_UNSIGNED_BYTE,0);

  //gen depth
  gl_shadow_texture_active_n = 4;
  glActiveTexture(GL_TEXTURE0+gl_shadow_texture_active_n);
  glGenTextures(1, &gl_shadow_texture_buff_id);
  glBindTexture(GL_TEXTURE_2D, gl_shadow_texture_buff_id);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT16,1024,1024,0,GL_DEPTH_COMPONENT,GL_FLOAT,0);

  glGenFramebuffers(1, &gl_shadow_framebuffer_id); //gen fb
  glBindFramebuffer(GL_FRAMEBUFFER, gl_shadow_framebuffer_id); //bind fb
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl_shadow_bogus_texture_buff_id, 0); //attach tex
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, gl_shadow_texture_buff_id, 0); //attach depth

note that the whole gl_shadow_bogus_texture_buff_id thing (the generation and binding of the color texture) is just an attempt to fill out the framebuffer, thus getting rid of the error. I don't actually care about the color data.

a further note is that the "color tex gen/bind" is not necessary on mac, and can be replaced with glDrawBuffer(GL_NONE) and glReadBuffer(GL_NONE). But I can't compile with those in for android...


Solution

  • You do not have a stencil attachment, which is likely why you've getting a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT error. Although not explicitly stated in the spec, most drivers require a stencil attachment when a depth attachment is also present. However, depending on the implementation, you may run into problems, because using GL_DEPTH_COMPONENT16 with a stencil attachment may not be a supported combination (in fact, in my experience, it usually isn't). Each driver is only required to support at least one combination - unfortunately, there's no way to query exactly what the combination might be. You basically just have to guess and hope it succeeds.

    The majority of Android implementations support the packed depth stencil format DEPTH24_STENCIL8_OES, and the extension implicitly supports the depth being a texture which can be sampled. You will likely have considerably more success using it (but success is not guarateed!).