All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hendrixshen.magiclib.impl.render.CameraPositionTransformer Maven / Gradle / Ivy

There is a newer version: 0.6.59
Show newest version
/*
 * This file is part of the TweakerMore project, licensed under the
 * GNU Lesser General Public License v3.0
 *
 * Copyright (C) 2023  Fallen_Breath and contributors
 *
 * TweakerMore is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TweakerMore is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with TweakerMore.  If not, see .
 */

package top.hendrixshen.magiclib.impl.render;

import org.joml.Matrix4f;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.api.render.context.RenderContext;

import java.util.Objects;
import net.minecraft.class_243;
import net.minecraft.class_310;
import net.minecraft.class_4184;

//#if MC < 11500
//$$ import com.mojang.blaze3d.platform.GlStateManager;
//$$ import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
//#endif

/**
 * Reference to TweakerMore
 */
public class CameraPositionTransformer {
    private final class_243 pos;

    private RenderContext context;

    public static @NotNull CameraPositionTransformer create(class_243 pos) {
        return new CameraPositionTransformer(pos);
    }

    private CameraPositionTransformer(class_243 pos) {
        this.pos = pos;
    }

    /**
     * Pose stack of renderContext will be pushed
     */
    public void apply(@NotNull RenderContext context) {
        this.context = context;
        class_310 mc = class_310.method_1551();
        class_4184 camera = mc.field_1773.method_19418();
        class_243 vec3 = this.pos.method_1020(camera.method_19326());
        context.pushPose();
        context.translate(vec3.method_10216(), vec3.method_10214(), vec3.method_10215());
        //#if MC > 11404
        context.mulPoseMatrix(
                //#if MC > 11902
                new Matrix4f().rotation(camera.method_23767())
                //#else
                //$$ new Matrix4f(camera.rotation())
                //#endif
        );
        //#else
        //$$ EntityRenderDispatcher entityRenderDispatcher = mc.getEntityRenderDispatcher();
        //$$ GlStateManager.rotatef(-entityRenderDispatcher.playerRotY, 0.0F, 1.0F, 0.0F);
        //$$ GlStateManager.rotatef(entityRenderDispatcher.playerRotX, 1.0F, 0.0F, 0.0F);
        //#endif
    }

    /**
     * Pose stack of renderContext will be popped
     */
    public void restore() {
        if (this.context == null) {
            throw new RuntimeException("CameraPositionTransformer: Calling restore before calling apply");
        }

        this.context.popPose();
        this.context = null;
    }

    public RenderContext getContext() {
        return Objects.requireNonNull(this.context);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy