ModelViewMatrix provides Identity matrix instead of modelview #3428
-
DescriptionI've been trying to get a Vector3f of a position, then multiply the projection with the view matrix to get 2D screen coordinates, but for some reason the view matrix don't seem to change, even when directly using public static Vector2d projectWorldPointToScreenSpace(Vector3f worldPosition, Window window, GameRenderer renderer, MatrixStack stack) {
Matrix4f viewMatrix = stack.peek().getPositionMatrix();
Matrix4f projectionMatrix = renderer.getBasicProjectionMatrix(getProjectionViaOptions());
Matrix4f viewProjMatrix = new Matrix4f(projectionMatrix);
viewProjMatrix.mul(viewMatrix);
Vector3f screenPosition = viewProjMatrix.transformProject(worldPosition);
screenPosition.add(1f,1f,1f).mul(0.5f);
return new Vector2d(screenPosition.x * window.getScaledWidth(),screenPosition.y * window.getScaledHeight());
} I'm probably overlooking something really obvious, so any advice is appreciated. After trying every now and then over the past week, It seems that Rendersystem resets their viewmatrix to an identity matrix constantly, which makes getting a working one annoying. Even directly grabbing the modelview from raw OpenGL returns this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After getting better at graphics, I found a solution not long ago for this that works with // This is a (modified) code snippet from an existing project I'm working on, currently licensed under Apache-2.0.
// The place I sourced this matrix from was an @Inject Invoke at `applyModelViewMatrix` in WorldRenderer's `render`.
public void example(Quaternionf rotation, Matrix4f matrix, Vector3f pos){
var camRot = new Quaternionf(MinecraftClient.getInstance().gameRenderer.getCamera().getRotation());
var dc = new DrawContext(MinecraftClient.getInstance(),MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers());
new Quaternionf().conjugate(camRot);
dc.getMatrices().multiplyPositionMatrix(new Matrix4f(matrix));
if(pos == null){
pos = new Vector3f(1.5f,4,1.5f); // old transform
}
// We then want to counteract the rotation in our matrix with the conjugated camera rotation, and then place it where we want it to render.
// What gets rendered will be massive (due to being big enough to show on your screen), so you'll have to scale that down quite a lot.
// The rotation component is optional, and you'll have to bring your own quaternion for that.
dc.getMatrices().peek().getPositionMatrix().rotate(camRot).translate(new Vector3f(pos).sub(MinecraftClient.getInstance().gameRenderer.getCamera().getPos().toVector3f())).scale(0.005f).rotate(rotation);
// do rendering stuff like drawing textures here
} The issue is that the only "workable" matrix for this purpose is during a specific part of world rendering, so my old approach wouldn't have worked. after doing this though, everything works flawlessly. |
Beta Was this translation helpful? Give feedback.
After getting better at graphics, I found a solution not long ago for this that works with
DrawContext
s. The code is somewhat like this: