Skip to content

Commit

Permalink
Error
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Oct 7, 2024
1 parent 30b0d24 commit 77d5b5c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 54 deletions.
81 changes: 75 additions & 6 deletions common/src/main/java/dev/ultreon/mcgdx/GdxMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@
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.FrameBufferCubemap;
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.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.joml.AxisAngle4f;
import org.joml.Vector3f;
import org.lwjgl.system.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -52,14 +56,15 @@
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 Color fogColor = new Color();
// public static Cubemap cubeMap;
//
// public static FrameBufferCubemap cubemapFBO;
private static final ColorAttribute FOG = ColorAttribute.createFog(fogColor);

public static boolean disableCubemapUsage = false;

private static GdxMinecraft instance;
private static Material material;
private static Model cube;
private static ModelInstance cubeInstance;
private final Lwjgl3WindowListener windowListener = new UselessWindowListener();
Expand Down Expand Up @@ -117,7 +122,7 @@ public static void initialize() {
gdxThread = Thread.currentThread();
bitmapFont = new BitmapFont(true);

material = new Material("box_id");
Material material = new Material("box_id");
material.set(ColorAttribute.createDiffuse(new Color(1, 1, 1, 1)));
ModelBuilder modelBuilder = new ModelBuilder();
cube = modelBuilder.createSphere(1, 1, 1, 50, 50, GL20.GL_TRIANGLES, material, VertexAttributes.Usage.Normal | VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates | VertexAttributes.Usage.ColorPacked);
Expand All @@ -131,6 +136,69 @@ 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);
}
}

@Override
public void create() {
Gdx.app.log("GDX-Minecraft", "LibGDX version: " + Version.VERSION);
Expand Down Expand Up @@ -202,6 +270,7 @@ public void resume() {
@Override
public void dispose() {
batch.dispose();
cube.dispose();
}

public ApplicationLogger getApplicationLogger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,17 @@
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.DefaultRenderableSorter;
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.ultreon.mcgdx.GdxMinecraft;
import dev.ultreon.mcgdx.api.Gdx3DRenderSource;
import dev.ultreon.mcgdx.mixin.accessors.GameRendererAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import org.joml.AxisAngle4f;
import org.joml.Vector3f;

public class GdxBlockEntityRenderer implements BlockEntityRenderer<GdxBlockEntity>, Gdx3DRenderSource<GdxBlockEntity> {
private final PerspectiveCamera camera = new PerspectiveCamera(70, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
private final Vector3f pos = new Vector3f();
private final AxisAngle4f rotation = new AxisAngle4f();

private final ModelBatch batch = new ModelBatch(new DefaultShaderProvider("""
#if defined(diffuseTextureFlag) || defined(specularTextureFlag) || defined(emissiveTextureFlag)
Expand Down Expand Up @@ -265,10 +257,10 @@ void main() {
#ifdef boneWeight0Flag
skinning += (a_boneWeight0.y) * u_bones[int(a_boneWeight0.x)];
#endif //boneWeight0Flag
#ifdef boneWeight1Flag
#ifdef boneWeight1Flag
skinning += (a_boneWeight1.y) * u_bones[int(a_boneWeight1.x)];
#endif //boneWeight1Flag
#ifdef boneWeight2Flag
#ifdef boneWeight2Flag
skinning += (a_boneWeight2.y) * u_bones[int(a_boneWeight2.x)];
#endif //boneWeight2Flag
#ifdef boneWeight3Flag
Expand Down Expand Up @@ -324,7 +316,7 @@ void main() {
vec3 ambientLight = vec3(0.0);
#endif
#ifdef ambientCubemapFlag
#ifdef ambientCubemapFlag
vec3 squaredNormal = normal * normal;
vec3 isPositive = step(0.0, normal);
ambientLight += squaredNormal.x * mix(u_ambientCubemap[0], u_ambientCubemap[1], isPositive.x) +
Expand All @@ -341,7 +333,7 @@ void main() {
ambientLight += u_sphericalHarmonics[5] * (normal.z * normal.y);
ambientLight += u_sphericalHarmonics[6] * (normal.y * normal.x);
ambientLight += u_sphericalHarmonics[7] * (3.0 * normal.z * normal.z - 1.0);
ambientLight += u_sphericalHarmonics[8] * (normal.x * normal.x - normal.y * normal.y);
ambientLight += u_sphericalHarmonics[8] * (normal.x * normal.x - normal.y * normal.y);
#endif // sphericalHarmonicsFlag
#ifdef ambientFlag
Expand Down Expand Up @@ -679,9 +671,7 @@ void main() {

});
private final Gdx3DRenderable renderable;
private final Vector3 tmp = new Vector3();
private final Environment environment = new Environment();
private final ColorAttribute FOG = ColorAttribute.createFog(GdxMinecraft.fogColor);
public static final Environment environment = new Environment();

public GdxBlockEntityRenderer(BlockEntityRendererProvider.Context context, Gdx3DRenderable renderable) {
this.renderable = renderable;
Expand All @@ -690,39 +680,11 @@ public GdxBlockEntityRenderer(BlockEntityRendererProvider.Context context, Gdx3D

@Override
public final void render(GdxBlockEntity blockEntity, float f, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int j) {
PoseStack.Pose pose = poseStack.last();
pose.pose().getTranslation(pos);

camera.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(GdxMinecraft.fogColor);

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), camera.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);
GdxMinecraft.setupCamera(camera, f, poseStack);

batch.begin(camera);
Gdx.gl.glCullFace(GL20.GL_BACK);
RenderContext renderContext = batch.getRenderContext();

render(blockEntity);

Expand Down
38 changes: 38 additions & 0 deletions common/src/main/resources/assets/mcgdx/models/item/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"parent": "builtin/entity",
"textures": {
"particle": "block/oak_planks"
},
"display": {
"gui": {
"rotation": [ 30, 45, 0 ],
"translation": [ 0, 0, 0],
"scale":[ 0.625, 0.625, 0.625 ]
},
"ground": {
"rotation": [ 0, 0, 0 ],
"translation": [ 0, 3, 0],
"scale":[ 0.25, 0.25, 0.25 ]
},
"head": {
"rotation": [ 0, 180, 0 ],
"translation": [ 0, 0, 0],
"scale":[ 1, 1, 1]
},
"fixed": {
"rotation": [ 0, 180, 0 ],
"translation": [ 0, 0, 0],
"scale":[ 0.5, 0.5, 0.5 ]
},
"thirdperson_righthand": {
"rotation": [ 75, 315, 0 ],
"translation": [ 0, 2.5, 0],
"scale": [ 0.375, 0.375, 0.375 ]
},
"firstperson_righthand": {
"rotation": [ 0, 315, 0 ],
"translation": [ 0, 0, 0],
"scale": [ 0.4, 0.4, 0.4 ]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package dev.ultreon.mcgdx.impl.fabric;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import dev.architectury.registry.client.level.entity.EntityRendererRegistry;
import dev.architectury.registry.client.rendering.BlockEntityRendererRegistry;
import dev.architectury.registry.item.ItemPropertiesRegistry;
import dev.ultreon.mcgdx.GdxMinecraft;
import dev.ultreon.mcgdx.api.GameEnvironment;
import dev.ultreon.mcgdx.api.ModLoader;
Expand All @@ -25,11 +29,14 @@
import dev.ultreon.mcgdx.impl.GdxBlockEntityRenderer;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.impl.client.rendering.BlockEntityRendererRegistryImpl;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
Expand All @@ -39,4 +40,8 @@ public GdxEntityBlock(Properties properties, Supplier<BlockEntityType<?>> typeSu
public @Nullable BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return typeSupplier.get().create(blockPos, blockState);
}

public RenderShape getRenderShape(BlockState arg) {
return RenderShape.ENTITYBLOCK_ANIMATED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ForgeGdxLauncher {
public ForgeGdxLauncher() {
if (FMLEnvironment.dist != Dist.CLIENT) return;

GdxMinecraft.setup();
// GdxMinecraft.setup();
GdxMinecraft.loader = new ForgeModLoader();
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ org.gradle.jvmargs=-Xmx3G
minecraft_version=1.20.2

archives_base_name=mcgdx
mod_version=0.1.0+devtest.3
mod_version=0.1.0+devtest.4
maven_group=dev.ultreon

architectury_version=10.1.20
Expand Down

0 comments on commit 77d5b5c

Please sign in to comment.