private int checkHitAxisAlignedBoundingBox(Vector3 eye, Vector3 ray, SmBoundingBox boundingBox, Vector3 position) { Vector3 dirFrac = new Vector3(1.0f / ray.X, 1.0f / ray.Y, 1.0f / ray.Z); Vector3 lowerBound = boundingBox.minimum + position; Vector3 upperBound = boundingBox.maximum + position; float t1 = (lowerBound.X - eye.X) * dirFrac.X; float t2 = (upperBound.X - eye.X) * dirFrac.X; float t3 = (lowerBound.Y - eye.Y) * dirFrac.Y; float t4 = (upperBound.Y - eye.Y) * dirFrac.Y; float t5 = (lowerBound.Z - eye.Z) * dirFrac.Z; float t6 = (upperBound.Z - eye.Z) * dirFrac.Z; float tmin = Math.Max(Math.Max(Math.Min(t1, t2), Math.Min(t3, t4)), Math.Min(t5, t6)); float tmax = Math.Min(Math.Min(Math.Max(t1, t2), Math.Max(t3, t4)), Math.Max(t5, t6)); if (tmax < 0) { return(0); } if (tmin > tmax) { return(0); } else { return(1); } }
public SmModel(Model model, ByteOrder byteOrder) { // Create a List to hold all shape vertices for BBox calculation List <Vector3> positionVectors = new List <Vector3>(); foreach (String shapeKey in model.Shapes.Keys) { Shape shape = model.Shapes[shapeKey]; // Create a List to hold the raw vertices for this Shape List <float> rawVertices = new List <float>(); // Create the VertexBufferHelper with this Shape's vertex buffer VertexBufferHelper helper = new VertexBufferHelper(model.VertexBuffers[shape.VertexBufferIndex], byteOrder); // Get the positions in Vector4Fs VertexBufferHelperAttrib positionAttrib = helper["_p0"]; Syroot.Maths.Vector4F[] vec4Positions = positionAttrib.Data; foreach (Syroot.Maths.Vector4F position in vec4Positions) { // Switch based on format switch (positionAttrib.Format) { case GX2AttribFormat.Format_32_32_32_32_Single: case GX2AttribFormat.Format_32_32_32_Single: rawVertices.Add(position.X); rawVertices.Add(position.Y); rawVertices.Add(position.Z); if (positionAttrib.Format == GX2AttribFormat.Format_32_32_32_32_Single) { rawVertices.Add(position.W); } break; case GX2AttribFormat.Format_16_16_16_16_Single: rawVertices.Add(position.X); rawVertices.Add(position.Y); rawVertices.Add(position.Z); rawVertices.Add(position.W); break; default: throw new Exception("Unhandled attribute format " + positionAttrib.Format + ", go nag OatmealDome"); } } // Convert the list into an array float[] verticesArray = rawVertices.ToArray(); // Create Vector3s for BBox calculation for (int i = 0; i < verticesArray.Length; i += 3) { if (model.Name != "PipePackunDenStepA") { positionVectors.Add(new Vector3(verticesArray[i], verticesArray[i + 1], verticesArray[i + 2])); } else { positionVectors.Add(new Vector3(verticesArray[i], verticesArray[i], verticesArray[i])); } } // Generate the VBO for this Shape int vboId; GL.GenBuffers(1, out vboId); GL.BindBuffer(BufferTarget.ArrayBuffer, vboId); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * verticesArray.Length, verticesArray, BufferUsageHint.StaticDraw); // Use LoD 0 as the mesh meshes.Add(new SmMesh(shape.Meshes[0], vboId)); } // Create the bounding box for this model boundingBox = new SmBoundingBox(positionVectors); }