public void Normalise(_3Dpoint v) { double length; length = Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z); v.x /= length; v.y /= length; v.z /= length; }
public _3Dpoint CrossProduct(_3Dpoint p1, _3Dpoint p2) { _3Dpoint p3; p3 = new _3Dpoint(0, 0, 0); p3.x = p1.y * p2.z - p1.z * p2.y; p3.y = p1.z * p2.x - p1.x * p2.z; p3.z = p1.x * p2.y - p1.y * p2.x; return(p3); }
public bool Trans_ClipEye(_3Dpoint e1, _3Dpoint e2) { double mu; /* Is the vector totally in front of the front cutting plane ? */ if (e1.y <= camera.front && e2.y <= camera.front) { return(false); } /* Is the vector totally behind the back cutting plane ? */ if (e1.y >= camera.back && e2.y >= camera.back) { return(false); } /* Is the vector partly in front of the front cutting plane ? */ if ((e1.y < camera.front && e2.y > camera.front) || (e1.y > camera.front && e2.y < camera.front)) { mu = (camera.front - e1.y) / (e2.y - e1.y); if (e1.y < camera.front) { e1.x = e1.x + mu * (e2.x - e1.x); e1.z = e1.z + mu * (e2.z - e1.z); e1.y = camera.front; } else { e2.x = e1.x + mu * (e2.x - e1.x); e2.z = e1.z + mu * (e2.z - e1.z); e2.y = camera.front; } } /* Is the vector partly behind the back cutting plane ? */ if ((e1.y < camera.back && e2.y > camera.back) || (e1.y > camera.back && e2.y < camera.back)) { mu = (camera.back - e1.y) / (e2.y - e1.y); if (e1.y < camera.back) { e2.x = e1.x + mu * (e2.x - e1.x); e2.z = e1.z + mu * (e2.z - e1.z); e2.y = camera.back; } else { e1.x = e1.x + mu * (e2.x - e1.x); e1.z = e1.z + mu * (e2.z - e1.z); e1.y = camera.back; } } return(true); }
public bool Trans_Initialise() { /* Is the camera position and view vector coincident ? */ if (EqualVertex(camera.to, camera.from)) { return(false); } /* Is there a legal camera up vector ? */ if (EqualVertex(camera.up, origin)) { return(false); } basisb.x = camera.to.x - camera.from.x; basisb.y = camera.to.y - camera.from.y; basisb.z = camera.to.z - camera.from.z; Normalise(basisb); basisa = CrossProduct(camera.up, basisb); Normalise(basisa); /* Are the up vector and view direction colinear */ if (EqualVertex(basisa, origin)) { return(false); } basisc = CrossProduct(basisb, basisa); /* Do we have legal camera apertures ? */ if (camera.angleh < EPSILON || camera.anglev < EPSILON) { return(false); } /* Calculate camera aperture statics, note: angles in degrees */ tanthetah = Math.Tan(camera.angleh * DTOR / 2); tanthetav = Math.Tan(camera.anglev * DTOR / 2); /* Do we have a legal camera zoom ? */ if (camera.zoom < EPSILON) { return(false); } /* Are the clipping planes legal ? */ if (camera.front < 0 || camera.back < 0 || camera.back <= camera.front) { return(false); } return(true); }
public Coefficient_Of_Plane Coefficient_Value(_3Dpoint pt1, _3Dpoint pt2, _3Dpoint pt3) { // double Y = 0; Coefficient_Of_Plane pt; pt = new Coefficient_Of_Plane(); pt.A = (pt2.z - pt3.z) * (pt1.y - pt2.y) - (pt1.z - pt2.z) * (pt2.y - pt3.y); pt.B = (pt2.x - pt3.x) * (pt1.z - pt2.z) - (pt1.x - pt2.x) * (pt2.z - pt3.z); pt.C = (pt2.y - pt3.y) * (pt1.x - pt2.x) - (pt1.y - pt2.y) * (pt2.x - pt3.x); pt.D = -pt1.x * (pt2.y * pt3.z - pt2.z * pt3.y) + pt1.y * (pt2.x * pt3.z - pt2.z * pt3.x) - pt1.z * (pt2.x * pt3.y - pt2.y * pt3.x); return(pt); }
public Camera() { from = new _3Dpoint(0, -50, 0); to = new _3Dpoint(0, 50, 0); up = new _3Dpoint(0, 0, 1); angleh = 45.0; anglev = 45.0; zoom = 1.0; front = 1.0; back = 200.0; projection = 0; }
public void Trans_World2Eye(_3Dpoint w, _3Dpoint e) { /* Translate world so that the camera is at the origin */ w.x -= camera.from.x; w.y -= camera.from.y; w.z -= camera.from.z; /* Convert to eye coordinates using basis vectors */ e.x = w.x * basisa.x + w.y * basisa.y + w.z * basisa.z; e.y = w.x * basisb.x + w.y * basisb.y + w.z * basisb.z; e.z = w.x * basisc.x + w.y * basisc.y + w.z * basisc.z; }
public bool Trans_Point(_3Dpoint w1) { Trans_World2Eye(w1, e1); if (e1.y >= camera.front && e1.y <= camera.back) { Trans_Eye2Norm(e1, n1); if (n1.x >= -1 && n1.x <= 1 && n1.z >= -1 && n1.z <= 1) { Trans_Norm2Screen(n1, p1); return(true); } } return(false); }
public bool EqualVertex(_3Dpoint p1, _3Dpoint p2) { if (Math.Abs(p1.x - p2.x) > EPSILON) { return(false); } if (Math.Abs(p1.y - p2.y) > EPSILON) { return(false); } if (Math.Abs(p1.z - p2.z) > EPSILON) { return(false); } return(true); }
//public bool Trans_Point(); public bool Trans_Line(_3Dpoint w1, _3Dpoint w2) { Trans_World2Eye(w1, e1); Trans_World2Eye(w2, e2); if (Trans_ClipEye(e1, e2)) { Trans_Eye2Norm(e1, n1); Trans_Eye2Norm(e2, n2); if (Trans_ClipNorm(n1, n2)) { Trans_Norm2Screen(n1, p1); Trans_Norm2Screen(n2, p2); return(true); } } return(true); }
public void Trans_Eye2Norm(_3Dpoint e, _3Dpoint n) { double d; if (camera.projection == 0) { d = camera.zoom / e.y; n.x = d * e.x / tanthetah; n.y = e.y; n.z = d * e.z / tanthetav; } else { n.x = camera.zoom * e.x / tanthetah; n.y = e.y; n.z = camera.zoom * e.z / tanthetav; } }
public Form1() { InitializeComponent(); CenterToScreen(); SetStyle(ControlStyles.ResizeRedraw, true); Dpoint[0] = new _3Dpoint(neg, near, pov); Dpoint[1] = new _3Dpoint(pov, near, pov); Dpoint[2] = new _3Dpoint(pov, near, neg); Dpoint[3] = new _3Dpoint(neg, near, neg); Dpoint[4] = new _3Dpoint(neg + 20, far, pov + 20); Dpoint[5] = new _3Dpoint(pov + 20, far, pov + 20); Dpoint[6] = new _3Dpoint(pov + 20, far, neg + 20); Dpoint[7] = new _3Dpoint(neg + 20, far, neg + 20); depthbuffer = new DepthBuffer(); proc = new Projection(); }
public Projection() { EPSILON = 0.001; DTOR = 0.01745329252; camera = new Camera(); screen = new Screen(); origin = new _3Dpoint(); basisa = new _3Dpoint(); basisb = new _3Dpoint(); basisc = new _3Dpoint(); p1 = new _2Dpoint(); p2 = new _2Dpoint(); e1 = new _3Dpoint(); e2 = new _3Dpoint(); n1 = new _3Dpoint(); n2 = new _3Dpoint(); if (Trans_Initialise() != true) { MessageBox.Show("Error in initializing variable"); } }
public bool Trans_ClipNorm(_3Dpoint n1, _3Dpoint n2) { double mu; /* Is the line segment totally right of x = 1 ? */ if (n1.x >= 1 && n2.x >= 1) { return(false); } /* Is the line segment totally left of x = -1 ? */ if (n1.x <= -1 && n2.x <= -1) { return(false); } /* Does the vector cross x = 1 ? */ if ((n1.x > 1 && n2.x < 1) || (n1.x < 1 && n2.x > 1)) { mu = (1 - n1.x) / (n2.x - n1.x); if (n1.x < 1) { n2.z = n1.z + mu * (n2.z - n1.z); n2.x = 1; } else { n1.z = n1.z + mu * (n2.z - n1.z); n1.x = 1; } } /* Does the vector cross x = -1 ? */ if ((n1.x < -1 && n2.x > -1) || (n1.x > -1 && n2.x < -1)) { mu = (-1 - n1.x) / (n2.x - n1.x); if (n1.x > -1) { n2.z = n1.z + mu * (n2.z - n1.z); n2.x = -1; } else { n1.z = n1.z + mu * (n2.z - n1.z); n1.x = -1; } } /* Is the line segment totally above z = 1 ? */ if (n1.z >= 1 && n2.z >= 1) { return(false); } /* Is the line segment totally below z = -1 ? */ if (n1.z <= -1 && n2.z <= -1) { return(false); } /* Does the vector cross z = 1 ? */ if ((n1.z > 1 && n2.z < 1) || (n1.z < 1 && n2.z > 1)) { mu = (1 - n1.z) / (n2.z - n1.z); if (n1.z < 1) { n2.x = n1.x + mu * (n2.x - n1.x); n2.z = 1; } else { n1.x = n1.x + mu * (n2.x - n1.x); n1.z = 1; } } /* Does the vector cross z = -1 ? */ if ((n1.z < -1 && n2.z > -1) || (n1.z > -1 && n2.z < -1)) { mu = (-1 - n1.z) / (n2.z - n1.z); if (n1.z > -1) { n2.x = n1.x + mu * (n2.x - n1.x); n2.z = -1; } else { n1.x = n1.x + mu * (n2.x - n1.x); n1.z = -1; } } return(true); }
private void DrawCube() { ////front color //color[0] = Color.Blue; //color[1] = Color.Blue; ////left color //color[2] = Color.Red; //color[3] = Color.Red; ////right color //color[4] = Color.Green; //color[5] = Color.Green; ////top color //color[6] = Color.Orange; //color[7] = Color.Orange; ////bottom color //color[8] = Color.Yellow; //color[9] = Color.Yellow; ////back color //color[10] = Color.White; //color[11] = Color.White; for (int i = 0; i < 8; i++) { temp = new _3Dpoint(Dpoint[i]); if (proc.Trans_Point(temp)) { _2dpoint[i, 0] = proc.p1.h; _2dpoint[i, 1] = proc.p1.v; } else { MessageBox.Show("conversion is invalid"); break; } } Point point1 = new Point(_2dpoint[0, 0], _2dpoint[0, 1]); Point point2 = new Point(_2dpoint[1, 0], _2dpoint[1, 1]); Point point3 = new Point(_2dpoint[2, 0], _2dpoint[2, 1]); Point point4 = new Point(_2dpoint[3, 0], _2dpoint[3, 1]); Point point5 = new Point(_2dpoint[4, 0], _2dpoint[4, 1]); Point point6 = new Point(_2dpoint[5, 0], _2dpoint[5, 1]); Point point7 = new Point(_2dpoint[6, 0], _2dpoint[6, 1]); Point point8 = new Point(_2dpoint[7, 0], _2dpoint[7, 1]); //front surface dividing into two triangle surface[0] = depthbuffer.Coefficient_Value(Dpoint[0], Dpoint[1], Dpoint[2]); color[0] = Color.Blue; surface[1] = depthbuffer.Coefficient_Value(Dpoint[2], Dpoint[3], Dpoint[0]); color[1] = Color.Blue; //left surface dividing into two triangle surface[2] = depthbuffer.Coefficient_Value(Dpoint[0], Dpoint[4], Dpoint[7]); color[2] = Color.Red; surface[3] = depthbuffer.Coefficient_Value(Dpoint[7], Dpoint[3], Dpoint[0]); color[3] = Color.Red; //right surface dividing into two triangle surface[4] = depthbuffer.Coefficient_Value(Dpoint[1], Dpoint[5], Dpoint[6]); color[4] = Color.Green; surface[5] = depthbuffer.Coefficient_Value(Dpoint[6], Dpoint[2], Dpoint[1]); color[5] = Color.Green; //top surface dividing into two triangle surface[6] = depthbuffer.Coefficient_Value(Dpoint[0], Dpoint[1], Dpoint[5]); color[6] = Color.Orange; surface[7] = depthbuffer.Coefficient_Value(Dpoint[5], Dpoint[4], Dpoint[0]); color[7] = Color.Orange; //bottom surface dividing into two triangle surface[8] = depthbuffer.Coefficient_Value(Dpoint[3], Dpoint[2], Dpoint[6]); color[8] = Color.Yellow; surface[9] = depthbuffer.Coefficient_Value(Dpoint[6], Dpoint[7], Dpoint[3]); color[9] = Color.Yellow; //back surface dividing into two triangle surface[10] = depthbuffer.Coefficient_Value(Dpoint[4], Dpoint[5], Dpoint[6]); color[10] = Color.White; surface[11] = depthbuffer.Coefficient_Value(Dpoint[6], Dpoint[7], Dpoint[4]); color[11] = Color.White; m_Canvas = new Bitmap(1440, 920); // Doesn't have to be initialized here for (int x = 320; x < 1120; x++) { for (int y = 20; y < 820; y++) { for (int numsurf = 0; numsurf < 12; numsurf++) { depth[numsurf] = 200; Finalcolor = Color.Gray; } //front if (depthbuffer.inside_triangle_check(point1, point2, point3, x, y)) { depth[0] = depthbuffer.DepthValue(surface[0], 0, 0); } if (depthbuffer.inside_triangle_check(point3, point4, point1, x, y)) { depth[1] = depthbuffer.DepthValue(surface[1], 0, 0); } //left if (depthbuffer.inside_triangle_check(point1, point5, point8, x, y)) { depth[2] = depthbuffer.DepthValue(surface[2], 0, 0); } if (depthbuffer.inside_triangle_check(point8, point4, point1, x, y)) { depth[3] = depthbuffer.DepthValue(surface[3], 0, 0); } //right if (depthbuffer.inside_triangle_check(point2, point6, point7, x, y)) { depth[4] = depthbuffer.DepthValue(surface[4], 0, 0); } if (depthbuffer.inside_triangle_check(point7, point3, point2, x, y)) { depth[5] = depthbuffer.DepthValue(surface[5], 0, 0); } //top if (depthbuffer.inside_triangle_check(point1, point2, point6, x, y)) { depth[6] = depthbuffer.DepthValue(surface[6], 0, 0); } if (depthbuffer.inside_triangle_check(point6, point5, point1, x, y)) { depth[7] = depthbuffer.DepthValue(surface[7], 0, 0); } //bottom if (depthbuffer.inside_triangle_check(point4, point3, point7, x, y)) { depth[8] = depthbuffer.DepthValue(surface[8], 0, 0); } if (depthbuffer.inside_triangle_check(point7, point8, point4, x, y)) { depth[9] = depthbuffer.DepthValue(surface[9], 0, 0); } //back if (depthbuffer.inside_triangle_check(point5, point6, point7, x, y)) { depth[10] = depthbuffer.DepthValue(surface[10], 0, 0); } if (depthbuffer.inside_triangle_check(point7, point8, point5, x, y)) { depth[11] = depthbuffer.DepthValue(surface[11], 0, 0); } z_buffer = 200; Finalcolor = Color.Gray; for (int numsurf = 0; numsurf < 12; numsurf++) { if (depth[numsurf] < z_buffer) { z_buffer = depth[numsurf]; Finalcolor = color[numsurf]; } } m_Canvas.SetPixel(x, y, Finalcolor); } } SetCanvasAsImage(); }
public void Trans_Norm2Screen(_3Dpoint norm, _2Dpoint projected) { //MessageBox.Show("the value of are"); projected.h = Convert.ToInt32(screen.center.h - screen.size.h * norm.x / 2); projected.v = Convert.ToInt32(screen.center.v - screen.size.v * norm.z / 2); }
public _3Dpoint(_3Dpoint Object) { this.x = Object.x; this.y = Object.y; this.z = Object.z; }
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { double neg = -20; double pov = 20; double near = 100; double far = 140; Projection proc; proc = new Projection(); //create a graphics object from the form Graphics g = this.CreateGraphics(); // Create font and brush. Pen blackPen = new Pen(Color.Black); Font drawFont = new Font("Arial", 16); Pen p = new Pen(Color.Red, 1); _3Dpoint Dpoint1, Dpoint2, Dpoint3, Dpoint4, Dpoint5, Dpoint6, Dpoint7, Dpoint8; //draw front Dpoint1 = new _3Dpoint(neg, near, pov); Dpoint2 = new _3Dpoint(pov, near, pov); Dpoint3 = new _3Dpoint(pov, near, pov); Dpoint4 = new _3Dpoint(pov, near, neg); Dpoint5 = new _3Dpoint(pov, near, neg); Dpoint6 = new _3Dpoint(neg, near, neg); Dpoint7 = new _3Dpoint(neg, near, neg); Dpoint8 = new _3Dpoint(neg, near, pov); proc.Trans_Line(Dpoint1, Dpoint2); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint3, Dpoint4); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint5, Dpoint6); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint7, Dpoint8); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); //draw back Dpoint1 = new _3Dpoint(neg + 20, far, pov + 20); Dpoint2 = new _3Dpoint(pov + 20, far, pov + 20); Dpoint3 = new _3Dpoint(pov + 20, far, pov + 20); Dpoint4 = new _3Dpoint(pov + 20, far, neg + 20); Dpoint5 = new _3Dpoint(pov + 20, far, neg + 20); Dpoint6 = new _3Dpoint(neg + 20, far, neg + 20); Dpoint7 = new _3Dpoint(neg + 20, far, neg + 20); Dpoint8 = new _3Dpoint(neg + 20, far, pov + 20); proc.Trans_Line(Dpoint1, Dpoint2); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint3, Dpoint4); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint5, Dpoint6); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint7, Dpoint8); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); //draw left side Dpoint1 = new _3Dpoint(neg, near, pov); Dpoint2 = new _3Dpoint(neg + 20, far, pov + 20); Dpoint3 = new _3Dpoint(neg + 20, far, pov + 20); Dpoint4 = new _3Dpoint(neg + 20, far, -pov + 20); Dpoint5 = new _3Dpoint(neg + 20, far, -pov + 20); Dpoint6 = new _3Dpoint(neg + 20, near, -pov + 20); Dpoint7 = new _3Dpoint(neg + 20, near, -pov + 20); Dpoint8 = new _3Dpoint(neg, near, -pov); proc.Trans_Line(Dpoint1, Dpoint2); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint3, Dpoint4); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint5, Dpoint6); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint7, Dpoint8); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); //draw right side Dpoint1 = new _3Dpoint(pov, near, pov); Dpoint2 = new _3Dpoint(pov + 20, far, pov + 20); Dpoint3 = new _3Dpoint(pov + 20, far, pov + 20); Dpoint4 = new _3Dpoint(pov + 20, far, -pov + 20); Dpoint5 = new _3Dpoint(pov + 20, far, -pov + 20); Dpoint6 = new _3Dpoint(pov, near, -pov); Dpoint7 = new _3Dpoint(pov, near, -pov); Dpoint8 = new _3Dpoint(pov, near, -pov); proc.Trans_Line(Dpoint1, Dpoint2); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint3, Dpoint4); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint5, Dpoint6); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); proc.Trans_Line(Dpoint7, Dpoint8); g.DrawLine(p, proc.p1.h, proc.p1.v, proc.p2.h, proc.p2.v); }