0

我正在尝试让我的渲染到纹理工作。到目前为止,它完成了所有必要的 GL gubbins 来绘制纹理和所有内容 - 唯一的问题是它完全关闭了缩放。

我想我想将视口设置为纹理的大小,并将 gluOrtho2d(我将在纹理上绘制的方式)设置为 -halfwidth、halfwidth、-halfheight、halfheight。这意味着当绘制位置 0,0 时应该在中心。halfwdith,halfheight 的位置应该在右上角等。

我得到了非常奇怪的效果,似乎它没有以正确的比例绘制纹理 - 所以一切都被扭曲了,谁能建议我可能做错了什么?

谢谢

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);
    }
  }

图片:

这是在任何纹理渲染之前的游戏。

图像1

这是在图 1 中显示的“竞技场”背景纹理上渲染“血迹”(目前是猪!)之后。请注意,原始纹理已经缩小到中间看不到(它的几个像素)和猪“血溅”以之字形跳跃,翻转纹理的中心并变得更小......

图2

(抱歉,没有足够的代表在帖子中发布图片!)

4

1 回答 1

0

只是一个推测性的猜测,但你记得GL_PROJECTION在进入 renderToTexture 函数之前设置 matrixMode 吗?它没有设置在函数内部,它看起来应该是。也不要忘记在函数结束时恢复投影矩阵和视口。

于 2012-04-03T18:26:30.140 回答