由于 tve 和 what_nick 在此线程中的贡献,结果证明这相当简单:GLSL Shader support that renders corrects 。具体来说,我重用了 tve 贡献的 GLSL 类(在shader_support_051308.zip中)并修改了 what_nick 的示例代码。
我定义了一个 DecoratedLayer 类,它允许使用新的渲染策略“装饰”任何层,然后定义了一个 ShadingDecorator ,它在委托给底层层的render
方法之前设置 GLSL 着色器:
public class DecoratedLayer implements Layer {
private final Layer _layer;
private final I_LayerDecorator _decorator;
public DecoratedLayer(Layer layer, I_LayerDecorator decorator) {
_layer = layer;
_decorator = decorator;
}
@Override
public void preRender(DrawContext dc) {
_decorator.preRender(dc, _layer);
}
@Override
public void render(DrawContext dc) {
_decorator.render(dc, _layer);
}
// all other methods delegate to _layer
}
public class ShadingDecorator implements I_LayerDecorator {
private GLSL glsl;
private final File vertfile;
private final File fragfile;
public ShadingDecorator(final File vertexShaderFile,
final File fragmentShaderFile) {
vertfile = vertexShaderFile;
fragfile = fragmentShaderFile;
}
@Override
public void preRender(DrawContext dc, Layer layer) {
if (glsl == null) {
glsl = new GLSL(dc.getGL());
glsl.loadVertexShader(vertfile);
glsl.loadFragmentShader(fragfile);
}
layer.preRender(dc);
}
@Override
public void render(DrawContext dc, Layer layer) {
if (glsl != null) {
glsl.useShaders();
glsl.startShader();
GL gl = dc.getGL();
gl.glUniform1i(glsl.getUniformLocation("tile_image"), 0);
layer.render(dc);
glsl.endShader();
}
}
}
需要做更多的工作才能使其完全通用,但这应该是一个合理的起点。