Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

写一个Forge 1.12.2 MOD,在Screen中使用GL Perspective透视视图渲染的一个旋转的正方体骨架,请提供关键代码

  1. User

    写一个Forge 1.12.2 MOD,在Screen中使用GL Perspective透视视图渲染的一个旋转的正方体骨架,请提供关键代码

  2. Assistant

    在Forge 1.12.2 MOD中使用GL Perspective透视视图渲染旋转的正方体骨架,需要在自定义的Screen类中编写关键代码。下面是一个示例:

    首先,在你的MOD的主类中注册渲染器:

    @Mod.EventBusSubscriber(modid = YourMod.MODID, value = Dist.CLIENT)
    public class ClientEventSubscriber {
    
        @SubscribeEvent
        public static void onClientSetupEvent(FMLClientSetupEvent event) {
            ScreenManager.registerFactory(YourMod.CONTAINER_TYPE, YourModScreen::new);
        }
    }
    

    接下来,创建YourModScreen类继承自Screen,并实现KeyBindingHandler接口:

    public class YourModScreen extends Screen {
    
        private static final int CUBE_SIZE = 1;
        private static final float ROTATION_SPEED = 0.5f;
        
        private float rotation = 0.0f;
    
        public YourModScreen(Container container, PlayerInventory inventory, ITextComponent title) {
            super(title);
            // 设置宽高
            this.xSize = 176;
            this.ySize = 166;
        }
    
        @Override
        public void render(int mouseX, int mouseY, float partialTicks) {
            // 渲染背景
            this.renderBackground();
    
            // 渲染正方体骨架
            renderCubeSkeleton();
    
            // 渲染组件和文字
            super.render(mouseX, mouseY, partialTicks);
        }
    
        private void renderCubeSkeleton() {
            GlStateManager.pushMatrix();
            GlStateManager.translate(this.guiLeft + this.xSize / 2, this.guiTop + this.ySize / 2, 100.0f);
            GlStateManager.scale(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
            GlStateManager.rotate(rotation, 0.0f, 1.0f, 0.0f);
            GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
            
            // 渲染正方体骨架的代码
            // ...
    
            GlStateManager.popMatrix();
        }
    
        @Override
        public boolean isPauseScreen() {
            return false;
        }
    
        @Override
        public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
            // 处理键盘按下事件
            // ...
    
            return super.keyPressed(keyCode, scanCode, modifiers);
        }
    }
    

    以上代码使用了GLStateManager类来进行GL操作,通过GlStateManager.translate()和GlStateManager.rotate()方法实现视角的移动和旋转。请根据自己的需求修改上述代码,以适应具体的渲染需求。请注意,这只是一个示例,你可能需要进一步拓展和调整代码以适应你的MOD的需求。