Question

I use Rajawali in android (3d opengl library).

I am trying to reload material (start to use different shaders dynamicly) - but when I set new material (at runtime) to the object it disappear (after on\off screen by power button this object it appears again with new material).

After setting new material I'm trying to reload objects' attributes (function reload), but it doesn't help (and if i call reload() on other objects - they dissappear too =) ).

Function reload() in materials (shaders class) is:

    public void reload() {
        setShaders(mUntouchedVertexShader, mUntouchedFragmentShader);

        for(int i=0; i<mNumTextures; i++) {
            addTexture(mTextureInfoList.get(i), true, true);
        }
    }

I can't understand why they disappear and why they appear again after restart with new shaders.

Was it helpful?

Solution

The problem is that you're trying to add an object in the middle of a render cycle. When the material tries to create the shader an error is returned.

Work is currently being done to the API to make this more user-friendly. In the meantime you can do something similar to this:

private boolean mSetShaders;

public void onTouch() {
    // -- boolean that indicates that an object should be added onDrawFrame()
    mSetShaders = true;
}

public void onDrawFrame(GL10 glUnused) {
    super.onDrawFrame(glUnused);
    if(mSetShaders) {
        // ... set your shaders here ...
        mSetShaders = false;
    }
}

This example is for onTouch() events but it shouldn't be a big effort to rewrite it to suit your needs. Basically you just queue the set shader event until the next render cycle happens.

Not the prettiest solution but it'll do until the next framework update ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top