From 7cb8e414df7c7cdab51976ece60426964b29711c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 6 Sep 2017 16:33:57 +1000 Subject: [PATCH 1/2] Fix skin appearing wrongly for very short time, after changing your own skin with /skin --- ClassicalSharp/Entities/Model/ModelBuilder.cs | 8 ++++---- ClassicalSharp/Entities/Player.cs | 3 ++- ClassicalSharp/Network/NetworkProcessor.Helpers.cs | 3 +-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ClassicalSharp/Entities/Model/ModelBuilder.cs b/ClassicalSharp/Entities/Model/ModelBuilder.cs index 08f4b9d31..5d6e66f1f 100644 --- a/ClassicalSharp/Entities/Model/ModelBuilder.cs +++ b/ClassicalSharp/Entities/Model/ModelBuilder.cs @@ -93,8 +93,8 @@ namespace ClassicalSharp.Model { YQuad(m, x + sidesW + bodyW, y, bodyW, sidesW, x2, x1, z2, z1, y1, false); // bottom ZQuad(m, x + sidesW, y + sidesW, bodyW, bodyH, x1, x2, y1, y2, z1, true); // front ZQuad(m, x + sidesW + bodyW + sidesW, y + sidesW, bodyW, bodyH, x2, x1, y1, y2, z2, true); // back - XQuad(m, x, y + sidesW, sidesW, bodyH, z1, z2, y1, y2, x2, true); // left - XQuad(m, x + sidesW + bodyW, y + sidesW, sidesW, bodyH, z2, z1, y1, y2, x1, true); // right + XQuad(m, x, y + sidesW, sidesW, bodyH, z1, z2, y1, y2, x2, true); // right + XQuad(m, x + sidesW + bodyW, y + sidesW, sidesW, bodyH, z2, z1, y1, y2, x1, true); // left return new ModelPart(m.index - 6 * 4, 6 * 4, desc.RotX, desc.RotY, desc.RotZ); } @@ -121,8 +121,8 @@ namespace ClassicalSharp.Model { YQuad(m, x + sidesW, y + sidesW, bodyW, bodyH, x2, x1, z1, z2, y1, false); // bottom ZQuad(m, x + sidesW, y, bodyW, sidesW, x2, x1, y1, y2, z1, false); // front ZQuad(m, x + sidesW + bodyW, y, bodyW, sidesW, x1, x2, y2, y1, z2, false); // back - XQuad(m, x, y + sidesW, sidesW, bodyH, y2, y1, z2, z1, x2, false); // left - XQuad(m, x + sidesW + bodyW, y + sidesW, sidesW, bodyH, y1, y2, z2, z1, x1, false); // right + XQuad(m, x, y + sidesW, sidesW, bodyH, y2, y1, z2, z1, x2, false); // right + XQuad(m, x + sidesW + bodyW, y + sidesW, sidesW, bodyH, y1, y2, z2, z1, x1, false); // left // rotate left and right 90 degrees for (int i = m.index - 8; i < m.index; i++) { diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index 52b066d9d..24fd69038 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -26,6 +26,7 @@ namespace ClassicalSharp.Entities { Player first = FirstOtherWithSameSkin(); if (first == null) { game.Graphics.DeleteTexture(ref TextureId); + ResetSkin(); } ContextLost(); } @@ -182,7 +183,7 @@ namespace ClassicalSharp.Entities { if (Utils.IsUrlPrefix(SkinName, 0)) MobTextureId = TextureId; } - void ResetSkin() { + internal void ResetSkin() { uScale = 1; vScale = 1; MobTextureId = -1; TextureId = -1; diff --git a/ClassicalSharp/Network/NetworkProcessor.Helpers.cs b/ClassicalSharp/Network/NetworkProcessor.Helpers.cs index 4ca32482f..c7bb25955 100644 --- a/ClassicalSharp/Network/NetworkProcessor.Helpers.cs +++ b/ClassicalSharp/Network/NetworkProcessor.Helpers.cs @@ -51,8 +51,7 @@ namespace ClassicalSharp.Network { game.LocalPlayer.Despawn(); // Always reset the texture here, in case other network players are using the same skin as us. // In that case, we don't want the fetching of new skin for us to delete the texture used by them. - game.LocalPlayer.TextureId = -1; - game.LocalPlayer.MobTextureId = -1; + game.LocalPlayer.ResetSkin(); game.LocalPlayer.fetchedSkin = false; game.LocalPlayer.DisplayName = displayName; From f986d5254e2666e6a0b6b542d2de425f190f1ddc Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Sep 2017 10:18:54 +1000 Subject: [PATCH 2/2] get rid of implicit matrix operator usage to avoid a copy --- ClassicalSharp/2D/IsometricBlockDrawer.cs | 2 +- ClassicalSharp/Entities/Player.cs | 2 +- ClassicalSharp/Game/Game.cs | 17 +++---- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 3 +- ClassicalSharp/GraphicsAPI/IGraphicsApi.cs | 3 +- .../Rendering/Env/SkyboxRenderer.cs | 13 +++--- ClassicalSharp/Rendering/HeldBlockRenderer.cs | 17 ++++--- ClassicalSharp/Utils/Camera.cs | 22 +++++---- OpenTK/Math/Matrix4.cs | 45 ++----------------- src/Client/Picking.h | 7 +-- 10 files changed, 51 insertions(+), 80 deletions(-) diff --git a/ClassicalSharp/2D/IsometricBlockDrawer.cs b/ClassicalSharp/2D/IsometricBlockDrawer.cs index d2e10dde7..616255f8e 100644 --- a/ClassicalSharp/2D/IsometricBlockDrawer.cs +++ b/ClassicalSharp/2D/IsometricBlockDrawer.cs @@ -45,7 +45,7 @@ namespace ClassicalSharp { Matrix4 rotY, rotX; Matrix4.RotateY(out rotY, 45 * Utils.Deg2Rad); Matrix4.RotateX(out rotX, -30f * Utils.Deg2Rad); - transform = rotY * rotX; + Matrix4.Mult(out transform, ref rotY, ref rotX); cosX = (float)Math.Cos(30f * Utils.Deg2Rad); sinX = (float)Math.Sin(30f * Utils.Deg2Rad); diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index 24fd69038..c5a6efb7a 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -136,7 +136,7 @@ namespace ClassicalSharp.Entities { if (SkinType == SkinType.Invalid) { SetSkinAll(true); } else { - if (Model.UsesHumanSkin) ClearHat(bmp, SkinType); + if (Model.UsesHumanSkin) ClearHat(bmp, SkinType); TextureId = game.Graphics.CreateTexture(bmp, true, false); SetSkinAll(false); } diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index b39f385eb..88d8983ba 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -133,10 +133,9 @@ namespace ClassicalSharp { void UpdateViewMatrix() { Graphics.SetMatrixMode(MatrixType.Modelview); - Matrix4 modelView = Camera.GetView(); - View = modelView; - Graphics.LoadMatrix(ref modelView); - Culling.CalcFrustumEquations(ref Projection, ref modelView); + Camera.GetView(out View); + Graphics.LoadMatrix(ref View); + Culling.CalcFrustumEquations(ref Projection, ref View); } void Render3D(double delta, float t) { @@ -223,11 +222,10 @@ namespace ClassicalSharp { public void UpdateProjection() { DefaultFov = Options.GetInt(OptionsKey.FieldOfView, 1, 150, 70); - Matrix4 projection = Camera.GetProjection(); - Projection = projection; + Camera.GetProjection(out Projection); Graphics.SetMatrixMode(MatrixType.Projection); - Graphics.LoadMatrix(ref projection); + Graphics.LoadMatrix(ref Projection); Graphics.SetMatrixMode(MatrixType.Modelview); Events.RaiseProjectionChanged(); } @@ -337,10 +335,9 @@ namespace ClassicalSharp { public bool CanPick(BlockID block) { if (BlockInfo.Draw[block] == DrawType.Gas) return false; if (BlockInfo.Draw[block] == DrawType.Sprite) return true; - if (BlockInfo.Collide[block] != CollideType.Liquid) return true; - return !ModifiableLiquids ? false : - BlockInfo.CanPlace[block] && BlockInfo.CanDelete[block]; + if (BlockInfo.Collide[block] != CollideType.Liquid) return true; + return ModifiableLiquids && BlockInfo.CanPlace[block] && BlockInfo.CanDelete[block]; } diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 6aa0d56f9..a2ecdd1d8 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -558,7 +558,8 @@ namespace ClassicalSharp.GraphicsAPI { } protected unsafe override void LoadOrthoMatrix(float width, float height) { - Matrix4 matrix = Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -10000, 10000); + Matrix4 matrix; + Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -10000, 10000, out matrix); const float zN = -10000, zF = 10000; matrix.M33 = 1 / (zN - zF); matrix.M43 = zN / (zN - zF); diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index 7b8f1d21c..62e05ada2 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -241,7 +241,8 @@ namespace ClassicalSharp.GraphicsAPI { public string[] ApiInfo; protected virtual void LoadOrthoMatrix(float width, float height) { - Matrix4 matrix = Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -10000, 10000); + Matrix4 matrix; + Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -10000, 10000, out matrix); LoadMatrix(ref matrix); } diff --git a/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs b/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs index 191602b7a..75c63877c 100644 --- a/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs +++ b/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs @@ -65,13 +65,14 @@ namespace ClassicalSharp.Renderers { Matrix4 m = Matrix4.Identity, rotY, rotX; Vector2 rotation = game.Camera.GetCameraOrientation(); - Matrix4.RotateY(out rotY, rotation.X); // yaw - m *= rotY; - Matrix4.RotateX(out rotX, rotation.Y); // pitch - m *= rotX; - m *= game.Camera.tiltM; - game.Graphics.LoadMatrix(ref m); + Matrix4.RotateY(out rotY, rotation.X); // yaw + Matrix4.Mult(out m, ref m, ref rotY); + Matrix4.RotateX(out rotX, rotation.Y); // pitch + Matrix4.Mult(out m, ref m, ref rotX); + Matrix4.Mult(out m, ref m, ref game.Camera.tiltM); + + game.Graphics.LoadMatrix(ref m); game.Graphics.BindVb(vb); game.Graphics.DrawVb_IndexedTris(count * 6 / 4); diff --git a/ClassicalSharp/Rendering/HeldBlockRenderer.cs b/ClassicalSharp/Rendering/HeldBlockRenderer.cs index 510bb5914..878d3348c 100644 --- a/ClassicalSharp/Rendering/HeldBlockRenderer.cs +++ b/ClassicalSharp/Rendering/HeldBlockRenderer.cs @@ -57,7 +57,7 @@ namespace ClassicalSharp.Renderers { DoAnimation(delta, lastSwingY); SetBaseOffset(); - game.Graphics.FaceCulling = true; + game.Graphics.FaceCulling = true; game.Graphics.Texturing = true; game.Graphics.SetupAlphaState(BlockInfo.Draw[block]); game.Graphics.DepthTest = false; @@ -74,7 +74,7 @@ namespace ClassicalSharp.Renderers { game.Graphics.Texturing = false; game.Graphics.RestoreAlphaState(BlockInfo.Draw[block]); - game.Graphics.DepthTest = true; + game.Graphics.DepthTest = true; game.Graphics.FaceCulling = false; game.Graphics.LoadMatrix(ref game.View); @@ -88,7 +88,10 @@ namespace ClassicalSharp.Renderers { void SetMatrix() { Player p = game.LocalPlayer; Vector3 eyePos = p.EyePosition; - Matrix4 m = Matrix4.LookAt(eyePos, eyePos - Vector3.UnitZ, Vector3.UnitY) * game.Camera.tiltM; + + Matrix4 m, lookAt; + Matrix4.LookAt(eyePos, eyePos - Vector3.UnitZ, Vector3.UnitY, out lookAt); + Matrix4.Mult(out m, ref lookAt, ref game.Camera.tiltM); game.Graphics.LoadMatrix(ref m); } @@ -122,8 +125,8 @@ namespace ClassicalSharp.Renderers { void ProjectionChanged(object sender, EventArgs e) { float aspectRatio = (float)game.Width / game.Height; float zNear = game.Graphics.MinZNear; - heldBlockProj = Matrix4.CreatePerspectiveFieldOfView(70 * Utils.Deg2Rad, - aspectRatio, zNear, game.ViewDistance); + Matrix4.CreatePerspectiveFieldOfView(70 * Utils.Deg2Rad, + aspectRatio, zNear, game.ViewDistance, out heldBlockProj); } @@ -171,7 +174,7 @@ namespace ClassicalSharp.Renderers { if (swinging) { // i.e. the block has gone to bottom of screen and is now returning back up - // at this point we switch over to the new held block. + // at this point we switch over to the new held block. if (swingY > lastSwingY) lastBlock = block; block = lastBlock; held.ModelBlock = block; @@ -207,7 +210,7 @@ namespace ClassicalSharp.Renderers { void ResetAnim(bool setLastHeld, double period) { time = 0; swingY = 0; - animating = false; swinging = false; + animating = false; swinging = false; this.period = period; if (setLastHeld) lastBlock = game.Inventory.Selected; diff --git a/ClassicalSharp/Utils/Camera.cs b/ClassicalSharp/Utils/Camera.cs index 69239b258..39aae682e 100644 --- a/ClassicalSharp/Utils/Camera.cs +++ b/ClassicalSharp/Utils/Camera.cs @@ -13,10 +13,10 @@ namespace ClassicalSharp { internal float bobbingVer, bobbingHor; /// Calculates the projection matrix for this camera. - public abstract Matrix4 GetProjection(); + public abstract void GetProjection(out Matrix4 m); /// Calculates the world/view matrix for this camera. - public abstract Matrix4 GetView(); + public abstract void GetView(out Matrix4 m); /// Calculates the location of the camera's position in the world. public abstract Vector3 GetCameraPos(float t); @@ -70,11 +70,11 @@ namespace ClassicalSharp { return Utils.GetDirVector(player.HeadYRadians, AdjustHeadX(player.HeadX)); } - public override Matrix4 GetProjection() { + public override void GetProjection(out Matrix4 m) { float fovy = game.Fov * Utils.Deg2Rad; float aspectRatio = (float)game.Width / game.Height; float zNear = game.Graphics.MinZNear; - return Matrix4.CreatePerspectiveFieldOfView(fovy, aspectRatio, zNear, game.ViewDistance); + Matrix4.CreatePerspectiveFieldOfView(fovy, aspectRatio, zNear, game.ViewDistance, out m); } public override void GetPickedBlock(PickedPos pos) { @@ -172,11 +172,14 @@ namespace ClassicalSharp { return true; } - public override Matrix4 GetView() { + public override void GetView(out Matrix4 m) { Vector3 camPos = game.CurrentCameraPos; Vector3 eyePos = player.EyePosition; eyePos.Y += bobbingVer; - return Matrix4.LookAt(camPos, eyePos, Vector3.UnitY) * tiltM; + + Matrix4 lookAt; + Matrix4.LookAt(camPos, eyePos, Vector3.UnitY, out lookAt); + Matrix4.Mult(out m, ref lookAt, ref tiltM); } public override Vector2 GetCameraOrientation() { @@ -201,10 +204,13 @@ namespace ClassicalSharp { public FirstPersonCamera(Game window) : base(window) { } public override bool IsThirdPerson { get { return false; } } - public override Matrix4 GetView() { + public override void GetView(out Matrix4 m) { Vector3 camPos = game.CurrentCameraPos; Vector3 dir = GetDirVector(); - return Matrix4.LookAt(camPos, camPos + dir, Vector3.UnitY) * tiltM; + + Matrix4 lookAt; + Matrix4.LookAt(camPos, camPos + dir, Vector3.UnitY, out lookAt); + Matrix4.Mult(out m, ref lookAt, ref tiltM); } public override Vector2 GetCameraOrientation() { diff --git a/OpenTK/Math/Matrix4.cs b/OpenTK/Math/Matrix4.cs index 48af7e23d..90ebfbc7c 100644 --- a/OpenTK/Math/Matrix4.cs +++ b/OpenTK/Math/Matrix4.cs @@ -150,16 +150,6 @@ namespace OpenTK { result.Row3 = Vector4.UnitW; } - public static void CreateOrthographic(float width, float height, float zNear, float zFar, out Matrix4 result) { - CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); - } - - public static Matrix4 CreateOrthographic(float width, float height, float zNear, float zFar) { - Matrix4 result; - CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); - return result; - } - public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result) { result = new Matrix4(); @@ -176,12 +166,6 @@ namespace OpenTK { result.M43 = -(zFar + zNear) * invFN; result.M44 = 1; } - - public static Matrix4 CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar) { - Matrix4 result; - CreateOrthographicOffCenter(left, right, bottom, top, zNear, zFar, out result); - return result; - } public static void CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar, out Matrix4 result) { if (fovy <= 0 || fovy > Math.PI) @@ -202,12 +186,6 @@ namespace OpenTK { CreatePerspectiveOffCenter(xMin, xMax, yMin, yMax, zNear, zFar, out result); } - - public static Matrix4 CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar) { - Matrix4 result; - CreatePerspectiveFieldOfView(fovy, aspect, zNear, zFar, out result); - return result; - } public static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result) { if (zNear <= 0) @@ -229,14 +207,8 @@ namespace OpenTK { a, b, c, -1, 0, 0, d, 0); } - - public static Matrix4 CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar) { - Matrix4 result; - CreatePerspectiveOffCenter(left, right, bottom, top, zNear, zFar, out result); - return result; - } - public static Matrix4 LookAt(Vector3 eye, Vector3 target, Vector3 up) { + public static void LookAt(Vector3 eye, Vector3 target, Vector3 up, out Matrix4 result) { Vector3 z = Vector3.Normalize(eye - target); Vector3 x = Vector3.Normalize(Vector3.Cross(up, z)); Vector3 y = Vector3.Normalize(Vector3.Cross(z, x)); @@ -245,17 +217,10 @@ namespace OpenTK { new Vector4(x.Y, y.Y, z.Y, 0.0f), new Vector4(x.Z, y.Z, z.Z, 0.0f), Vector4.UnitW); - Matrix4 trans; - Matrix4.Translate(out trans, -eye.X, -eye.Y, -eye.Z); - return trans * rot; - } - - public static Matrix4 Mult(Matrix4 left, Matrix4 right) { - Matrix4 result; - Mult(out result, ref left, ref right); - return result; + Translate(out trans, -eye.X, -eye.Y, -eye.Z); + Mult(out result, ref trans, ref rot); } public static void Mult(out Matrix4 result, ref Matrix4 left, ref Matrix4 right) { @@ -291,10 +256,6 @@ namespace OpenTK { result.Row3.W = (((lM41 * rM14) + (lM42 * rM24)) + (lM43 * rM34)) + (lM44 * rM44); } - public static Matrix4 operator * (Matrix4 left, Matrix4 right) { - return Matrix4.Mult(left, right); - } - public static bool operator == (Matrix4 left, Matrix4 right) { return left.Equals(right); } diff --git a/src/Client/Picking.h b/src/Client/Picking.h index 176020b9e..f4d17a6c3 100644 --- a/src/Client/Picking.h +++ b/src/Client/Picking.h @@ -4,8 +4,8 @@ #include "Constants.h" #include "Vectors.h" #include "BlockID.h" -/* Data for picking/selecting block by the user, and clipping the camera. - Copyright 2014 - 2017 ClassicalSharp | Licensed under BSD-3 +/* Data for picking/selecting block by the user, and clipping the camera. + Copyright 2014 - 2017 ClassicalSharp | Licensed under BSD-3 */ /* Describes the picked/selected block by the user and its position. */ @@ -29,7 +29,8 @@ typedef struct PickedPos_ { } PickedPos; /* Mark as having a selected block, and calculates the closest face of the selected block's position. */ -void PickedPos_SetAsValid(PickedPos* pos, Int32 x, Int32 y, Int32 z, Vector3 min, Vector3 max); +void PickedPos_SetAsValid(PickedPos* pos, Int32 x, Int32 y, Int32 z, + Vector3 min, Vector3 max, BlockID block, Vector3 intersect); /* Marks as not having a selected block. */ void PickedPos_SetAsInvalid(PickedPos* pos);