public override bool drawLine(Vector3 start, Vector3 end)
        {
            GraphicsDevice device = UWB_XNAGraphicsDevice.m_TheAPI.GraphicsDevice;
            Debug.Assert(device != null, "device not valid on DrawHelper drawCircle call");
            VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[2];

            Color flatColor = mColor1;

            Vector3 dir = end - start;
            Vector3 normal = Vector3.UnitX;

            if ((dir.X > 0.01f) && (dir.Y > 0.01f))
            {
                normal.X = -dir.Y;
                normal.Y = dir.X;
                normal.Z = 0.0f; // a normal to the line direction
            }

            v[0].Position = start;
            v[0].TextureCoordinate = new Vector2(0f, 0f);
            v[1].Position = end;
            v[1].TextureCoordinate = new Vector2(1f, 1f);

            v[0].Normal = normal;
            v[1].Normal = normal;

            UWB_XNAEffect effect = UWB_XNAGraphicsDevice.m_TheAPI.LightingEffect;

            Texture2D texture = null;

            if (m_bTexturingEnabled && m_TexFileName != "")
                texture = UWB_XNAGraphicsDevice.m_TheAPI.RetrieveTexture(m_TexFileName);

            effect.Material = this.m_Material;

            if (texture != null)
            {
                effect.Texture = texture;
                effect.TextureEnabled = true;
            }
            else
            {
                effect.Texture = texture;
                effect.TextureEnabled = false;
            }

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                device.DrawUserPrimitives(PrimitiveType.LineList, v, 0, 1);
            }

            return true;
        }
        public override bool drawTriangle(Vector3 v1, Vector3 v2, Vector3 v3)
        {
            GraphicsDevice device = UWB_XNAGraphicsDevice.m_TheAPI.GraphicsDevice;
            Debug.Assert(device != null, "device not valid on DrawHelper drawTriangle call");
            VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[3];

            Vector3 d1 = v2 - v1;
            Vector3 d2 = v3 - v1;
            Vector3 n = Vector3.Cross(d1, d2);
            n.Normalize();

            v[0].Position = v1;
            v[0].TextureCoordinate = new Vector2(0.5f);
            v[0].Normal = n;

            v[1].Position = v2;
            v[1].TextureCoordinate = new Vector2(0.5f);
            v[1].Normal = n;

            v[2].Position = v3;
            v[2].TextureCoordinate = new Vector2(0.5f);
            v[2].Normal = n;

            UWB_XNAEffect effect = UWB_XNAGraphicsDevice.m_TheAPI.LightingEffect;

            Texture2D texture = null;

            if (m_bTexturingEnabled && m_TexFileName != "")
                texture = UWB_XNAGraphicsDevice.m_TheAPI.RetrieveTexture(m_TexFileName);

            effect.Material = this.m_Material;

            if (texture != null)
            {
                effect.Texture = texture;
                effect.TextureEnabled = true;
            }
            else
            {
                effect.Texture = texture;
                effect.TextureEnabled = false;
            }

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                device.DrawUserPrimitives(PrimitiveType.TriangleList, v, 0, 1);
            }

            return true;
        }
        public void Initialize()
        {
            m_LightingEffect = new UWB_XNAEffect(mResources.Load <Effect>("Effects/UWB_LightingEffect"));

            m_LightManager = new UWB_XNALightManager(m_LightingEffect);


            m_GraphicsDevice.RasterizerState   = m_DefaultState;
            m_GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            m_LightingEffect.World      = Matrix.Identity;
            m_LightingEffect.View       = Matrix.Identity;
            m_LightingEffect.Projection = Matrix.Identity;
        }
Пример #4
0
        public virtual void DrawMesh(bool bShowflatColor, Color MaterialColor, UWB_DrawHelper drawHelper)
        {
            UWB_XNAGraphicsDevice graphics = UWB_XNAGraphicsDevice.m_TheAPI;
            GraphicsDevice        device   = UWB_XNAGraphicsDevice.m_TheAPI.GraphicsDevice;

            UWB_XNAEffect effect = UWB_XNAGraphicsDevice.m_TheAPI.LightingEffect;

            SetupDrawAttributes(drawHelper);

            UWB_Material material = this.m_Material;

            //if (bShowflatColor && !m_bTexturingEnabled)
            //{
            //    material = new UWB_Material(Color.Gray.ToVector4(),
            //        Color.Gray.ToVector4(),
            //        Color.Gray.ToVector4(),
            //        Color.White.ToVector4(), 1f);

            //    material.Emissive = MaterialColor.ToVector4();
            //}

            /* If texturing is enabled, there are two option... one is that
             * there has been a texture set for the mesh, and another is that
             * the mesh has a texture defined inside the mesh file. In the
             * second case, this only checks the FIRST mesh part and uses the
             * FIRST texture. */

            Texture2D texture = null;

            if (this.m_bTexturingEnabled)
            {
                texture = UWB_XNAGraphicsDevice.m_TheAPI.RetrieveTexture(m_TexFileName);

                BasicEffect meshEffect  = (mesh.Meshes[0].MeshParts[0].Effect as BasicEffect);
                Texture2D   partTexture = meshEffect.Texture;

                // If there is no supplied texture then use the texture and material of the first mesh part
                if (texture == null && partTexture != null)
                {
                    texture           = partTexture;
                    material.Ambient  = new Vector4(meshEffect.AmbientLightColor, meshEffect.Alpha);
                    material.Diffuse  = new Vector4(meshEffect.DiffuseColor, meshEffect.Alpha);
                    material.Emissive = new Vector4(meshEffect.EmissiveColor, meshEffect.Alpha);
                    material.Specular = new Vector4(meshEffect.SpecularColor, meshEffect.Alpha);
                    material.Power    = meshEffect.SpecularPower;
                }
            }

            effect.Material = material;

            if (texture != null)
            {
                effect.Texture        = texture;
                effect.TextureEnabled = true;
            }
            else
            {
                effect.Texture        = texture;
                effect.TextureEnabled = false;
            }


            for (int m = 0; m < mesh.Meshes.Count; m++)
            {
                for (int part = 0; part < mesh.Meshes[m].MeshParts.Count; part++)
                {
                    ModelMeshPart mp = mesh.Meshes[m].MeshParts[part];

                    device.Indices = mp.IndexBuffer;
                    device.SetVertexBuffer(mp.VertexBuffer);

                    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();

                        device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                     mp.VertexOffset, 0,
                                                     mp.NumVertices, mp.StartIndex, mp.PrimitiveCount);
                    }
                }
            }
        }
        public void Initialize()
        {
            m_LightingEffect = new UWB_XNAEffect(mResources.Load<Effect>("Effects/UWB_LightingEffect"));

            m_LightManager = new UWB_XNALightManager(m_LightingEffect);

            m_GraphicsDevice.RasterizerState = m_DefaultState;
            m_GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            m_LightingEffect.World = Matrix.Identity;
            m_LightingEffect.View = Matrix.Identity;
            m_LightingEffect.Projection = Matrix.Identity;
        }
        public override bool drawCircle(Vector3 center, float radius)
        {
            GraphicsDevice device = UWB_XNAGraphicsDevice.m_TheAPI.GraphicsDevice;
            Debug.Assert(device != null, "device not valid on DrawHelper drawCircle call");

            if (radius > 0)
            {
                int kNumTriangles = 40;
                if (mLod == eLevelofDetail.lodLow)
                    kNumTriangles = 10;
                else if (mLod == eLevelofDetail.lodMed)
                    kNumTriangles = 20;
                VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[kNumTriangles * 3];

                float theta = (2.0f * (float)Math.PI) / kNumTriangles;

                v[0].Position = center;
                v[0].TextureCoordinate = new Vector2(0.5f, 0.5f);
                v[0].Normal = Vector3.UnitZ;
                float px = center.X + radius;
                float py = center.Y;
                Vector2 puv = new Vector2(1, 0);

                for (int i = 0; i < kNumTriangles; i++)
                {
                    int offset = i * 3;
                    float x = center.X + (radius * (float)Math.Cos((i + 1) * theta));
                    float y = center.Y + (radius * (float)Math.Sin((i + 1) * theta));
                    float tu = 0.5f + (0.5f * (float)Math.Cos((i + 1) * theta));
                    float tv = 0.5f - (0.5f * (float)Math.Sin((i + 1) * theta));
                    v[offset].Position = v[0].Position;
                    v[offset].TextureCoordinate = v[0].TextureCoordinate;
                    v[offset].Normal = Vector3.UnitZ;

                    v[offset + 1].Position = new Vector3(px, py, 0f);
                    v[offset + 1].TextureCoordinate = puv;
                    v[offset + 1].Normal = Vector3.UnitZ;

                    v[offset + 2].Position = new Vector3(x, y, 0f);
                    v[offset + 2].TextureCoordinate = new Vector2(tu, tv);
                    v[offset + 2].Normal = Vector3.UnitZ;

                    px = x;
                    py = y;
                    puv = v[offset + 2].TextureCoordinate;
                }

                UWB_XNAEffect effect = UWB_XNAGraphicsDevice.m_TheAPI.LightingEffect;

                Texture2D texture = null;

                if (m_bTexturingEnabled && m_TexFileName != "")
                    texture = UWB_XNAGraphicsDevice.m_TheAPI.RetrieveTexture(m_TexFileName);

                effect.Material = this.m_Material;

                if (texture != null)
                {
                    effect.Texture = texture;
                    effect.TextureEnabled = true;
                }
                else
                {
                    effect.Texture = texture;
                    effect.TextureEnabled = false;
                }

                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    device.DrawUserPrimitives(PrimitiveType.TriangleList, v, 0, kNumTriangles);
                }
            }

            return true;
        }
        private void drawARectangle(Vector3 min, Vector3 max, Vector3 n)
        {
            GraphicsDevice device = UWB_XNAGraphicsDevice.m_TheAPI.GraphicsDevice;
            Debug.Assert(device != null, "device not valid on DrawHelper drawRectangle call");
            VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[12];

            v[0].Position = Vector3.Divide((min + max), 2);
            v[0].TextureCoordinate = new Vector2(0.5f, 0.5f);
            v[0].Normal = n;

            v[1].Position = new Vector3(max.X, min.Y, min.Z);
            v[1].TextureCoordinate = new Vector2(1f, 1f);
            v[1].Normal = n;

            v[2].Position = max;
            v[2].TextureCoordinate = new Vector2(1f, 0f);
            v[2].Normal = n;

            v[3] = v[0];
            v[4] = v[2];

            v[5].Position = new Vector3(min.X, max.Y, max.Z);
            v[5].TextureCoordinate = new Vector2(0f, 0f);
            v[5].Normal = n;

            v[6] = v[0];
            v[7] = v[5];
            v[8].Position = min;
            v[8].TextureCoordinate = new Vector2(0f, 1f);
            v[8].Normal = n;

            v[9] = v[0];
            v[10] = v[8];
            v[11] = v[1];

            UWB_XNAEffect effect = UWB_XNAGraphicsDevice.m_TheAPI.LightingEffect;

            Texture2D texture = null;

            if (m_bTexturingEnabled && m_TexFileName != "")
                texture = UWB_XNAGraphicsDevice.m_TheAPI.RetrieveTexture(m_TexFileName);

            effect.Material = this.m_Material;

            if (texture != null)
            {
                effect.Texture = texture;
                effect.TextureEnabled = true;
            }
            else
            {
                effect.Texture = texture;
                effect.TextureEnabled = false;
            }

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                device.DrawUserPrimitives(PrimitiveType.TriangleList, v, 0, 4);
            }
        }