mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-18 11:29:08 +00:00
Add translucency layer targets and depth-sorted composite
This commit is contained in:
4 files changed
+538
No files matched your search
+298
@@ -0,0 +1,298 @@
|
||||
package com.gtnewhorizons.angelica.rendering.translucency;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMAttribStack;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMBlend;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMDraws;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMFramebuffer;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMPrograms;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMQueries;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMRasterizer;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMTextures;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMVertexInput;
|
||||
import com.gtnewhorizons.angelica.glsm.RenderSystem;
|
||||
import com.gtnewhorizons.angelica.glsm.StateSet;
|
||||
import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard;
|
||||
import com.gtnewhorizons.angelica.glsm.texture.DepthBufferFormat;
|
||||
import com.gtnewhorizons.angelica.glsm.texture.TextureInfoCache;
|
||||
import com.gtnewhorizons.angelica.proxy.ClientProxy;
|
||||
import com.gtnewhorizons.angelica.render.PanoramaRenderer;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
|
||||
import net.coderbot.iris.pipeline.FixedFunctionWorldRenderingPipeline;
|
||||
import net.coderbot.iris.rendertarget.IRenderTargetExt;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.shader.Framebuffer;
|
||||
import org.embeddedt.embeddium.impl.gl.shader.GlProgram;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/// Draws each [TranslucentLayer] into its own target, seeded with the main depth, and composites the drawn layers over
|
||||
/// the main framebuffer per pixel, farthest first. Layers hold premultiplied color with coverage alpha (GLSM layer
|
||||
/// blending), and the composite writes the nearest layer depth.
|
||||
public final class TranslucencyLayers {
|
||||
|
||||
public static final TranslucencyLayers INSTANCE = new TranslucencyLayers(new MinecraftTarget());
|
||||
|
||||
private static final int LAYERS = TranslucentLayer.values().length;
|
||||
private static final int FIRST_LAYER_UNIT = 1;
|
||||
|
||||
interface MainTarget {
|
||||
int framebuffer();
|
||||
int depthTexture();
|
||||
int width();
|
||||
int height();
|
||||
int depthVersion();
|
||||
}
|
||||
|
||||
private final MainTarget main;
|
||||
private final Target[] layers = new Target[LAYERS];
|
||||
private Target depthSnapshot;
|
||||
private int width;
|
||||
private int height;
|
||||
private int depthVersion = -1;
|
||||
private DepthBufferFormat depthFormat;
|
||||
|
||||
private boolean active;
|
||||
private int drawnMask;
|
||||
private TranslucentLayer open;
|
||||
private int savedDraw;
|
||||
private int savedRead;
|
||||
|
||||
private GlProgram<Object> program;
|
||||
private int maskUniform;
|
||||
private int vao;
|
||||
private StateSet prepareState;
|
||||
private StateSet compositeState;
|
||||
|
||||
TranslucencyLayers(MainTarget main) {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
public void beginFrame() {
|
||||
beginFrame(ClientProxy.options().performance.translucencyMode.composites() && Iris.enabled
|
||||
&& Iris.getPipelineManager().getPipelineNullable() instanceof FixedFunctionWorldRenderingPipeline);
|
||||
}
|
||||
|
||||
void beginFrame(boolean active) {
|
||||
this.active = active;
|
||||
this.drawnMask = 0;
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void begin(TranslucentLayer layer) {
|
||||
if (open != null) throw new IllegalStateException("Translucency layer " + open + " is still open");
|
||||
BatchStateGuard.beforeChange();
|
||||
savedDraw = GLSMFramebuffer.getDrawFramebuffer();
|
||||
savedRead = GLSMFramebuffer.getReadFramebuffer();
|
||||
syncSize();
|
||||
final Target target = layer(layer);
|
||||
final int bit = 1 << layer.ordinal();
|
||||
if ((drawnMask & bit) == 0) {
|
||||
drawnMask |= bit;
|
||||
prepare(target);
|
||||
}
|
||||
target.framebuffer.bind();
|
||||
open = layer;
|
||||
GLSMBlend.setLayerBlending(true);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
BatchStateGuard.beforeChange();
|
||||
GLSMBlend.setLayerBlending(false);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, savedDraw);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, savedRead);
|
||||
open = null;
|
||||
}
|
||||
|
||||
public boolean redirectsMainBind() {
|
||||
return open != null;
|
||||
}
|
||||
|
||||
public void bindOpenLayer() {
|
||||
layers[open.ordinal()].framebuffer.bind();
|
||||
}
|
||||
|
||||
public void composite() {
|
||||
if (drawnMask == 0) return;
|
||||
BatchStateGuard.beforeChange();
|
||||
final int draw = GLSMFramebuffer.getDrawFramebuffer();
|
||||
final int read = GLSMFramebuffer.getReadFramebuffer();
|
||||
final int activeProgram = GLSMPrograms.getActiveProgram();
|
||||
final int activeVao = GLSMQueries.glGetInteger(GL30.GL_VERTEX_ARRAY_BINDING);
|
||||
if (program == null) initProgram();
|
||||
syncSize();
|
||||
if (drawnMask == 0) return;
|
||||
final Target snapshot = depthSnapshot();
|
||||
final int depth = GLSMAttribStack.pushState(compositeState);
|
||||
try {
|
||||
GLSMRasterizer.disableScissorTest();
|
||||
blit(main.framebuffer(), snapshot.framebuffer.getId(), GL11.GL_DEPTH_BUFFER_BIT);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_FRAMEBUFFER, main.framebuffer());
|
||||
GLSMRasterizer.glViewport(0, 0, width, height);
|
||||
GLSMBlend.enableBlend();
|
||||
GLSMBlend.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ZERO, GL11.GL_ONE);
|
||||
GLSMRasterizer.disableCull();
|
||||
GLSMDepthStencilAlpha.enableDepthTest();
|
||||
GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_ALWAYS);
|
||||
GLSMDepthStencilAlpha.glDepthMask(true);
|
||||
bindTexture(0, snapshot.depth);
|
||||
for (int i = 0; i < LAYERS; i++) {
|
||||
if ((drawnMask & (1 << i)) == 0) continue;
|
||||
bindTexture(FIRST_LAYER_UNIT + i, layers[i].color);
|
||||
bindTexture(FIRST_LAYER_UNIT + LAYERS + i, layers[i].depth);
|
||||
}
|
||||
GLSMPrograms.glUseProgram(program.handle());
|
||||
GLSMPrograms.glUniform1i(maskUniform, drawnMask);
|
||||
GLSMVertexInput.glBindVertexArray(vao);
|
||||
GLSMDraws.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
|
||||
} finally {
|
||||
GLSMVertexInput.glBindVertexArray(activeVao);
|
||||
GLSMPrograms.glUseProgram(activeProgram);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, draw);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, read);
|
||||
GLSMAttribStack.popStateTo(depth);
|
||||
drawnMask = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
destroyTargets();
|
||||
if (program != null) {
|
||||
program.delete();
|
||||
GLSMVertexInput.glDeleteVertexArrays(vao);
|
||||
program = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void initProgram() {
|
||||
program = PanoramaRenderer.loadProgram("angelica:translucency_composite", "angelica:panorama_blur", "angelica:translucency_composite", ctx -> ctx);
|
||||
final int handle = program.handle();
|
||||
GLSMPrograms.glUseProgram(handle);
|
||||
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_Depth"), 0);
|
||||
for (int i = 0; i < LAYERS; i++) {
|
||||
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_Layer" + i), FIRST_LAYER_UNIT + i);
|
||||
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_LayerDepth" + i), FIRST_LAYER_UNIT + LAYERS + i);
|
||||
}
|
||||
maskUniform = GLSMPrograms.glGetUniformLocation(handle, "u_LayerMask");
|
||||
GLSMPrograms.glUseProgram(0);
|
||||
vao = GLSMVertexInput.glGenVertexArrays();
|
||||
compositeState = StateSet.forMask(GL11.GL_ALL_ATTRIB_BITS);
|
||||
}
|
||||
|
||||
private void prepare(Target target) {
|
||||
if (prepareState == null) prepareState = StateSet.forMask(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_SCISSOR_BIT);
|
||||
final int depth = GLSMAttribStack.pushState(prepareState);
|
||||
try {
|
||||
GLSMRasterizer.disableScissorTest();
|
||||
GLSMBlend.glColorMask(true, true, true, true);
|
||||
target.framebuffer.bind();
|
||||
GLSMBlend.glClearColor(0f, 0f, 0f, 0f);
|
||||
GLSMFramebuffer.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
blit(main.framebuffer(), target.framebuffer.getId(), depthFormat.isCombinedStencil() ? GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT : GL11.GL_DEPTH_BUFFER_BIT);
|
||||
} finally {
|
||||
GLSMAttribStack.popStateTo(depth);
|
||||
}
|
||||
}
|
||||
|
||||
private void blit(int from, int to, int mask) {
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, from);
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, to);
|
||||
GLSMFramebuffer.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, mask, GL11.GL_NEAREST);
|
||||
}
|
||||
|
||||
private static void bindTexture(int unit, int texture) {
|
||||
GLSMTextures.glActiveTexture(GL13.GL_TEXTURE0 + unit);
|
||||
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||
}
|
||||
|
||||
private void syncSize() {
|
||||
if (main.width() == width && main.height() == height && main.depthVersion() == depthVersion) return;
|
||||
destroyTargets();
|
||||
width = main.width();
|
||||
height = main.height();
|
||||
depthVersion = main.depthVersion();
|
||||
depthFormat = DepthBufferFormat.fromGlEnumOrDefault(TextureInfoCache.INSTANCE.getInfo(main.depthTexture()).getInternalFormat());
|
||||
}
|
||||
|
||||
private Target layer(TranslucentLayer layer) {
|
||||
final int i = layer.ordinal();
|
||||
if (layers[i] == null) layers[i] = new Target(width, height, depthFormat, true);
|
||||
return layers[i];
|
||||
}
|
||||
|
||||
private Target depthSnapshot() {
|
||||
if (depthSnapshot == null) depthSnapshot = new Target(width, height, depthFormat, false);
|
||||
return depthSnapshot;
|
||||
}
|
||||
|
||||
private void destroyTargets() {
|
||||
for (int i = 0; i < LAYERS; i++) {
|
||||
if (layers[i] != null) layers[i].destroy();
|
||||
layers[i] = null;
|
||||
}
|
||||
if (depthSnapshot != null) depthSnapshot.destroy();
|
||||
depthSnapshot = null;
|
||||
drawnMask = 0;
|
||||
}
|
||||
|
||||
private static final class Target {
|
||||
final int color;
|
||||
final int depth;
|
||||
final GlFramebuffer framebuffer;
|
||||
|
||||
Target(int width, int height, DepthBufferFormat depthFormat, boolean withColor) {
|
||||
depth = texture(width, height, depthFormat.getGlInternalFormat(), depthFormat.getGlType(), depthFormat.getGlFormat());
|
||||
framebuffer = new GlFramebuffer();
|
||||
framebuffer.addDepthAttachment(depth);
|
||||
if (withColor) {
|
||||
color = texture(width, height, GL11.GL_RGBA8, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE);
|
||||
framebuffer.addColorAttachment(0, color);
|
||||
framebuffer.drawBuffers(new int[] { 0 });
|
||||
framebuffer.readBuffer(0);
|
||||
} else {
|
||||
color = 0;
|
||||
framebuffer.noDrawBuffers();
|
||||
RenderSystem.readBuffer(framebuffer.getId(), GL11.GL_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
private static int texture(int width, int height, int internalFormat, int format, int type) {
|
||||
final int previous = GLSMQueries.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||
final int texture = GLSMTextures.glGenTextures();
|
||||
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
|
||||
GLSMTextures.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, (ByteBuffer) null);
|
||||
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, previous);
|
||||
return texture;
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
framebuffer.destroy();
|
||||
if (color != 0) GLSMTextures.glDeleteTextures(color);
|
||||
GLSMTextures.glDeleteTextures(depth);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class MinecraftTarget implements MainTarget {
|
||||
private static Framebuffer fb() {
|
||||
return Minecraft.getMinecraft().getFramebuffer();
|
||||
}
|
||||
|
||||
@Override public int framebuffer() { return fb().framebufferObject; }
|
||||
@Override public int depthTexture() { return ((IRenderTargetExt) fb()).iris$getDepthTextureId(); }
|
||||
@Override public int width() { return fb().framebufferWidth; }
|
||||
@Override public int height() { return fb().framebufferHeight; }
|
||||
@Override public int depthVersion() { return ((IRenderTargetExt) fb()).iris$getDepthBufferVersion(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.gtnewhorizons.angelica.rendering.translucency;
|
||||
|
||||
public enum TranslucentLayer {
|
||||
TERRAIN,
|
||||
BLOCK_ENTITIES,
|
||||
PARTICLES,
|
||||
WEATHER,
|
||||
CLOUDS
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#version 330 core
|
||||
|
||||
// A sample is skipped only when all zero, so additive light with no coverage still counts.
|
||||
|
||||
in vec2 v_TexCoord;
|
||||
|
||||
uniform sampler2D u_Depth;
|
||||
uniform sampler2D u_Layer0;
|
||||
uniform sampler2D u_Layer1;
|
||||
uniform sampler2D u_Layer2;
|
||||
uniform sampler2D u_Layer3;
|
||||
uniform sampler2D u_Layer4;
|
||||
uniform sampler2D u_LayerDepth0;
|
||||
uniform sampler2D u_LayerDepth1;
|
||||
uniform sampler2D u_LayerDepth2;
|
||||
uniform sampler2D u_LayerDepth3;
|
||||
uniform sampler2D u_LayerDepth4;
|
||||
uniform int u_LayerMask;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
vec4 colors[5];
|
||||
float depths[5];
|
||||
int count = 0;
|
||||
float mainDepth;
|
||||
|
||||
void insert(int bit, sampler2D colorTexture, sampler2D depthTexture) {
|
||||
if ((u_LayerMask & bit) == 0) return;
|
||||
vec4 color = texture(colorTexture, v_TexCoord);
|
||||
if (color == vec4(0.0)) return;
|
||||
float depth = texture(depthTexture, v_TexCoord).r;
|
||||
if (depth > mainDepth) return;
|
||||
int j = count++;
|
||||
colors[j] = color;
|
||||
depths[j] = depth;
|
||||
while (j > 0 && depths[j] > depths[j - 1]) {
|
||||
vec4 c = colors[j];
|
||||
colors[j] = colors[j - 1];
|
||||
colors[j - 1] = c;
|
||||
float d = depths[j];
|
||||
depths[j] = depths[j - 1];
|
||||
depths[j - 1] = d;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
mainDepth = texture(u_Depth, v_TexCoord).r;
|
||||
insert(1, u_Layer0, u_LayerDepth0);
|
||||
insert(2, u_Layer1, u_LayerDepth1);
|
||||
insert(4, u_Layer2, u_LayerDepth2);
|
||||
insert(8, u_Layer3, u_LayerDepth3);
|
||||
insert(16, u_Layer4, u_LayerDepth4);
|
||||
vec4 result = vec4(0.0);
|
||||
for (int i = 0; i < count; i++) {
|
||||
result = result * (1.0 - colors[i].a) + colors[i];
|
||||
}
|
||||
fragColor = result;
|
||||
gl_FragDepth = count > 0 ? depths[count - 1] : mainDepth;
|
||||
}
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
package com.gtnewhorizons.angelica.rendering.translucency;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.GLCoreTest;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMBlend;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMDraws;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMFramebuffer;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMPrograms;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMRasterizer;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMTextures;
|
||||
import com.gtnewhorizons.angelica.glsm.GLSMVertexInput;
|
||||
import com.gtnewhorizons.angelica.glsm.GlTestSupport;
|
||||
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@GLCoreTest
|
||||
class TranslucencyCompositeGLTest {
|
||||
|
||||
private static final int WIDTH = 4;
|
||||
private static final String QUAD_VS = """
|
||||
#version 330 core
|
||||
uniform float u_Depth;
|
||||
void main() {
|
||||
vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
|
||||
gl_Position = vec4(p * 2.0 - 1.0, u_Depth * 2.0 - 1.0, 1.0);
|
||||
}
|
||||
""";
|
||||
private static final String QUAD_FS = """
|
||||
#version 330 core
|
||||
uniform vec4 u_Color;
|
||||
out vec4 color;
|
||||
void main() { color = u_Color; }
|
||||
""";
|
||||
|
||||
private int colorTex;
|
||||
private int depthTex;
|
||||
private GlFramebuffer main;
|
||||
private int quadProgram;
|
||||
private int vao;
|
||||
private TranslucencyLayers layers;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
colorTex = texture(GL11.GL_RGBA8, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE);
|
||||
depthTex = texture(GL30.GL_DEPTH24_STENCIL8, GL30.GL_DEPTH_STENCIL, GL30.GL_UNSIGNED_INT_24_8);
|
||||
main = new GlFramebuffer();
|
||||
main.addColorAttachment(0, colorTex);
|
||||
main.addDepthAttachment(depthTex);
|
||||
main.drawBuffers(new int[] { 0 });
|
||||
main.readBuffer(0);
|
||||
main.bind();
|
||||
GLSMRasterizer.glViewport(0, 0, WIDTH, 1);
|
||||
quadProgram = GlTestSupport.program(QUAD_VS, QUAD_FS);
|
||||
vao = GLSMVertexInput.glGenVertexArrays();
|
||||
layers = new TranslucencyLayers(new TranslucencyLayers.MainTarget() {
|
||||
@Override public int framebuffer() { return main.getId(); }
|
||||
@Override public int depthTexture() { return depthTex; }
|
||||
@Override public int width() { return WIDTH; }
|
||||
@Override public int height() { return 1; }
|
||||
@Override public int depthVersion() { return 0; }
|
||||
});
|
||||
GLSMDepthStencilAlpha.enableDepthTest();
|
||||
GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_LEQUAL);
|
||||
GLSMDepthStencilAlpha.glDepthMask(true);
|
||||
GLSMBlend.glClearColor(1f, 0f, 0f, 1f);
|
||||
GLSMDepthStencilAlpha.glClearDepth(0.9);
|
||||
GLSMFramebuffer.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
layers.destroy();
|
||||
GLSMFramebuffer.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
|
||||
main.destroy();
|
||||
GLSMTextures.glDeleteTextures(colorTex);
|
||||
GLSMTextures.glDeleteTextures(depthTex);
|
||||
GLSMPrograms.glUseProgram(0);
|
||||
GLSMPrograms.glDeleteProgram(quadProgram);
|
||||
GLSMVertexInput.glDeleteVertexArrays(vao);
|
||||
GLSMBlend.disableBlend();
|
||||
GLSMDepthStencilAlpha.disableDepthTest();
|
||||
GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_LESS);
|
||||
GLSMBlend.glClearColor(0f, 0f, 0f, 0f);
|
||||
GLSMDepthStencilAlpha.glClearDepth(1.0);
|
||||
}
|
||||
|
||||
private static int texture(int internalFormat, int format, int type) {
|
||||
final int tex = GLSMTextures.glGenTextures();
|
||||
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, tex);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
|
||||
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
GLSMTextures.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, WIDTH, 1, 0, format, type, (ByteBuffer) null);
|
||||
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
return tex;
|
||||
}
|
||||
|
||||
private void drawInto(TranslucentLayer layer, int column, float depth, float r, float g, float b, float a, int srcRgb, int dstRgb) {
|
||||
layers.begin(layer);
|
||||
GLSMBlend.enableBlend();
|
||||
GLSMBlend.glBlendFunc(srcRgb, dstRgb);
|
||||
draw(column, depth, r, g, b, a);
|
||||
GLSMBlend.disableBlend();
|
||||
layers.end();
|
||||
}
|
||||
|
||||
private void draw(int column, float depth, float r, float g, float b, float a) {
|
||||
GLSMRasterizer.glViewport(column, 0, 1, 1);
|
||||
GLSMPrograms.glUseProgram(quadProgram);
|
||||
GLSMPrograms.glUniform1f(GLSMPrograms.glGetUniformLocation(quadProgram, "u_Depth"), depth);
|
||||
GLSMPrograms.glUniform4f(GLSMPrograms.glGetUniformLocation(quadProgram, "u_Color"), r, g, b, a);
|
||||
GLSMVertexInput.glBindVertexArray(vao);
|
||||
GLSMDraws.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
|
||||
GLSMVertexInput.glBindVertexArray(0);
|
||||
GLSMPrograms.glUseProgram(0);
|
||||
GLSMRasterizer.glViewport(0, 0, WIDTH, 1);
|
||||
}
|
||||
|
||||
private static void assertColumn(ByteBuffer rgba, int column, int r, int g, int b) {
|
||||
final int at = column * 4;
|
||||
assertEquals(r, rgba.get(at) & 0xFF, 2, "column " + column + " red");
|
||||
assertEquals(g, rgba.get(at + 1) & 0xFF, 2, "column " + column + " green");
|
||||
assertEquals(b, rgba.get(at + 2) & 0xFF, 2, "column " + column + " blue");
|
||||
}
|
||||
|
||||
private static ByteBuffer readColor() {
|
||||
final ByteBuffer rgba = BufferUtils.createByteBuffer(WIDTH * 4);
|
||||
GLSMFramebuffer.glReadPixels(0, 0, WIDTH, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, rgba);
|
||||
return rgba;
|
||||
}
|
||||
|
||||
private static FloatBuffer readDepth() {
|
||||
final FloatBuffer depth = BufferUtils.createFloatBuffer(WIDTH);
|
||||
GLSMFramebuffer.glReadPixels(0, 0, WIDTH, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, depth);
|
||||
return depth;
|
||||
}
|
||||
|
||||
@Test
|
||||
void compositesLayersByDepthAndWritesNearestDepth() {
|
||||
layers.beginFrame(true);
|
||||
drawInto(TranslucentLayer.TERRAIN, 0, 0.3f, 0f, 1f, 0f, 0.5f, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
drawInto(TranslucentLayer.PARTICLES, 0, 0.5f, 0f, 0f, 1f, 0.5f, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
drawInto(TranslucentLayer.TERRAIN, 1, 0.95f, 0f, 0f, 1f, 0.5f, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
drawInto(TranslucentLayer.CLOUDS, 2, 0.5f, 0f, 0.4f, 0f, 0f, GL11.GL_ONE, GL11.GL_ONE);
|
||||
drawInto(TranslucentLayer.TERRAIN, 3, 0.5f, 0f, 1f, 0f, 0.5f, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
draw(3, 0.2f, 0f, 0f, 1f, 1f);
|
||||
layers.composite();
|
||||
|
||||
final ByteBuffer color = readColor();
|
||||
assertColumn(color, 0, 64, 128, 64);
|
||||
assertColumn(color, 1, 255, 0, 0);
|
||||
assertColumn(color, 2, 255, 102, 0);
|
||||
assertColumn(color, 3, 0, 0, 255);
|
||||
final FloatBuffer depth = readDepth();
|
||||
final float[] expected = { 0.3f, 0.9f, 0.5f, 0.2f };
|
||||
for (int i = 0; i < WIDTH; i++) assertEquals(expected[i], depth.get(i), 1e-4f, "column " + i + " depth");
|
||||
|
||||
layers.composite();
|
||||
final ByteBuffer again = readColor();
|
||||
for (int i = 0; i < WIDTH * 4; i++) assertEquals(color.get(i), again.get(i), "a composite with no new layer draws changes nothing");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user