Merge branch 'master' of github.com:UnknownShadow200/ClassicalSharp

This commit is contained in:
UnknownShadow200 2017-09-07 14:15:09 +10:00
commit a67cf0fd9f
12 changed files with 58 additions and 87 deletions

View File

@ -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);

View File

@ -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++) {

View File

@ -26,6 +26,7 @@ namespace ClassicalSharp.Entities {
Player first = FirstOtherWithSameSkin();
if (first == null) {
game.Graphics.DeleteTexture(ref TextureId);
ResetSkin();
}
ContextLost();
}
@ -135,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);
}
@ -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;

View File

@ -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];
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -13,10 +13,10 @@ namespace ClassicalSharp {
internal float bobbingVer, bobbingHor;
/// <summary> Calculates the projection matrix for this camera. </summary>
public abstract Matrix4 GetProjection();
public abstract void GetProjection(out Matrix4 m);
/// <summary> Calculates the world/view matrix for this camera. </summary>
public abstract Matrix4 GetView();
public abstract void GetView(out Matrix4 m);
/// <summary> Calculates the location of the camera's position in the world. </summary>
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() {

View File

@ -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);
}

View File

@ -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);