Jump to content

[1.18.1] Trying to draw a small image on the screen


Marshall7

Recommended Posts

I am trying to draw a simple icon on the screen. For starters, I thought I'd just try and draw part of an existing 256x256 texture so that I have the basic concepts working.

I have the following code:

public static void RenderGameOverlayEvent(RenderGameOverlayEvent.Post event) {
    Minecraft minecraft = Minecraft.getInstance();
    TextureManager tm = minecraft.getTextureManager();
    ResourceLocation rl = new ResourceLocation("minecraft", "textures/gui/recipe_book.png");
    RenderSystem.setShader(GameRenderer::getPositionTexShader);
    tm.bindForSetup(rl); // tried with and without this
    RenderSystem.enableTexture(); // tried with and without this
    RenderSystem.bindTexture(tm.getTexture(rl).getId()); // tried with and without this
    RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0f);
    GuiUtils.drawTexturedModalRect(event.getMatrixStack(), 0, 0, 0, 0, 16, 16, 0);
}

And this is what gets rendered in-game (garbage):

Image2.jpg

I have found that, even if I omit the calls for binding texture entirely, it makes no difference to the image. So I assume that I am not setting the texture correctly (my calls are effectively noops).
I have searched the code, forum and google for examples of how to do this drawing correctly but as far as I can tell I'm doing the same as others for whom it is working.

Can anyone see what I'm missing / doing wrong? Many thanks.

Link to comment
Share on other sites

I did see that as an alternative approach, and I tried to understand how OverlayRegistry works but couldn't see any usage examples. I found classes that call register but could not find any places where the registered items were subsequently used. Could you point me at an example or docs on how to use OverlayRegistry?

Also, is there a reason why my approach above doesn't work (apparently it works for other people) ?

Link to comment
Share on other sites

tysm I have it working now. Doing it via OverlayRegistry as follows works:

MyHudOverlay = OverlayRegistry.registerOverlayTop("MyHudOverlay", (gui, mStack, partialTicks, screenWidth, screenHeight) -> {
    Minecraft minecraft = Minecraft.getInstance();
    if (minecraft.gameMode == null) return;
    if (minecraft.level == null) return;
    TextureManager tm = minecraft.getTextureManager();
    ResourceLocation rl = new ResourceLocation("minecraft", "textures/gui/recipe_book.png");
    gui.setupOverlayRenderState(true, false, rl);
    RenderSystem.setShader(GameRenderer::getPositionTexShader);
    tm.bindForSetup(rl);
    RenderSystem.enableTexture();
    RenderSystem.bindTexture(tm.getTexture(rl).getId());
    RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0f);
    GuiUtils.drawTexturedModalRect(new PoseStack(), 0, 0, 0, 0, 16, 16, 0);
});

I also found that the key thing my original code was missing was

RenderSystem.setShaderTexture(0, res);

(which is called inside setupOverlayRenderState)

If I use that with my original code instead of using OverlayRegistry, I also get the desired result.
Is there a reason/benefit to using OverlayRegistry for this task rather than RenderGameOverlayEvent ?

Edited by Marshall7
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.