Search code examples
javaopengl-esviewportrender-to-texture

Problems rendering to texture - scaling issues(?)


I'm trying to get my rendering-to-texture working. So far, it does all the necessary GL gubbins to draw on the texture and everything - the only problem is its getting the scaling all off.

I figured I'd want to set the viewport to the size of the texture, and the gluOrtho2d (the way I'm going to be drawing onto the texture) as -halfwidth, halfwidth, -halfheight, halfheight. This means when drawing position 0,0 should be in the center. A position of halfwdith, halfheight should be in the top right corner etc etc.

I'm getting really weird effects though, it seems that its not drawing on the texture in the right scale - so everything gets skewed, can anyone suggest what I might be doing wrong?

Thanks

public void renderToTexture(GLRenderer glRenderer, GL10 gl)
  {
    boolean checkIfContextSupportsExtension = checkIfContextSupportsExtension(gl, "GL_OES_framebuffer_object");
    if(checkIfContextSupportsExtension)
    {
      GL11ExtensionPack gl11ep = (GL11ExtensionPack) gl;

      int mFrameBuffer = createFrameBuffer(gl,texture.getWidth(), texture.getHeight(), texture.getGLID());
      if (mFrameBuffer == -1)
      {
        return;
      }
      gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, mFrameBuffer);

      int halfWidth = texture.getWidth()/2;//width/2;
      int halfHeight = texture.getHeight()/2;//height/2;

      gl.glViewport(0,0,texture.getWidth(), texture.getHeight());
      gl.glLoadIdentity();

      GLU.gluOrtho2D(gl, -halfWidth, halfWidth , -halfHeight, halfHeight);

      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();

      gl.glClearColor(0f, 1f, 0f, 1f);
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

      //draw the old version of the texture to framebuffer:
      Quad quad = new Quad(texture.getWidth(), texture.getHeight());
      quad.setTexture(texture);

      SpriteRenderable sr = new SpriteRenderable(quad);
      sr.renderTo(glRenderer, gl, 1);

      //draw the new gl objects etc to framebuffer
      for (Renderable renderable : renderThese)
      {
        if (renderable.isVisible()) {
          renderable.renderTo(glRenderer, gl, 1);
        }
      }

      //default to the old framebuffer
      gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, 0);
    }
  }

images:

This one is the game prior to any texture rendering.

image1

This is after the "blood splats" (currently pigs!) are rendered on the "arena" background texture shown in picture 1. Notice that the original texture has shrunk too small to see in the middle (its a few pixles) and the pig "blood splat" jumps in a zig-zag, flipping over the center of the texture and becoming smaller...

image2

(sorry, dont have enough rep to post images in the post!)


Solution

  • Just a speculative guess, but do you remember to set matrixMode to GL_PROJECTION prior to entering renderToTexture function? It's not set inside the function, where it seems like it should be. Also don't forget to restore projection matrix and viewport at the end of the function.