Skip to content

Commit

Permalink
Eh i forgor. but spheres?
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Oct 28, 2024
1 parent b7e286f commit a3f101a
Show file tree
Hide file tree
Showing 12 changed files with 791 additions and 363 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ subprojects {
dependencies {
minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
// The following line declares the mojmap mappings, you may use other mappings as well
mappings loom.officialMojangMappings()
mappings loom.layered() {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.20.2:2023.12.10@zip")
}
// The following line declares the yarn mappings you may select this one as well.
// mappings "net.fabricmc:yarn:@YARN_MAPPINGS@:v2"
}
Expand All @@ -58,6 +61,11 @@ allprojects {
// for more information about repositories.

maven { url "https://jitpack.io/" }

maven {
name = 'ParchmentMC'
url = 'https://maven.parchmentmc.org'
}
}

tasks.withType(JavaCompile) {
Expand Down
146 changes: 74 additions & 72 deletions common/src/main/java/dev/ultreon/mcgdx/GdxMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,38 @@
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.GdxNativesLoader;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.ultreon.mcgdx.api.Gdx3DRenderSource;
import dev.ultreon.mcgdx.api.McGdx;
import dev.ultreon.mcgdx.api.ModLoader;
import dev.ultreon.mcgdx.api.NamespaceID;
import dev.ultreon.mcgdx.impl.*;
import dev.ultreon.mcgdx.mixin.accessors.GameRendererAccessor;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.ApiStatus;
import org.joml.AxisAngle4f;
import org.joml.Vector3f;
import org.lwjgl.system.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.earlygrey.shapedrawer.ShapeDrawer;

@ApiStatus.Internal
@SuppressWarnings("GDXJavaStaticResource")
public class GdxMinecraft implements ApplicationListener {
public static final String MOD_ID = "mcgdx";
public static final Logger LOGGER = LoggerFactory.getLogger("GDX-Minecraft");
private static final Vector3f pos = new Vector3f();
private static final AxisAngle4f rotation = new AxisAngle4f();
private static final Vector3 tmp = new Vector3();

public static final Vector3f pos = new Vector3f();
public static final AxisAngle4f rotation = new AxisAngle4f();
public static final Vector3 tmp = new Vector3();
public static Color fogColor = new Color();
private static final ColorAttribute FOG = ColorAttribute.createFog(fogColor);
public static final ColorAttribute FOG = ColorAttribute.createFog(fogColor);

public static boolean disableCubemapUsage = false;

Expand Down Expand Up @@ -88,10 +92,7 @@ private GdxMinecraft() {
GdxMinecraft.app = new MinecraftApplication(this, new MinecraftApplicationConfiguration(), Minecraft.getInstance());
McGdx.blockEntityManager = new MinecraftBlockEntityManager();

McGdx.blockEntityManager.register(new NamespaceID("mcgdx", "example"), source -> {
ModelBatch batch1 = source.getBatch();
batch1.render(cubeInstance);
});
McGdx.blockEntityManager.register(new NamespaceID("mcgdx", "example"), GdxMinecraft::renderExample);
} catch (Throwable e) {
CrashReport libGDXCrash = new CrashReport("LibGDX failed to initialize", e);
CrashReportCategory libGDX = libGDXCrash.addCategory("LibGDX");
Expand All @@ -112,8 +113,67 @@ public static GdxMinecraft instance() {
return instance;
}

public static String toVert150(String vert120) {
vert120 = vert120.replace("\nattribute ", "\nin ");
vert120 = vert120.replace(" attribute ", " in ");

vert120 = vert120.replace("\nvarying ", "\nout ");
vert120 = vert120.replace(" varying ", " out ");

vert120 = vert120.replace("texture2D(", "texture(");

return vert120;
}

public static String toFrag150(String frag120) {
frag120 = frag120.replace("\nattribute ", "\nout ");
frag120 = frag120.replace(" attribute ", " out ");

frag120 = frag120.replace("\nvarying ", "\nin ");
frag120 = frag120.replace(" varying ", " in ");

if (frag120.contains("gl_FragColor")) {
frag120 = frag120.replace("void main()",
"out vec4 fragColor; \nvoid main()");
frag120 = frag120.replace("gl_FragColor", "fragColor");
}

frag120 = frag120.replace("texture2D(", "texture(");
frag120 = frag120.replace("textureCube(", "texture(");

return frag120;
}

public static final String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ "uniform mat4 u_projTrans;\n" //
+ "varying vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "\n" //
+ "void main()\n" //
+ "{\n" //
+ " v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ " v_color.a = v_color.a * (255.0/254.0);\n" //
+ " v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ " gl_Position = u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "}\n";
public static final String fragmentShader = "#ifdef GL_ES\n" //
+ "#define LOWP lowp\n" //
+ "precision mediump float;\n" //
+ "#else\n" //
+ "#define LOWP \n" //
+ "#endif\n" //
+ "varying LOWP vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "uniform sampler2D u_texture;\n" //
+ "void main()\n"//
+ "{\n" //
+ " gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" //
+ "}";

public static void initialize() {
batch = new SpriteBatch();
batch = new SpriteBatch(1000, new ShaderProgram(toVert150(toVert150(vertexShader)), toFrag150(fragmentShader)));
Pixmap whitePix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
whitePix.drawPixel(0, 0, 0xffffffff);
Texture white = new Texture(whitePix, true);
Expand All @@ -136,67 +196,9 @@ public static void init() {
}
}

public static void setupCamera(Camera camera, float f, PoseStack poseStack) {
PoseStack.Pose pose = poseStack.last();
pose.pose().getTranslation(pos);

if (camera instanceof PerspectiveCamera perspectiveCamera) {
perspectiveCamera.fieldOfView = (float) ((GameRendererAccessor) Minecraft.getInstance().gameRenderer).invokeGetFov(Minecraft.getInstance().gameRenderer.getMainCamera(), f, true);
camera.viewportWidth = Gdx.graphics.getWidth();
camera.viewportHeight = Gdx.graphics.getHeight();

FOG.color.set(fogColor);

GdxBlockEntityRenderer.environment.set(FOG);

camera.near = 0.05f;
camera.far = Minecraft.getInstance().gameRenderer.getDepthFar();

camera.position.set(-pos.x, -pos.y, -pos.z);
camera.direction.set(0, 0, -1);
camera.up.set(0, 1, 0);

pose.pose().getRotation(rotation);
camera.rotateAround(Vector3.Zero, tmp.set(rotation.x, rotation.y, rotation.z), rotation.angle);

float aspect = (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), perspectiveCamera.fieldOfView, aspect);
camera.view.setToTranslation(pos.x, pos.y, pos.z).rotateRad(rotation.x, rotation.y, rotation.z, rotation.angle);
camera.combined.set(camera.projection);
Matrix4.mul(camera.combined.val, camera.view.val);

camera.invProjectionView.set(camera.combined);
Matrix4.inv(camera.invProjectionView.val);
camera.frustum.update(camera.invProjectionView);
}
if (camera instanceof OrthographicCamera orthographicCamera) {
camera.viewportWidth = Gdx.graphics.getWidth();
camera.viewportHeight = Gdx.graphics.getHeight();

FOG.color.set(fogColor);

GdxBlockEntityRenderer.environment.set(FOG);

camera.near = 0f;
camera.far = 15000;

camera.position.set(-pos.x, -pos.y, -pos.z);
camera.direction.set(0, 0, -1);
camera.up.set(0, 1, 0);

pose.pose().getRotation(rotation);
camera.rotateAround(Vector3.Zero, tmp.set(rotation.x, rotation.y, rotation.z), rotation.angle);

float aspect = (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
camera.projection.setToOrtho(0, Gdx.graphics.getWidth(), 0, Gdx.graphics.getHeight(), Math.abs(camera.near), Math.abs(camera.far));
camera.view.setToTranslation(pos.x, pos.y, pos.z).rotateRad(rotation.x, rotation.y, rotation.z, rotation.angle);
camera.combined.set(camera.projection);
Matrix4.mul(camera.combined.val, camera.view.val);

camera.invProjectionView.set(camera.combined);
Matrix4.inv(camera.invProjectionView.val);
camera.frustum.update(camera.invProjectionView);
}
private static void renderExample(Gdx3DRenderSource<?> source) {
ModelBatch batch1 = source.getBatch();
batch1.render(cubeInstance, source.getEnvironment());
}

@Override
Expand Down
100 changes: 100 additions & 0 deletions common/src/main/java/dev/ultreon/mcgdx/impl/BlockEntityShader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package dev.ultreon.mcgdx.impl;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GLTexture;
import com.badlogic.gdx.graphics.g3d.Attributes;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.shaders.BaseShader;
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.sun.jna.platform.win32.GL;
import dev.ultreon.mcgdx.mixin.accessors.LightTextureAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.texture.DynamicTexture;
import org.jetbrains.annotations.NotNull;

import static dev.ultreon.mcgdx.impl.MinecraftRendererContext.packedLight;

public class BlockEntityShader extends DefaultShader {
private final int u_mcLight;
private final int u_mcLightTexture;

public BlockEntityShader(final Renderable renderable) {
this(renderable, new Config());
}

public BlockEntityShader(final Renderable renderable, final Config config) {
this(renderable, config, createPrefix(renderable, config));
}

public BlockEntityShader(final Renderable renderable, final Config config, final String prefix) {
this(renderable, config, prefix, config.vertexShader != null ? config.vertexShader : getDefaultVertexShader(),
config.fragmentShader != null ? config.fragmentShader : getDefaultFragmentShader());
}

public BlockEntityShader(final Renderable renderable, final Config config, final String prefix, final String vertexShader,
final String fragmentShader) {
this(renderable, config, new ShaderProgram(prefix + vertexShader, prefix + fragmentShader));
}

public BlockEntityShader(Renderable renderable, Config config, ShaderProgram shaderProgram) {
super(renderable, config, shaderProgram);

LightTexture lightTexture = Minecraft.getInstance().gameRenderer.lightTexture();
GLTexture glTexture = getGlTexture((LightTextureAccessor) lightTexture);

this.u_mcLight = register(new Uniform("u_mcLight"), new LocalSetter() {
@Override
public void set(BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
shader.set(inputID, (float) LightTexture.block(packedLight), (float) LightTexture.sky(packedLight));
}
});

this.u_mcLightTexture = register(new Uniform("u_mcLightTexture"), new GlobalSetter() {
@Override
public void set(BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
shader.set(inputID, glTexture);
}
});
}

private static @NotNull GLTexture getGlTexture(LightTextureAccessor lightTexture) {
LightTextureAccessor accessor = lightTexture;
DynamicTexture dynamicTexture = accessor.getLightTexture();

int id = dynamicTexture.getId();
// Unreloadable
return new GLTexture(GL20.GL_TEXTURE_2D, id) {
@Override
public int getWidth() {
return 16;
}

@Override
public int getHeight() {
return 16;
}

@Override
public int getDepth() {
return 32;
}

@Override
public boolean isManaged() {
return false;
}

@Override
protected void reload() {
// Unreloadable
}
};
}

public static String createPrefix(final Renderable renderable, final Config config) {
return "#version 150\n" + DefaultShader.createPrefix(renderable, config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package dev.ultreon.mcgdx.impl;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
import com.badlogic.gdx.graphics.g3d.utils.BaseShaderProvider;

public class BlockEntityShaderProvider extends BaseShaderProvider {
public final DefaultShader.Config config;

public BlockEntityShaderProvider(final DefaultShader.Config config) {
this.config = (config == null) ? new DefaultShader.Config() : config;
}

public BlockEntityShaderProvider(final String vertexShader, final String fragmentShader) {
this(new DefaultShader.Config(vertexShader, fragmentShader));
}

public BlockEntityShaderProvider(final FileHandle vertexShader, final FileHandle fragmentShader) {
this(vertexShader.readString(), fragmentShader.readString());
}

public BlockEntityShaderProvider() {
this(null);
}

@Override
protected Shader createShader (final Renderable renderable) {
return new BlockEntityShader(renderable, config);
}
}
Loading

0 comments on commit a3f101a

Please sign in to comment.