CCRenderTexture,GL11ExtensionPack,Libgdx How TO

走远了吗. 提交于 2019-12-12 10:16:45

问题


I currently work on a effect such as "Tiny Wings" http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture ,and find CCRenderTexture is the solution. So I want to know how to make this effect on android , finally I found this link https://github.com/ZhouWeikuan/cocos2d/blob/master/cocos2d-android/src/org/cocos2d/opengl/CCRenderTexture.java It shows that its GL11ExtensionPack

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

But in GLWrapperBase.java ,it shows

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

Seems gdx have'nt implement this function . I want to know what's the same feature of libgdx or how to use GL11ExtensionPack at desktop ~ Thanks


回答1:


In libGDX, you want to use a FrameBuffer object to do the equivalent of a "CCRenderTexture". The FrameBuffer basically lets you use OpenGL commands to draw into an off-screen buffer, and then you can display that buffer's contents as a texture later. See http://code.google.com/p/libgdx/wiki/OpenGLFramebufferObject. Note that the FrameBuffer object is only available if your app requires OpenGL ES 2.0.

Depending on what you want to draw, you might also look at the Pixmap class in libGDX. This supports some simple run-time drawing operations (like lines, rectangels, and pixels). Again the idea is that you draw into this texture and then render the resulting texture on-screen later. This is available in OpenGL ES 1.0, too.

Both FrameBuffer and Pixmap should work fine on Android and on the Desktop (and I believe on GWT and iOS, too..)

Be careful to understand what happens on Android when your app loses focus temporarily (OpenGL context loss causes some texture contents to disappear).




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


来源:https://stackoverflow.com/questions/13337936/ccrendertexture-gl11extensionpack-libgdx-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!