Jump to content

[SOLVED] Getting an entities position on the screen


Fzzy

Recommended Posts

I've been trying to figure this out for the last couple hours and I'm having a really hard time figuring out what's going on with LWJGL. I'm trying to display text on top of an entity, but i can't seem to get it's coordinates on the screen from its coordinates in the world. I've found that there is a method for this in LWJGL called gluProject (https://stackoverflow.com/questions/21950105/java-lwjgl-opengl-convert-3d-point-to-2d-point), but i can't seem to get it to work.

 

I managed to use LWJGL to display some lines on the entity but I'm not sure how this can be applied to display text over them as a sprite.

 

If anybody has done something like this before i could really use some insight, thanks!

Edited by Fzzy
Link to comment
Share on other sites

47 minutes ago, Fzzy said:

I've been trying to figure this out for the last couple hours and I'm having a really hard time figuring out what's going on with LWJGL. I'm trying to display text on top of an entity, but i can't seem to get it's coordinates on the screen from its coordinates in the world. I've found that there is a method for this in LWJGL called gluProject (https://stackoverflow.com/questions/21950105/java-lwjgl-opengl-convert-3d-point-to-2d-point), but i can't seem to get it to work.

 

I managed to use LWJGL to display some lines on the entity but I'm not sure how this can be applied to display text over them as a sprite.

 

If anybody has done something like this before i could really use some insight, thanks!

What’s wrong with Minecraft’s

renderLivingLabel()?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

10 hours ago, Cadiboo said:

What’s wrong with Minecraft’s

renderLivingLabel()?

I wanted to draw more than just text. I also wanted to draw a texture using the drawRect thing.

 

I found this thread from years ago describing exactly what i want, and claims to have solved it but never explained how he did it

 

Link to comment
Share on other sites

1 minute ago, jabelar said:

There are events for rendering. You can just handle the event for when the entity is rendered and render anything you want.

I'm currently using the RenderGameOverlayEvent, but even if i use the event for entity rendering, it doesn't give me any coordinates for where the entity is on the screen. I feel like im quite close to making it work. This is where i'm at right now

 

GL11.glPushAttrib(GL11.GL_TRANSFORM_BIT);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glPushMatrix();
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glPushMatrix();

    try
    {
        Method setupCameraTransform = EntityRenderer.class.getDeclaredMethod("setupCameraTransform", float.class, int.class);
        setupCameraTransform.setAccessible(true);
        setupCameraTransform.invoke(mc.entityRenderer, event.getPartialTicks(), 0);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    FloatBuffer modelMatrix = BufferUtils.createFloatBuffer(16);
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelMatrix);

    FloatBuffer projMatrix = BufferUtils.createFloatBuffer(16);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projMatrix);

    IntBuffer viewport = BufferUtils.createIntBuffer(16);
    GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glPopMatrix();
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glPopMatrix();

    GL11.glPopAttrib();

    FloatBuffer screen2D = BufferUtils.createFloatBuffer(16);
    if(Project.gluProject(242, 76, 25, modelMatrix, projMatrix, viewport, screen2D))
    {
        int[] screen = new int[] {(int)screen2D.get(0), (int)screen2D.get(1)};

        mc.ingameGUI.drawString(mc.fontRenderer, "overlay", screen[0], screen[1], 0xFF00FF00);
    }

 

Link to comment
Share on other sites

No, not the game overlay event. As you found out, that would be very complicated to figure out where to draw things. Handle the event where the entity itself is rendered and add your stuff at that time. You won't need to know where it is on the screen, that will be taken care of as long as you render it relative to the entity.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

5 minutes ago, jabelar said:

No, not the game overlay event. As you found out, that would be very complicated to figure out where to draw things. Handle the event where the entity itself is rendered and add your stuff at that time. You won't need to know where it is on the screen, that will be taken care of as long as you render it relative to the entity.

So how could i use something like the RenderLivingEvent to render an overlay on top of an entity? It provides a RenderLivingBase but that doesnt have methods like drawString or drawTexturedModalRect for me to use.

Link to comment
Share on other sites

5 hours ago, Fzzy said:

So how could i use something like the RenderLivingEvent to render an overlay on top of an entity? It provides a RenderLivingBase but that doesnt have methods like drawString or drawTexturedModalRect for me to use.

You can do any GL11 type stuff you want, so you can just copy the code for drawing a string or whatever. You don't need a preset method to draw things, although I admit that sometimes GL11 can get a little gnarly.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

  • 1 month later...
On 8/30/2018 at 9:05 PM, Fzzy said:

If somebody comes across this thread in the future, the code written in my post a few posts back is correct, i just needed to scale it properly to the screen using the ScaledResolution class

Can you explain what you meant about this? I'm trying to do something similar, and it would be very nice to understand what you exactly mean

ExplosivesBanner-1.gif
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.