Skip to content

Commit

Permalink
2..4.11 - ITexture
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterpicker committed Nov 14, 2023
1 parent bb1c7df commit 1e7c24d
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 113 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.10-SNAPSHOT"
version = "2.4.11-SNAPSHOT"

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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 Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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 Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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 Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;
import gg.generations.rarecandy.renderer.loading.ITexture;

public abstract class TextureLoader {
private static TextureLoader instance;
Expand All @@ -16,9 +14,11 @@ public static void setInstance(TextureLoader instance) {
TextureLoader.instance = instance;
}

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

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

public abstract void remove(String name);

public abstract void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.thebombzen.jxlatte.JXLDecoder;
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public void close() throws IOException {
super.close();
model.close();
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gg.generations.rarecandy.renderer.loading;

import java.io.Closeable;

public interface ITexture extends Closeable {
void bind(int slot);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gg.generations.rarecandy.pokeutils.BlendType;
import gg.generations.rarecandy.pokeutils.CullType;
import gg.generations.rarecandy.pokeutils.reader.TextureLoader;
import gg.generations.rarecandy.renderer.loading.Texture;
import gg.generations.rarecandy.renderer.loading.ITexture;
import gg.generations.rarecandy.renderer.pipeline.Pipeline;

import java.io.Closeable;
Expand All @@ -30,7 +30,7 @@ public Material(String materialName, Map<String, String> images, Map<String, Obj
this.values = values;
}

public Texture getDiffuseTexture() {
public ITexture getDiffuseTexture() {
return getTexture("diffuse");
}

Expand All @@ -46,8 +46,8 @@ public BlendType blendType() {
return blendType;
}

public Texture getTexture(String imageType) {
return TextureLoader.instance().getTexture(images.get(imageType)).get();
public ITexture getTexture(String imageType) {
return TextureLoader.instance().getTexture(images.get(imageType));
}

public Object getValue(String valueType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gg.generations.rarecandy.renderer.pipeline;

import gg.generations.rarecandy.renderer.loading.Texture;
import gg.generations.rarecandy.renderer.loading.ITexture;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
Expand Down Expand Up @@ -109,7 +109,7 @@ private int getLocation() {
return locations[0];
}

public void uploadTexture(Texture texture, int slot) {
public void uploadTexture(ITexture texture, int slot) {
texture.bind(slot);
uploadInt(slot);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gg.generations.rarecandy.renderer.loading;

import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import org.lwjgl.opengl.GL11C;
import org.lwjgl.opengl.GL13C;
import org.lwjgl.opengl.GL40;
import org.lwjgl.system.MemoryUtil;

import java.awt.image.BufferedImage;
import java.io.Closeable;
import java.io.IOException;
import java.util.function.Supplier;

public class Texture implements ITexture, Supplier<ITexture> {

public final String name;
public int id;

private boolean initalized = false;

private final BufferedImage image;

public Texture(TextureReference reference) {
name = reference.name();
image = reference.data();

}

public void bind(int slot) {
assert (slot >= 0 && slot <= 31);
GL13C.glActiveTexture(GL13C.GL_TEXTURE0 + slot);
GL11C.glBindTexture(GL11C.GL_TEXTURE_2D, this.id);
}

@Override
public void close() throws IOException {
GL40.glDeleteTextures(id);
}

@Override
public ITexture get() {
if(!initalized) {
id = GL11C.glGenTextures();

var buffer = TextureReference.read(image);

GL11C.glBindTexture(GL11C.GL_TEXTURE_2D, this.id);
GL11C.glTexImage2D(GL11C.GL_TEXTURE_2D, 0, GL11C.GL_RGBA8, image.getWidth(), image.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(buffer);

initalized = true;
}

return this;
}
}
16 changes: 10 additions & 6 deletions src/main/java/gg/generations/rarecandy/tools/TextureLoader.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package gg.generations.rarecandy.tools;

import gg.generations.rarecandy.pokeutils.reader.TextureReference;
import gg.generations.rarecandy.renderer.loading.ITexture;
import gg.generations.rarecandy.renderer.loading.Texture;
import gg.generations.rarecandy.renderer.model.material.CloseableSupplier;
import gg.generations.rarecandy.renderer.model.material.ImageSupplier;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TextureLoader extends gg.generations.rarecandy.pokeutils.reader.TextureLoader {
public static Map<String, CloseableSupplier<Texture>> MAP = new HashMap<>();
public static Map<String, Texture> MAP = new HashMap<>();

@Override
public CloseableSupplier<Texture> getTexture(String name) {
return MAP.getOrDefault(name, ImageSupplier.BLANK);
public ITexture getTexture(String name) {
return MAP.getOrDefault(name, null).get();
}

@Override
public void register(String name, TextureReference reference) {
MAP.computeIfAbsent(name, s -> new ImageSupplier(reference));
MAP.computeIfAbsent(name, s -> new Texture(reference));
}

@Override
Expand All @@ -32,4 +31,9 @@ public void remove(String name) {
}
}
}

@Override
public void clear() {
MAP.clear();
}
}

0 comments on commit 1e7c24d

Please sign in to comment.