void buildAndAddRenderingMesh(GameObjectTemplate template, PhysicsComponent physicsComponent) { if (template.meshPath == "meta:fromMainShape") // the mesh is derived from the mainshape { MeshWithExplicitFaces renderMesh; if (template.mainMassShapeType == "box") { renderMesh = PlatonicFactory.createBox(template.mainMassDimensions[0], template.mainMassDimensions[1], template.mainMassDimensions[2]); } else { throw new NotImplementedException("meta:fromMainShape mesh path is not implemented for shape " + template.meshPath + "!"); } MeshComponent meshComponent = new MeshComponent(); meshComponent.mesh = renderMesh; TransformedMeshComponent transformedMeshComponentForRendering = new TransformedMeshComponent(); transformedMeshComponentForRendering.meshComponent = meshComponent; softwareRenderer.physicsAndMeshPairs.Add(new PhysicsComponentAndMeshPair(physicsComponent, transformedMeshComponentForRendering)); } else { throw new NotImplementedException(); } }
private void renderMesh(TransformedMeshComponent transformedMeshComponent) { foreach (MeshWithExplicitFaces.Face iterationFace in transformedMeshComponent.meshComponent.mesh.faces) { for (int edgeI = 0; edgeI < iterationFace.verticesIndices.Length - 1; edgeI++) { int vertexIndex0 = (int)iterationFace.verticesIndices[edgeI]; int vertexIndex1 = (int)iterationFace.verticesIndices[edgeI + 1]; SpatialVectorDouble vertex0 = translateTo4ComponentVector(transformedMeshComponent.transformedVertices[vertexIndex0]); SpatialVectorDouble vertex1 = translateTo4ComponentVector(transformedMeshComponent.transformedVertices[vertexIndex1]); renderEdge(vertex0, vertex1); } { int vertexIndex0 = (int)iterationFace.verticesIndices[iterationFace.verticesIndices.Length - 1]; int vertexIndex1 = (int)iterationFace.verticesIndices[0]; SpatialVectorDouble vertex0 = translateTo4ComponentVector(transformedMeshComponent.transformedVertices[vertexIndex0]); SpatialVectorDouble vertex1 = translateTo4ComponentVector(transformedMeshComponent.transformedVertices[vertexIndex1]); renderEdge(vertex0, vertex1); } } }
/* outdated, was implementing a universal (convex or concave) mesh vs ray check * * public static void checkMeshRayCollision(SpatialVectorDouble rayOrigin, SpatialVectorDouble rayDirection, PhysicsComponentAndCollidersPair physicsComponentAndMesh, CollisionDescriptor collisionDescriptor) { * TransformedMeshComponent transformedMeshComponent = physicsComponentAndMesh.transformedMeshComponent; * * PlueckerCoordinate plueckerCoordinateOfRay = PlueckerCoordinate.createByVector(rayOrigin, rayDirection); * * for(int faceIndex = 0; faceIndex < transformedMeshComponent.meshComponent.mesh.faces.Length; faceIndex++) { * if( !doesFaceIntersectRay(plueckerCoordinateOfRay, (uint)faceIndex, transformedMeshComponent) ) { * continue; * } * * SpatialVectorDouble planeNormal; * SpatialVectorDouble planeOrigin; * { * uint * vertexIndex0 = physicsComponentAndMesh.transformedMeshComponent.meshComponent.mesh.faces[faceIndex].verticesIndices[0], * vertexIndex1 = physicsComponentAndMesh.transformedMeshComponent.meshComponent.mesh.faces[faceIndex].verticesIndices[1], * vertexIndex2 = physicsComponentAndMesh.transformedMeshComponent.meshComponent.mesh.faces[faceIndex].verticesIndices[2]; * * SpatialVectorDouble * p0 = physicsComponentAndMesh.transformedMeshComponent.transformedVertices[vertexIndex0], * p1 = physicsComponentAndMesh.transformedMeshComponent.transformedVertices[vertexIndex1], * p2 = physicsComponentAndMesh.transformedMeshComponent.transformedVertices[vertexIndex2]; * * SpatialVectorDouble * diff01 = p1 - p0, * diff02 = p2 - p0; * * planeOrigin = p0; * * planeNormal = SpatialVectorDouble.crossProduct(diff01, diff02); * // TODO< normalize > * } * * // calculate distance of intersection to face * double intersectionDistance = WhiteSphereEngine.geometry.Plane.calcD(planeOrigin, planeNormal, rayOrigin, rayDirection); * * // check if distance to plane is smaller than the last collision and bigger than 0.0 * if ( intersectionDistance < 0.0 ) { * continue; * } * * if (collisionDescriptor.rayDistance < intersectionDistance) { * continue; * } * * // store collision information * collisionDescriptor.rayDistance = intersectionDistance; * collisionDescriptor.faceIndex = (uint)faceIndex; * collisionDescriptor.faceNormal = planeNormal; * collisionDescriptor.physicsComponentAndMesh = physicsComponentAndMesh; * collisionDescriptor.hit = true; * } * } */ public static bool doesFaceIntersectRay(PlueckerCoordinate plueckerCoordinate, uint faceIndex, TransformedMeshComponent transformedMeshComponent) { uint[] verticesIndices = transformedMeshComponent.meshComponent.mesh.faces[(int)faceIndex].verticesIndices; for (int edgeIndex = 0; edgeIndex < verticesIndices.Length - 1; edgeIndex++) { uint vertexIndex0 = verticesIndices[edgeIndex]; uint vertexIndex1 = verticesIndices[edgeIndex + 1]; SpatialVectorDouble vertex0 = transformedMeshComponent.transformedVertices[vertexIndex0]; SpatialVectorDouble vertex1 = transformedMeshComponent.transformedVertices[vertexIndex1]; if (!checkPlueckerCoordinateAgainstEdge(plueckerCoordinate, vertex1, vertex0)) { return(false); } } // check last vertex to first { SpatialVectorDouble vertex0 = transformedMeshComponent.transformedVertices[verticesIndices.Length - 1]; SpatialVectorDouble vertex1 = transformedMeshComponent.transformedVertices[0]; if (!checkPlueckerCoordinateAgainstEdge(plueckerCoordinate, vertex1, vertex0)) { return(false); } } return(true); }
public PhysicsComponentAndMeshPair(PhysicsComponent physicsComponent, TransformedMeshComponent transformedMeshComponent) { this.physicsComponent = physicsComponent; this.transformedMeshComponent = transformedMeshComponent; }