Skip to content

Commit

Permalink
基本逻辑上传
Browse files Browse the repository at this point in the history
  • Loading branch information
YESshowMeCode committed May 4, 2019
1 parent 5c06449 commit cdb6e99
Show file tree
Hide file tree
Showing 25 changed files with 640 additions and 54 deletions.
Binary file added .vs/SoftRender/v15/.suo
Binary file not shown.
Empty file.
Binary file added .vs/SoftRender/v15/Server/sqlite3/storage.ide
Binary file not shown.
Binary file not shown.
Binary file added .vs/SoftRender/v15/Server/sqlite3/storage.ide-wal
Binary file not shown.
32 changes: 26 additions & 6 deletions SoftRender/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,44 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using SoftRender.Render;

namespace SoftRender
{
public partial class SoftRender : Form
{

//private Bitmap _texture;
//private Bitmap _frameBuffer;
//private Graphics _frameG;
//private float[,] _zBuffer;
//private Mesh _mesh;
private Device mDevice;
private Bitmap mBmp;
private Rectangle mRect;
private Graphics mGraphics;
private Scene mScene;
private PixelFormat mPixelFormat;
private BitmapData mData;

private Matrix4x4 mViewMat;
private Matrix4x4 mProjectionMat;
private Mesh mCube;
private bool mIsMouseLeftDown = false;
private Vector2 mMouseLeftPos = new Vector2();


public SoftRender()
{
InitializeComponent();
}

private void InitSettings()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}



}
}
129 changes: 128 additions & 1 deletion SoftRender/Render/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,133 @@ 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 Matrix GetLookAt()
{
Matrix view = new Matrix();

Vector4 xAxis, yAxis, zAxis;

zAxis = m_target - m_Position;
zAxis.Normalize();
yAxis = Vector4.Cross(m_Up, m_Position);
yAxis.Normalize();
xAxis = Vector4.Cross(zAxis, yAxis);
xAxis.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 Matrix GetProject(float fov,float aspect,float zn,float zf)
{
Matrix project = new Matrix();

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 = (Matrix.RotateX(x) * Matrix.RotateY(y)).LeftApply(position);
}

public void MoveForward(float distance)
{
Vector4 dir = (Target - 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 * Matrix.RotateX(r);
}

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

}

}
34 changes: 34 additions & 0 deletions SoftRender/Render/Clip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
class Clip
{
private Device m_Device;
private List<Vertex> m_OutputList;

public Device Device
{
get { return m_Device; }
}

public List<Vertex> OutputList
{
get { return m_OutputList; }
}

public Clip(Device device)
{
m_Device = device;
m_OutputList = new List<Vertex>();
}




}
}
42 changes: 42 additions & 0 deletions SoftRender/Render/Face.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
public enum FaceType
{
LEFT = 1,
RIGHT,
BUTTOM,
NEAR,
FAR,
NONE,
}

class Face
{
public int A;
public int B;
public int C;
public FaceType FaceType;

public Face(int a,int b,int c)
{
A = a;
B = b;
C = c;
}

public Face(int a,int b,int c,FaceType faceType)
{
A = a;
B = b;
C = c;
FaceType = faceType;
}

}
}
33 changes: 33 additions & 0 deletions SoftRender/Render/Light.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
class Light
{

private Vector4 m_Position;
private Color m_Color;

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

public Color Color
{
get { return m_Color; }
set { m_Color = value; }
}

public Light(Vector4 pos,Color col)
{
m_Color = col;
m_Position = pos;
}
}
}
34 changes: 34 additions & 0 deletions SoftRender/Render/Material.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
class Material
{

private float m_AmbientStrength;
private Color m_Diffuse;

public float AmbientStrength
{
get { return m_AmbientStrength; }
set { m_AmbientStrength = value; }
}

public Color Diffuse
{
get { return m_Diffuse; }
set { m_Diffuse = value; }
}

public Material(float ambient,Color color)
{
m_AmbientStrength = ambient;
m_Diffuse = color;
}

}
}
3 changes: 3 additions & 0 deletions SoftRender/Render/MathUntil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace SoftRender.Render
class MathUntil
{




public static Matrix GetTranslate(float x, float y, float z)
{
return new Matrix(1, 0, 0, 0,
Expand Down
32 changes: 32 additions & 0 deletions SoftRender/Render/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ public Matrix(float a1, float a2, float a3, float a4,
return matrix;
}

public Vector4 LeftApply(Vector4 vec)
{
Vector4 res = new Vector4();
res.x = vec.x * _m[0, 0] + vec.y * _m[1, 0] + vec.z * _m[2, 0] + vec.w * _m[3, 0];
res.y = vec.x * _m[0, 1] + vec.y * _m[1, 1] + vec.z * _m[2, 1] + vec.w * _m[3, 1];
res.z = vec.x * _m[0, 2] + vec.y * _m[1, 2] + vec.z * _m[2, 2] + vec.w * _m[3, 2];
res.w = vec.x * _m[0, 3] + vec.y * _m[1, 3] + vec.z * _m[2, 3] + vec.w * _m[3, 3];
return res;
}

public static Matrix RotateX(float rotateX)
{
Matrix matrix = new Matrix();
rotateX = rotateX * (float)Math.PI / 180;
matrix[1, 1] = (float)Math.Cos(rotateX);
matrix[1, 2] = (float)Math.Sin(rotateX);
matrix[2, 1] = (float)Math.Sin(rotateX);
matrix[2, 2] = (float)Math.Cos(rotateX);
return matrix;
}

public static Matrix RotateY(float rotateY)
{
Matrix matrix = new Matrix();
rotateY = rotateY * (float)Math.PI / 180;
matrix[0, 0] = (float)Math.Cos(rotateY);
matrix[0, 2] = (float)Math.Sin(rotateY);
matrix[2, 0] = (float)Math.Sin(rotateY);
matrix[2, 2] = (float)Math.Cos(rotateY);
return matrix;
}

public void SetZero()
{
for (int i = 0; i < 4; ++i)
Expand Down
Loading

0 comments on commit cdb6e99

Please sign in to comment.