Usage of shared GLSLShader

Started by Wojtek, August 20, 2011, 10:39:26 PM

Previous topic - Next topic

Wojtek

Hello,

I am wondering how to use one instance of GLSLShader with multiple objects.

I am pointing to the situation when my shader is using additional uniform value, and each object where shader will be applied have different value for that uniform.

What I read in docs:
* I can call setUniform() to pass value during runtime
* I can call Object3D.setRenderHook() to assign shader to object.
* I can overwrite beforeRendering() method to pass my uniforms

My question is how to detect which obejct3d my shared is currently rendering?

Thanks for help,
Wojtek

EgonOlsen

You can't...i should pass this information to the GLSLShader-class, i guess. For now, just use different shaders if the uniforms differs per object.

Wojtek

I think I have managed that problem.
Basically what I am doing is to:
* create one GLSLShader instance so vertex/fragment shaders code is compiled once
* create one instance of proxy IRenderHook anonymous class per Object3D and delegates all calls to my one GLSLShader plus add additional setUniform() calls to beforeRendering() method.

The code looks more or less like that:

public void apply(final Object3D obj)
{
final GLSLShader su = getShader();

IRenderHook hook = new IRenderHook()
{
@Override
public boolean repeatRendering()
{
return su.repeatRendering();
}

@Override
public void onDispose()
{
su.onDispose();
}

@Override
public void clear()
{
su.clear();
}

@Override
public void beforeRendering(int arg)
{
su.setUniform(...);
su.beforeRendering(arg);
}

@Override
public void afterRendering(int arg)
{
su.afterRendering(arg);
}
};
obj.setRenderHook(hook);
}


The only thing I am wondering is when the clear() / onDispose() methods are called and if multiple calls of them will be not harming...

Egon,
If you think that this code is safe and usefull, perhaps it will be a good ideat to add it to JPCT?
As I understand the purpose of uniform values in GLSL shaders is to allow sharing the same shader between different objects.

Thanks,
Wojtek

EgonOlsen

Once you've called onDispose(), following calls to onDispose() or clear() won't do anything.

About the proxy solution...i would rather add this to the GLSLShader class itself. In your case, the proxy is a good solution until it has been added to GLSLShader itself, which will most likely happen in the next version.