Before anyone read this - just be sure to note that I'm using DX11 and never will use DX10 or 9. So, D3DXIntersect is not an option. Another note is that I'm very poor in math.
Anyways; In the editor I noticed when I move the mouse over the mesh the cursor sets to cross. This is good, however - calculation is totally wrong and off path. I believe I successfully Unprojected the 2D Mouse Coordiants to 3D World Space. This is what I did:
I stored the vectors into float3 because I can actually see what is going on. Onward the managed side, if the mouse moves inside the DX Window inside the editor; it goes back and calculates the intesection of Mouse point and Mesh Verticies.
I am having difficult time with finding p0, p1, p2 then finding center point of the triangle.
I updated the Computing function to return the distance. Also, I updated when the mesh loads it loads the vertexes into TRI1, TRI2, TRI3 which would make up a triangle.
Now, inside the computing function where it calculates the position of the mouse point in 3D Space compared to the Triangle's Center - I need to know how to calculate either the distance or flag it true that the mouse is inside the mesh triangle. Is it best to calculate the distance between the center of the triangle or what?
This is a ray-triangle intersection code of mine in Python, from my raytracer. It will return the distance along the ray where the intersection occurs, or will return -1 if the ray doesn't intersect the triangle. For triangle picking, the ray should be the projected camera ray from the cursor's position, with the correct field of view. Note the precomputation steps with the three triangle vertices. Perhaps you can adapt it to your problem, I'm not quite sure what issue you are having - just throwing it out there, in case.
[source lang="python"]""" This is a triangle primitive. """
class Triangle(Primitive):
""" Creates a triangle, defined by three vertices. """
def __init__(self, material, v1, v2, v3):
super().__init__(material)
# Precompute the edges and normal
self.vertex = [v1, v2, v3]
self.edge = [v2 - v1, v3 - v1]
self.normal = normalize(cross(self.edge[0], self.edge[1]))
""" Intersects a ray with this triangle. """
def Intersect(self, ray):
# Compute some initial values
distance = ray.origin - self.vertex[0]
d = 1 / dot(s, self.edge[0])
# Calculate the first barycentric coordinate
s = cross(ray.direction, self.edge[1])
u = dot(distance, s) * d
# Reject the intersection if the barycentric coordinate is out of range.
if (u < -1e-6) or (u > 1.0 + 1e-6):
return -1
# Calculate the second barycentric coordinate.
s = cross(distance, self.edge[0])
v = dot(ray.direction, s) * d
# Reject the intersection if the barycentric coordinate is out of range.
if (v < -1e-6) or (u + v > 1.0 + 1e-6):
return -1
# Compute the final intersection point.
return dot(self.edge[1], s) * d
""" Returns the triangle's surface normal at a point. """
def Normal(self, point):
return self.normal
[/source]
This might be overkill for your needs, but at least it'll give you correct triangle picking if it's done properly.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
No, the edges are just v2 - v1 and v3 - v1. The normal is calculated by taking the cross product of these two edges, and normalizing that. You can ignore the normal's calculation if you are just doing intersection tests since you don't need it for that, I just added for completeness so I could copy-paste my whole class. The interesting part is in the Intersect() method.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Just a question - if the Camera is positioned at (0,5,-25) and Looked At (0,5,0). Is this normal for a unprojected mouse coords to be (0.098,0.00973,0.089) for instance. I have the Debug::WriteLine printing out the values of MouseRayOrigion and MouseRayDirection.