//第一个 着色模式 第二三个 线框模式 private void DrawPrimitive(Vertex v1, Vertex v2, Vertex v3) { Vector v4 = v1.point * View; Vector v5 = v2.point * View; Vector v6 = v3.point * View; Vector v7 = v4 * Projection; Vector v8 = v5 * Projection; Vector v9 = v6 * Projection; Vector p1 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v7); Vector p2 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v8); Vector p3 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v9); //纹理渲染模式 IsShaded true Vertex v11 = new Vertex(v1); v11.point = new Vector(p1); v11.point.t = v7.t; Vertex v22 = new Vertex(v2); v22.point = new Vector(p2); v22.point.t = v8.t; Vertex v33 = new Vertex(v3); v33.point = new Vector(p3); v33.point.t = v9.t; DrawTriangle_Shaded(v11, v22, v33); }
public MyColor ReadTexture(double u, double v) { u = u * (TextureWidth - 1); v = v * (TextureHeight - 1); int x = (int)(u + 0.5); int y = (int)(v + 0.5); x = MyStaticMethod.CMID(x, 0, TextureWidth - 1); y = MyStaticMethod.CMID(y, 0, TextureHeight - 1); return(TextureImage[TextureHeight - 1 - y, x]); }
public void LoadObj(bool IsTest) { //加载Obj模型 还在改善 if (IsTest) { this.mesh = new Mesh(IsTest); } //加载测试用例cube模型 else { OBJReader o = new OBJReader(this.ObjPath); this.mesh = o.mesh; MyStaticMethod.RotateYAroundPoint(this.mesh, new Vector(0, 0, 0, 1), -1.57); } }
private void DrawPrimitive(Vector v1, Vector v2, Vector v3) { Vector v5 = v1 * View; Vector v6 = v2 * View; Vector v7 = v3 * View; Vector v9 = v5 * Projection; Vector v10 = v6 * Projection; Vector v11 = v7 * Projection; Vector p1 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v9); Vector p2 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v10); Vector p3 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v11); //三角形的线框模式 DrawTriangle_WireFrame(p1, p2, p3); }
public Color ConvertToColor() { /* * int r = (int)(this.r * 255); * int g = (int)(this.g * 255); * int b = (int)(this.b * 255); */ int r = (int)(this.r); int g = (int)(this.g); int b = (int)(this.b); r = MyStaticMethod.CMID(r, 0, 255); g = MyStaticMethod.CMID(g, 0, 255); b = MyStaticMethod.CMID(b, 0, 255); return(Color.FromArgb(r, g, b)); }
//------顶点部分--------- private void UpdateMatrix() { Vector up = new Vector(0, 1, 0, 1); //垂直向上的法向量 this.View = MyStaticMethod.GetViewMatrix(this.MyCamera.eye, this.MyCamera.lookat, up); this.Projection = MyStaticMethod.GetPerspectiveMatrix(this.MyCamera.Fovy, this.MyCamera.Aspect, 1, 100); //this.Transform = View * Projection; //顺序不能乱 如果先去除隐藏面,然后根据显示的面来计算顶点法向量,这样有一些点的法向量会缺失,一些点法向量不完整 //比如0点 因为隐藏面全消除的关系,法向量为0,而1点的法向量,也因为只添加上1357面的法向量的缘故,法向量为1 0 0 应该是0.33 -0.33 -0.33 //所以必须先正确计算出顶点的法向量 再消除隐藏面 MyStaticMethod.GetNormalFromFaces(mesh); //获得每一个面的法向量 //MyStaticMethod.HidenFacesDemolish(mesh, at - eye); 移动至最后一行 MyStaticMethod.GetVerticesNormal(mesh); //每一个面法向量添加到相应的顶点 并归一化 MyStaticMethod.HidenFacesDemolish(mesh, this.MyCamera.lookat - this.MyCamera.eye); MyStaticMethod.PolygonToTriangle(mesh); //把面分割成两个三角形并且赋值相应的法向量 }
private void DrawPrimitive(Vector v1, Vector v2, Vector v3, Vector v4) { Vector v5 = v1 * View; Vector v6 = v2 * View; Vector v7 = v3 * View; Vector v8 = v4 * View; Vector v9 = v5 * Projection; Vector v10 = v6 * Projection; Vector v11 = v7 * Projection; Vector v12 = v8 * Projection; Vector p1 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v9); Vector p2 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v10); Vector p3 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v11); Vector p4 = MyStaticMethod.TransformHomogenize(this.BitMapWidth, this.BitMapHeight, v12); //四边形的线框模式 DrawRect_WireFrame(p1, p2, p3, p4); }
/* * private void Button_ReadObj_Click(object sender, EventArgs e) * { * OpenFileDialog o = new OpenFileDialog(); * o.ShowDialog(); * if (o.FileName != null) * { * this.MyDevice.ObjPath = o.FileName; * this.MyDevice.LoadObj(); * this.pictureBox1.Image = this.MyDevice.GetBigMap_UpdateMatNBitmap(); * } * } */ private void Button_CameraMove_Click(object sender, EventArgs e) { MyStaticMethod.RotateYAroundPoint(this.MyDevice.mesh, new Vector(0, 0, 0, 1), 0.1); this.pictureBox1.Image = this.MyDevice.GetBigMap_UpdateMatNBitmap(); }
public static void InitializedRenderTriangle(Vertex v1, Vertex v2, Vertex v3, out List <RenderTriangle> list) { //Y轴,v3 v2 v1,所以当v1=v2的时候,只有一个三角形,底边在下方。v2=v3时,底边在上. list = new List <RenderTriangle>(2); if (v1.point.y > v2.point.y) { MyStaticMethod.SwapVertex(ref v1, ref v2); } if (v1.point.y > v3.point.y) { MyStaticMethod.SwapVertex(ref v1, ref v3); } if (v2.point.y > v3.point.y) { MyStaticMethod.SwapVertex(ref v2, ref v3); } if (v1.point.y == v2.point.y && v1.point.y == v3.point.y) { return; } if (v1.point.x == v2.point.x && v1.point.x == v3.point.x) { return; } //三角形尖角向下 if (v1.point.y == v2.point.y) { if (v1.point.x > v2.point.x) { MyStaticMethod.SwapVertex(ref v1, ref v2); } RenderTriangle n1 = new RenderTriangle(v1, v3, v2, v3, v1.point.y, v3.point.y); list.Add(n1); return; } //三角形尖角向上 if (v2.point.y == v3.point.y) { if (v2.point.x > v3.point.x) { MyStaticMethod.SwapVertex(ref v2, ref v3); } list.Add(new RenderTriangle(v1, v2, v1, v3, v1.point.y, v2.point.y)); return; } //剩下的情况必定是在Y轴上 v3 > v2 > v1 注意d3d的规则,图像上面的y轴比较小,从上向下排列是v1 2 3 double k = (v3.point.y - v1.point.y) / (v2.point.y - v1.point.y); double x = v1.point.x + (v2.point.x - v1.point.x) * k; if (x <= v3.point.x) //计算其延长线,如果顶点v3在延长线左边,则v1 连上v3为最长边,如果在右边。。。 { list.Add(new RenderTriangle(v1, v2, v1, v3, v1.point.y, v2.point.y)); list.Add(new RenderTriangle(v2, v3, v1, v3, v2.point.y, v3.point.y)); } else { list.Add(new RenderTriangle(v1, v3, v1, v2, v1.point.y, v2.point.y)); list.Add(new RenderTriangle(v1, v3, v2, v3, v2.point.y, v3.point.y)); } }
/// <summary> /// FragmentShader是结构体 值类型,MyColor是类类型,引用类型 /// </summary> /// <param name="FS"></param> /// <param name="mycolor"></param> /// <returns></returns> public static MyColor LightMode_BlinnPhong(a2v FS, MyColor mycolor, bool IsTexture, bool IsPointLighting, Light PointLight) { //----------------------所有颜色统一在0-1内计算----------------返回mycolor时,在回到0-255 //材质 光照 暂时写在这边 完成光照模型后,写在外部 //MyColor Ambient = new MyColor(0, 0, 0); //环境光 可以不和下面两个一样 MyColor Ambient = new MyColor(0.3, 0.3, 0.3); //MyColor Diffuse = new MyColor(0, 0, 0); MyColor Diffuse = new MyColor(0.3, 0.3, 0.3); //MyColor Specular = new MyColor(0, 0, 0); MyColor Specular = new MyColor(0.8, 0.8, 0.8); Material material = new Material(Ambient, Diffuse, Specular); //PointLight = new Light(new Vector(5, 10, 0, 1), new MyColor(1, 1, 1)); //读取纹理的颜色 先假设为mycolor MyColor texture = new MyColor(); if (IsTexture) { texture = new MyColor(mycolor).Scale(1.0 / 255); } else { texture.r = 0.5; texture.g = 0.5; texture.b = 0.5; } if (IsPointLighting) { Vector normal = new Vector(FS.WorldNormal); Vector ViewDir = FS.eye - FS.WorldPos; ViewDir.NormalizedVector(); Vector LightDir = PointLight.LightPos - FS.WorldPos; double distance = LightDir.Length();//光源与渲染点的距离 LightDir.NormalizedVector(); //----衰减计算---- double constant = 1; double linear = 0.0009; double quadratic = 0.000032; double num = constant + linear * distance + quadratic * (distance * distance); double attenuation = 0; if (num != 0) { attenuation = 1.0 / num; } attenuation = MyStaticMethod.MaxNumber(attenuation, 0); MyColor LightAmbient = texture * ((PointLight.LightColor + EnvironmentLight) * material.Ambient); //漫反射系数计算 double diff = MyStaticMethod.MaxNumber(Vector.DotMultiply(normal, LightDir), 0); MyColor LightDiffuse = texture * (PointLight.LightColor * material.Diffuse) * diff; //反射计算 blinn-phong Vector H = (LightDir + ViewDir).NormalizedVector(); double shininess = 256;//镜面反射系数 double reflect = Math.Pow(MyStaticMethod.MaxNumber(Vector.DotMultiply(H, normal), 0), shininess); MyColor LightSpecular = texture * PointLight.LightColor * material.Specular * reflect; //MyColor ret = LightAmbient + (LightDiffuse + LightSpecular); MyColor ret = LightAmbient + (LightDiffuse + LightSpecular) * attenuation; ret.Scale(255.0); return(ret); } else { MyColor ret = texture.Scale(255.0); return(ret); } }