Skip to content

Commit

Permalink
lights work
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Oct 7, 2023
1 parent 90b0b95 commit dc320ea
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,25 @@ protected void paintVertices(PolygonSpriteBatch batch, float[] vertices) {
public FloatArray pathToFloatArray(PathIterator it) {
tmpFloat.clear();

float moveToX = 0;
float moveToY = 0;
for (; !it.isDone(); it.next()) {
int type = it.currentSegment(floatsFromArea);

switch (type) {
case PathIterator.SEG_MOVETO:
// System.out.println("Move to: ( " + floatsFromArea[0] + ", " +
// floatsFromArea[1] + ")");
moveToX = floatsFromArea[0];
moveToY = -floatsFromArea[1];
tmpFloat.add(floatsFromArea[0], -floatsFromArea[1]);

break;
case PathIterator.SEG_CLOSE:
// System.out.println("Close");
if (tmpFloat.get(0) == tmpFloat.get(tmpFloat.size - 2)
&& tmpFloat.get(1) == tmpFloat.get(tmpFloat.size - 1)) {
// make sure we don't have last and first point the same
tmpFloat.pop();
tmpFloat.pop();
}
return tmpFloat;
tmpFloat.add(moveToX, moveToY);
break;
// return tmpFloat;
case PathIterator.SEG_LINETO:
// System.out.println("Line to: ( " + floatsFromArea[0] + ", " +
// floatsFromArea[1] + ")");
Expand Down Expand Up @@ -207,6 +207,12 @@ public FloatArray pathToFloatArray(PathIterator it) {
System.out.println("Type: " + type);
}
}
if (tmpFloat.get(0) == tmpFloat.get(tmpFloat.size - 2)
&& tmpFloat.get(1) == tmpFloat.get(tmpFloat.size - 1)) {
// make sure we don't have last and first point the same
tmpFloat.pop();
tmpFloat.pop();
}
return tmpFloat;
}

Expand Down
127 changes: 45 additions & 82 deletions src/main/java/net/rptools/maptool/client/ui/zone/gdx/GdxRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator;
import com.google.common.eventbus.Subscribe;
import com.jogamp.opengl.GL;
import java.awt.*;
import java.awt.Shape;
import java.awt.geom.*;
Expand All @@ -48,7 +47,6 @@
import java.util.*;
import java.util.List;
import java.util.zip.Deflater;
import javax.annotation.Nullable;
import javax.swing.*;
import net.rptools.lib.CodeTimer;
import net.rptools.lib.MD5Key;
Expand Down Expand Up @@ -255,6 +253,7 @@ public void create() {
updateCam();

batch = new PolygonSpriteBatch();
batch.enableBlending();

backBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);

Expand Down Expand Up @@ -483,8 +482,6 @@ private void initializeTimer() {

public void invalidateCurrentViewCache() {
flushFog = true;
drawableLights = null;
drawableAuras = null;
visibleScreenArea = null;
lastView = null;

Expand Down Expand Up @@ -791,8 +788,11 @@ private void renderFog(PlayerView view) {
timer.start("renderFog");
// if (flushFog || cacheNotValid)
{
batch.flush();

backBuffer.begin();
ScreenUtils.clear(Color.CLEAR);

batch.setBlendFunction(GL20.GL_ONE, GL20.GL_NONE);
setProjectionMatrix(cam.combined);

Expand Down Expand Up @@ -903,8 +903,10 @@ private void renderFog(PlayerView view) {
timer.stop("renderFogArea");

flushFog = false;
// createScreenShot("fog");
// createScreenshot("fog");
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
batch.flush();

backBuffer.end();
}

Expand Down Expand Up @@ -1195,35 +1197,20 @@ private void showBlockedMoves(ZonePoint zp, double angle, Sprite image, float si
image.draw(batch);
}

/** Holds the auras from lightSourceMap after they have been combined. */
private List<DrawableLight> drawableAuras;

private void renderAuras(PlayerView view) {
var alpha = AppPreferences.getAuraOverlayOpacity() / 255.0f;

timer.start("auras-4");
/*
for (DrawableLight light : zoneRenderer.getZoneView().getLights(LightSource.Type.AURA)) {
var paint = light.getPaint();
if (paint != null && paint instanceof DrawableColorPaint) {
var colorPaint = (DrawableColorPaint) paint;
Color.argb8888ToColor(tmpColor, colorPaint.getColor());
tmpColor.a = alpha;
} else {
tmpColor.set(1, 1, 1, 0.59f);
}
drawer.setColor(tmpColor);
areaRenderer.fillArea(light.getArea());
}
*/
timer.stop("auras-4");
}
// Setup
timer.start("renderAuras:getAuras");
final var drawableAuras = zoneRenderer.getZoneView().getDrawableAuras();
timer.stop("renderAuras:getAuras");

/**
* Cached set of lights arranged by lumens for some stability. TODO Token draw order would be
* nice.
*/
private List<DrawableLight> drawableLights = null;
timer.start("renderAuras:renderAuraOverlay");
renderLightOverlay(
drawableAuras,
alpha);
timer.stop("renderAuras:renderAuraOverlay");
}

private void renderLights(PlayerView view) {
// Collect and organize lights
Expand All @@ -1234,28 +1221,10 @@ private void renderLights(PlayerView view) {
if (AppState.isShowLights()) {
// Lighting enabled.
timer.start("renderLights:renderLightOverlay");
// final var overlayBlending =
// switch (zone.getLightingStyle()) {
// case OVERTOP -> AlphaComposite.SrcOver.derive(
// AppPreferences.getLightOverlayOpacity() / 255.f);
// case ENVIRONMENTAL -> LightingComposite.OverlaidLights;
// };

final var overlayFillColor =
switch (zone.getLightingStyle()) {
case OVERTOP -> Color.CLEAR;
case ENVIRONMENTAL -> Color.BLACK;
};

renderLightOverlay(
GL.GL_SRC_COLOR,
GL.GL_ONE_MINUS_SRC_COLOR,
GL.GL_SRC_ALPHA,
GL.GL_ONE_MINUS_SRC_ALPHA,
new Color(1, 1, 1, AppPreferences.getLightOverlayOpacity() / 255.f),
view.isGMView() ? null : ZoneRenderer.LightOverlayClipStyle.CLIP_TO_VISIBLE_AREA,
drawableLights,
overlayFillColor);
// zone.getLightingStyle() is not supported currently as you would probably need a custom
// shader

renderLightOverlay(drawableLights, AppPreferences.getLightOverlayOpacity() / 255.f);
timer.stop("renderLights:renderLightOverlay");
}

Expand All @@ -1273,28 +1242,20 @@ private void renderLights(PlayerView view) {
private void renderLumensOverlay(
PlayerView view, ZoneRenderer.LightOverlayClipStyle lightOverlayClipStyle, float v) {}

private void renderLightOverlay(
int lightBlendSrcFunc,
int lightBlendDstFunc,
int overlayBlendSrcFunc,
int overlayBlendDstFunc,
Color tintColor,
@Nullable ZoneRenderer.LightOverlayClipStyle clipStyle,
Collection<DrawableLight> lights,
Color backgroundFill) {
private void renderLightOverlay(Collection<DrawableLight> lights, float alpha) {
if (lights.isEmpty()) {
// No points spending resources accomplishing nothing.
return;
}

// Set up a buffer image for lights to be drawn onto before the map
timer.start("renderLightOverlay:allocateBuffer");
batch.flush();
backBuffer.begin();
batch.setBlendFunction(GL20.GL_ONE, GL20.GL_NONE);

ScreenUtils.clear(Color.CLEAR);
setProjectionMatrix(cam.combined);
batch.setBlendFunction(lightBlendSrcFunc, lightBlendDstFunc);
//batch.setColor(tintColor);
batch.setBlendFunctionSeparate(GL20.GL_SRC_COLOR, GL20.GL_ONE_MINUS_SRC_COLOR, GL20.GL_ONE, GL20.GL_NONE);
timer.stop("renderLightOverlay:allocateBuffer");
// drawer.setColor(tintColor);
// Draw lights onto the buffer image so the map doesn't affect how they blend
Expand All @@ -1312,34 +1273,35 @@ private void renderLightOverlay(
System.out.println("unexpected color type");
continue;
}

tmpColor.set(tmpColor.r, tmpColor.g, tmpColor.b, alpha);
areaRenderer.setColor(tmpColor);
areaRenderer.fillArea(batch, light.getArea());
}

batch.flush();
backBuffer.end();
timer.stop("renderLightOverlay:drawLights");
batch.end();

// Draw the buffer image with all the lights onto the map
var file = Gdx.files.absolute("C:\\Users\\tkunze\\OneDrive\\Desktop\\buffer.png");
if(!file.exists()) {
Pixmap pixmap = Pixmap.createFromFrameBuffer(0, 0, width, height);
PixmapIO.writePNG(file, pixmap, Deflater.DEFAULT_COMPRESSION, true);
pixmap.dispose();
}
timer.start("renderLightOverlay:drawBuffer");
backBuffer.end();
batch.begin();
batch.setBlendFunction(overlayBlendSrcFunc, overlayBlendDstFunc);
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
setProjectionMatrix(hudCam.combined);



batch.draw(
backBuffer.getColorBufferTexture(), 0, 0, width, height, 0, 0, width, height, false, true);
batch.setColor(Color.WHITE);
//batch.setColor(1, 1, 1, alpha);
batch.draw(backBuffer.getColorBufferTexture(), 0, 0, width, height, 0, 0, 1, 1);
setProjectionMatrix(cam.combined);
//batch.setColor(Color.WHITE);
timer.stop("renderLightOverlay:drawBuffer");
}

private void createScreenshot(String name) {
var file = Gdx.files.absolute("C:\\Users\\tkunze\\OneDrive\\Desktop\\" + name + ".png");
if (!file.exists()) {
Pixmap pixmap = Pixmap.createFromFrameBuffer(0, 0, width, height);
PixmapIO.writePNG(file, pixmap, Deflater.DEFAULT_COMPRESSION, true);
pixmap.dispose();
}
}

private void renderGrid(PlayerView view) {
var grid = zone.getGrid();
var scale = (float) zoneRenderer.getScale();
Expand Down Expand Up @@ -2841,7 +2803,7 @@ protected java.awt.Shape getSquareFacingArrow(int angle, int size) {
}

private void paintClipped(Sprite image, Area bounds, Area clip) {

batch.flush();
backBuffer.begin();
ScreenUtils.clear(Color.CLEAR);

Expand All @@ -2855,6 +2817,7 @@ private void paintClipped(Sprite image, Area bounds, Area clip) {
tmpArea.add(bounds);
tmpArea.subtract(clip);
areaRenderer.fillArea(batch, tmpArea);
batch.flush();

backBuffer.end();

Expand Down

0 comments on commit dc320ea

Please sign in to comment.