Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Retrieving distance from viewport to player head


Recommended Posts

Hi, this is my first time posting on here so sorry for everything that I'm gonna do wrong in advance

 

I am currently trying to render custom models using OpenGL. Now I have a mesh which follows the player, but as soon as I go into third person the position of the mesh isn't correct anymore because OpenGL translates from the viewport and not the actual player "eyes" position where the viewport is located in first person (as far as I know).

Now my question is, how do I compensate for the offset? Is there a way to get the actual viewport position in relation to the player "eyes" or is there a more elegant solution which I just don't know about. Or maybe 2am is too late for my brain to work properly.

 

Sorry if I ask stupid questions but I just got back into Forge yesterday which is why I'm not quite familiar with all the new stuff.

All help is appreciated, thanks in advance :"

 

@SubscribeEvent
public void RenderWorldLastEvent(RenderWorldLastEvent event)
{
    float partialTicks = event.getPartialTicks();

    ClientPlayerEntity playerEntity = Minecraft.getInstance().player;
    double x = playerEntity.lastTickPosX + (playerEntity.posX - playerEntity.lastTickPosX) * partialTicks;
    double y = playerEntity.lastTickPosY + (playerEntity.posY - playerEntity.lastTickPosY) * partialTicks;
    double z = playerEntity.lastTickPosZ + (playerEntity.posZ - playerEntity.lastTickPosZ) * partialTicks;

    GlStateManager.pushMatrix();

    GlStateManager.translated(-0.5 d, -playerEntity.getEyeHeight(), -0.5 d);

    GlStateManager.disableCull();
    GlStateManager.disableDepthTest();
    GlStateManager.disableTexture();

    GlStateManager.color4f(1, 1, 1, 1);

    Tessellator tessellator = Tessellator.getInstance();
    BufferBuilder wr = tessellator.getBuffer();

    wr.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);

    wr.pos(0, 0, 0).endVertex();
    wr.pos(1, 0, 0).endVertex();
    wr.pos(1, 0, 1).endVertex();
    wr.pos(0, 0, 1).endVertex();

    wr.pos(0, 1, 0).endVertex();
    wr.pos(1, 1, 0).endVertex();
    wr.pos(1, 1, 1).endVertex();
    wr.pos(0, 1, 1).endVertex();

    tessellator.draw();

    GlStateManager.enableCull();
    GlStateManager.enableDepthTest();
    GlStateManager.enableTexture();

    GlStateManager.popMatrix();
}

 

Link to post
Share on other sites

I just managed to fix the issue. For anyone who find this post in the future, I used the following code to do it:

Vec3d projectedView = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();

ClientPlayerEntity playerEntity = Minecraft.getInstance().player;
double x = playerEntity.lastTickPosX + (playerEntity.posX - playerEntity.lastTickPosX) * partialTicks;
double y = playerEntity.lastTickPosY + (playerEntity.posY - playerEntity.lastTickPosY) * partialTicks;
double z = playerEntity.lastTickPosZ + (playerEntity.posZ - playerEntity.lastTickPosZ) * partialTicks;

GlStateManager.translated(x - projectedView.x - 0.5d, y - projectedView.y, z - projectedView.z - 0.5d);

 

Link to post
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.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.