Allow DefaultChunkRenderer to be more extensible #17

Closed
venn0 wants to merge 1 commits from venn0/celeritas:ven-dev-2 into stonecutter
pull from: venn0/celeritas:ven-dev-2
2 changed files with 42 additions and 12 deletions

No files matched your search

@@ -56,8 +56,35 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
return buffer; return buffer;
} }
protected void configureShaderInterface() {
if (this.activeProgram == null) {
throw new IllegalStateException("No active program");
}
configureShaderInterface(this.activeProgram.getInterface());
}
protected abstract void configureShaderInterface(ChunkShaderInterface shader); protected abstract void configureShaderInterface(ChunkShaderInterface shader);
protected boolean canRender() {
return this.activeProgram != null;
}
protected void setMatrices(ChunkRenderMatrices matrices) {
if (this.activeProgram == null) {
throw new IllegalStateException("No active program");
}
ChunkShaderInterface shader = this.activeProgram.getInterface();
shader.setProjectionMatrix(matrices.projection());
shader.setModelViewMatrix(matrices.modelView());
}
protected GlPrimitiveType getPrimitiveType() {
if (this.activeProgram == null) {
throw new IllegalStateException("No active program");
}
return this.activeProgram.getInterface().getPrimitiveType();
}
@Override @Override
public void render(ChunkRenderMatrices matrices, public void render(ChunkRenderMatrices matrices,
CommandList commandList, CommandList commandList,
@@ -69,26 +96,25 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
return; return;
} }
super.begin(renderPass); this.begin(renderPass);
// Default implementation will check if an active program exists
// If there is no active program, shader compilation probably failed, and we can't render anything. // If there is no active program, shader compilation probably failed, and we can't render anything.
if (this.activeProgram != null) { if (this.canRender()) {
boolean useBlockFaceCulling = this.useBlockFaceCulling(); boolean useBlockFaceCulling = this.useBlockFaceCulling();
GLDebug.pushGroup(770, renderPass.name() + " terrain pass"); GLDebug.pushGroup(770, renderPass.name() + " terrain pass");
ChunkShaderInterface shader = this.activeProgram.getInterface(); this.setMatrices(matrices);
shader.setProjectionMatrix(matrices.projection());
shader.setModelViewMatrix(matrices.modelView());
var primitiveType = shader.getPrimitiveType(); var primitiveType = this.getPrimitiveType();
Iterator<ChunkRenderList> iterator = renderLists.iterator(renderPass.isReverseOrder()); Iterator<ChunkRenderList> iterator = renderLists.iterator(renderPass.isReverseOrder());
this.currentRenderPass = renderPass; this.currentRenderPass = renderPass;
this.currentVertexFormat = this.renderPassConfiguration.getVertexTypeForPass(this.currentRenderPass).getVertexFormat(); this.currentVertexFormat = this.renderPassConfiguration.getVertexTypeForPass(this.currentRenderPass).getVertexFormat();
this.configureShaderInterface(shader); this.configureShaderInterface();
while (iterator.hasNext()) { while (iterator.hasNext()) {
ChunkRenderList renderList = iterator.next(); ChunkRenderList renderList = iterator.next();
@@ -112,7 +138,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
var tessellation = this.prepareTessellation(commandList, region); var tessellation = this.prepareTessellation(commandList, region);
setModelMatrixUniforms(shader, region, camera); this.setModelMatrixUniforms(region, camera);
executeDrawBatch(commandList, tessellation, primitiveType, this.batch); executeDrawBatch(commandList, tessellation, primitiveType, this.batch);
} }
@@ -122,7 +148,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
GLDebug.popGroup(); GLDebug.popGroup();
} }
super.end(renderPass); this.end(renderPass);
} }
private static void fillCommandBuffer(MultiDrawBatch batch, private static void fillCommandBuffer(MultiDrawBatch batch,
@@ -258,12 +284,16 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
return planes; return planes;
} }
private static void setModelMatrixUniforms(ChunkShaderInterface shader, RenderRegion region, CameraTransform camera) { protected void setModelMatrixUniforms(RenderRegion region, CameraTransform camera) {
if (this.activeProgram == null) {
throw new IllegalStateException("No active program");
}
float x = getCameraTranslation(region.getOriginX(), camera.intX, camera.fracX); float x = getCameraTranslation(region.getOriginX(), camera.intX, camera.fracX);
float y = getCameraTranslation(region.getOriginY(), camera.intY, camera.fracY); float y = getCameraTranslation(region.getOriginY(), camera.intY, camera.fracY);
float z = getCameraTranslation(region.getOriginZ(), camera.intZ, camera.fracZ); float z = getCameraTranslation(region.getOriginZ(), camera.intZ, camera.fracZ);
shader.setRegionOffset(x, y, z); this.activeProgram.getInterface().setRegionOffset(x, y, z);
} }
private static float getCameraTranslation(int chunkBlockPos, int cameraBlockPos, float cameraPos) { private static float getCameraTranslation(int chunkBlockPos, int cameraBlockPos, float cameraPos) {
@@ -27,7 +27,7 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer {
protected final RenderDevice device; protected final RenderDevice device;
protected GlProgram<ChunkShaderInterface> activeProgram; protected @Nullable GlProgram<ChunkShaderInterface> activeProgram;
public ShaderChunkRenderer(RenderDevice device, RenderPassConfiguration<?> renderPassConfiguration) { public ShaderChunkRenderer(RenderDevice device, RenderPassConfiguration<?> renderPassConfiguration) {
this.device = device; this.device = device;