Ejemplo n.º 1
0
Intersection Mesh::GetIntersection(Ray r){
    Ray r_loc = r.GetTransformedCopy(transform.invT());
    Intersection closest;
    for(int i = 0; i < faces.size(); i++){
        Intersection isx = faces[i]->GetIntersection(r_loc);
        if(isx.object_hit != NULL && isx.t > 0 && (isx.t < closest.t || closest.t < 0)){
            closest = isx;
        }
    }
    if(closest.object_hit != NULL)
    {
        Triangle* tri = (Triangle*)closest.object_hit;
        glm::vec4 P = glm::vec4(closest.t * r_loc.direction + r_loc.origin, 1);
        closest.point = glm::vec3(transform.T() * P);
        closest.normal = glm::normalize(glm::vec3(transform.invTransT() * tri->GetNormal(P)));
        closest.object_hit = this;
        closest.t = glm::distance(closest.point, r.origin);//The t used for the closest triangle test was in object space
        //TODO: Store the tangent and bitangent
    }
    return closest;
}
void Mesh::CalculateVertexNormals()
{
	// Clear all vertex normals
	for( unsigned int i = 0; i < this->_vertices.size(); ++i )
	{
		Vertex* vertex = this->_vertices.at(i);
		if( vertex )
		{
			vertex->Normal() = 0.0f;
		}
	}

	// Sum all adjacent triangle normals
	for( unsigned int i = 0; i < this->_triangles.size(); ++i )
	{
		Triangle* triangle = this->_triangles.at(i);
		if( triangle )
		{
			if( triangle->IsValid() )
			{
				for( unsigned int j = 0; j < 3; ++j )
				{
					Vertex* vertex = triangle->GetVertex(j);
					vertex->Normal() += triangle->GetNormal();
				}
			}
		}
	}

	// Normalize all vertex normals
	for( unsigned int i = 0; i < this->_vertices.size(); ++i )
	{
		Vertex* vertex = this->_vertices.at(i);
		if( vertex )
		{
			vertex->Normal().Normalize();
		}
	}
}