Skip to content

Commit

Permalink
上传纹理
Browse files Browse the repository at this point in the history
  • Loading branch information
YESshowMeCode committed May 5, 2019
1 parent 354c0fa commit 3ae375f
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 135 deletions.
Binary file modified SoftRender.v12.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion SoftRender/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 28 additions & 25 deletions SoftRender/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,40 @@ private void AddCubeToScene(Scene scene)
return;

m_Cube = new Mesh("Cube");
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(1, 0), new Color3(0, 0, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, -1, 1), new Vector4(-1, -1, -1, 1), new Vector2(0, 0), new Color3(0, 0, 0)));
m_Cube.VertexBuffer = 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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(1, 0, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(0, 0), new Color3(1, 0, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, -1, 1), new Vector4(1, -1, -1, 1), new Vector2(1, 0), new Color3(1, 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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 0), new Color3(1, 1, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 1), new Color3(1, 1, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, -1, 1), new Vector4(1, 1, -1, 1), new Vector2(1, 1), new Color3(1, 1, 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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 0), new Color3(0, 1, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(1, 1), new Color3(0, 1, 0)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, -1, 1), new Vector4(-1, 1, -1, 1), new Vector2(0, 1), new Color3(0, 1, 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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 1), new Color3(0, 0, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, -1, 1, 1), new Vector4(-1, -1, 1, 1), new Vector2(0, 0), new Color3(0, 0, 1)));
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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 1), new Color3(1, 0, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(1, 0, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, -1, 1, 1), new Vector4(1, -1, 1, 1), new Vector2(1, 0), new Color3(1, 0, 1)));
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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(1, 1, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(1, 1, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), new Vector2(1, 1), new Color3(1, 1, 1)));
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)),

m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 1, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 1, 1)));
m_Cube.VertexBuffer.Add(new Vertex(new Vector4(-1, 1, 1, 1), new Vector4(-1, 1, 1, 1), new Vector2(0, 1), new Color3(0, 1, 1)));
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)),

};

m_Cube.Face = new Face[] {
// 正面
Expand Down Expand Up @@ -180,7 +183,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
m_Device.RenderMode = RenderMode.CUBETEXTURED;
break;
case Keys.F1:
Light light = new Light(new Vector4(5, 5, -5, 1), new Color3(0.8f, 1.0f, 1.0f));
Light light = new Light(new Vector4(5, 5, -5, 1), new Color3(200, 255, 255));
m_Scene.AddLight(light);
m_Scene.UseLight = true;
break;
Expand Down
130 changes: 50 additions & 80 deletions SoftRender/Render/Color3.cs
Original file line number Diff line number Diff line change
@@ -1,98 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftRender.Render
{
class Color3
// rgb 0-255
public struct Color3
{

public float _r;
public float _g;
public float _b;


public Color3()
{

}

public Color3(float r, float g, float b)
{
this._r = MathUntil.Range(r, 0, 1);
this._g = MathUntil.Range(g, 0, 1);
this._b = MathUntil.Range(b, 0, 1);
}

public Color3(System.Drawing.Color c)
{
this._r = MathUntil.Range((float)c.R / 255, 0, 1);
this._g = MathUntil.Range((float)c.G / 255, 0, 1);
this._b = MathUntil.Range((float)c.B / 255, 0, 1);
}

public float r
{
get
{
return MathUntil.Range(_r, 0, 1);
}
set
{
_r = MathUntil.Range(value, 0, 1);
}
}

public float g
{
get
{
return MathUntil.Range(_g, 0, 1);
}
set
{
_g = MathUntil.Range(value, 0, 1);
}
}

public float b
{
get { return MathUntil.Range(_b, 0, 1); }
set { _b = MathUntil.Range(value, 0, 1); }
}


public static Color3 Lerp(Color3 right, Color3 left, float f)
public byte R;
public byte G;
public byte B;

/// <summary>
/// 用rgb构造一个颜色
/// </summary>
/// <param name="red"></param>
/// <param name="green"></param>
/// <param name="blue"></param>
public Color3(byte red, byte green, byte blue)
: this()
{
return new Color3(right.r * f + left.r * (1 - f), right.g * f + left.g * (1 - f), right.b * f + left.b * (1 - f));
this.R = red;
this.G = green;
this.B = blue;
}


public static Color3 operator +(Color3 right, Color3 left)
/// <summary>
/// 颜色之间相乘
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static Color3 operator *(Color3 c1, Color3 c2)
{
return new Color3(right.r + left.r, right.g + left.g, right.b + left.b);
float r = (c1.R / 255f) * (c2.R / 255f);
float g = (c1.G / 255f) * (c2.G / 255f);
float b = (c1.B / 255f) * (c2.B / 255f);
return new Color3((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}

public static Color3 operator -(Color3 right, Color3 left)
/// <summary>
/// 颜色乘数值
/// </summary>
/// <param name="c1"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Color3 operator *(Color3 c1, float t)
{
return new Color3(right.r - left.r, right.g - left.g, right.b - left.b);
byte r = (byte)Math.Min((c1.R * t), 255);
byte g = (byte)Math.Min((c1.G * t), 255);
byte b = (byte)Math.Min((c1.B * t), 255);
return new Color3(r, g, b);
}

public static Color3 operator *(Color3 right, float f)
/// <summary>
/// 颜色相加
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static Color3 operator +(Color3 c1, Color3 c2)
{
return new Color3(right.r * f, right.g * f, right.b * f);
}

public static Color3 operator *(Color3 right, Color3 left)
{
Color3 col = new Color3();
col.r = (right.r / 255) * (left.r / 255);
col.g = (right.g / 255) * (left.g / 255);
col.b = (right.b / 255) * (left.b / 255);
return col;

byte r = (byte)Math.Min(c1.R + c2.R, 255);
byte g = (byte)Math.Min(c1.G + c2.G, 255);
byte b = (byte)Math.Min(c1.B + c2.B, 255);
return new Color3(r, g, b);
}
}
}
54 changes: 35 additions & 19 deletions SoftRender/Render/Device.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;

Expand Down Expand Up @@ -60,21 +57,21 @@ public void Clear(BitmapData bmpData)
{
m_DepthBuffer[index] = float.MaxValue;
}
//unsafe
//{
// byte* ptr = (byte*)(bmpData.Scan0);
// for(int i = 0; i < bmpData.Height; i++)
// {
// for(int j = 0; j < bmpData.Width; j++)
// {
// *ptr = 0;
// *(ptr + 1) = 0;
// *(ptr + 2) = 0;
// ptr += 3;
// }
// ptr += bmpData.Stride - bmpData.Width * 3;
// }
//}
unsafe
{
byte* ptr = (byte*)(bmpData.Scan0);
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
*ptr = 0;
*(ptr + 1) = 0;
*(ptr + 2) = 0;
ptr += 3;
}
ptr += bmpData.Stride - bmpData.Width * 3;
}
}
}


Expand All @@ -84,6 +81,15 @@ public void PutPixel(int x,int y,float z,Color3 col)
if (m_DepthBuffer[index] < z)
return;
m_DepthBuffer[index] = z;

unsafe
{
byte* ptr = (byte*)(this.m_BmpData.Scan0);
byte* row = ptr + (y * this.m_BmpData.Stride);
row[x * 3] = col.B;
row[x * 3 + 1] = col.G;
row[x * 3 + 2] = col.R;
}
}

public Vector4 Projection(Vector4 vector,Matrix4x4 mvp)
Expand All @@ -96,7 +102,7 @@ public Vector4 Projection(Vector4 vector,Matrix4x4 mvp)
private Vector4 ProjectionDev(Vector4 v)
{
Vector4 vec = new Vector4();
float rhw = 1.0f / v.x;
float rhw = 1.0f / v.w;
vec.x = (1.0f + v.x * rhw) * Width * 0.5f;
vec.y = (1.0f - v.y * rhw) * Height * 0.5f;
vec.z = v.z * rhw;
Expand Down Expand Up @@ -208,6 +214,16 @@ public Color3 GetPixelColor(float u, float v, TextureMap texture)
byte red = 0;
byte green = 0;
byte blue = 0;

unsafe
{
byte* ptr = (byte*)(texture.bitmapData.Scan0);
byte* row = ptr + (y * texture.bitmapData.Stride);

red = row[x * 3 + 2];
green = row[x * 3 + 1];
blue = row[x * 3];
}
return new Color3(red, green, blue);
}
}
Expand Down
4 changes: 2 additions & 2 deletions SoftRender/Render/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace SoftRender.Render
{
public enum FaceType
{
LEFT = 1,
LEFT = 0,
RIGHT,
BUTTOM,
TOP,
BUTTOM,
NEAR,
FAR,
NONE,
Expand Down
6 changes: 3 additions & 3 deletions SoftRender/Render/MathUntil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static float Lerp(float right, float left, float f)

public static Color3 Lerp(Color3 c1, Color3 c2, float k)
{
byte r = (byte)(c1.r + (c2.r - c1.r) * k);
byte g = (byte)(c1.g + (c2.g - c1.g) * k);
byte b = (byte)(c1.b + (c2.b - c1.b) * k);
byte r = (byte)(c1.R + (c2.R - c1.R) * k);
byte g = (byte)(c1.G + (c2.G - c1.G) * k);
byte b = (byte)(c1.B + (c2.B - c1.B) * k);
return new Color3(r, g, b);
}

Expand Down
9 changes: 4 additions & 5 deletions SoftRender/Render/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SoftRender.Render
class Mesh
{
private string m_Name;
private List<Vertex> m_VertexBuffer;
private Vertex[] m_VertexBuffer;
private Face[] m_Face;
private Material m_Material;
private RenderTexture[] m_RenderTexture;
Expand All @@ -22,7 +22,7 @@ public string Name
get { return m_Name; }
}

public List<Vertex> VertexBuffer
public Vertex[] VertexBuffer
{
get { return m_VertexBuffer; }
set { m_VertexBuffer = value; }
Expand Down Expand Up @@ -58,7 +58,6 @@ public Mesh(string name)
m_Name = name;
m_Material = new Material(0.9f, new Color3(200, 200, 200));
m_Transform = new Matrix4x4();
m_VertexBuffer = new List<Vertex>();
m_Transform.Identity();
}

Expand Down Expand Up @@ -124,8 +123,8 @@ public void Render(Scene scene,Device device,Matrix4x4 viewMat,Matrix4x4 projMat
if (scene.UseLight && scene.Light != null)
{
verA2.LightColor = GetLightColor(verA.Position, verA.Normal, scene.Light);
verB2.LightColor= GetLightColor(verA.Position, verA.Normal, scene.Light);
verC2.LightColor= GetLightColor(verA.Position, verA.Normal, scene.Light);
verB2.LightColor= GetLightColor(verA.Position, verB.Normal, scene.Light);
verC2.LightColor= GetLightColor(verA.Position, verC.Normal, scene.Light);
}

verA.ClipPosition = device.ToHomogeneous(verA.Position, viewMatrix);
Expand Down
1 change: 1 addition & 0 deletions SoftRender/SoftRender.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Binary file added SoftRender/Texture/env0.bmp
Binary file not shown.
Binary file added SoftRender/Texture/env1.bmp
Binary file not shown.
Binary file added SoftRender/Texture/env2.bmp
Binary file not shown.
Binary file added SoftRender/Texture/env3.bmp
Binary file not shown.
Binary file added SoftRender/Texture/env4.bmp
Binary file not shown.
Binary file added SoftRender/Texture/env5.bmp
Binary file not shown.
Binary file modified SoftRender/bin/Debug/SoftRender.exe
Binary file not shown.
Binary file modified SoftRender/bin/Debug/SoftRender.pdb
Binary file not shown.
Binary file modified SoftRender/obj/Debug/SoftRender.exe
Binary file not shown.
Binary file modified SoftRender/obj/Debug/SoftRender.pdb
Binary file not shown.

0 comments on commit 3ae375f

Please sign in to comment.