Jump to content

Projecting a 3D vector from object space into screen space


504185787

Recommended Posts

I'm having an issue with getting the correct screen coordinates. Also the inventory textures are not getting rendered. Which makes me think I'm not restoring the the state properly.

 

I have also underflowed and overflowed the stack. I don't see how. For every push I have a corresponding pop. EntityRenderer#setupCameraTransform doesn't seem to push/pop anything either.

 

Can someone take a look and tell me what I am doing wrong?

 

I'm hooking RenderGameOverlay which seems like the right place for 2D rendering.

 

However the matrices at this point aren't setup properly (not relative to camera) for gluProject.

 

I first backup the matrices and other state information (current matrix mode) with GL11#glPushMatrix/glPushAttrib.

 

Then I update the matrices (make them relative to camera) for gluProject by calling EntityRenderer#setupCameraTransform, store them in a buffer, and restore state.

 

package org.504185787.test;

import java.lang.reflect.Method;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.Project;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod(modid = "testmod", name = "Test Mod", version = "0")
public class TestMod
{
private Minecraft mc;

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
    	MinecraftForge.EVENT_BUS.register(this);
    	mc = Minecraft.getMinecraft();
    }
    
@SubscribeEvent(priority = EventPriority.NORMAL)
public void RenderGameOverlay(RenderGameOverlayEvent event)
{
	if(!event.isCancelable() && event.type == ElementType.ALL)
	{
		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.partialTicks, 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.fontRendererObj, "Text", screen[0], screen[1], 0xFF00FF00);
		}

		mc.ingameGUI.drawString(mc.fontRendererObj, "[RenderGameOverlay]", 5, 5, 0xFF00FF00);
	}
}
}

Link to comment
Share on other sites

StackUnderflow means that you do popMatrix to often. For every PushMatrix() you need a related popmatrix call.  That also fixes the fact that inventorytextures aren't rendered.

 

Right, but as I said in my post I don't see any push without a corresponding pop. Two pushMatrix, two popMatrix, one pushAttrib, one popAttrib.

 

But what i don't get, is what you want to do with that complicated GLStateManager stuff? If you wanna render an overlay, just use ScaledResolution to get the correct screen coordinates.

 

The text should overlay the 3d point. If the camera turns then the text should move as well and stay on that 3d point.

 

For example: If the 3d point is the coordinates of a pig then the text should always be drawn on the pig.

Link to comment
Share on other sites

@TheGreyGhost: Thanks the inventory textures are now being rendered again. The underflow/overflow also seems to be fixed.

 

Oh, you can do this an easier way i think why don't you do it with objectMouseOver.entityHit to get the entity and then maybe this helps you https://www.evl.uic.edu/luc/488/slides/class7.pdf

 

objectMouseOver.entityHit ("Picking" / "ray cast") is the opposite of what I want.

I don't need to get the entity. I already have it. I want the 2D point on the screen where that entity is so that I can draw text on it.

 

I haven't done it with OpenGL before, only directX. I could also do it by disabling depth testing and drawing I believe.

I'll see if I can get that to work.

 

Link to comment
Share on other sites

I want the 2D point on the screen where that entity is so that I can draw text on it.

 

Why not think about this in 3D instead? Maybe you should do it as an entity render thing, draw the text oriented toward the camera (and possibly move the text towards the camera). Basically similar to the way names are displayed over entities/players. It really depends on the effect you're trying to get, but might work for you.

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

Link to comment
Share on other sites

@TheGreyGhost: Thanks. Got it working. That was the technique I was thinking about in my last post that Render#renderLivingLabel uses. Scaling is kind of annoying when doing it this way though. Have to scale with distance and set a min/max scale value. With the other method the text is always the same size regardless of distance.

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.