Author Topic: Multiple shared shaders  (Read 755 times)

Offline lawless_c

  • int
  • **
  • Posts: 91
    • View Profile
Multiple shared shaders
« on: May 15, 2014, 10:56:02 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11770
    • View Profile
    • http://www.jpct.net
Re: Multiple shared shaders
« Reply #1 on: May 16, 2014, 07:55:22 am »
You can use one shader instance for multiple objects. In fact, you should. I tend to solve the uniform problem this way:

Code: [Select]
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

}
}

Offline lawless_c

  • int
  • **
  • Posts: 91
    • View Profile
Re: Multiple shared shaders
« Reply #2 on: June 01, 2014, 04:22:51 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11770
    • View Profile
    • http://www.jpct.net
Re: Multiple shared shaders
« Reply #3 on: June 01, 2014, 09:04:50 pm »
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.

Offline lawless_c

  • int
  • **
  • Posts: 91
    • View Profile
Re: Multiple shared shaders
« Reply #4 on: June 03, 2014, 01:06:46 am »
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)