Skip to content

Commit

Permalink
Model Viewer: Code cleanup and organization
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Aug 10, 2024
1 parent 265c828 commit 6817106
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.decima.model.viewer.Camera;
import com.shade.decima.model.viewer.ModelViewport;
import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.isr.impl.NodeModel;
import com.shade.decima.model.viewer.NodeModel;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.decima.ui.data.ValueController;
import com.shade.decima.ui.data.viewer.model.isr.SceneSerializer;
import com.shade.decima.ui.data.viewer.model.scene.SceneSerializer;
import com.shade.decima.ui.menu.MenuConstants;
import com.shade.platform.model.Disposable;
import com.shade.platform.model.data.DataKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.shade.decima.model.app.Project;
import com.shade.decima.model.rtti.RTTICoreFile;
import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.decima.ui.data.viewer.model.ModelExporter;
import com.shade.decima.ui.data.viewer.model.ModelExporterProvider;
import com.shade.decima.ui.data.viewer.model.isr.SceneSerializer;
import com.shade.decima.ui.data.viewer.model.scene.SceneSerializer;
import com.shade.platform.model.runtime.ProgressMonitor;
import com.shade.util.NotNull;
import org.joml.Matrix4f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.google.gson.stream.JsonWriter;
import com.shade.decima.BuildConfig;
import com.shade.decima.model.viewer.isr.*;
import com.shade.decima.model.viewer.isr.impl.DynamicBuffer;
import com.shade.decima.model.viewer.scene.*;
import com.shade.gl.Attribute.ComponentType;
import com.shade.gl.Attribute.ElementType;
import com.shade.gl.Attribute.Semantic;
Expand Down Expand Up @@ -37,10 +36,11 @@ enum BinaryChunkType {

public static void writeBinary(@NotNull ProgressMonitor monitor, @NotNull Node node, @NotNull SeekableByteChannel channel) throws IOException {
try (var task = monitor.begin("Serializing scene", 2)) {
final CompactingNodeVisitor visitor = new CompactingNodeVisitor(task);
final DynamicBuffer buffer = new DynamicBuffer(0);
final Context context = new Context();

try (var ignored = task.split(1).begin("Compacting scene")) {
final CompactingNodeVisitor visitor = new CompactingNodeVisitor(task, buffer);
final Node transformed = node.apply(visitor);

if (transformed != null) {
Expand All @@ -54,7 +54,7 @@ public static void writeBinary(@NotNull ProgressMonitor monitor, @NotNull Node n
write(context, new JsonWriter(json), false);

final ByteBuffer jsonBuffer = ByteBuffer.wrap(json.toString().getBytes(StandardCharsets.UTF_8));
final ByteBuffer binaryBuffer = visitor.buffer.asByteBuffer();
final ByteBuffer binaryBuffer = buffer.asByteBuffer();

writeBinaryHeader(0, channel);
writeBinaryChunk(BinaryChunkType.JSON, jsonBuffer, channel);
Expand All @@ -66,7 +66,8 @@ public static void writeBinary(@NotNull ProgressMonitor monitor, @NotNull Node n

public static void writeText(@NotNull ProgressMonitor monitor, @NotNull Node node, @NotNull JsonWriter writer) throws IOException {
try (var task = monitor.begin("Serializing scene", 2)) {
final CompactingNodeVisitor visitor = new CompactingNodeVisitor(task);
final DynamicBuffer buffer = new DynamicBuffer(0);
final CompactingNodeVisitor visitor = new CompactingNodeVisitor(task, buffer);
final Context context = new Context();

try (var ignored = task.split(1).begin("Compacting scene")) {
Expand Down Expand Up @@ -352,12 +353,13 @@ private static String getSemanticName(@NotNull Semantic semantic) {
}

private static class CompactingNodeVisitor implements NodeVisitor<Node> {
private final DynamicBuffer buffer = new DynamicBuffer(0);
private final Map<Mesh, List<Node>> meshes = new HashMap<>();
private final ProgressMonitor.IndeterminateTask task;
private final DynamicBuffer buffer;

public CompactingNodeVisitor(@NotNull ProgressMonitor.IndeterminateTask task) {
public CompactingNodeVisitor(@NotNull ProgressMonitor.IndeterminateTask task, @NotNull DynamicBuffer buffer) {
this.task = task;
this.buffer = buffer;
}

@Nullable
Expand Down Expand Up @@ -501,6 +503,40 @@ private Accessor compactAccessor(@Nullable Semantic semantic, @NotNull Accessor
}
}

private static class DynamicBuffer implements Buffer {
private byte[] data;

public DynamicBuffer(int size) {
this.data = new byte[size];
}

public void grow(int size) {
if (size <= 0) {
throw new IllegalArgumentException("size must be positive");
}
this.data = Arrays.copyOf(data, data.length + size);
}

@NotNull
@Override
public BufferView asView(int offset, int length) {
return new BufferView(this, offset, length);
}

@NotNull
@Override
public ByteBuffer asByteBuffer() {
return ByteBuffer
.wrap(data)
.order(ByteOrder.LITTLE_ENDIAN);
}

@Override
public int length() {
return data.length;
}
}

private static class Context {
private final List<Scene> scenes = new ArrayList<>();
private final List<Node> nodes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.shade.decima.ui.data.viewer.model.menu;

import com.shade.decima.model.viewer.ModelViewport;
import com.shade.decima.model.viewer.isr.impl.NodeModel;
import com.shade.decima.model.viewer.NodeModel;
import com.shade.platform.ui.menus.MenuItem;
import com.shade.platform.ui.menus.MenuItemContext;
import com.shade.platform.ui.menus.MenuItemRegistration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.ui.data.viewer.model.isr;
package com.shade.decima.ui.data.viewer.model.scene;

import com.shade.decima.model.app.Project;
import com.shade.decima.model.base.GameType;
Expand All @@ -7,9 +7,8 @@
import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.decima.model.rtti.objects.RTTIReference;
import com.shade.decima.model.rtti.types.java.HwDataSource;
import com.shade.decima.model.viewer.isr.*;
import com.shade.decima.model.viewer.isr.Accessor.Target;
import com.shade.decima.model.viewer.isr.impl.StaticBuffer;
import com.shade.decima.model.viewer.scene.*;
import com.shade.decima.model.viewer.scene.Accessor.Target;
import com.shade.decima.ui.data.ValueController;
import com.shade.gl.Attribute.ComponentType;
import com.shade.gl.Attribute.ElementType;
Expand All @@ -24,6 +23,8 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -761,4 +762,25 @@ private static class Context {
private record WithPosition<T>(T value, int position) {}

private record IndexRange(int start, int end) {}

private record StaticBuffer(@NotNull byte[] data) implements Buffer {
@NotNull
@Override
public BufferView asView(int offset, int length) {
return new BufferView(this, offset, length);
}

@NotNull
@Override
public ByteBuffer asByteBuffer() {
return ByteBuffer
.wrap(data)
.order(ByteOrder.LITTLE_ENDIAN);
}

@Override
public int length() {
return data.length;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private void updatePan(@NotNull InputState input, float dt, @NotNull Vector2f mo
}

public void resize(int width, int height) {
if (windowSize.equals(width, height)) {
return;
}
final ModelViewerSettings settings = ModelViewerSettings.getInstance();
windowSize.set(width, height);
aspectRatio = (float) width / height;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.shade.decima.model.viewer;

import com.formdev.flatlaf.util.UIScale;
import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.isr.impl.NodeModel;
import com.shade.decima.model.viewer.outline.OutlineDialog;
import com.shade.decima.model.viewer.renderer.DebugRenderer;
import com.shade.decima.model.viewer.renderer.GridRenderer;
import com.shade.decima.model.viewer.renderer.ModelRenderer;
import com.shade.decima.model.viewer.renderer.OutlineRenderer;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.gl.DebugGroup;
import com.shade.platform.model.Disposable;
import com.shade.platform.model.data.DataKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.shade.decima.model.viewer.isr.impl;
package com.shade.decima.model.viewer;

import com.shade.decima.model.viewer.Model;
import com.shade.decima.model.viewer.ModelViewport;
import com.shade.decima.model.viewer.isr.*;
import com.shade.decima.model.viewer.scene.*;
import com.shade.decima.model.viewer.shader.ModelShaderProgram;
import com.shade.decima.model.viewer.shader.RegularShaderProgram;
import com.shade.gl.Attribute;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shade.decima.model.viewer.outline;

import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.platform.ui.util.UIUtils;
import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.shade.decima.model.viewer.outline;

import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.outline.menu.OutlineMenuConstants;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.platform.ui.controls.tree.Tree;
import com.shade.platform.ui.menus.MenuManager;
import com.shade.util.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shade.decima.model.viewer.outline;

import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.platform.model.runtime.ProgressMonitor;
import com.shade.platform.ui.controls.tree.TreeNode;
import com.shade.platform.ui.controls.tree.TreeNodeLazy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.shade.decima.model.viewer.outline.menu;

import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.outline.OutlineTreeNode;
import com.shade.decima.model.viewer.scene.Node;
import com.shade.platform.ui.PlatformDataKeys;
import com.shade.platform.ui.menus.MenuItem;
import com.shade.platform.ui.menus.MenuItemContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.shade.decima.model.viewer.renderer;

import com.shade.decima.model.viewer.Camera;
import com.shade.decima.model.viewer.Model;
import com.shade.decima.model.viewer.ModelViewport;
import com.shade.decima.model.viewer.Renderer;
import com.shade.decima.model.viewer.isr.impl.NodeModel;
import com.shade.decima.model.viewer.*;
import com.shade.decima.model.viewer.shader.NormalShaderProgram;
import com.shade.decima.model.viewer.shader.RegularShaderProgram;
import com.shade.platform.model.Disposable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,20 @@ public void dispose() {
Disposable.dispose(outlineProgram);
outlineProgram = null;

if (framebufferId != 0)
lastWidth = 0;
lastHeight = 0;

if (framebufferId != 0) {
glDeleteFramebuffers(framebufferId);
if (colorTextureId != 0)
}
if (colorTextureId != 0) {
glDeleteTextures(colorTextureId);
if (maskTextureId != 0)
}
if (maskTextureId != 0) {
glDeleteTextures(maskTextureId);
if (depthBufferId != 0)
}
if (depthBufferId != 0) {
glDeleteRenderbuffers(depthBufferId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.model.viewer.isr;
package com.shade.decima.model.viewer.scene;

import com.shade.gl.Attribute.ComponentType;
import com.shade.gl.Attribute.ElementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.model.viewer.isr;
package com.shade.decima.model.viewer.scene;

import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.model.viewer.isr;
package com.shade.decima.model.viewer.scene;

import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.model.viewer.isr;
package com.shade.decima.model.viewer.scene;

import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.model.viewer.isr;
package com.shade.decima.model.viewer.scene;

import com.shade.util.NotNull;
import com.shade.util.Nullable;
Expand Down
Loading

0 comments on commit 6817106

Please sign in to comment.