CCRenderTexture,GL11ExtensionPack,Libgdx如何操作

时间:2012-11-12 03:17:30

标签: android opengl-es libgdx

我目前正致力于“Tiny Wings”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 〜 感谢

2 个答案:

答案 0 :(得分:1)

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

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

FrameBuffer和Pixmap都可以在Android和桌面上正常工作(我也相信GWT和iOS ......)

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

答案 1 :(得分:0)

    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
    //---------------------------