Multiple shared shaders

Started by lawless_c, May 15, 2014, 10:56:02 PM

Previous topic - Next topic

lawless_c

Is there a way to quickly copy or share GLSLShader objects. I incorrectly assumed that using one shader among many objects would be ok but ive only just copped it that you need a shader for every instance, otherwise the uniforms will be set to those of the very last item updated before rendering.

EgonOlsen

You can use one shader instance for multiple objects. In fact, you should. I tend to solve the uniform problem this way:


private static class GrassShader extends GLSLShader implements IRenderHook {

private float trans = 0;

public GrassShader(String vertexShaderSource, String fragmentShaderSource) {
super(vertexShaderSource, fragmentShaderSource);
}

@Override
public void afterRendering(int arg0) {
// TODO Auto-generated method stub
}

@Override
public void beforeRendering(int arg0) {
// TODO Auto-generated method stub
}

@Override
public void onDispose() {
// TODO Auto-generated method stub
}

@Override
public boolean repeatRendering() {
// TODO Auto-generated method stub
return false;
}

@Override
public void setCurrentObject3D(Object3D obj) {
this.setUniform("alpha", trans);
this.setUniform("random", Ticker.getShaderTime());
}

@Override
public void setTransparency(float val) {
trans = val;
}

@Override
public void setCurrentShader(GLSLShader arg0) {
// TODO Auto-generated method stub

}
}

lawless_c

How exactly is this used?

hmm http://www.jpct.net/wiki/index.php/Shaders looking at the example here it may not work for me.
Would their be a way to call the uniforms i need to set before each rendering?
The only way i can think of doing this for now is to add some sort of list/vector to the class that holds the set uniforms for each object as they are updated.
and gets called and iterated through during the rendering.

EgonOlsen

Well, the example above is one way to solve this. You create a instance of that class and assign that instance to all objects in question as shader AND as a render hook (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#setRenderHook(com.threed.jpct.IRenderHook)). In the setCurrentObject() method of your implementation, you can then set the parameters. If this approach doesn't fit exactly in your case, you can always modify it. But the basic idea is to use an IRenderHook implementation to the set uniform of a shared shader. Whether shader and render hook have to be same instance or not depends on your needs and taste.

lawless_c

oh yes  ;D ;D ;D ;D

For some silly reason i thought IrenderHook could only be implemented with via a shader object.

It works perfectly now with the IrenderHook implemented by entities themselves. (hopefully this wont bite in the ass later but it seems to work  ;D)