@@ -77,8 +77,77 @@ void Renderer::setExposureLevel(Number level) {
7777 exposureLevel = level;
7878}
7979
80+
81+ bool Renderer::test2DCoordinateInPolygon (Number x, Number y, Polycode::Polygon *poly, const Matrix4 &matrix, bool ortho, bool testBackfacing, bool billboardMode) {
82+
83+ Vector3 dirVec;
84+ Vector3 origin;
85+
86+ if (ortho) {
87+ origin = Vector3 (((x/(Number)xRes)*orthoSizeX) - (orthoSizeX*0.5 ), (((yRes-y)/(Number)yRes)*orthoSizeY) - (orthoSizeY*0.5 ), 0.0 );
88+ origin = cameraMatrix * origin;
89+
90+ dirVec = Vector3 (0.0 , 0.0 , -1.0 );
91+ dirVec = cameraMatrix.rotateVector (dirVec);
92+ } else {
93+ dirVec = projectRayFrom2DCoordinate (x, y);
94+ origin = cameraMatrix.getPosition ();
95+ }
96+
97+ Vector3 hitPoint;
98+
99+ Matrix4 fullMatrix = matrix;
100+
101+ if (billboardMode) {
102+ Matrix4 camInverse = cameraMatrix.inverse ();
103+ fullMatrix = fullMatrix * camInverse;
104+
105+ fullMatrix.m [0 ][0 ] = 1 ;
106+ fullMatrix.m [0 ][1 ] = 0 ;
107+ fullMatrix.m [0 ][2 ] = 0 ;
108+
109+ fullMatrix.m [1 ][0 ] = 0 ;
110+ fullMatrix.m [1 ][1 ] = 1 ;
111+ fullMatrix.m [1 ][2 ] = 0 ;
112+
113+ fullMatrix.m [2 ][0 ] = 0 ;
114+ fullMatrix.m [2 ][1 ] = 0 ;
115+ fullMatrix.m [2 ][2 ] = 1 ;
116+
117+ origin = camInverse * origin;
118+ dirVec = camInverse.rotateVector (dirVec);
119+ }
120+
121+ bool retStatus = false ;
122+
123+
124+ if (poly->getVertexCount () == 3 ) {
125+ retStatus = rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (0 )), fullMatrix * (*poly->getVertex (1 )), fullMatrix * (*poly->getVertex (2 )), &hitPoint);
126+ if (testBackfacing && !retStatus) {
127+ retStatus = rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (2 )), fullMatrix * (*poly->getVertex (1 )), fullMatrix * (*poly->getVertex (0 )), &hitPoint);
128+
129+ }
130+ } else if (poly->getVertexCount () == 4 ) {
131+ retStatus = (rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (2 )), fullMatrix * (*poly->getVertex (1 )), fullMatrix * (*poly->getVertex (0 )), &hitPoint) ||
132+ rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (0 )), fullMatrix * (*poly->getVertex (3 )), fullMatrix * (*poly->getVertex (2 )), &hitPoint));
133+ if (testBackfacing && !retStatus) {
134+ retStatus = (rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (0 )), fullMatrix * (*poly->getVertex (1 )), fullMatrix * (*poly->getVertex (2 )), &hitPoint) ||
135+ rayTriangleIntersect (origin, dirVec, fullMatrix * (*poly->getVertex (2 )), fullMatrix * (*poly->getVertex (3 )), fullMatrix * (*poly->getVertex (0 )), &hitPoint));
136+
137+ }
138+ } else {
139+ retStatus = false ;
140+ }
141+
142+ return retStatus;
143+ }
144+
80145bool Renderer::rayTriangleIntersect (Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint)
81146{
147+
148+ // printf("TESTING RAY\nORIGIN: %f,%f,%f\nDIR: %f,%f,%f\nVERT0: %f,%f,%f\nnVERT1: %f,%f,%f\nnVERT2: %f,%f,%f\n", ray_origin.x, ray_origin.y, ray_origin.z, ray_direction.x, ray_direction.y, ray_direction.z, vert0.x, vert0.y, vert0.z, vert1.x, vert1.y, vert1.z, vert2.x, vert2.y, vert2.z);
149+
150+
82151 Number t,u,v;
83152 t = 0 ; u = 0 ; v = 0 ;
84153
0 commit comments