1515 */
1616package org .terasology .rendering .logic ;
1717
18- import org .terasology .math .JomlUtil ;
19- import org .terasology .math .Transform ;
2018import com .google .common .collect .HashMultimap ;
2119import com .google .common .collect .SetMultimap ;
22- import java .nio .FloatBuffer ;
23- import java .util .Arrays ;
24- import java .util .Set ;
20+ import org .joml .Matrix3f ;
21+ import org .joml .Matrix4f ;
22+ import org .joml .Quaternionf ;
23+ import org .joml .Vector3f ;
24+ import org .joml .Vector3fc ;
2525import org .lwjgl .BufferUtils ;
2626import org .slf4j .Logger ;
2727import org .slf4j .LoggerFactory ;
3838import org .terasology .logic .location .LocationComponent ;
3939import org .terasology .logic .players .LocalPlayer ;
4040import org .terasology .math .AABB ;
41- import org .terasology .math .MatrixUtils ;
42- import org .terasology .math .geom .Matrix4f ;
43- import org .terasology .math .geom .Quat4f ;
44- import org .terasology .math .geom .Vector3f ;
41+ import org .terasology .math .JomlUtil ;
42+ import org .terasology .math .Transform ;
4543import org .terasology .network .ClientComponent ;
4644import org .terasology .network .NetworkSystem ;
4745import org .terasology .registry .In ;
5048import org .terasology .rendering .world .WorldRenderer ;
5149import org .terasology .world .WorldProvider ;
5250
51+ import java .nio .FloatBuffer ;
52+ import java .util .Arrays ;
53+ import java .util .Set ;
54+
5355/**
5456 * TODO: This should be made generic (no explicit shader or mesh) and ported directly into WorldRenderer? Later note: some GelCube functionality moved to a module
5557 */
@@ -167,10 +169,13 @@ private void renderEntities(Iterable<EntityRef> entityRefs) {
167169 }
168170
169171 private void renderEntitiesByMaterial (SetMultimap <Material , EntityRef > meshByMaterial ) {
170- Vector3f cameraPosition = JomlUtil . from ( worldRenderer .getActiveCamera ().getPosition () );
172+ Vector3f cameraPosition = worldRenderer .getActiveCamera ().getPosition ();
171173
172- Quat4f worldRot = new Quat4f ();
174+ Quaternionf worldRot = new Quaternionf ();
173175 Vector3f worldPos = new Vector3f ();
176+ Matrix3f normalMatrix = new Matrix3f ();
177+ Matrix4f matrixCameraSpace = new Matrix4f ();
178+ Matrix4f modelViewMatrix = new Matrix4f ();
174179
175180 FloatBuffer tempMatrixBuffer44 = BufferUtils .createFloatBuffer (16 );
176181 FloatBuffer tempMatrixBuffer33 = BufferUtils .createFloatBuffer (12 );
@@ -190,23 +195,23 @@ private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMat
190195 MeshComponent meshComp = entity .getComponent (MeshComponent .class );
191196 LocationComponent location = entity .getComponent (LocationComponent .class );
192197
193- if (isHidden (entity , meshComp ) || location == null || Float .isNaN (location .getWorldPosition ().x )|| meshComp .mesh == null || !isRelevant (entity , location .getWorldPosition ())) {
198+ if (isHidden (entity , meshComp ) || location == null || Float .isNaN (location .getWorldPosition ().x ) || meshComp .mesh == null || !isRelevant (entity , JomlUtil . from ( location .getWorldPosition () ))) {
194199 continue ;
195200 }
196201 if (meshComp .mesh .isDisposed ()) {
197202 logger .error ("Attempted to render disposed mesh" );
198203 continue ;
199204 }
200205
201- location .getWorldRotation (worldRot );
202- location .getWorldPosition (worldPos );
206+ worldRot . set ( JomlUtil . from ( location .getWorldRotation ()) );
207+ worldPos . set ( JomlUtil . from ( location .getWorldPosition ()) );
203208 float worldScale = location .getWorldScale ();
204209
205- Transform toWorldSpace = new Transform (worldPos , worldRot , worldScale );
210+ Transform toWorldSpace = new Transform (JomlUtil .from (worldPos ), JomlUtil .from (worldRot ), worldScale );
211+
212+ Vector3f offsetFromCamera = worldPos .sub (cameraPosition , new Vector3f ());
213+ matrixCameraSpace .translationRotateScale (offsetFromCamera , worldRot , worldScale );
206214
207- Vector3f offsetFromCamera = new Vector3f ();
208- offsetFromCamera .sub (worldPos , cameraPosition );
209- Matrix4f matrixCameraSpace = new Matrix4f (worldRot , offsetFromCamera , worldScale );
210215
211216 AABB aabb = meshComp .mesh .getAABB ().transform (toWorldSpace );
212217 if (worldRenderer .getActiveCamera ().hasInSight (aabb )) {
@@ -218,17 +223,17 @@ private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMat
218223 lastMesh .preRender ();
219224 }
220225
221- Matrix4f modelViewMatrix = MatrixUtils . calcModelViewMatrix (worldRenderer .getActiveCamera ().getViewMatrix (), matrixCameraSpace );
222- MatrixUtils . matrixToFloatBuffer ( modelViewMatrix , tempMatrixBuffer44 );
223- MatrixUtils . matrixToFloatBuffer ( MatrixUtils . calcNormalMatrix ( modelViewMatrix ), tempMatrixBuffer33 );
226+ modelViewMatrix . set (worldRenderer .getActiveCamera ().getViewMatrix ()). mul ( matrixCameraSpace );
227+ modelViewMatrix . get ( tempMatrixBuffer44 );
228+ modelViewMatrix . normal ( normalMatrix ). get ( tempMatrixBuffer33 );
224229
225230 material .setMatrix4 ("projectionMatrix" , worldRenderer .getActiveCamera ().getProjectionMatrix (), true );
226231 material .setMatrix4 ("worldViewMatrix" , tempMatrixBuffer44 , true );
227232 material .setMatrix3 ("normalMatrix" , tempMatrixBuffer33 , true );
228233
229234 material .setFloat3 ("colorOffset" , meshComp .color .rf (), meshComp .color .gf (), meshComp .color .bf (), true );
230- material .setFloat ("sunlight" , worldRenderer .getMainLightIntensityAt (worldPos ), true );
231- material .setFloat ("blockLight" , Math .max (worldRenderer .getBlockLightIntensityAt (worldPos ), meshComp .selfLuminance ), true );
235+ material .setFloat ("sunlight" , worldRenderer .getMainLightIntensityAt (JomlUtil . from ( worldPos ) ), true );
236+ material .setFloat ("blockLight" , Math .max (worldRenderer .getBlockLightIntensityAt (JomlUtil . from ( worldPos ) ), meshComp .selfLuminance ), true );
232237
233238 lastMesh .doRender ();
234239 }
@@ -251,7 +256,7 @@ private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMat
251256 * @param position the world position the entity is located
252257 * @return true if the entity itself or the block at the given position are relevant, false otherwise.
253258 */
254- private boolean isRelevant (EntityRef entity , Vector3f position ) {
259+ private boolean isRelevant (EntityRef entity , Vector3fc position ) {
255260 return worldProvider .isBlockRelevant (position ) || entity .isAlwaysRelevant ();
256261 }
257262
0 commit comments