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);
}
}
}