bool Triangle::intersect( const Ray& r , Hit& h , float tmin){ Vector3f R_o = r.getOrigin(); Vector3f R_d = r.getDirection(); Matrix3f A( this->a.x()-this->b.x() , this->a.x()-this->c.x() , R_d.x() , this->a.y()-this->b.y() , this->a.y()-this->c.y() , R_d.y() , this->a.z()-this->b.z() , this->a.z()-this->c.z() , R_d.z() ); Matrix3f BetaM( this->a.x()-R_o.x() , this->a.x()-this->c.x() , R_d.x() , this->a.y()-R_o.y() , this->a.y()-this->c.y() , R_d.y() , this->a.z()-R_o.z() , this->a.z()-this->c.z() , R_d.z() ); float beta = BetaM.determinant() / A.determinant(); Matrix3f GammaM( this->a.x()-this->b.x() , this->a.x()-R_o.x() , R_d.x() , this->a.y()-this->b.y() , this->a.y()-R_o.y() , R_d.y() , this->a.z()-this->b.z() , this->a.z()-R_o.z() , R_d.z() ); float gamma = GammaM.determinant() / A.determinant(); float alpha = 1.0f - beta - gamma; Matrix3f tM( this->a.x()-this->b.x() , this->a.x()-this->c.x() , this->a.x()-R_o.x() , this->a.y()-this->b.y() , this->a.y()-this->c.y() , this->a.y()-R_o.y() , this->a.z()-this->b.z() , this->a.z()-this->c.z() , this->a.z()-R_o.z() ); float t = tM.determinant() / A.determinant(); if (beta + gamma > 1){ return false; } if (beta < 0){ return false; } if (gamma < 0){ return false; } if (t > tmin && t < h.getT()){ Vector3f newNormal = (alpha*this->normals[0] + beta*this->normals[1] + gamma*this->normals[2]).normalized(); h.set(t, this->material, newNormal); Vector2f newTexCoord = (alpha*this->texCoords[0] + beta*this->texCoords[1] + gamma*this->texCoords[2]); h.setTexCoord(newTexCoord); return true; } else{ return false; } }
bool Triangle::intersect(const Ray& ray, Hit& hit, float tmin) { Vector3f direction = ray.getDirection(); Vector3f origin = ray.getOrigin(); float list[9] = { (a.x()) - (b.x()),(a.y()) - (b.y()),(a.z()) - (b.z()), (a.x()) - (c.x()),(a.y()) - (c.y()),(a.z()) - (c.z()), (a[0]) - (origin[0]),(a[1]) - (origin[1]),(a[2]) - (origin[2]), }; Matrix3f A = Matrix3f(list[0], list[3], direction[0], list[1], list[4], direction[1], list[2], list[5], direction[2]); float determinant = A.determinant(); if (determinant == 0) { return false; } float t = Matrix3f::determinant3x3(list[0], list[3], list[6], list[1], list[4], list[7], list[2], list[5], list[8]) / determinant; if (t <= tmin || t >= hit.getT()) { return false; } float beta = Matrix3f::determinant3x3(list[6], list[3], direction[0], list[7], list[4], direction[1], list[8], list[5], direction[2]) / determinant; float gamma = Matrix3f::determinant3x3(list[0], list[6], direction[0], list[1], list[7], direction[1], list[2], list[8], direction[2]) / determinant; if ((beta >= 0 && gamma >= 0 && beta + gamma <= 1) ) { if(hasTex) { hit.hasTex = true; hit.setTexCoord((1 - beta - gamma)*texCoords[0] + beta*texCoords[1] + gamma*texCoords[2]); } hit.set(t, this->material, ((1-beta-gamma)*normals[0]+beta*normals[1]+gamma*normals[2]).normalized()); return true; } return false; }