Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.19.4-forge' into 1.20.1-forge
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Dec 8, 2023
2 parents 17fe88c + 689b831 commit f7da14e
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 38 deletions.
9 changes: 7 additions & 2 deletions src/main/java/cam72cam/mod/entity/ModdedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,13 @@ void removeSeat(SeatEntity seat) {

Vec3d pos = calculatePassengerPosition(offset);

while (!(level().isEmptyBlock(new Vec3i(pos).internal()) && level().isEmptyBlock(new Vec3i(pos).up().internal()))) {
pos = pos.add(0, 1, 0);
Vec3d adjusted = pos;
for (int i = 0; i < 6; i++) {
if (level().isEmptyBlock(new Vec3i(adjusted).internal()) && level().isEmptyBlock(new Vec3i(adjusted).up().internal())) {
pos = adjusted;
break;
}
adjusted = adjusted.add(0, 1, 0);
}
passenger.setPosition(pos);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cam72cam/mod/entity/SeatEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static EntityType<SeatEntity> makeType() {

static {
World.onTick(SeatEntity::ticker);
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> ClientEvents.TICK.subscribe(() -> {
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> ClientEvents.TICK_POST.subscribe(() -> {
if (MinecraftClient.isReady()) {
ticker(MinecraftClient.getPlayer().getWorld());
}
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/cam72cam/mod/event/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,30 @@ public enum MouseAction {
CLICK,
RELEASE,
MOVE,
SCROLL,
}

public static class MouseGuiEvent {
public final MouseAction action;
public final int x;
public final int y;
public final int button;
public final double scroll;

public MouseGuiEvent(MouseAction action, int x, int y, int button) {
public MouseGuiEvent(MouseAction action, int x, int y, int button, double scroll) {
this.action = action;
this.x = x;
this.y = y;
this.button = button;
this.scroll = scroll;
}
}

public static final Event<Runnable> TICK = new Event<>();
@Deprecated // TODO find a better hack
public static final Event<Runnable> TICK_POST = new Event<>();
public static final Event<Function<Player.Hand, Boolean>> DRAG = new Event<>();
public static final Event<Function<Double, Boolean>> SCROLL = new Event<>();
public static final Event<Function<Player.Hand, Boolean>> CLICK = new Event<>();
public static final Event<Function<MouseGuiEvent, Boolean>> MOUSE_GUI = new Event<>();
public static final Event<Runnable> MODEL_CREATE = new Event<>();
Expand All @@ -109,16 +115,24 @@ public static class ClientEventBusForge {

@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
TICK.execute(Runnable::run);
if (event.phase == TickEvent.Phase.START) {
TICK.execute(Runnable::run);
}
if (event.phase == TickEvent.Phase.END) {
TICK_POST.execute(Runnable::run);
}
}

private static void onGuiMouse(ScreenEvent event, int x, int y, int btn, MouseAction action) {
MouseGuiEvent mevt = new MouseGuiEvent(action, x, y, btn);
MouseGuiEvent mevt = new MouseGuiEvent(action, x, y, btn, event instanceof ScreenEvent.MouseScrolled ? (int) ((ScreenEvent.MouseScrolled) event).getScrollDelta() : 0);

if (!MOUSE_GUI.executeCancellable(h -> h.apply(mevt))) {
event.setCanceled(true);
// Apparently cancelling this input event only cancels it for the *GUI* handlers, not all input handlers
// Therefore we need to track that ourselves. Thanks for changing that from 1.12.2-forge
skipNextMouseInputEvent = true;
if (!(event instanceof ScreenEvent.MouseScrolled)) {
// Apparently cancelling this input event only cancels it for the *GUI* handlers, not all input handlers
// Therefore we need to track that ourselves. Thanks for changing that from 1.12.2-forge
skipNextMouseInputEvent = true;
}
}
}

Expand All @@ -134,6 +148,11 @@ public static void onGuiDrag(ScreenEvent.MouseDragged.Pre event) {
public static void onGuiRelease(ScreenEvent.MouseButtonReleased.Pre event) {
onGuiMouse(event, (int) event.getMouseX(), (int) event.getMouseY(), event.getButton(), MouseAction.RELEASE);
}
@SubscribeEvent
public static void onGuiScroll(ScreenEvent.MouseScrolled.Pre event) {
System.out.println(event.getScrollDelta());
onGuiMouse(event, (int) event.getMouseX(), (int) event.getMouseY(), -1, MouseAction.RELEASE);
}

private static void hackInputState(int event) {
int attackID = Minecraft.getInstance().options.keyAttack.getKey().getValue();
Expand All @@ -148,6 +167,14 @@ private static void hackInputState(int event) {
}
}

@SubscribeEvent
public static void onScroll(InputEvent.MouseScrollingEvent event) {
System.out.println(event.getScrollDelta());
if (!SCROLL.executeCancellable(x -> x.apply(event.getScrollDelta()))) {
event.setCanceled(true);
}
}

@SubscribeEvent
public static void onClick(InputEvent.MouseButton event) {
if (skipNextMouseInputEvent) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cam72cam/mod/model/obj/OBJModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,13 @@ public void apply(RenderState state) {
}

public OBJRender.Binding bind(RenderState state) {
return bind(state, false);
}

public OBJRender.Binding bind(RenderState state, boolean waitForLoad) {
state = state.clone();
apply(state);
return vbo.bind(state);
return vbo.bind(state, waitForLoad);
}

public OBJRender.Builder builder() {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/cam72cam/mod/render/obj/OBJRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ public OBJRender(OBJModel model, Supplier<VertexBuffer> buffer) {
}

public Binding bind(RenderState state) {
return new Binding(state);
return bind(state, false);
}
public Binding bind(RenderState state, boolean waitForLoad) {
return new Binding(state, waitForLoad);
}

public class Binding extends VBO.Binding {
protected Binding(RenderState state) {
super(state);
protected Binding(RenderState state, boolean wait) {
super(state, wait);
}

public void draw(Collection<String> groups, Consumer<RenderState> mod) {
if (!isLoaded()) {
return;
}
try (With pus = super.push(mod)) {
draw(groups);
}
Expand All @@ -44,6 +50,9 @@ public void draw(Collection<String> groups, Consumer<RenderState> mod) {
* Draw these groups in the VB
*/
public void draw(Collection<String> groups) {
if (!isLoaded()) {
return;
}
RenderContext.checkError();
List<String> sorted = new ArrayList<>(groups);
sorted.sort(Comparator.naturalOrder());
Expand Down
107 changes: 82 additions & 25 deletions src/main/java/cam72cam/mod/render/opengl/VBO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL32;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand Down Expand Up @@ -45,6 +47,17 @@ public static void registerClientEvents() {
private long lastUsed;
private VertexBuffer vbInfo;

private static final ExecutorService pool = new ThreadPoolExecutor(0, Runtime.getRuntime().availableProcessors(),
5L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
runnable -> {
Thread thread = new Thread(runnable);
thread.setName("UMC-VertexBufferLoader");
thread.setPriority(Thread.MIN_PRIORITY);
return thread;
});
private Future<FloatBuffer> loader = null;

public VBO(Supplier<VertexBuffer> buffer, Consumer<RenderState> settings) {
this.buffer = buffer;
this.vao = -1;
Expand All @@ -57,49 +70,87 @@ public VBO(Supplier<VertexBuffer> buffer, Consumer<RenderState> settings) {
}

private void init() {
VertexBuffer vb = buffer.get();
this.length = vb.data.length / (vb.stride);
this.vbInfo = new VertexBuffer(0, vb.hasNormals);
FloatBuffer buffer = BufferUtils.createFloatBuffer(vb.data.length);
buffer.put(vb.data);
buffer.position(0);

int oldVao = GL32.glGetInteger(GL32.GL_VERTEX_ARRAY_BUFFER_BINDING);// TODO this should be GL32
int oldVbo = GL32.glGetInteger(GL32.GL_ARRAY_BUFFER_BINDING);

vao = GL32.glGenVertexArrays();
GL32.glBindVertexArray(vao);
vbo = GL32.glGenBuffers();
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, vbo);
GL32.glBufferData(GL32.GL_ARRAY_BUFFER, buffer, GL32.GL_STATIC_DRAW);

GL32.glBindVertexArray(oldVao);
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, oldVbo);
if (loader != null) {
if (loader.isDone()) {
try {
int oldVao = GL32.glGetInteger(GL32.GL_VERTEX_ARRAY_BUFFER_BINDING);// TODO this should be GL32
int oldVbo = GL32.glGetInteger(GL32.GL_ARRAY_BUFFER_BINDING);

vao = GL32.glGenVertexArrays();
GL32.glBindVertexArray(vao);
vbo = GL32.glGenBuffers();
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, vbo);
GL32.glBufferData(GL32.GL_ARRAY_BUFFER, loader.get(), GL32.GL_STATIC_DRAW);

GL32.glBindVertexArray(oldVao);
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, oldVbo);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
loader = null;
}
} else {
// Start thread
loader = pool.submit(() -> {
VertexBuffer vb = buffer.get();
this.length = vb.data.length / (vb.stride);
this.vbInfo = new VertexBuffer(0, vb.hasNormals);
FloatBuffer buffer = BufferUtils.createFloatBuffer(vb.data.length);
buffer.put(vb.data);
buffer.position(0);
return buffer;
});
}
}

public Binding bind(RenderState state) {
return new Binding(state);
return bind(state, false);
}

public Binding bind(RenderState state, boolean waitForLoad) {
return new Binding(state, waitForLoad);
}

public class Binding implements With {
private final With restore;
private final RenderState state;

protected Binding(RenderState state) {
RenderContext.checkError();
if (vbo == -1) {
public boolean isLoaded() {
return vbo != -1;
}


protected Binding(RenderState state, boolean wait) {
if (!isLoaded()) {
init();
}
RenderContext.checkError();

lastUsed = System.currentTimeMillis();

if (!wait) {
if (!isLoaded()) {
restore = () -> {
};
this.state = null;
return;
}
} else {
while (!isLoaded()) {
init();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

settings.accept(state);
this.state = state;

ShaderInstance oldShader = RenderSystem.getShader();
int oldVao = GL32.glGetInteger(GL32.GL_VERTEX_ARRAY_BUFFER_BINDING);
int oldVbo = GL32.glGetInteger(GL32.GL_ARRAY_BUFFER_BINDING);
//int oldVao = GL32.glGetInteger(GL32.GL_VERTEX_ARRAY_BUFFER_BINDING);
//int oldVbo = GL32.glGetInteger(GL32.GL_ARRAY_BUFFER_BINDING);


/*
Expand Down Expand Up @@ -183,6 +234,9 @@ public void restore() {
}

protected With push(Consumer<RenderState> mod) {
if (!isLoaded()) {
return () -> {};
}
RenderState state = this.state.clone();
mod.accept(state);
return RenderContext.apply(state);
Expand All @@ -192,6 +246,9 @@ protected With push(Consumer<RenderState> mod) {
* Draw the entire VB
*/
public void draw() {
if (!isLoaded()) {
return;
}
GL32.glDrawArrays(GL32.GL_TRIANGLES, 0, length);
RenderContext.checkError();
}
Expand Down

0 comments on commit f7da14e

Please sign in to comment.