Skip to content

Commit

Permalink
顶点数组优化
Browse files Browse the repository at this point in the history
  • Loading branch information
YESshowMeCode committed May 7, 2019
1 parent 16c232c commit 9f52a70
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 156 deletions.
Binary file modified SoftRender.v12.suo
Binary file not shown.
60 changes: 31 additions & 29 deletions SoftRender/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,37 @@ private void AddCubeToScene(Scene scene)

m_Cube = new Mesh("Cube");
m_Cube.Vertices = new Vertex[24] {
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)),
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(1, 0), new Color3(0, 0, 0)),
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)),
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)),
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(1, 0), new Color3(0, 0, 0)),
new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)),

new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(255, 0, 0)),
new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(0, 0), new Color3(255, 0, 0)),
new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(255, 0, 0)),
new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(255, 0, 0)),
new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(0, 0), new Color3(255, 0, 0)),
new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(255, 0, 0)),

new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 0), new Color3(255, 255, 0)),
new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(0, 1), new Color3(255, 255, 0)),
new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 1), new Color3(255, 255, 0)),
new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 0), new Color3(255, 255, 0)),
new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(0, 1), new Color3(255, 255, 0)),
new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 1), new Color3(255, 255, 0)),

new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 0), new Color3(0, 255, 0)),
new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(1, 1), new Color3(0, 255, 0)),
new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 1), new Color3(0, 255, 0)),
new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 0), new Color3(0, 255, 0)),
new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(1, 1), new Color3(0, 255, 0)),
new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 1), new Color3(0, 255, 0)),

new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 1), new Color3(0, 0, 255)),
new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 255)),
new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 255)),
new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 1), new Color3(0, 0, 255)),
new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 255)),
new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 255)),

new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 1), new Color3(255, 0, 255)),
new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(255, 0, 255)),
new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(255, 0, 255)),
new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 1), new Color3(255, 0, 255)),
new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(255, 0, 255)),
new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(255, 0, 255)),

new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),
new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),
new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),
new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),
new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),
new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(255, 255, 255)),

new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),
new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),
new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),
new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),
new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),
new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 255, 255)),

};

Expand All @@ -122,9 +122,11 @@ private void AddCubeToScene(Scene scene)
};
RenderTexture[] textures = new RenderTexture[6];
for (int i = 0; i < 6; i++)
textures[i] = new RenderTexture(@"env" + i.ToString() + ".bmp");
{
string path = "../../Texture/env2.bmp";
textures[i] = new RenderTexture(path);
}
m_Cube.TextureMaps = textures;

scene.AddMesh(m_Cube);
}

Expand Down Expand Up @@ -233,8 +235,8 @@ private void OnMouseMove(object sender, MouseEventArgs e)
float y = e.Y;
float dx = m_MouseLeftPos.X - x;
float dy = m_MouseLeftPos.Y - y;
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateY(dx / 5f);
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateX(dy / 5f);
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateY(dx / 10f);
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateX(dy / 10f);
m_MouseLeftPos.X = x;
m_MouseLeftPos.Y = y;
this.Invalidate();
Expand All @@ -252,7 +254,7 @@ private void OnMouseDown(object sender, MouseEventArgs e)
{
m_IsMouseLeftDown = true;
m_MouseLeftPos.X = e.X;
m_MouseLeftPos.X = e.Y;
m_MouseLeftPos.Y = e.Y;
}
}

Expand Down
45 changes: 22 additions & 23 deletions SoftRender/Render/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@

namespace SoftRender.Render
{
//相机类
class Camera
{
private Vector4 mPosition;
private Vector4 mTarget;
private Vector4 mUp;
private Vector4 m_Position;
private Vector4 m_Target;
private Vector4 m_Up;

/// <summary>
/// 相机位置
/// </summary>
public Vector4 Position
{
get { return mPosition; }
set { mPosition = value; }
get { return m_Position; }
set { m_Position = value; }
}

/// <summary>
/// 目标
/// </summary>
public Vector4 Target
{
get { return mTarget; }
set { mTarget = value; }
get { return m_Target; }
set { m_Target = value; }
}

/// <summary>
/// 上方
/// </summary>
public Vector4 Up
{
get { return mUp; }
set { mUp = value; }
get { return m_Up; }
set { m_Up = value; }
}

/// <summary>
Expand All @@ -46,27 +45,27 @@ public Matrix4x4 GetLookAt()
Vector4 xaxis, yaxis, zaxis;

//法向量 z
zaxis = mTarget - mPosition;
zaxis = m_Target - m_Position;
zaxis.Normalize();
xaxis = Vector4.Cross(mUp, zaxis);
xaxis = Vector4.Cross(m_Up, zaxis);
xaxis.Normalize();
yaxis = Vector4.Cross(zaxis, xaxis);
yaxis.Normalize();

view.matrix[0, 0] = xaxis.X;
view.matrix[1, 0] = xaxis.Y;
view.matrix[2, 0] = xaxis.Z;
view.matrix[3, 0] = -Vector4.Dot(xaxis, mPosition);
view.matrix[3, 0] = -Vector4.Dot(xaxis, m_Position);

view.matrix[0, 1] = yaxis.X;
view.matrix[1, 1] = yaxis.Y;
view.matrix[2, 1] = yaxis.Z;
view.matrix[3, 1] = -Vector4.Dot(yaxis, mPosition);
view.matrix[3, 1] = -Vector4.Dot(yaxis, m_Position);

view.matrix[0, 2] = zaxis.X;
view.matrix[1, 2] = zaxis.Y;
view.matrix[2, 2] = zaxis.Z;
view.matrix[3, 2] = -Vector4.Dot(zaxis, mPosition);
view.matrix[3, 2] = -Vector4.Dot(zaxis, m_Position);

view.matrix[0, 3] = view.matrix[1, 3] = view.matrix[2, 3] = 0.0f;
view.matrix[3, 3] = 1.0f;
Expand Down Expand Up @@ -103,7 +102,7 @@ public Matrix4x4 GetProject(float fov, float aspect, float zn, float zf)
/// <param name="y"></param>
public void Rotate(Vector4 position, float x, float y)
{
mPosition = (Matrix4x4.RotateX(x) * Matrix4x4.RotateY(y)).LeftApply(position);
m_Position = (Matrix4x4.RotateX(x) * Matrix4x4.RotateY(y)) * position;
}

/// <summary>
Expand All @@ -112,7 +111,7 @@ public void Rotate(Vector4 position, float x, float y)
/// <param name="pos"></param>
public void UpdatePosition(Vector4 pos)
{
mPosition = pos;
m_Position = pos;
}

/// <summary>
Expand All @@ -121,16 +120,16 @@ public void UpdatePosition(Vector4 pos)
/// <param name="distance">移动的距离</param>
public void MoveForward(float distance)
{
Vector4 dir = (mTarget - mPosition);
float w = mPosition.W;
Vector4 dir = (m_Target - m_Position);
float w = m_Position.W;
if (distance > 0 && dir.Length < 1.5f)
return;

if (distance < 0 && dir.Length > 30)
return;

mPosition = mPosition + (dir.Normalize() * distance);
mPosition.W = w;
m_Position = m_Position + (dir.Normalize() * distance);
m_Position.W = w;
}

/// <summary>
Expand All @@ -139,7 +138,7 @@ public void MoveForward(float distance)
/// <param name="r"></param>
public void MoveTheta(float r)
{
mPosition = mPosition * Matrix4x4.RotateX(r);
m_Position = m_Position * Matrix4x4.RotateX(r);
}

/// <summary>
Expand All @@ -148,7 +147,7 @@ public void MoveTheta(float r)
/// <param name="r"></param>
public void MovePhi(float r)
{
mPosition = mPosition * Matrix4x4.RotateY(r);
m_Position = m_Position * Matrix4x4.RotateY(r);
}
}
}
7 changes: 7 additions & 0 deletions SoftRender/Render/Color3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public Color3(byte red, byte green, byte blue)
this.B = blue;
}

public Color3()
{
R = 255;
G = 255;
B = 255;
}

/// <summary>
/// 颜色之间相乘
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions SoftRender/Render/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Device(Bitmap bmp)
this.mHeight = bmp.Height;
this.mWidth = bmp.Width;
this.mDepthBuffer = new float[bmp.Width * bmp.Height];
mRenderMode = RenderMode.WIREFRAME;
mRenderMode = RenderMode.TEXTURED;
}

/// <summary>
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Putpixel(int x, int y, float z, Color3 color)
/// <returns></returns>
public Vector4 Projection(Vector4 vector, Matrix4x4 mvp)
{
Vector4 point = mvp.LeftApply(vector);
Vector4 point = mvp * vector;
Vector4 viewpoint = ProjectionDev(point);
return viewpoint;
}
Expand All @@ -147,7 +147,7 @@ public Vector4 Projection(Vector4 vector, Matrix4x4 mvp)
/// <returns></returns>
public Vector4 ToHomogeneous(Vector4 x, Matrix4x4 M)
{
Vector4 val = M.LeftApply(x);
Vector4 val = M*x;
float rh = 1.0f / val.W;
val.X = val.X * rh;
val.Y = val.Y * rh;
Expand Down
57 changes: 26 additions & 31 deletions SoftRender/Render/Matrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SoftRender.Render
{
//矩阵类
struct Matrix4x4
class Matrix4x4
{
//用数组表示4x4矩阵的表示
public float[,] matrix;
Expand All @@ -20,7 +20,7 @@ struct Matrix4x4
set { matrix[i, j] = value; }
}

public Matrix4x4(int x): this()
public Matrix4x4(int x)
{
matrix = new float[4, 4];
this.Identity();
Expand All @@ -41,17 +41,17 @@ public void Identity()
/// <summary>
/// 矩阵相加
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="right"></param>
/// <param name="left"></param>
/// <returns></returns>
public static Matrix4x4 operator +(Matrix4x4 a, Matrix4x4 b)
public static Matrix4x4 operator +(Matrix4x4 right, Matrix4x4 left)
{
Matrix4x4 temp = new Matrix4x4(1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp.matrix[i, j] = a.matrix[i, j] + b.matrix[i, j];
temp.matrix[i, j] = right.matrix[i, j] + left.matrix[i, j];
}
}
return temp;
Expand All @@ -60,17 +60,17 @@ public void Identity()
/// <summary>
/// 矩阵相减
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="right"></param>
/// <param name="left"></param>
/// <returns></returns>
public static Matrix4x4 operator -(Matrix4x4 a, Matrix4x4 b)
public static Matrix4x4 operator -(Matrix4x4 right, Matrix4x4 left)
{
Matrix4x4 temp = new Matrix4x4(1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp.matrix[i, j] = a.matrix[i, j] + b.matrix[i, j];
temp.matrix[i, j] = right.matrix[i, j] + left.matrix[i, j];
}
}
return temp;
Expand All @@ -79,20 +79,20 @@ public void Identity()
/// <summary>
/// 矩阵相乘
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="right"></param>
/// <param name="left"></param>
/// <returns></returns>
public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b)
public static Matrix4x4 operator *(Matrix4x4 right, Matrix4x4 left)
{
Matrix4x4 temp = new Matrix4x4(1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp.matrix[j, i] = (a.matrix[j, 0] * b.matrix[0, i]) +
(a.matrix[j, 1] * b.matrix[1, i]) +
(a.matrix[j, 2] * b.matrix[2, i]) +
(a.matrix[j, 3] * b.matrix[3, i]);
temp.matrix[j, i] = (right.matrix[j, 0] * left.matrix[0, i]) +
(right.matrix[j, 1] * left.matrix[1, i]) +
(right.matrix[j, 2] * left.matrix[2, i]) +
(right.matrix[j, 3] * left.matrix[3, i]);
}
}
return temp;
Expand Down Expand Up @@ -135,21 +135,16 @@ public void Scale(float c)

}

/// <summary>
/// 向量乘矩阵
/// </summary>
/// <param name="vector"></param>
/// <returns></returns>
public Vector4 LeftApply(Vector4 vector)
{
Vector4 V = new Vector4();
V.X = vector.X * this.matrix[0, 0] + vector.Y * this.matrix[1, 0] + vector.Z * this.matrix[2, 0] + vector.W * this.matrix[3, 0];
V.Y = vector.X * this.matrix[0, 1] + vector.Y * this.matrix[1, 1] + vector.Z * this.matrix[2, 1] + vector.W * this.matrix[3, 1];
V.Z = vector.X * this.matrix[0, 2] + vector.Y * this.matrix[1, 2] + vector.Z * this.matrix[2, 2] + vector.W * this.matrix[3, 2];
V.W = vector.X * this.matrix[0, 3] + vector.Y * this.matrix[1, 3] + vector.Z * this.matrix[2, 3] + vector.W * this.matrix[3, 3];
return V;
public static Vector4 operator *(Matrix4x4 matrix, Vector4 vector)
{
Vector4 V = new Vector4();
V.X = vector.X * matrix[0, 0] + vector.Y * matrix[1, 0] + vector.Z * matrix[2, 0] + vector.W * matrix[3, 0];
V.Y = vector.X * matrix[0, 1] + vector.Y * matrix[1, 1] + vector.Z * matrix[2, 1] + vector.W * matrix[3, 1];
V.Z = vector.X * matrix[0, 2] + vector.Y * matrix[1, 2] + vector.Z * matrix[2, 2] + vector.W * matrix[3, 2];
V.W = vector.X * matrix[0, 3] + vector.Y * matrix[1, 3] + vector.Z * matrix[2, 3] + vector.W * matrix[3, 3];
return V;
}

}

/// <summary>
/// 平移变换矩阵
Expand Down
Loading

0 comments on commit 9f52a70

Please sign in to comment.