【问题标题】:CCRenderTexture,GL11ExtensionPack,Libgdx How TOCCRenderTexture,GL11ExtensionPack,Libgdx 如何
【发布时间】:2012-11-12 03:17:30
【问题描述】:

我目前正在制作诸如“小翅膀”http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture 之类的效果,发现 CCRenderTexture 是解决方案。所以我想知道如何在android上制作这个效果,最后我找到了这个链接 https://github.com/ZhouWeikuan/cocos2d/blob/master/cocos2d-android/src/org/cocos2d/opengl/CCRenderTexture.java 它表明它的 GL11ExtensionPack

GL11ExtensionPack egl = (GL11ExtensionPack)CCDirector.gl;
        egl.glGetIntegerv(GL11ExtensionPack.GL_FRAMEBUFFER_BINDING_OES, oldFBO_, 0);
...

但在 GLWrapperBase.java 中,它显示

// Unsupported GL11ExtensionPack methods
public void glBindFramebufferOES (int target, int framebuffer) {
        throw new UnsupportedOperationException();
}

好像gdx还没有实现这个功能。我想知道libgdx有什么相同的功能或者如何在桌面使用GL11ExtensionPack~ 谢谢

【问题讨论】:

    标签: android opengl-es libgdx


    【解决方案1】:

    在 libGDX 中,您想使用 FrameBuffer 对象来执行相当于“CCRenderTexture”的操作。 FrameBuffer 基本上允许您使用 OpenGL 命令绘制到屏幕外缓冲区,然后您可以稍后将该缓冲区的内容显示为纹理。见http://code.google.com/p/libgdx/wiki/OpenGLFramebufferObject。请注意,FrameBuffer 对象仅在您的应用需要 OpenGL ES 2.0 时可用。

    根据您要绘制的内容,您还可以查看 libGDX 中的 Pixmap 类。这支持一些简单的运行时绘图操作(如线条、矩形和像素)。同样的想法是您绘制到此纹理中,然后稍后在屏幕上渲染生成的纹理。这在 OpenGL ES 1.0 中也可用。

    FrameBuffer 和 Pixmap 应该可以在 Android 和桌面上正常工作(我相信 GWT 和 iOS 也是如此......)

    当您的应用暂时失去焦点(OpenGL 上下文丢失导致一些纹理内容消失)时,请注意了解 Android 上会发生什么。

    【讨论】:

      【解决方案2】:
          Question   :   CCRenderTexture,GL11ExtensionPack,Libgdx How TO
      interpreted as :   In libgdx, how to create dynamic texture.
          Answer     :   Use a private render function to draw in a private frame
          Example framework:
          ==================
          package com.badlogic.gdx.tests.bullet;
      
          /**
          Question   :   CCRenderTexture,GL11ExtensionPack,Libgdx How TO
      interpreted as :   In libgdx, how to create dynamic texture?
          Answer     :   Use a private render function to draw in a private frame buffer
                      convert the frame bufder to Pixmap, create Texture.
          Author  :   Jon Goodwin
          **/
      
          import com.badlogic.gdx.graphics.Texture;
          import com.badlogic.gdx.graphics.Pixmap;
          ...//(ctrl-shift-o) to auto-load imports in Eclipse
      
      
          public class BaseBulletTest extends BulletTest
          {
          //class variables
          =================
          public Texture           texture     = null;//create this
          public Array<Disposable> disposables = new Array<Disposable>();
          public Pixmap            pm          = null;
          //---------------------------
              @Override
              public void create ()
              {
                  init();
              }
          //---------------------------
              public static void init ()
              {
                  if(texture == null) texture(Color.BLUE, Color.WHITE);
                  TextureAttribute ta_tex     = TextureAttribute.createDiffuse(texture);
                  final Material material_box = new Material(ta_tex, ColorAttribute.createSpecular(1, 1, 1, 1),
                                                             FloatAttribute.createShininess(8f));
                  final long attributes1      = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
                  final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, material_box, attributes1);
                  ...
              }
          //---------------------------
              public Texture texture(Color fg_color, Color bg_color)
              {
                  Pixmap pm = render( fg_color, bg_color );
                  texture = new Texture(pm);//***here's your new dynamic texture***
                  disposables.add(texture);//store the texture
              }
          //---------------------------
              public Pixmap render(Color fg_color, Color bg_color)
              {
                  int width = Gdx.graphics.getWidth();
                  int height = Gdx.graphics.getHeight();
      
                  SpriteBatch spriteBatch = new SpriteBatch();
      
                  m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
                  m_fbo.begin();
                  Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
                  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                  Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());
                  spriteBatch.setProjectionMatrix(normalProjection);
      
                  spriteBatch.begin();
                  spriteBatch.setColor(fg_color);
                  //do some drawing ***here's where you draw your dynamic texture***
                  ...
                  spriteBatch.end();//finish write to buffer
      
                  pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap
      
                  m_fbo.end();
          //      pm.dispose();
          //      flipped.dispose();
          //      tx.dispose();
                  m_fbo.dispose();
                  m_fbo = null;
                  spriteBatch.dispose();
          //      return texture;
                  return pm;
              }
          //---------------------------
          }//class BaseBulletTest
          //---------------------------
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        相关资源
        最近更新 更多