Skip to content

Commit

Permalink
2.4.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterpicker committed Nov 14, 2023
1 parent eaf1c48 commit bb1c7df
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 98 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "gg.generations"
version = "2.4.9-SNAPSHOT"
version = "2.4.10-SNAPSHOT"

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.generations.rarecandy.pokeutils;

import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import gg.generations.rarecandy.renderer.model.material.ImageSupplier;
import gg.generations.rarecandy.renderer.model.material.Material;
import org.jetbrains.annotations.NotNull;

Expand All @@ -18,13 +19,8 @@ public class CustomMaterialReference extends MaterialReference {
private Map<String, String> textureMap = new HashMap<>();

@Override
public Material process(String name, @NotNull Map<String, TextureReference> images) {
var map = textureMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, entry -> {
var reference = images.get(entry.getValue());

if(reference != null) return new Material.ImageSupplier(reference);
else return Material.ImageSupplier.BLANK;
}));
public Material process(String name, @NotNull Map<String, String> images) {
var map = textureMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, entry -> images.get(entry.getValue())));

return new Material(name, map, Map.of(), cull, blend, shader);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.generations.rarecandy.pokeutils;

import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import gg.generations.rarecandy.renderer.model.material.ImageSupplier;
import gg.generations.rarecandy.renderer.model.material.Material;

import java.util.Map;
Expand All @@ -22,11 +23,9 @@ public DiffuseMaterialReference(String texture) {
}

@Override
public Material process(String name, Map<String, TextureReference> images) {
public Material process(String name, Map<String, String> images) {
var reference = images.get(texture);

var supplier = reference != null ? new Material.ImageSupplier(reference) : Material.ImageSupplier.BLANK;

return new Material(name, Map.of("diffuse", supplier), Map.of(), cullType(), blendType(), shader());
return new Material(name, Map.of("diffuse", reference), Map.of(), cullType(), blendType(), shader());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.generations.rarecandy.pokeutils;

import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import gg.generations.rarecandy.renderer.model.material.ImageSupplier;
import gg.generations.rarecandy.renderer.model.material.Material;
import org.joml.Vector3f;

Expand All @@ -22,14 +23,10 @@ public String shader() {
}

@Override
public Material process(String name, Map<String, TextureReference> images) {
public Material process(String name, Map<String, String> images) {
var reference = images.get(texture);
var maskReference = images.get(maskTexture);


var diffuse = reference != null ? new Material.ImageSupplier(reference) : Material.ImageSupplier.BLANK;
var mask = maskReference != null ? new Material.ImageSupplier(maskReference) : Material.ImageSupplier.BLANK;

return new Material(name, Map.of("diffuse", diffuse, "mask", mask), Map.of("color", color), cullType(), blendType(), shader());
return new Material(name, Map.of("diffuse", reference, "mask", maskReference), Map.of("color", color), cullType(), blendType(), shader());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Map;

public abstract class MaterialReference {
public abstract Material process(String name, Map<String, TextureReference> images);
public abstract Material process(String name, Map<String, String> images);
public static final class Serializer implements JsonDeserializer<MaterialReference> {
@Override
public MaterialReference deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gg.generations.rarecandy.pokeutils.reader;

import gg.generations.rarecandy.renderer.loading.Texture;
import gg.generations.rarecandy.renderer.model.material.CloseableSupplier;
import gg.generations.rarecandy.renderer.model.material.ImageSupplier;

public abstract class TextureLoader {
private static TextureLoader instance;


public static TextureLoader instance() {
return instance;
}

public static void setInstance(TextureLoader instance) {
TextureLoader.instance = instance;
}

public abstract CloseableSupplier<Texture> getTexture(String name);

public abstract void register(String name, TextureReference reference);

public abstract void remove(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.thebombzen.jxlatte.JXLOptions;
import de.javagl.jgltf.model.image.PixelData;
import gg.generations.rarecandy.pokeutils.util.DefaultPixelData;
import gg.generations.rarecandy.renderer.loading.Texture;
import org.lwjgl.system.MemoryUtil;

import javax.imageio.ImageIO;
Expand All @@ -14,8 +15,10 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Objects;

public record TextureReference(BufferedImage data, String name) {

public record TextureReference(PixelData data, String name) {
public static TextureReference read(byte[] imageBytes, String name) throws IOException {
BufferedImage pixelData;
BufferedImage temp;
Expand All @@ -42,10 +45,10 @@ public static TextureReference read(byte[] imageBytes, String name) throws IOExc
}
}

return new TextureReference(read(pixelData), name);
return new TextureReference(pixelData, name);
}

public static PixelData read(BufferedImage image) {
public static ByteBuffer read(BufferedImage image) {
if (image == null) {
// ImageUtils.warning("Could not read image from image data");
return null;
Expand All @@ -60,8 +63,6 @@ public static PixelData read(BufferedImage image) {
if (buffer instanceof DataBufferFloat intBuffer) {
var rawData = intBuffer.getData();
readyData = MemoryUtil.memAlloc(rawData.length * 4);
width = image.getWidth();
height = image.getHeight();

for (var hdrChannel : rawData) {
var channelValue = hdrToRgb(hdrChannel);
Expand All @@ -72,8 +73,6 @@ public static PixelData read(BufferedImage image) {
} else if (buffer instanceof DataBufferInt floatBuffer) {
var rawData = floatBuffer.getData();
readyData = MemoryUtil.memAlloc(rawData.length * 4);
width = image.getWidth();
height = image.getHeight();

for (var pixel : rawData) {
readyData.put((byte) ((pixel >> 16) & 0xFF));
Expand All @@ -86,17 +85,32 @@ public static PixelData read(BufferedImage image) {
} else if (buffer instanceof DataBufferByte dataBufferByte) {
var rawData = dataBufferByte.getData();
readyData = MemoryUtil.memAlloc(rawData.length);
width = image.getWidth();
height = image.getHeight();

readyData.put(rawData);
readyData.flip();
} else throw new RuntimeException("Unknown Data Type: " + buffer.getClass().getName());

return new DefaultPixelData(width, height, readyData);
return readyData;
}

private static int hdrToRgb(float hdr) {
return (int) Math.min(Math.max(Math.pow(hdr, 1.0 / 2.2) * 255, 0), 255);
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (TextureReference) obj;
return Objects.equals(this.data, that.data) &&
Objects.equals(this.name, that.name);
}

@Override
public String toString() {
return "TextureReference[" +
"data=" + data + ", " +
"name=" + name + ']';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
*/
package gg.generations.rarecandy.pokeutils.util;

import de.javagl.jgltf.model.io.Buffers;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
Expand Down Expand Up @@ -134,4 +139,22 @@ private static IntBuffer getBuffer(BufferedImage image) {
return IntBuffer.wrap(dataBufferInt.getData());
}


public static BufferedImage readAsBufferedImage(ByteBuffer byteBuffer)
{
if (byteBuffer == null)
{
return null;
}
try (InputStream inputStream =
Buffers.createByteBufferInputStream(byteBuffer.slice()))
{
return ImageIO.read(inputStream);
}
catch (IOException e)
{
// logger.severe(e.toString());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ public <V extends RenderObject> void render(List<ObjectInstance> instances, V ob
// pl.unbind();
// });
// } else {
for (T object : this.objects) {
object.render(instances, object);
}
this.objects.forEach(object -> object.render(instances, object));
// }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import dev.thecodewarrior.binarysmd.studiomdl.SMDFile;
import dev.thecodewarrior.binarysmd.studiomdl.SkeletonBlock;
import gg.generations.rarecandy.pokeutils.*;
import gg.generations.rarecandy.pokeutils.GFLib.Anim.AnimationT;
import gg.generations.rarecandy.pokeutils.reader.TextureLoader;
import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import gg.generations.rarecandy.pokeutils.util.ImageUtils;
import gg.generations.rarecandy.renderer.ThreadSafety;
import gg.generations.rarecandy.renderer.animation.Animation;
import gg.generations.rarecandy.renderer.animation.Skeleton;
Expand All @@ -32,6 +35,7 @@
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -50,11 +54,11 @@ public ModelLoader() {
this.modelLoadingPool = Executors.newFixedThreadPool(2);
}

public static <T extends MeshObject> void create2(MultiRenderObject<T> objects, GltfModel gltfModel, Map<String, SMDFile> smdFileMap, Map<String, byte[]> gfbFileMap, Map<String, byte[]> tranmFilesMap, Map<String, TextureReference> images, ModelConfig config, List<Runnable> glCalls, Supplier<T> supplier) {
public static <T extends MeshObject> void create2(MultiRenderObject<T> objects, GltfModel gltfModel, Map<String, SMDFile> smdFileMap, Map<String, byte[]> gfbFileMap, Map<String, byte[]> tranmFilesMap, Map<String, String> images, ModelConfig config, List<Runnable> glCalls, Supplier<T> supplier) {
create2(objects, gltfModel, smdFileMap, gfbFileMap, tranmFilesMap, images, config, glCalls, supplier, Animation.GLB_SPEED);
}

public static <T extends MeshObject> void create2(MultiRenderObject<T> objects, GltfModel gltfModel, Map<String, SMDFile> smdFileMap, Map<String, byte[]> gfbFileMap, Map<String, byte[]> tranmFilesMap, Map<String, TextureReference> images, ModelConfig config, List<Runnable> glCalls, Supplier<T> supplier, int animationSpeed) {
public static <T extends MeshObject> void create2(MultiRenderObject<T> objects, GltfModel gltfModel, Map<String, SMDFile> smdFileMap, Map<String, byte[]> gfbFileMap, Map<String, byte[]> tranmFilesMap, Map<String, String> images, ModelConfig config, List<Runnable> glCalls, Supplier<T> supplier, int animationSpeed) {
checkForRootTransformation(objects, gltfModel);
if (gltfModel.getSceneModels().size() > 1) throw new RuntimeException("Cannot handle more than one scene");

Expand All @@ -80,7 +84,7 @@ public static <T extends MeshObject> void create2(MultiRenderObject<T> objects,
for (var entry : gfbFileMap.entrySet()) {
var name = entry.getKey();

var gfbAnim = gg.generations.rarecandy.pokeutils.GFLib.Anim.AnimationT.deserializeFromBinary(entry.getValue());
var gfbAnim = AnimationT.deserializeFromBinary(entry.getValue());

if(gfbAnim.getSkeleton() == null) continue;

Expand Down Expand Up @@ -165,21 +169,27 @@ public static <T extends MeshObject> void create2(MultiRenderObject<T> objects,
var matMap = reverseMap(variantMaterialMap);
var hidMap = reverseListMap(variantHideMap);

for (NodeModel nodeModel : gltfModel.getSceneModels().get(0).getNodeModels()) {
System.out.println(nodeModel.getName());
}
// for (NodeModel nodeModel : gltfModel.getSceneModels().get(0).getNodeModels()) {
// System.out.println(nodeModel.getName());
// }
for (var node : gltfModel.getSceneModels().get(0).getNodeModels()) {
var transform = new Matrix4f();

traverseTree(transform, node, objects, supplier, matMap, hidMap, glCalls, skeleton, animations, hideDuringAnimation);
}

System.out.println();
} else {

//Original model loading code

var textures = gltfModel.getTextureModels().stream().collect(Collectors.toMap(textureModel -> textureModel.getImageModel().getName(), raw -> new TextureReference(PixelDatas.create(raw.getImageModel().getImageData()), raw.getImageModel().getName())));
var textures = gltfModel.getTextureModels().stream().collect(Collectors.toMap(textureModel -> textureModel.getImageModel().getName(), raw -> {
var id = gltfModel.getNodeModels().get(0).getName() + "-" + raw.getImageModel().getName();

var image = ImageUtils.readAsBufferedImage(raw.getImageModel().getImageData());

TextureLoader.instance().register(id, new TextureReference(image, raw.getImageModel().getName()));

return id;
}));

var materials = gltfModel.getMaterialModels().stream().map(MaterialModelV2.class::cast).map(raw -> {
var textureName = raw.getBaseColorTexture().getImageModel().getName();
Expand Down Expand Up @@ -519,14 +529,17 @@ private <T extends RenderObject> Runnable threadedCreateObject(MultiRenderObject
});
}

private Map<String, TextureReference> readImages(PixelAsset asset) {
private Map<String, String> readImages(PixelAsset asset) {
var images = asset.getImageFiles();
var map = new HashMap<String, TextureReference>();
var map = new HashMap<String, String>();
for (var entry : images) {
var key = entry.getKey();

try {
map.put(key, TextureReference.read(entry.getValue(), key));
var id = asset.modelName + "-" + key;
TextureLoader.instance().register(id, TextureReference.read(entry.getValue(), key));

map.put(key, id);
} catch (IOException e) {
System.out.print("Error couldn't load: " + key); //TODO: Logger solution.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ public class Texture implements Closeable {
public final String name;
public final int id;

public Texture(String name, int id) {
this.name = name;
this.id = id;
}

public Texture(TextureReference reference) {
this.name = reference.name();
this.id = GL11C.glGenTextures();
this(reference.name(), GL11C.glGenTextures());

var buffer = TextureReference.read(reference.data());

GL13C.glActiveTexture(GL13C.GL_TEXTURE0);
GL11C.glBindTexture(GL11C.GL_TEXTURE_2D, this.id);
GL11C.glTexImage2D(GL11C.GL_TEXTURE_2D, 0, GL11C.GL_RGBA8, reference.data().getWidth(), reference.data().getHeight(), 0, GL11C.GL_RGBA, GL11C.GL_UNSIGNED_BYTE, reference.data().getPixelsRGBA());
GL11C.glTexImage2D(GL11C.GL_TEXTURE_2D, 0, GL11C.GL_RGBA8, reference.data().getWidth(), reference.data().getHeight(), 0, GL11C.GL_RGBA, GL11C.GL_UNSIGNED_BYTE, buffer);

GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_WRAP_S, GL11C.GL_REPEAT);
GL11C.glTexParameteri(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_WRAP_T, GL11C.GL_REPEAT);

GL11C.glTexParameterf(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_MIN_FILTER, GL11C.GL_NEAREST);
GL11C.glTexParameterf(GL11C.GL_TEXTURE_2D, GL11C.GL_TEXTURE_MAG_FILTER, GL11C.GL_NEAREST);

MemoryUtil.memFree(reference.data().getPixelsRGBA());
MemoryUtil.memFree(buffer);
}

public void bind(int slot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
@FunctionalInterface
public interface GlCallSupplier<T extends RenderObject> {

List<Runnable> getCalls(GltfModel model, Map<String, SMDFile> smdFiles, Map<String, byte[]> gfbamnFiles, Map<String, byte[]> tranmFiles, Map<String, TextureReference> imageFiles, ModelConfig config, MultiRenderObject<T> mro);
List<Runnable> getCalls(GltfModel model, Map<String, SMDFile> smdFiles, Map<String, byte[]> gfbamnFiles, Map<String, byte[]> tranmFiles, Map<String, String> imageFiles, ModelConfig config, MultiRenderObject<T> mro);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gg.generations.rarecandy.renderer.model.material;

import java.io.Closeable;
import java.util.function.Supplier;

public interface CloseableSupplier<T> extends Supplier<T>, Closeable {

}
Loading

0 comments on commit bb1c7df

Please sign in to comment.