Program: 99123
Project: 99123
Buggy source:
private static Texture2D createTerrainNormalMap(Terrain terrain){
  int segment=Plane.getInstance().getSegment();
  IntBuffer normalmapTextureBuffer=ByteBuffer.allocateDirect(segment * segment * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
  int[] frameBuffer=new int[1];
  int[] depthRenderBuffer=new int[1];
  int[] renderTextureBuffer=new int[1];
  IntBuffer textureBuffer;
  glGenFramebuffers(1,frameBuffer,0);
  glGenRenderbuffers(1,depthRenderBuffer,0);
  glGenTextures(1,renderTextureBuffer,0);
  glBindTexture(GL_TEXTURE_2D,renderTextureBuffer[0]);
  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_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  int[] buf=new int[segment * segment];
  textureBuffer=ByteBuffer.allocateDirect(buf.length * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,segment,segment,0,GL_RGBA,GL_UNSIGNED_BYTE,textureBuffer);
  glGenerateMipmap(GL_TEXTURE_2D);
  glBindRenderbuffer(GL_RENDERBUFFER,depthRenderBuffer[0]);
  glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT16,segment,segment);
  glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,renderTextureBuffer[0],0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthRenderBuffer[0]);
  float[] projection=new float[16];
  float[] modelView=new float[16];
  int scale=terrain.getTerrainScale();
  int height=terrain.getTerrainMaxHeight();
  Matrix.orthoM(projection,0,0,scale,-scale,0,1,height * 2);
  Matrix.setLookAtM(modelView,0,0,height * 2,0,0,0,0,0,0,-1);
  glViewport(0,0,segment,segment);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  ShaderProgram sprogram=Shader.AMBIENT.getProgram(7);
  int program=sprogram.getProgramID();
  glUseProgram(program);
  int vertex_handle=glGetAttribLocation(program,"String_Node_Str");
  int modelview_handle=glGetUniformLocation(program,"String_Node_Str");
  int projection_handle=glGetUniformLocation(program,"String_Node_Str");
  int heightmap_handle=glGetUniformLocation(program,"String_Node_Str");
  int terrainData_handle=glGetUniformLocation(program,"String_Node_Str");
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D,terrain.getHeightmap().getTextureBuffer());
  glUniform1i(heightmap_handle,0);
  glUniform3f(terrainData_handle,height,segment,scale);
  glUniformMatrix4fv(modelview_handle,1,false,modelView,0);
  glUniformMatrix4fv(projection_handle,1,false,projection,0);
  glBindBuffer(GL_ARRAY_BUFFER,instance.terrainBuffers[0]);
  glEnableVertexAttribArray(vertex_handle);
  glVertexAttribPointer(vertex_handle,2,GL_FLOAT,false,0,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,instance.terrainBuffers[1]);
  glDrawElements(GL_TRIANGLES,Plane.getInstance().getIndicesCount(),GL_UNSIGNED_INT,0);
  glBindBuffer(GL_ARRAY_BUFFER,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  glDisableVertexAttribArray(vertex_handle);
  glBindFramebuffer(GL_FRAMEBUFFER,0);
  glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,renderTextureBuffer[0],0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthRenderBuffer[0]);
  glReadPixels(0,0,segment,segment,GL_RGBA,GL_UNSIGNED_BYTE,normalmapTextureBuffer);
  glBindFramebuffer(GL_FRAMEBUFFER,0);
  int[] pixels=new int[normalmapTextureBuffer.capacity()];
  for (int i=0; i < normalmapTextureBuffer.capacity(); i++) {
    pixels[i]=normalmapTextureBuffer.get(i);
  }
  Bitmap bitmap=Bitmap.createBitmap(pixels,segment,segment,Config.RGB_565);
  Texture2D normalmap=new Texture2D("String_Node_Str",renderTextureBuffer[0],pixels,segment,segment);
  String key="String_Node_Str" + terrain.hashCode();
  instance.texturesList.put(key,normalmap);
  instance.bitmapList.put(key,bitmap);
  return normalmap;
}

Fixed version (ground truth):
private static Texture2D createTerrainNormalMap(Terrain terrain){
  int segment=Plane.getInstance().getSegment();
  IntBuffer normalmapTextureBuffer=ByteBuffer.allocateDirect(segment * segment * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
  int[] frameBuffer=new int[1];
  int[] depthRenderBuffer=new int[1];
  int[] renderTextureBuffer=new int[1];
  IntBuffer textureBuffer;
  glGenFramebuffers(1,frameBuffer,0);
  glGenRenderbuffers(1,depthRenderBuffer,0);
  glGenTextures(1,renderTextureBuffer,0);
  glBindTexture(GL_TEXTURE_2D,renderTextureBuffer[0]);
  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_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  int[] buf=new int[segment * segment];
  textureBuffer=ByteBuffer.allocateDirect(buf.length * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,segment,segment,0,GL_RGBA,GL_UNSIGNED_BYTE,textureBuffer);
  glGenerateMipmap(GL_TEXTURE_2D);
  glBindRenderbuffer(GL_RENDERBUFFER,depthRenderBuffer[0]);
  glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT16,segment,segment);
  glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer[0]);
  glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,renderTextureBuffer[0],0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthRenderBuffer[0]);
  float[] projection=new float[16];
  float[] modelView=new float[16];
  float[] mvp=new float[16];
  int scale=terrain.getTerrainScale();
  int height=terrain.getTerrainMaxHeight();
  Matrix.orthoM(projection,0,0,segment,0,segment,1,10);
  Matrix.setLookAtM(modelView,0,0,0,1,0,0,0,0,1,0);
  Matrix.multiplyMM(mvp,0,projection,0,modelView,0);
  glViewport(0,0,segment,segment);
  glClearColor(0.3f,0.6f,0.9f,1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  ShaderProgram sprogram=Shader.AMBIENT.getProgram(7);
  int program=sprogram.getProgramID();
  glUseProgram(program);
  int vertex_handle=glGetAttribLocation(program,"String_Node_Str");
  int mvp_handle=glGetUniformLocation(program,"String_Node_Str");
  int heightmap_handle=glGetUniformLocation(program,"String_Node_Str");
  int terrainData_handle=glGetUniformLocation(program,"String_Node_Str");
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D,terrain.getHeightmap().getTextureBuffer());
  glUniform1i(heightmap_handle,0);
  glUniform3f(terrainData_handle,height,segment,scale);
  glUniformMatrix4fv(mvp_handle,1,false,mvp,0);
  glBindBuffer(GL_ARRAY_BUFFER,instance.terrainBuffers[0]);
  glEnableVertexAttribArray(vertex_handle);
  glVertexAttribPointer(vertex_handle,2,GL_FLOAT,false,0,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,instance.terrainBuffers[1]);
  glDrawElements(GL_TRIANGLES,Plane.getInstance().getIndicesCount(),GL_UNSIGNED_INT,0);
  glBindBuffer(GL_ARRAY_BUFFER,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  glDisableVertexAttribArray(vertex_handle);
  glBindFramebuffer(GL_FRAMEBUFFER,0);
  glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer[0]);
  glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,renderTextureBuffer[0],0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthRenderBuffer[0]);
  glReadPixels(0,0,segment,segment,GL_RGBA,GL_UNSIGNED_BYTE,normalmapTextureBuffer);
  glBindFramebuffer(GL_FRAMEBUFFER,0);
  int[] pixels=new int[normalmapTextureBuffer.capacity()];
  for (int i=0; i < normalmapTextureBuffer.capacity(); i++) {
    pixels[i]=normalmapTextureBuffer.get(i);
  }
  Bitmap bitmap=Bitmap.createBitmap(pixels,segment,segment,Config.RGB_565);
  Texture2D normalmap=new Texture2D("String_Node_Str",renderTextureBuffer[0],pixels,segment,segment);
  String key="String_Node_Str" + terrain.hashCode();
  instance.texturesList.put(key,normalmap);
  instance.bitmapList.put(key,bitmap);
  return normalmap;
}