Ejemplo n.º 1
0
 private void Copy(ParticleGenerator other)
 {
     m_sName        = other.m_sName;
     m_Flush        = other.m_Flush;
     m_Discard      = other.m_Discard;
     m_sTexture     = other.m_sTexture;
     m_Texture      = other.m_Texture;
     m_Method       = other.m_Method;
     m_fRate        = other.m_fRate;
     m_fEmitVel     = other.m_fEmitVel;
     m_Attitude     = other.m_Attitude;
     m_PitchWidth   = other.m_PitchWidth;
     m_HeadingWidth = other.m_HeadingWidth;
     m_PointSize    = other.m_PointSize;
     m_PointSizeMin = other.m_PointSizeMin;
     m_PointScaleA  = other.m_PointScaleA;
     m_PointScaleB  = other.m_PointScaleB;
     m_PointScaleC  = other.m_PointScaleC;
     m_bValid       = other.m_bValid;
 }
        public void Render()
        {
            Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f);

            if (m_AttachedObject != null)
            {
                if (m_LookAtObject != null)
                {
                    m_LookAt = m_LookAtObject.Position;
                }
                else
                {
                    m_LookAt    = m_AttachedObject.Position;
                    m_LookAt.X += (float)Math.Sin(m_Attitude.Heading) * 10.0f;
                    m_LookAt.Y += (float)Math.Sin(m_Attitude.Pitch) * 10.0f;
                    m_LookAt.Z += (float)Math.Cos(m_Attitude.Heading) * 10.0f;
                }
                Matrix transpose = Matrix.Identity;

                m_Attitude.Heading = Attitude.Aepc(m_AttachedObject.Heading);
                m_Attitude.Pitch   = m_AttachedObject.Pitch;
                m_Attitude.Roll    = 0.0f;
                transpose.RotateYawPitchRoll(m_Attitude.Heading,
                                             m_Attitude.Pitch, m_Attitude.Roll);

                m_Eye = m_AttachedObject.Position +
                        Vector3.TransformCoordinate(m_Offset, transpose);
            }
            else
            {
                m_Eye = new Vector3(m_X, m_Y, m_Z);
                if (m_LookAtObject != null)
                {
                    m_LookAt = m_LookAtObject.Position;
                }
                else
                {
                    m_LookAt    = m_Eye;
                    m_LookAt.X += (float)Math.Sin(m_Attitude.Heading) * 10.0f;
                    m_LookAt.Y += (float)Math.Sin(m_Attitude.Pitch) * 10.0f;
                    m_LookAt.Z += (float)Math.Cos(m_Attitude.Heading) * 10.0f;
                }
            }
            // Set the app view matrix for normal viewing
            m_matView = Matrix.LookAtLH(m_Eye, m_LookAt, Up);

            CGameEngine.Device3D.Transform.View = m_matView;

            CGameEngine.Device3D.Transform.Projection = m_matProj;

            Matrix mat = Matrix.Multiply(m_matView, m_matProj);

            mat.Invert();

            vecFrustum[0] = new Vector3(-1.0f, -1.0f, 0.0f);              // xyz
            vecFrustum[1] = new Vector3(1.0f, -1.0f, 0.0f);               // Xyz
            vecFrustum[2] = new Vector3(-1.0f, 1.0f, 0.0f);               // xYz
            vecFrustum[3] = new Vector3(1.0f, 1.0f, 0.0f);                // XYz
            vecFrustum[4] = new Vector3(-1.0f, -1.0f, 1.0f);              // xyZ
            vecFrustum[5] = new Vector3(1.0f, -1.0f, 1.0f);               // XyZ
            vecFrustum[6] = new Vector3(-1.0f, 1.0f, 1.0f);               // xYZ
            vecFrustum[7] = new Vector3(1.0f, 1.0f, 1.0f);                // XYZ

            for (int i = 0; i < 8; i++)
            {
                vecFrustum[i] = Vector3.TransformCoordinate(vecFrustum[i], mat);
            }

            planeFrustum[0] = Plane.FromPoints(vecFrustum[7],
                                               vecFrustum[3], vecFrustum[5]); // Right
            planeFrustum[1] = Plane.FromPoints(vecFrustum[2],
                                               vecFrustum[6], vecFrustum[4]); // Left
            planeFrustum[2] = Plane.FromPoints(vecFrustum[6],
                                               vecFrustum[7], vecFrustum[5]); // Far
            planeFrustum[3] = Plane.FromPoints(vecFrustum[0],
                                               vecFrustum[1], vecFrustum[2]); // Near
            planeFrustum[4] = Plane.FromPoints(vecFrustum[2],
                                               vecFrustum[3], vecFrustum[6]); // Top
            planeFrustum[5] = Plane.FromPoints(vecFrustum[1],
                                               vecFrustum[0], vecFrustum[4]); // Bottom
        }
        public Attitude GetSlope(Vector3 Position, float Heading)
        {
            Attitude attitude = new Attitude();
            Matrix   matrix   = Matrix.Identity;

            matrix.RotateY(Heading);

            int x1 = (int)(Position.X / m_Spacing);
            int z1 = (int)(Position.Z / m_Spacing);

            Vector3 normal = m_Quads[x1, z1].FaceNormals;

            normal.TransformCoordinate(matrix);

            if (normal.Z == 0.0f)
            {
                attitude.Pitch = 0.0f;
            }
            else
            {
                attitude.Pitch = -(float)Math.Atan(normal.Y / normal.Z);
                if (attitude.Pitch > 0.0)
                {
                    attitude.Pitch = (float)(Math.PI / 2.0) - attitude.Pitch;
                }
                else
                {
                    attitude.Pitch = -((float)(Math.PI / 2.0) + attitude.Pitch);
                }
            }
            if (attitude.Pitch > (Math.PI / 4.0) || attitude.Pitch < -(Math.PI / 4.0))
            {
                Console.AddLine("Pitch " + attitude.Pitch * 180.0 / Math.PI + " " + normal.ToString());
            }

            if (normal.X == 0.0f)
            {
                attitude.Roll = 0.0f;
            }
            else
            {
                attitude.Roll = -(float)Math.Atan(normal.Y / normal.X);
                if (attitude.Roll > 0.0)
                {
                    attitude.Roll = (float)(Math.PI / 2.0) - attitude.Roll;
                }
                else
                {
                    attitude.Roll = -((float)(Math.PI / 2.0) + attitude.Roll);
                }
            }
            if (attitude.Roll > (Math.PI / 4.0) || attitude.Roll < -(Math.PI / 4.0))
            {
                Console.AddLine("Roll " + attitude.Roll * 180.0 / Math.PI + " " + normal.ToString());
            }


            attitude.Heading = Heading;

            return(attitude);
        }
Ejemplo n.º 4
0
        public Model(string name, string meshFile, Vector3 offset, Attitude adjust) : base(name)
        {
            Mesh         pTempMesh = null;
            WeldEpsilons Epsilons  = new WeldEpsilons();

            Vector3 objectCenter;                    // Center of bounding sphere of object

            m_vOffset        = offset;
            m_AttitudeOffset = adjust;
            m_vPosition.X    = 100.0f;
            m_vPosition.Z    = 100.0f;
            ExtendedMaterial[] materials = null;

            try
            {
                // Load the m_mesh from the specified file
                m_mesh = Mesh.FromFile(meshFile, MeshFlags.SystemMemory, CGameEngine.Device3D, out m_adj, out materials);
                // Lock the vertex buffer to generate a simple bounding sphere
                VertexBuffer   vb         = m_mesh.VertexBuffer;
                GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.NoSystemLock);
                m_fRadius = Geometry.ComputeBoundingSphere(vertexData, m_mesh.NumberVertices, m_mesh.VertexFormat, out objectCenter);
                Geometry.ComputeBoundingBox(vertexData, m_mesh.NumberVertices, m_mesh.VertexFormat, out m_NegativeExtents, out m_PositiveExtents);
                vb.Unlock();
                vb.Dispose();

                m_vOffset.Y = -m_NegativeExtents.Y;

                m_Corners[0].X = m_NegativeExtents.X;
                m_Corners[0].Y = m_NegativeExtents.Y + m_vOffset.Y;
                m_Corners[0].Z = m_NegativeExtents.Z;

                m_Corners[1].X = m_PositiveExtents.X;
                m_Corners[1].Y = m_NegativeExtents.Y + m_vOffset.Y;
                m_Corners[1].Z = m_NegativeExtents.Z;

                m_Corners[2].X = m_NegativeExtents.X;
                m_Corners[2].Y = m_PositiveExtents.Y + m_vOffset.Y;
                m_Corners[2].Z = m_NegativeExtents.Z;

                m_Corners[3].X = m_PositiveExtents.X;
                m_Corners[3].Y = m_PositiveExtents.Y + m_vOffset.Y;
                m_Corners[3].Z = m_NegativeExtents.Z;

                m_Corners[4].X = m_NegativeExtents.X;
                m_Corners[4].Y = m_NegativeExtents.Y + m_vOffset.Y;
                m_Corners[4].Z = m_PositiveExtents.Z;

                m_Corners[5].X = m_PositiveExtents.X;
                m_Corners[5].Y = m_NegativeExtents.Y + m_vOffset.Y;
                m_Corners[5].Z = m_PositiveExtents.Z;

                m_Corners[6].X = m_PositiveExtents.X;
                m_Corners[6].Y = m_PositiveExtents.Y + m_vOffset.Y;
                m_Corners[6].Z = m_PositiveExtents.Z;

                m_Corners[7].X = m_PositiveExtents.X;
                m_Corners[7].Y = m_PositiveExtents.Y + m_vOffset.Y;
                m_Corners[7].Z = m_PositiveExtents.Z;

//					Console.AddLine("Max extents " + m_PositiveExtents);
//					Console.AddLine("Min extents " + m_NegativeExtents);


                // perform simple cleansing operations on m_mesh
                pTempMesh = Mesh.Clean(m_mesh, m_adj, m_adj);
                m_mesh.Dispose();

                m_mesh = pTempMesh;
                //  Perform a weld to try and remove excess vertices like the model bigship1.x in the DX9.0 SDK (current model is fixed)
                //    Weld the m_mesh using all epsilons of 0.0f.  A small epsilon like 1e-6 works well too
                m_mesh.WeldVertices(0, Epsilons, m_adj, m_adj);
                // verify validity of m_mesh for simplification
                m_mesh.Validate(m_adj);

                CreateLod();
            }
            catch (DirectXException d3de)
            {
                Console.AddLine("Unable to load mesh " + meshFile);
                Console.AddLine(d3de.ErrorString);
            }
            catch (Exception e)
            {
                Console.AddLine("Unable to load mesh " + meshFile);
                Console.AddLine(e.Message);
            }

            if (m_meshTextures == null && materials != null)
            {
                // We need to extract the material properties and texture names
                m_meshTextures  = new Texture[materials.Length];
                m_meshMaterials = new Material[materials.Length];
                for (int i = 0; i < materials.Length; i++)
                {
                    m_meshMaterials[i] = materials[i].Material3D;
                    // Set the ambient color for the material (D3DX does not do this)
                    m_meshMaterials[i].Ambient = m_meshMaterials[i].Diffuse;

                    // Create the texture
                    try
                    {
                        if (materials[i].TextureFilename != null)
                        {
                            m_meshTextures[i] = TextureLoader.FromFile(CGameEngine.Device3D, @"..\..\Resources\" + materials[i].TextureFilename);
                        }
                    }
                    catch (DirectXException d3de)
                    {
                        Console.AddLine("Unable to load texture " + materials[i].TextureFilename);
                        Console.AddLine(d3de.ErrorString);
                    }
                    catch (Exception e)
                    {
                        Console.AddLine("Unable to load texture " + materials[i].TextureFilename);
                        Console.AddLine(e.Message);
                    }
                }
            }
        }