Skip to content

Commit

Permalink
部分逻辑上传
Browse files Browse the repository at this point in the history
  • Loading branch information
YESshowMeCode committed May 6, 2019
1 parent 2835949 commit 10d767a
Show file tree
Hide file tree
Showing 28 changed files with 2,120 additions and 1,899 deletions.
Binary file modified SoftRender.v12.suo
Binary file not shown.
54 changes: 27 additions & 27 deletions SoftRender/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void AddCubeToScene(Scene scene)
return;

m_Cube = new Mesh("Cube");
m_Cube.VertexBuffer = new Vertex[24] {
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)),
Expand Down Expand Up @@ -100,30 +100,30 @@ private void AddCubeToScene(Scene scene)

};

m_Cube.Face = new Face[] {
m_Cube.Faces = new Face[] {
// 正面
new Face(2, 5, 8, FaceType.NEAR),
new Face(2, 8, 11, FaceType.NEAR),
new Face(2, 5, 8, FaceTypes.NEAR),
new Face(2, 8, 11, FaceTypes.NEAR),
// 右面
new Face(4, 16, 7, FaceType.RIGHT),
new Face(16, 19, 7, FaceType.RIGHT),
new Face(4, 16, 7, FaceTypes.RIGHT),
new Face(16, 19, 7, FaceTypes.RIGHT),
// 左面
new Face(13, 1, 10, FaceType.LEFT),
new Face(13, 10, 22, FaceType.LEFT),
new Face(13, 1, 10, FaceTypes.LEFT),
new Face(13, 10, 22, FaceTypes.LEFT),
// 背面
new Face(17, 14, 23, FaceType.FAR),
new Face(17, 23, 20, FaceType.FAR),
new Face(17, 14, 23, FaceTypes.FAR),
new Face(17, 23, 20, FaceTypes.FAR),
// 上面
new Face(9, 6, 18, FaceType.TOP),
new Face(9, 18, 21, FaceType.TOP),
new Face(9, 6, 18, FaceTypes.TOP),
new Face(9, 18, 21, FaceTypes.TOP),
// 下面
new Face(12, 15, 3, FaceType.BUTTOM),
new Face(12, 3, 0, FaceType.BUTTOM)
new Face(12, 15, 3, FaceTypes.BUTTOM),
new Face(12, 3, 0, FaceTypes.BUTTOM)
};
RenderTexture[] textures = new RenderTexture[6];
for (int i = 0; i < 6; i++)
textures[i] = new RenderTexture(@"env" + i.ToString() + ".bmp");
m_Cube.RenderTexture = textures;
textures[i] = new RenderTexture("Texture/env" + i.ToString() + ".bmp");
m_Cube.TextureMaps = textures;

scene.AddMesh(m_Cube);
}
Expand Down Expand Up @@ -185,11 +185,11 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
case Keys.F1:
Light light = new Light(new Vector4(5, 5, -5, 1), new Color3(200, 255, 255));
m_Scene.AddLight(light);
m_Scene.UseLight = true;
m_Scene.IsUseLight = true;
break;
case Keys.F2:
m_Scene.DeleLight();
m_Scene.UseLight = false;
m_Scene.DelLight();
m_Scene.IsUseLight = false;
break;
case Keys.Escape:
Close();
Expand Down Expand Up @@ -231,12 +231,12 @@ private void OnMouseMove(object sender, MouseEventArgs e)
{
float x = e.X;
float y = e.Y;
float dx = m_MouseLeftPos.x - x;
float dy = m_MouseLeftPos.x - y;
float dx = m_MouseLeftPos.X - x;
float dy = m_MouseLeftPos.X - y;
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateY(dx / 5f);
m_Cube.Transform = m_Cube.Transform * Matrix4x4.RotateX(dy / 5f);
m_MouseLeftPos.x = x;
m_MouseLeftPos.x = y;
m_MouseLeftPos.X = x;
m_MouseLeftPos.X = y;
this.Invalidate();
}
}
Expand All @@ -251,8 +251,8 @@ private void OnMouseDown(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Left)
{
m_IsMouseLeftDown = true;
m_MouseLeftPos.x = e.X;
m_MouseLeftPos.y = e.Y;
m_MouseLeftPos.X = e.X;
m_MouseLeftPos.X = e.Y;
}
}

Expand All @@ -266,8 +266,8 @@ private void OnMouseUp(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Left)
{
m_IsMouseLeftDown = false;
m_MouseLeftPos.x = 0f;
m_MouseLeftPos.y = 0f;
m_MouseLeftPos.X = 0f;
m_MouseLeftPos.X = 0f;
}
}

Expand Down
283 changes: 149 additions & 134 deletions SoftRender/Render/Camera.cs
Original file line number Diff line number Diff line change
@@ -1,139 +1,154 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
class Camera
{
private Vector4 m_Position;
private Vector4 m_target;
private Vector4 m_Up;


public Vector4 Position
{
get
{
return m_Position;
}
set
{
m_Position = value;
}
}

public Vector4 Target
{
get
{
return m_target;
}
set
{
m_target = value ;
}
}

public Vector4 Up
{
get
{
return m_Up;
}
set
{
m_Up = value;
}
}

public Matrix4x4 GetLookAt()
{
Matrix4x4 view = new Matrix4x4(1);

Vector4 xAxis, yAxis, zAxis;

zAxis = m_target - m_Position;
zAxis.Normalize();
xAxis = Vector4.Cross(m_Up, m_Position);
xAxis.Normalize();
yAxis = Vector4.Cross(zAxis, xAxis);
yAxis.Normalize();

view[0, 0] = xAxis.x;
view[1, 0] = xAxis.y;
view[2, 0] = xAxis.z;
view[3, 0] = -Vector4.Dot(xAxis, m_Position);

view[0, 1] = yAxis.x;
view[1, 1] = yAxis.y;
view[2, 1] = yAxis.z;
view[3, 1] = -Vector4.Dot(yAxis, m_Position);

view[0, 2] = zAxis.x;
view[1, 2] = zAxis.y;
view[2, 2] = zAxis.z;
view[3, 2] = -Vector4.Dot(zAxis, m_Position);

view[0, 3] = 0.0f;
view[1, 3] = 0.0f;
view[2, 3] = 0.0f;
view[3, 3] = 1.0f;

return view;
}


public Matrix4x4 GetProject(float fov,float aspect,float zn,float zf)
{
Matrix4x4 project = new Matrix4x4(1);
project.SetZero();
project[0, 0] = 1 / ((float)Math.Tan(fov * 0.5f) * aspect);
project[1, 1] = 1 / ((float)Math.Tan(fov * 0.5f));
project[2, 2] = (zf + zn) / (zf - zn);
project[2, 3] = 1.0f;
project[3, 2] = 2 * (zn * zf) / (zn - zf);

return project;
}

public void Rotate(Vector4 position ,float x,float y)
{
m_Position = (Matrix4x4.RotateX(x) * Matrix4x4.RotateY(y)).LeftApply(position);
}

public void MoveForward(float distance)
{
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;
}

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



public void MoveTheta(float r)
{
m_Position = m_Position * Matrix4x4.RotateX(r);
}

public void MovePhi(float r)
{
m_Position = m_Position * Matrix4x4.RotateY(r);
}

}

//相机类
class Camera
{
private Vector4 mPosition;
private Vector4 mTarget;
private Vector4 mUp;

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

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

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

/// <summary>
/// 观察矩阵
/// </summary>
/// <returns></returns>
public Matrix4x4 GetLookAt()
{
Matrix4x4 view = new Matrix4x4(1);
Vector4 xaxis, yaxis, zaxis;

//法向量 z
zaxis = mTarget - mPosition;
zaxis.Normalize();
xaxis = Vector4.Cross(mUp, 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[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[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[0, 3] = view.matrix[1, 3] = view.matrix[2, 3] = 0.0f;
view.matrix[3, 3] = 1.0f;

return view;
}

/// <summary>
/// 投影矩阵
/// </summary>
/// <param name="fov">y方向的视角</param>
/// <param name="aspect">纵横比</param>
/// <param name="zn">近裁剪 平面到原点的距离</param>
/// <param name="zf">远裁剪 平面到原点的距离</param>
/// <returns></returns>
public Matrix4x4 GetProject(float fov, float aspect, float zn, float zf)
{
Matrix4x4 project = new Matrix4x4(1);
project.SetZero();
project.matrix[0, 0] = 1 / ((float)Math.Tan(fov * 0.5f) * aspect);
project.matrix[1, 1] = 1 / (float)Math.Tan(fov * 0.5f);
project.matrix[2, 2] = (zf + zn) / (zf - zn);
project.matrix[2, 3] = 1.0f;
project.matrix[3, 2] = 2 * (zn * zf) / (zn - zf);

return project;
}

/// <summary>
/// 旋转摄像机
/// </summary>
/// <param name="position"></param>
/// <param name="x"></param>
/// <param name="y"></param>
public void Rotate(Vector4 position, float x, float y)
{
mPosition = (Matrix4x4.RotateX(x) * Matrix4x4.RotateY(y)).LeftApply(position);
}

/// <summary>
/// 改变相机的位置
/// </summary>
/// <param name="pos"></param>
public void UpdatePosition(Vector4 pos)
{
mPosition = pos;
}

/// <summary>
/// 前后移动摄像机
/// </summary>
/// <param name="distance">移动的距离</param>
public void MoveForward(float distance)
{
Vector4 dir = (mTarget - mPosition);
float w = mPosition.W;
if (distance > 0 && dir.Length < 1.5f)
return;

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

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

/// <summary>
/// 绕X轴旋转
/// </summary>
/// <param name="r"></param>
public void MoveTheta(float r)
{
mPosition = mPosition * Matrix4x4.RotateX(r);
}

/// <summary>
/// 绕y轴旋转
/// </summary>
/// <param name="r"></param>
public void MovePhi(float r)
{
mPosition = mPosition * Matrix4x4.RotateY(r);
}
}
}
Loading

0 comments on commit 10d767a

Please sign in to comment.