1

我目前正在研究诸如“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 ~ 谢谢

4

2 回答 2

1

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

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

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

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

于 2012-11-12T15:23:04.050 回答
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
    //---------------------------
于 2016-06-25T08:02:33.477 回答