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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Another Minecraft Launcher https://multimc.org/#Download or https://atlauncher.com/downloads
    • Remove these mods and combine OptiFine HD U I6 with Forge 47.2.18 Start with Optifine - without any other mods - if this works, add the mods one by one or in groups until you find the mod, conflicting with Optifine
    • Ok yeah I think this might be where the problem starts. For starters, I think you can get rid of your RecipeType impl and make it a static constant instead. I'm not 100% sure this will fix it but it's posssible. RuneInscribingRecipe public class RuneInscribingRecipe implements Recipe<SimpleContainer> { public static final RecipeType<RuneInscribingRecipe> RECIPE_TYPE = new RecipeType<>(){}; // .... @Override public RecipeType<?> getType() { return RECIPE_TYPE; } // .... } RuneInscriberMenu private Optional<RecipeHolder<RuneInscribingRecipe>> getCurrentRecipe() { SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); List<RecipeHolder<RuneInscribingRecipe>> list = this.blockEntity.getLevel().getRecipeManager().getRecipesFor(RuneInscribingRecipe.RECIPE_TYPE, inventory, this.blockEntity.getLevel()); if (list.isEmpty()) { return Optional.empty(); } return Optional.of(list.get(0)); }   Also, this here is a little suspicious where you're getting the `this.inventory.getStackInSlot` calls. I'd try and make sure those are what you expect they are with either a Debug line in IDE or print statement SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); System.out.printf("CHECKING BASE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); System.out.printf("CHECKING TEMPLATE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); ^ See what the result of those print statements are, make sure they're right
    • Upon testing again after adding them, the game still doesn't crash however the resource packs are still unfortunately broken.
    • Hi! Can someone please point me to the correct FAQ or place? I'm not able do DuckDuckGo myself out of this one. As you can hopefully see, chunks are not rendered. I run a forge server, fresh install on a linux VM. Curseforge on the Win11 client with Embeddium, Oculus and Unbound shader. No mods. Forge 47.2.23 on server & client.  No errors, no crashes, no apparent CPU or RAM issues with the VM. This screenshot was captured while creative, but the same happens when flying in survival. Waiting (floating) for a few minutes sooner or (often) later, lets the missing chunks appear. Usually takes 5-10 minutes.  When on a local game, the chunks appear slowly, but they do appear without pause.   This must be something simple, obvious, but I can't find the correct "search words" to find my way out. "Chunks not loading / rendering" didn't help. Browsing several forums neither. 
  • Topics

×
×
  • Create New...

Important Information

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