#Matrix operations to make the image face the camera?

9 messages · Page 1 of 1 (latest)

grim panther
#

I have a world position as Vec3d pos.
In a world rendering event, I'm rendering an image using quads.

What matrix operations do I need to perform with a matrix stack to obtain a position matrix that rotates the image towards the camera from every angle?

azure jay
#

multiply by camera.rotation() or it's inverse id wager

grim panther
# azure jay multiply by camera.rotation() or it's inverse id wager

thanks for the response
i tried both but it must be the operation order that's important...
i dont have access to the actual code and i'm on my phone rn so i'll try to remember it

Vec3d transformedPos = pos.subtract(camera.getPosition());
MatrixStack matrixStack = new MatrixStack();

matrixStack.translate(transformedPos.x, transformedPos.y, transformedPos.z);
matrixStack.translate(0.5, 0.5, 0);
matrixStack.multiply(camera.getRotation());
matrixStack.translate(-0.5, -0.5, 0);
Matrix4f positionMatrix = matrixStack.getPositionMatrix();

// using QUADS, 4 vertices at (0, 0), (1, 0), (1, 1), (1, 0) or smth like that, z coordinate always 0
buffer.vertex(positionMatrix, ...).next();
...
#

i tried with and without the 0.5 translations, with the normal rotation and the inverse rotation, with the transformedPos translation at the beginning or at the end or in other positions... nothing seems to work

azure jay
#

the quad isn't backwards is it

grim panther
grim panther
#

the issue i keep running into is that when im looking perfectly north, and perfectly parallel to the ground, that's the only case when the images are rendered where i expect them
everything else just seems like the yaw/pitch rotations are applied twice
if i look 45 degrees right from the north direction, the image is about to be out of view

grim panther
#

FI-NA-LLY
found the winning formula

// the first rotations are object-relative, the last ones are camera-relative
Vec3d vec = vertex.vec()
    .rotateX(xRotation)
    .rotateY(yRotation)
    .multiply(scale)
    .add(pos)
    .rotateX(xRotation)
    .rotateY(yRotation);

buffer.vertex(
    positionMatrix, (float) vec.x, (float) vec.y, (float) vec.z
).color(
    color.red, color.green, color.blue, color.alpha
).texture(
    vertex.u(), vertex.v()
).next();
#

vertex.vec() is one of those (0, 1), (0, 0), (1, 0), (1, 1), with a z = 0 component