-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cs
More file actions
63 lines (53 loc) · 2.17 KB
/
Copy pathTriangle.cs
File metadata and controls
63 lines (53 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using Template;
namespace INFOGRTemplate
{
internal class Triangle: Primitive
{
public Vector3 normalVector;
public Vector3[] vertices;
public Vector3 A, B, C;
public Triangle(Color3 _color, Vector3[] _vertices, Materials _material, bool _checkers) : base(_color, _vertices[0], PrimitiveTypes.Triangle, _material, _checkers)
{
vertices = _vertices;
A = vertices[0];
B = vertices[1];
C = vertices[2];
normalVector = Vector3.Normalize(Vector3.Cross(B-A, C-A)); //compute the normal vector by taking the cross product of [B-A] and [C-A]
Debug.WriteLine(normalVector);
}
public override Color3 checkerBoards(Vector3 hitPoint, Vector3 normalVector, float distance)
{
float scale = 1f;
Vector3 temp = A * normalVector;
float equationD = -(temp.X + temp.Y + temp.Z);
// Create a new point to find the plane directions.
float X = position.X + 1;
float Y = position.Y;
float Z;
if (normalVector.Z == 0)
Z = position.Z;
else
Z = -(normalVector.X * X + normalVector.Y * Y + equationD) / normalVector.Z;
Vector3 secPoint = new Vector3(X, Y, Z);
Vector3 uVector = Vector3.Normalize(secPoint - position);
Vector3 vVector = Vector3.Normalize(Vector3.Cross(uVector, normalVector));
Vector3 target = hitPoint - position;
// Obtain the U and V directions of the plane in their respective directions.
float vFac = Vector3.Dot(target, vVector);
float uFac = Vector3.Dot(target, uVector);
int U = (int)Math.Floor(uFac / scale);
int V = (int)Math.Floor(vFac / scale);
// if the Tile is even, color it black (- the given color).
bool Tile = ((U + V) % 2) == 0;
if (Tile)
return new Color3(1f, 1f, 1f) - color;
else
return color;
}
}
}