ShadowCameraTransform DirectionalLight::CalcShadowCameraTransform(const Vector3f& mainCameraPos, const Quaternion& mainCameraRot) const { Vector3f resultPos = mainCameraPos + mainCameraRot.GetForward() * GetHalfShadowArea(); Quaternion resultRot = GetTransform().GetTransformedRot(); float worldTexelSize = (GetHalfShadowArea()*2)/((float)(1 << GetShadowInfo().GetShadowMapSizeAsPowerOf2())); Vector3f lightSpaceCameraPos = resultPos.Rotate(resultRot.Conjugate()); lightSpaceCameraPos.SetX(worldTexelSize * floor(lightSpaceCameraPos.GetX() / worldTexelSize)); lightSpaceCameraPos.SetY(worldTexelSize * floor(lightSpaceCameraPos.GetY() / worldTexelSize)); resultPos = lightSpaceCameraPos.Rotate(resultRot); return ShadowCameraTransform(resultPos, resultRot); }
void IndexedModel::CalcTangents() { m_tangents.clear(); m_tangents.reserve(m_positions.size()); for(unsigned int i = 0; i < m_positions.size(); i++) m_tangents.push_back(Vector3f(0,0,0)); for(unsigned int i = 0; i < m_indices.size(); i += 3) { int i0 = m_indices[i]; int i1 = m_indices[i + 1]; int i2 = m_indices[i + 2]; Vector3f edge1 = m_positions[i1] - m_positions[i0]; Vector3f edge2 = m_positions[i2] - m_positions[i0]; float deltaU1 = m_texCoords[i1].GetX() - m_texCoords[i0].GetX(); float deltaU2 = m_texCoords[i2].GetX() - m_texCoords[i0].GetX(); float deltaV1 = m_texCoords[i1].GetY() - m_texCoords[i0].GetY(); float deltaV2 = m_texCoords[i2].GetY() - m_texCoords[i0].GetY(); float dividend = (deltaU1 * deltaV2 - deltaU2 * deltaV1); float f = dividend == 0.0f ? 0.0f : 1.0f/dividend; Vector3f tangent = Vector3f(0,0,0); tangent.SetX(f * (deltaV2 * edge1.GetX() - deltaV1 * edge2.GetX())); tangent.SetY(f * (deltaV2 * edge1.GetY() - deltaV1 * edge2.GetY())); tangent.SetZ(f * (deltaV2 * edge1.GetZ() - deltaV1 * edge2.GetZ())); //Bitangent example, in Java // Vector3f bitangent = new Vector3f(0,0,0); // // bitangent.setX(f * (-deltaU2 * edge1.getX() - deltaU1 * edge2.getX())); // bitangent.setX(f * (-deltaU2 * edge1.getY() - deltaU1 * edge2.getY())); // bitangent.setX(f * (-deltaU2 * edge1.getZ() - deltaU1 * edge2.getZ())); m_tangents[i0] += tangent; m_tangents[i1] += tangent; m_tangents[i2] += tangent; } for(unsigned int i = 0; i < m_tangents.size(); i++) m_tangents[i] = m_tangents[i].Normalized(); }
void Mesh::CalcTangents(Vertex* vertices, int nVertices, INDEX* indices, int nIndices) { for(int i = 0; i < nIndices; i += 3) { Vertex v0 = vertices[indices[i]]; Vertex v1 = vertices[indices[i + 1]]; Vertex v2 = vertices[indices[i + 2]]; Vector3f edge1 = v1.Pos - v0.Pos; Vector3f edge2 = v2.Pos - v0.Pos; float deltaU1 = v1.TexCoord.GetX() - v0.TexCoord.GetX(); float deltaU2 = v2.TexCoord.GetX() - v0.TexCoord.GetX(); float deltaV1 = v1.TexCoord.GetY() - v0.TexCoord.GetY(); float deltaV2 = v2.TexCoord.GetY() - v0.TexCoord.GetY(); float f = 1.0f/(deltaU1 * deltaV2 - deltaU2 * deltaV1); Vector3f tangent = Vector3f(0,0,0); tangent.SetX(f * (deltaV2 * edge1.GetX() - deltaV1 * edge2.GetX())); tangent.SetY(f * (deltaV2 * edge1.GetY() - deltaV1 * edge2.GetY())); tangent.SetZ(f * (deltaV2 * edge1.GetZ() - deltaV1 * edge2.GetZ())); //Bitangent example, in Java // Vector3f bitangent = new Vector3f(0,0,0); // // bitangent.setX(f * (-deltaU2 * edge1.getX() - deltaU1 * edge2.getX())); // bitangent.setX(f * (-deltaU2 * edge1.getY() - deltaU1 * edge2.getY())); // bitangent.setX(f * (-deltaU2 * edge1.getZ() - deltaU1 * edge2.getZ())); v0.Tangent += tangent; v1.Tangent += tangent; v2.Tangent += tangent; } for(int i = 0; i < nVertices; i++) vertices[i].Tangent = vertices[i].Tangent.Normalized(); }