1

I have read some tutorials and trying to wrap my head around OpenGL ES 2.0. While trying to create a depth buffer, my JVM crashes. Btw I am using LibGDX framework.

  IntBuffer depthBuffer = BufferUtils.newIntBuffer(1);

  // AFAIK this puts 1 texture name into depthBuffer.
  Gdx.gl20.glGenTextures(1, depthBuffer);
  int depthBufferValue = depthBuffer.get();

  // I now bind the texture, so I can use it.
  Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_2D, depthBufferValue);

I have no idea what glTexImage2D does, I suppose It should generate depth texture.

Next line crashes the JVM

Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,  BufferUtils.newIntBuffer(1));

Next line causes a NullPointerException

I don't know what should I put as glTexImage2D last parameter. I have seen examples for iOS where they put NULL.

Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, null);

The rest of the code

  // This code should attach the depth texture into frame buffer
  IntBuffer depthFrameBuffer = BufferUtils.newIntBuffer(1);
  glGenFramebuffers(1, depthFrameBuffer);
  int depthFrameBufferValue = depthBuffer.get();
  glBindFramebuffer(GL_FRAMEBUFFER, depthFrameBufferValue);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFrameBufferValue, 0);
  // I dont know what should I call next or what type of shader should I use

Please point me the right direction, or whenever I did make a mistake in my assumptions.

Tutorial would be best, I didn't find much about shadowmaps in OpenGL ES 2.0

jellyfication
  • 1,595
  • 1
  • 16
  • 37
  • 1
    Err, actually I don't know if [this](https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ShadowMappingTest.java) might help you... – Stefan Hanke Jan 30 '13 at 09:25
  • That's great. I haven't found this one. – jellyfication Jan 30 '13 at 09:35
  • Google for `man glTexImage2D` and the first hit is: http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml it explains what that last parameter is for -- image data (and lots of it in your case). (`man` is the Unix/Linux manual pages command.) – P.T. Jan 30 '13 at 16:52
  • Well I looked into manual, I just did not get why they want to specify last parameter as `NULL` and not pass any data [here](http://stackoverflow.com/questions/13004616/how-to-efficiently-copy-depth-buffer-to-texture-on-opengl-es). Also, I don't know what is the equivalent in android/libgdx ? – jellyfication Jan 30 '13 at 17:33
  • 1
    Ah, seems you're correct. However, it looks like this was a known problem and was fixed (in 2011?): http://code.google.com/p/libgdx/issues/detail?id=538 As a work-around try creating an int buffer that will hold the texture (width*height). It should be harmless other than the wasted space? – P.T. Jan 31 '13 at 21:40
  • Strange, no way I had that old libs. I looked at what @StefanHanke posted and I'll stick to that. – jellyfication Feb 01 '13 at 08:43
  • @jellyfication: you are trying to use FBOs. Have a look at this [page](http://www.songho.ca/opengl/index.html). It's not Java oriented but you may easily translate the samples for Android. – Fabien R Feb 02 '13 at 11:25
  • Looks like this was only partially fixed in libGDX, and a patch has been submitted to fix it for GLES20 on libGDX: https://github.com/libgdx/libgdx/pull/228#issuecomment-13062354 – P.T. Feb 05 '13 at 17:17

1 Answers1

1

There was a bug in libGDX that required the glTexImage2D texture parameter to be non-null (implying there must always be some local texture data to upload). It was fixed with this change: https://github.com/libgdx/libgdx/pull/228. This will be part of the release after 0.9.8 (most likely the 0.9.9 release).

P.T.
  • 24,557
  • 7
  • 64
  • 95