I am attempting to put 3D objects on the HUD using LWJGL 3 and JOML. I just have a small problem where when I scale the object, it moves. I want to prevent that from happening.
In this image my object is set to a position of (0,0) (Z is always set to 0), and a scale of 100 (All scales are divided by 100 so the actual value of the scale is 1 when applied to the matrix. The code that does this can be seen below). https://i.sstatic.net/PHtN4.png
Then when I change the scale to 50 (0.5 when applied to the matrix) this is what is shown:
https://i.sstatic.net/SMyCZ.png
I want the object to still be at the top left corner of the screen.
Here is my code that comes up with the true scale and position (The screen size is 1080 x 720)
public Vector3f getTruePos(){
return new Vector3f(((position.x/1080) - 0.5), ((position.y/720) + (0.5)), 0);
}
public float getTrueScale(){
return scale/100;
}
Here is my code that does the rendering of the object.
public void renderHUD(List<UIObject> objects){
hudShaderProgram.bind();
for(UIObject object : objects){
Mesh mesh = object.getMesh();
hudShaderProgram.setUniform("projection", transformation.getOrthoProjectionMatrix());
hudShaderProgram.setUniform("model", transformation.buildModelViewMatrixUI(object));
mesh.render();
}
hudShaderProgram.unbind();
}
Then here is the code that handles the transformations of the object:
public Matrix4f buildModelViewMatrixUI(UIObject gameItem) {
Quaternionf rotation = gameItem.getRotation();
Matrix4f modelMatrix = new Matrix4f();
modelMatrix.translationRotateScale(gameItem.getTruePos().x, gameItem.getTruePos().y, 0, rotation.x, rotation.y, rotation.z, rotation.w, gameItem.getTrueScale(),
gameItem.getTrueScale(), gameItem.getTrueScale());
return modelMatrix;
}
Then getOrthoProjectionMatrix()
returns the value that is set by this:
transformation.updateProjectionMatrix(FOV, window.getWidth(), window.getHeight(), Z_NEAR, Z_FAR);
public Matrix4f updateOrthoProjectionMatrix(float left, float right, float bottom, float top, float zNear, float zFar) {
orthoProjMatrix.identity();
orthoProjMatrix.setOrtho(left, right, bottom, top, zNear, zFar);
return orthoProjMatrix;
}
I am not sure what I need to do to achieve what I desire. Any help would be appreciated.
The full code for the snippets above can be found here:
The UIObject class
The Rendering Class
The Transformation class
If this is not enough information please let me know, I will be happy to supply more.
I managed to solve this issue. I only really needed to update my ortho projectin matrix.
public Matrix4f buildOrtho(float left, float right, float bottom, float top){
Matrix4f orthoProjMatrix = new Matrix4f();
orthoProjMatrix.setOrtho(left, right, bottom, top, -100, 1000);
return orthoProjMatrix;
}
I then just changed my render method to this:
public void renderHUD(Window window, List<UIObject> objects){
hudShaderProgram.bind();
Matrix4f orthoProjection = transformation.buildOrtho(0, window.getWidth(), window.getHeight(), 0);
for(UIObject object : objects){
Mesh mesh = object.getMesh();
hudShaderProgram.setUniform("ortho", orthoProjection);
hudShaderProgram.setUniform("model", transformation.buildModelViewMatrixUI(object));
mesh.render();
}
hudShaderProgram.unbind();
}
Also since I am now using the correct ortho projection I no longer need the getTrueScale()
and getTruePositon()
methods.