tglpp.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "tgl.h"
  5. extern bool InEditor;
  6. //------------------------------------------------------------------------------------------------------------
  7. // This function checks each visible triangle against the mouse position and returns if we are over ANY poly!
  8. bool TG_Shape::PerPolySelect (float mouseX, float mouseY)
  9. {
  10. if (!InEditor)
  11. {
  12. if (!listOfVertices ||
  13. !listOfColors ||
  14. !listOfShadowTVertices ||
  15. !listOfTriangles ||
  16. !listOfVisibleFaces ||
  17. !listOfVisibleShadows ||
  18. ((lastTurnTransformed != (turn-1)) /*&& (lastTurnTransformed != turn)*/))
  19. return false;
  20. }
  21. else
  22. {
  23. if (!listOfVertices ||
  24. !listOfColors ||
  25. !listOfShadowTVertices ||
  26. !listOfTriangles ||
  27. !listOfVisibleFaces ||
  28. !listOfVisibleShadows ||
  29. (/*(lastTurnTransformed != (turn-1)) &&*/ (lastTurnTransformed != turn)))
  30. return false;
  31. }
  32. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  33. for (long j=0;j<numVisibleFaces;j++)
  34. {
  35. if (listOfVisibleFaces[j] != 0xffffffff)
  36. {
  37. TG_TypeTriangle triType = theShape->listOfTypeTriangles[listOfVisibleFaces[j]];
  38. Stuff::Point3D v0,v1,v2;
  39. v0.x = listOfVertices[triType.Vertices[0]].x;
  40. v0.y = listOfVertices[triType.Vertices[0]].y;
  41. v1.x = listOfVertices[triType.Vertices[1]].x;
  42. v1.y = listOfVertices[triType.Vertices[1]].y;
  43. v2.x = listOfVertices[triType.Vertices[2]].x;
  44. v2.y = listOfVertices[triType.Vertices[2]].y;
  45. v0.z = v1.z = v2.z = 0.0f;
  46. //Using the above vertex Data, determine if the mouse is over this poly!
  47. //
  48. //Theorem:
  49. // Given the sides of the triangle defined by the lines v0v1, v1v2 and v2v0
  50. // in the form Ax + By + C = 0
  51. //
  52. // the point mousex, mousey lies inside the triangle if and only if
  53. //
  54. // A0 * mouseX + B0 * mouseY + C0 = D0
  55. // A1 * mouseX * B1 * mouseY + c1 = D1
  56. // A2 * mouseX + B2 * mouseY + c2 = D2
  57. //
  58. // Dx is the same sign for each line as the correct sign for clockwise or counterclockwise vertices!
  59. //
  60. Stuff::Vector3D line1;
  61. Stuff::Vector3D line2;
  62. line1.Subtract(v0,v1);
  63. line2.Subtract(v1,v2);
  64. float order = line2.x * line1.y - line1.x * line2.y;
  65. order = sign(order);
  66. float A0 = -(v0.y - v1.y);
  67. float B0 = (v0.x - v1.x);
  68. float C0 = -B0*(v0.y) - A0*(v0.x);
  69. float D0 = A0 * mouseX + B0 * mouseY + C0;
  70. float A1 = -(v1.y - v2.y);
  71. float B1 = (v1.x - v2.x);
  72. float C1 = -B1*(v1.y) - A1*(v1.x);
  73. float D1 = A1 * mouseX + B1 * mouseY + C1;
  74. float A2 = -(v2.y - v0.y);
  75. float B2 = (v2.x - v0.x);
  76. float C2 = -B2*(v2.y) - A2*(v2.x);
  77. float D2 = A2 * mouseX + B2 * mouseY + C2;
  78. if ((sign(D0) == order) && (sign(D0) == sign(D1)) && (sign(D0) == sign(D2)))
  79. return true;
  80. }
  81. }
  82. return false;
  83. }