Ejemplo n.º 1
0
        public static int VertexFormatStride(VertexFormats format)
        {
            const int floatSz = 4;

            const int float4 = floatSz * 4;
            const int float3 = floatSz * 3;
            const int float2 = floatSz * 2;
            switch (format)
            {
                case VertexFormats.Diffuse:
                    return floatSz;
                case VertexFormats.Normal:
                    return float3;
                case VertexFormats.Position:
                    return float3;
                case VertexFormats.Texture0:
                case VertexFormats.Texture1:
                case VertexFormats.Texture2:
                case VertexFormats.Texture3:
                case VertexFormats.Texture4:
                    return float2;
                case VertexFormats.PointSize:
                    return floatSz;
                case VertexFormats.Transformed:
                    return float4;
                default:
                    return 0;
            }
        }
Ejemplo n.º 2
0
        //Class Constructor 3
        public GeometryIndexedSplatted(Device device, Type vertexType, VertexFormats vertexFormat, PrimitiveType primitivesType, int numDivision, uint[] divisionDistribution) :
            base(device, vertexType, vertexFormat, primitivesType)
        {
            this.UseDivisionVB = true;             //implicitly

            this.NumDivisions         = numDivision;
            this.DivisionDistribution = divisionDistribution;

            this.VBModified += new VBModifiedHandler(this.OnVBModified);
            this.IBModified += new IBModifiedHandler(this.OnIBModified);
        }
Ejemplo n.º 3
0
        public Shape(string id, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers,
                     int[] numVertices, int[] numIndices, int[] indexVRefs,
                     PrimitiveType[] pTypes, VertexFormats[] vFormats,
                     int[] primCounts, BoundingBox bBox)
            : base(id, null, null)
        {
            vBuffers = vertexBuffers;
            iBuffers = indexBuffers;

            vertexCount = numVertices;
            indexCount = numIndices;
            this.indexVRefs = indexVRefs;
            this.pTypes = pTypes;
            this.vFormats = vFormats;
            this.primCounts = primCounts;
            this.bBox = bBox;
        }
Ejemplo n.º 4
0
        /// <summary>Updates the mesh to a new vertex format</summary>
        public void SetVertexFormat(Device device, VertexFormats format)
        {
            Mesh          tempSystemMesh = null;
            Mesh          tempLocalMesh  = null;
            VertexFormats oldFormat      = VertexFormats.None;

            using (systemMemoryMesh)
            {
                using (localMemoryMesh)
                {
                    // Clone the meshes
                    if (systemMemoryMesh != null)
                    {
                        oldFormat      = systemMemoryMesh.VertexFormat;
                        tempSystemMesh = systemMemoryMesh.Clone(systemMemoryMesh.Options.Value,
                                                                format, device);
                    }
                    if (localMemoryMesh != null)
                    {
                        tempLocalMesh = localMemoryMesh.Clone(localMemoryMesh.Options.Value,
                                                              format, device);
                    }
                }
            }

            // Store the new meshes
            systemMemoryMesh = tempSystemMesh;
            localMemoryMesh  = tempLocalMesh;

            // Compute normals if they are being requested and the old mesh didn't have them
            if (((oldFormat & VertexFormats.Normal) == 0) && ((format & VertexFormats.None) != 0))
            {
                if (systemMemoryMesh != null)
                {
                    systemMemoryMesh.ComputeNormals();
                }
                if (localMemoryMesh != null)
                {
                    localMemoryMesh.ComputeNormals();
                }
            }
        }
Ejemplo n.º 5
0
        //Class Constructor 3
        public GeometryIndexed(Device device, Type vertexType, VertexFormats vertexFormat, PrimitiveType primitivesType) :
            base()
        {
            this.Device          = device;
            this._PrimitivesType = primitivesType;
            this._VertexType     = vertexType;
            this._VertexFormat   = vertexFormat;
            this._VB             = null;
            this._IB             = null;

            //can only use TriangleList or LineList for now
            if (this._PrimitivesType == PrimitiveType.LineList)
            {
                step = 2;
            }
            if (this._PrimitivesType == PrimitiveType.TriangleList)
            {
                step = 3;
            }
        }
Ejemplo n.º 6
0
        public static void CreateBufferStream(ICollection <DataFields[]> streams, out GeomDataBufferStream bufferStream)
        {
            bufferStream = new GeomDataBufferStream();

            // create format order
            Dictionary <VertexFormats, int> fields = new Dictionary <VertexFormats, int>();
            List <DataFields> dataFields           = new List <DataFields>();

            int index = 0;

            foreach (DataFields[] stream in streams)
            {
                foreach (DataFields field in stream)
                {
                    if (!fields.ContainsKey(field.Format))
                    {
                        dataFields.Add(field);
                        fields.Add(field.Format, index++);
                    }
                }
            }
            bufferStream.Fields         = new VertexFormats[dataFields.Count];
            bufferStream.FieldPositions = new int[dataFields.Count];
            VertexFormats format = VertexFormats.None;
            int           pos    = 0;

            for (int i = 0; i < dataFields.Count; i++)
            {
                bufferStream.Fields[i] = dataFields[i].Format;
                format |= dataFields[i].Format;
                bufferStream.FieldPositions[i] = pos;

                pos += VertexFormatStride(dataFields[i].Format);
            }

            bufferStream.Stride = pos;
            bufferStream.Format = format;
        }
Ejemplo n.º 7
0
 public static void FlipNormalsVB(VertexBuffer vb, VertexFormats vFormat, int size)
 {
     if (vFormat == CustomVertex.PositionNormal.Format)
     {
         CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])
                                               vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else if (vFormat == CustomVertex.PositionNormalColored.Format)
     {
         CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])
                                                      vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else if (vFormat == CustomVertex.PositionNormalTextured.Format)
     {
         CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])
                                                       vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else
     {
         throw new Exception("Unsupported format to flip normals with");
     }
 }
Ejemplo n.º 8
0
        public static int VertexFormatStride(VertexFormats format)
        {
            const int floatSz = 4;

            const int float4 = floatSz * 4;
            const int float3 = floatSz * 3;
            const int float2 = floatSz * 2;

            switch (format)
            {
            case VertexFormats.Diffuse:
                return(floatSz);

            case VertexFormats.Normal:
                return(float3);

            case VertexFormats.Position:
                return(float3);

            case VertexFormats.Texture0:
            case VertexFormats.Texture1:
            case VertexFormats.Texture2:
            case VertexFormats.Texture3:
            case VertexFormats.Texture4:
                return(float2);

            case VertexFormats.PointSize:
                return(floatSz);

            case VertexFormats.Transformed:
                return(float4);

            default:
                return(0);
            }
        }
Ejemplo n.º 9
0
        //Class Constructor 1
        public GeometryIndexed(Device device, VertexBuffer vB, IndexBuffer iB, Type vertexType, VertexFormats vertexFormat, PrimitiveType primitivesType) :
            base()
        {
            this.Device          = device;
            this._PrimitivesType = primitivesType;
            this._VertexType     = vertexType;
            this._VertexFormat   = vertexFormat;
            this._VB             = vB;
            this._IB             = iB;

            //can only use TriangleList or LineList for now
            if (this._PrimitivesType == PrimitiveType.LineList)
            {
                step = 2;
            }
            if (this._PrimitivesType == PrimitiveType.TriangleList)
            {
                step = 3;
            }

            this.UpdateNoOfVertices();
            this.UpdateNoOfIndices();
            this.UpdateNeighbourList();
        }
Ejemplo n.º 10
0
        //Class Constructor 2
        public GeometryIndexed(Device device, int numVertices, int numIndices, Type vertexType, VertexFormats vertexFormat, PrimitiveType primitivesType) :
            base()
        {
            this.Device          = device;
            this._PrimitivesType = primitivesType;
            this._VertexType     = vertexType;
            this._VertexFormat   = vertexFormat;
            this._VB             = new VertexBuffer(this._VertexType, numVertices, this.Device, Usage.SoftwareProcessing, this._VertexFormat, Pool.Default);
            this._IB             = new IndexBuffer(typeof(short), numIndices * 3, this.Device, Usage.SoftwareProcessing, Pool.Default);

            //can only use TriangleList or LineList for now
            if (this._PrimitivesType == PrimitiveType.LineList)
            {
                step = 2;
            }
            if (this._PrimitivesType == PrimitiveType.TriangleList)
            {
                step = 3;
            }

            this.UpdateNoOfVertices();
            this.UpdateNoOfIndices();
            this.UpdateNeighbourList();
        }
Ejemplo n.º 11
0
        /// <summary>Updates the mesh to a new vertex format</summary>
        public void SetVertexFormat(Device device, VertexFormats format)
        {
            Mesh tempSystemMesh = null;
            Mesh tempLocalMesh = null;
            VertexFormats oldFormat = VertexFormats.None;
            using(systemMemoryMesh)
            {
                using (localMemoryMesh)
                {
                    // Clone the meshes
                    if (systemMemoryMesh != null)
                    {
                        oldFormat = systemMemoryMesh.VertexFormat;
                        tempSystemMesh = systemMemoryMesh.Clone(systemMemoryMesh.Options.Value,
                            format, device);
                    }
                    if (localMemoryMesh != null)
                    {
                        tempLocalMesh = localMemoryMesh.Clone(localMemoryMesh.Options.Value,
                            format, device);
                    }
                }
            }

            // Store the new meshes
            systemMemoryMesh = tempSystemMesh;
            localMemoryMesh = tempLocalMesh;

            // Compute normals if they are being requested and the old mesh didn't have them
            if ( ((oldFormat & VertexFormats.Normal) == 0) && ((format & VertexFormats.None) != 0) )
            {
                if (systemMemoryMesh != null)
                    systemMemoryMesh.ComputeNormals();
                if (localMemoryMesh != null)
                    localMemoryMesh.ComputeNormals();
            }
        }
Ejemplo n.º 12
0
        protected void SetupColored()
        {
            m_ColoredVertices = new CustomVertex.PositionColored[m_NumVertices];
            m_VertexFormat = CustomVertex.PositionColored.Format;
            m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
                m_NumVertices, D3DDevice, Usage.Dynamic | Usage.WriteOnly,
                m_VertexFormat, Pool.Default);

            for(int i = 0; i < m_NumVertices; i++)
            {
                float angle = ((float)i / (float)m_NumVertices) * 2 * 3.14159f;
                double tempx = Radius * Math.Cos(angle);
                double tempy = Radius * Math.Sin(angle);
                m_ColoredVertices[i].Position = new Vector3(
                    (float)tempx + (XPos + Radius),
                    (float)tempy + (YPos + Radius),
                    0f);
                m_ColoredVertices[i].Color = m_Color.ToArgb();
            }

            m_VertexBuffer.SetData(m_ColoredVertices, 0, LockFlags.None);
        }
Ejemplo n.º 13
0
 public VertexBuffer(Type typeVertexType, int numVerts, Device device, Usage usage, VertexFormats vertexFormat, Pool pool) : base((IntPtr)null)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 14
0
		/// <summary>
		/// Save the mesh when the DirectX device is lost
		/// </summary>
		void d3d_DxLost(Direct3d d3d, Device dx)
		{
            if (mMesh == null)
                return;

			// Save all data needed to restore the mesh
			mNumFaces = mMesh.NumberFaces;
			mNumVertices = mMesh.NumberVertices;
			mNumBytesPerVertex = mMesh.NumberBytesPerVertex;
			mFlags = mMesh.Options.Value;
			mVertexFormat = mMesh.VertexFormat;

			// Copy pathIndex buffer
			mIndexBufferCopy = (byte[])mMesh.LockIndexBuffer(typeof(byte),
										LockFlags.ReadOnly, mMesh.IndexBuffer.Description.Size);
			mMesh.UnlockIndexBuffer();

			// Copy vertex buffer
			mVertexBufferCopy = (byte[])mMesh.LockVertexBuffer(typeof(byte),
							LockFlags.ReadOnly, mMesh.NumberBytesPerVertex * mMesh.NumberVertices);
			mMesh.UnlockVertexBuffer();

			// Copy attribute buffer
			mAttributeBufferCopy = mMesh.LockAttributeBufferArray(LockFlags.ReadOnly);
			mMesh.UnlockAttributeBuffer(mAttributeBufferCopy);

			mMesh.Dispose();
			mMesh = null;
			mVertexCache = null;
		}
Ejemplo n.º 15
0
 public unsafe VertexBuffer(IDirect3DVertexBuffer9 *lp, Device device, Usage usage, VertexFormats vertexFormat, Pool pool) : base((IntPtr)lp)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 16
0
 public static int GetFormatSize(VertexFormats vertexFormat)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 public DataFields(VertexFormats format, string usage)
 {
     this.Format = format;
     this.Usage  = usage;
 }
Ejemplo n.º 18
0
        public void Render()
        {
            if (_player == null)
            {
                return;
            }

            lock (_subtitleLock)
            {
                if (_clearOnNextRender)
                {
                    //Log.Debug("SubtitleRenderer: clearOnNextRender");
                    _clearOnNextRender = false;
                    if (_subTexture != null)
                    {
                        _subTexture.SafeDispose();
                    }
                    _subTexture      = null;
                    _currentSubtitle = null;
                }

                if (_renderSubtitles == false)
                {
                    return;
                }

                // ugly temp!
                bool timeForNext = false;
                if (_subtitles.Count > 0)
                {
                    Subtitle next = _subtitles.First.Value;
                    if (next.presentTime <= _player.StreamPosition)
                    {
                        timeForNext = true;
                    }
                }

                _posOnLastRender = _player.StreamPosition;

                // Check for subtitle if we dont have one currently or if the current one is beyond its timeout
                if (_currentSubtitle == null ||
                    _currentSubtitle.presentTime + _currentSubtitle.timeOut <= _player.StreamPosition ||
                    timeForNext)
                {
                    //Log.Debug("-Current position: ");
                    if (_currentSubtitle != null && !timeForNext)
                    {
                        //Log.Debug("-Current subtitle : " + currentSubtitle.ToString() + " time out expired");
                        _currentSubtitle = null;
                    }
                    if (timeForNext)
                    {
                        //if (currentSubtitle != null) Log.Debug("-Current subtitle : " + currentSubtitle.ToString() + " TIME FOR NEXT!");
                    }

                    Subtitle next = null;
                    while (_subtitles.Count > 0)
                    {
                        next = _subtitles.First.Value;

                        //Log.Debug("-next from queue: " + next.ToString());
                        // if the next should be displayed now or previously
                        if (next.presentTime <= _player.StreamPosition)
                        {
                            // remove from queue
                            _subtitles.RemoveFirst();

                            // if it is not too late for this sub to be displayed, break
                            // otherwise continue
                            if (next.presentTime + next.timeOut >= _player.StreamPosition)
                            {
                                _currentSubtitle = next;
                                break;
                            }
                        }
                        // next wants to be displayed in the future so break
                        else
                        {
                            //Log.Debug("-next is in the future");
                            break;
                        }
                    }
                    // if currentSubtitle is non-null we have a new subtitle
                    if (_currentSubtitle != null)
                    {
                        SetSubtitle(_currentSubtitle);
                    }
                    else
                    {
                        return;
                    }
                }

                VertexFormats vertexFormat = GUIGraphicsContext.DX9Device.VertexFormat;

                try
                {
                    int   wx = 0, wy = 0, wwidth = 0, wheight = 0;
                    float rationW = 1, rationH = 1;

                    Rectangle src, dst;
                    VMR9Util.g_vmr9.GetVideoWindows(out src, out dst);

                    rationH = dst.Height / (float)_currentSubtitle.screenHeight;
                    rationW = dst.Width / (float)_currentSubtitle.screenWidth;
                    wx      = dst.X + (int)(rationW * (float)_currentSubtitle.horizontalPosition);
                    wy      = dst.Y + (int)(rationH * (float)_currentSubtitle.firstScanLine);
                    wwidth  = (int)((float)_currentSubtitle.width * rationW);
                    wheight = (int)((float)_currentSubtitle.height * rationH);

                    // make sure the vertex buffer is ready and correct for the coordinates
                    CreateVertexBuffer(wx, wy, wwidth, wheight);

                    // Log.Debug("Subtitle render target: wx = {0} wy = {1} ww = {2} wh = {3}", wx, wy, wwidth, wheight);

                    // enable alpha blending so that the subtitle is rendered with transparent background
                    DXNative.FontEngineSetRenderState((int)D3DRENDERSTATETYPE.D3DRS_ALPHABLENDENABLE, 1);

                    // Make sure D3D objects haven't been disposed for some reason. This would  cause
                    // an access violation on native side, causing Skin Engine to halt rendering
                    if (!_subTexture.Disposed && !_vertexBuffer.Disposed)
                    {
                        GUIGraphicsContext.DX9Device.SetStreamSource(0, _vertexBuffer, 0);
                        GUIGraphicsContext.DX9Device.SetTexture(0, _subTexture);
                        GUIGraphicsContext.DX9Device.VertexFormat = CustomVertex.TransformedTextured.Format;
                        GUIGraphicsContext.DX9Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
                    }
                    else
                    {
                        Log.Debug("Subtitle renderer: D3D resource was disposed! Not trying to render the texture");
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                try
                {
                    // Restore device settings
                    GUIGraphicsContext.DX9Device.SetTexture(0, null);
                    GUIGraphicsContext.DX9Device.VertexFormat = vertexFormat;
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            } // end of lock (subtitle)
        }
Ejemplo n.º 19
0
        public void BuildBuffers(Device device)
        {
            if (Geometry.Normals != null)
            {
                if (Geometry.VertexClrs != null)
                {
                    vFormat = CustomVertex.PositionNormalColored.Format;
                    vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalColored), Geometry.Vertices.Length, device,
                                           Usage.None, CustomVertex.PositionNormalColored.Format, Pool.Managed);
                    CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])vBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.Vertices.Length; i++)
                    {
                        verts[i].Position = Geometry.Vertices[i];
                        verts[i].Normal = Geometry.Normals[i];
                        verts[i].Color = Geometry.VertexClrs[i];
                    }
                    vBuffer.Unlock();
                }
                else
                {
                    if (Geometry.TexCoords != null)
                    {
                        vFormat = CustomVertex.PositionNormalTextured.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionNormalTextured.Format, Pool.Managed);
                        CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Normal = Geometry.Normals[i];
                            verts[i].Tu = Geometry.TexCoords[i].X;
                            verts[i].Tv = Geometry.TexCoords[i].Y;
                        }
                        vBuffer.Unlock();
                    }
                    else
                    {
                        vFormat = CustomVertex.PositionNormal.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionNormal.Format, Pool.Managed);
                        CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Normal = Geometry.Normals[i];
                        }
                        vBuffer.Unlock();
                    }
                }
            }
            else
            {
                if (Geometry.VertexClrs != null)
                {
                    vFormat = CustomVertex.PositionColored.Format;
                    vBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), Geometry.Vertices.Length, device,
                                           Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
                    CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.Vertices.Length; i++)
                    {
                        verts[i].Position = Geometry.Vertices[i];
                        verts[i].Color = Geometry.VertexClrs[i];
                    }
                    vBuffer.Unlock();
                }
                else
                {
                    if (Geometry.TexCoords != null)
                    {
                        vFormat = CustomVertex.PositionTextured.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionTextured.Format, Pool.Managed);
                        CustomVertex.PositionTextured[] verts = (CustomVertex.PositionTextured[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Tu = Geometry.TexCoords[i].X;
                            verts[i].Tv = Geometry.TexCoords[i].Y;
                        }
                        vBuffer.Unlock();
                    }
                    else
                    {
                        vFormat = CustomVertex.PositionOnly.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionOnly.Format, Pool.Managed);
                        CustomVertex.PositionOnly[] verts = (CustomVertex.PositionOnly[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                        }
                        vBuffer.Unlock();
                    }
                }
            }

            if (Geometry.PrimIndices != null)
            {
                if (MaterialIndices != null)
                {
                    matIBuffers = new IndexBuffer[MaterialIndices.Length];
                    int mibIdx = 0;
                    foreach (ABMaterialIndex matIndex in MaterialIndices)
                    {
                        if (matIndex.Material.Texture == null && matIndex.Material.TextureName != null)
                        {
                            if (File.Exists(matIndex.Material.TextureName))
                                matIndex.Material.Texture = TextureLoader.FromFile(device, matIndex.Material.TextureName);
                        }

                        int numIndices = matIndex.Indices.Length * 3;
                        IndexBuffer buffer = matIBuffers[mibIdx++] = new IndexBuffer(typeof(int), numIndices, device, Usage.None, Pool.Managed);
                        int[] indices = (int[])buffer.Lock(0, LockFlags.None);
                        int iIdx = 0;
                        for (int i = 0; i < matIndex.Indices.Length; i++)
                        {
                            int index = matIndex.Indices[i] * 3;
                            indices[iIdx++] = Geometry.PrimIndices[index];
                            indices[iIdx++] = Geometry.PrimIndices[index + 1];
                            indices[iIdx++] = Geometry.PrimIndices[index + 2];
                        }
                        buffer.Unlock();

                        switch (Geometry.PrimType)
                        {
                            case PrimitiveType.LineList:
                                matIndex.PrimCount = numIndices / 2;
                                break;
                            case PrimitiveType.LineStrip:
                                matIndex.PrimCount = numIndices - 1;
                                break;
                            case PrimitiveType.PointList:
                                matIndex.PrimCount = numIndices;
                                break;
                            case PrimitiveType.TriangleList:
                                matIndex.PrimCount = numIndices / 3;
                                break;
                        }
                    }
                }
                else
                {
                    iBuffer = new IndexBuffer(typeof(int), Geometry.PrimIndices.Length, device, Usage.None, Pool.Managed);
                    //iBuffer.SetData(Geometry.PrimIndices, 0, LockFlags.None);
                    int[] indices = (int[])iBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.PrimIndices.Length; i++)
                    {
                        indices[i] = Geometry.PrimIndices[i];
                    }
                    iBuffer.Unlock();

                    switch (Geometry.PrimType)
                    {
                        case PrimitiveType.LineList:
                            numPrimitives = Geometry.PrimIndices.Length / 2;
                            break;
                        case PrimitiveType.LineStrip:
                            numPrimitives = Geometry.PrimIndices.Length - 1;
                            break;
                        case PrimitiveType.PointList:
                            numPrimitives = Geometry.PrimIndices.Length;
                            break;
                        case PrimitiveType.TriangleList:
                            numPrimitives = Geometry.PrimIndices.Length / 3;
                            break;
                    }
                }
            }
            else
            {
                switch (Geometry.PrimType)
                {
                    case PrimitiveType.LineList:
                        numPrimitives = Geometry.Vertices.Length / 2;
                        break;
                    case PrimitiveType.LineStrip:
                        numPrimitives = Geometry.Vertices.Length - 1;
                        break;
                    case PrimitiveType.PointList:
                        numPrimitives = Geometry.Vertices.Length;
                        break;
                    case PrimitiveType.TriangleList:
                        numPrimitives = Geometry.Vertices.Length / 3;
                        break;
                }
            }
        }
Ejemplo n.º 20
0
        protected void RebuildStructure(Device device, NuSceneBuffer3D sceneBuffer,
                                        GeneralStructuresShadingDesc shading)
        {
            boundingBox = GenerateBoundingBoxFromPoints(sceneBuffer.triangleStrips);

            bool useClrAxis = shading.RibbonsShadingDesc.PropertiesInUse.ContainsKey("UseClrAxis");
            Vector4 xClrStep = new Vector4(), yClrStep = new Vector4(), zClrStep = new Vector4();
            Vector4 xClrStart = new Vector4(), yClrStart = new Vector4(), zClrStart = new Vector4();
            if (useClrAxis)
            {
                // pre-calculate colour ranges scale from bounding box
                Color xClrA, xClrB;
                if (shading.RibbonsShadingDesc.Clrs.XEnabled)
                {
                    xClrA = shading.RibbonsShadingDesc.Clrs.Xa;
                    xClrB = shading.RibbonsShadingDesc.Clrs.Xb;
                }
                else
                    xClrA = xClrB = shading.RibbonsShadingDesc.DefaultClr;
                Color yClrA, yClrB;
                if (shading.RibbonsShadingDesc.Clrs.YEnabled)
                {
                    yClrA = shading.RibbonsShadingDesc.Clrs.Ya;
                    yClrB = shading.RibbonsShadingDesc.Clrs.Yb;
                }
                else
                    yClrA = yClrB = shading.RibbonsShadingDesc.DefaultClr;
                Color zClrA, zClrB;
                if (shading.RibbonsShadingDesc.Clrs.ZEnabled)
                {
                    zClrA = shading.RibbonsShadingDesc.Clrs.Za;
                    zClrB = shading.RibbonsShadingDesc.Clrs.Zb;
                }
                else
                    zClrA = zClrB = shading.RibbonsShadingDesc.DefaultClr;

                float difA = xClrB.A - xClrA.A;
                float difR = xClrB.R - xClrA.R;
                float difG = xClrB.G - xClrA.G;
                float difB = xClrB.B - xClrA.B;

                // calc steps
                xClrStep = new Vector4(difA / boundingBox.Dimensions.X,
                                       difR / boundingBox.Dimensions.X,
                                       difG / boundingBox.Dimensions.X,
                                       difB / boundingBox.Dimensions.X);

                difA = yClrB.A - yClrA.A;
                difR = yClrB.R - yClrA.R;
                difG = yClrB.G - yClrA.G;
                difB = yClrB.B - yClrA.B;
                yClrStep = new Vector4(difA / boundingBox.Dimensions.Y,
                                       difR / boundingBox.Dimensions.Y,
                                       difG / boundingBox.Dimensions.Y,
                                       difB / boundingBox.Dimensions.Y);

                difA = zClrB.A - zClrA.A;
                difR = zClrB.R - zClrA.R;
                difG = zClrB.G - zClrA.G;
                difB = zClrB.B - zClrA.B;
                zClrStep = new Vector4(difA / boundingBox.Dimensions.Z,
                                       difR / boundingBox.Dimensions.Z,
                                       difG / boundingBox.Dimensions.Z,
                                       difB / boundingBox.Dimensions.Z);
            }

            if (shading.RibbonsShadingDesc.ShadingType == RibbonsShadingDesc.Shading.Solid)
            {
                // make triangle strips
                List<Vector3[]> triStripsList = sceneBuffer.triangleStrips;

                triStrips = new VertexBuffer[triStripsList.Count];
                triStripsMirrored = new VertexBuffer[triStripsList.Count];
                triStripSizes = new int[triStripsList.Count];
                for (int strip = 0; strip < triStripsList.Count; strip++)
                {
                    Vector3[] points = triStripsList[strip];
                    if (useClrAxis)
                    {
                        vFormat = CustomVertex.PositionNormalColored.Format;
                        triStrips[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormalColored),
                                                            points.Length, device, Usage.None,
                                                            CustomVertex.PositionNormalColored.Format,
                                                            Pool.Managed);
                        triStripSizes[strip] = points.Length - 2;
                        CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])
                                                                     triStrips[strip].Lock(0, LockFlags.None);
                        float xStart = boundingBox.Centre.X - (boundingBox.Dimensions.X / 2.0f);
                        float yStart = boundingBox.Centre.Y - (boundingBox.Dimensions.Y / 2.0f);
                        float zStart = boundingBox.Centre.Z - (boundingBox.Dimensions.Z / 2.0f);
                        for (int v = 1; v < points.Length - 1; v++)
                        {
                            // V1
                            float xSteps = points[v - 1].X - xStart;
                            float ySteps = points[v - 1].Y - yStart;
                            float zSteps = points[v - 1].Z - zStart;

                            // blend XYZ colours
                            int A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            int R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            int G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            int B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v - 1].Color = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v - 1].Position = points[v - 1];

                            // V2
                            xSteps = points[v].X - xStart;
                            ySteps = points[v].Y - yStart;
                            zSteps = points[v].Z - zStart;

                            // blend XYZ colours
                            A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v].Color = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v].Position = points[v];

                            // V3
                            xSteps = points[v + 1].X - xStart;
                            ySteps = points[v + 1].Y - yStart;
                            zSteps = points[v + 1].Z - zStart;

                            // blend XYZ colours
                            A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v + 1].Color = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v + 1].Position = points[v + 1];


                            // normals
                            Vector3 v0 = verts[v + 1].Position;
                            Vector3 v1 = verts[v].Position;
                            Vector3 v2 = verts[v - 1].Position;

                            Vector3 e1 = v1 - v0, e2 = v2 - v0;
                            Vector3 vNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                            verts[v - 1].Normal += vNormal;
                            verts[v].Normal += vNormal;
                            verts[v + 1].Normal += vNormal;

                            if (v != 1)
                                verts[v - 1].Normal *= 0.5f;
                            if (v == points.Length - 1)
                                verts[v].Normal *= 0.5f;
                        }

                        // mirror for double-sided
                        triStripsMirrored[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormalColored),
                                                                    points.Length, device, Usage.None,
                                                                    CustomVertex.PositionNormalColored.Format,
                                                                    Pool.Managed);
                        // clone
                        triStripsMirrored[strip].SetData(verts, 0, LockFlags.None);
                        triStrips[strip].Unlock();
                        // flip
                        FlipNormalsVB(triStripsMirrored[strip], CustomVertex.PositionNormalColored.Format,
                                      triStripSizes[strip]);
                    }
                    else
                    {
                        vFormat = CustomVertex.PositionNormal.Format;
                        triStrips[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormal),
                                                            points.Length, device, Usage.None,
                                                            CustomVertex.PositionNormal.Format,
                                                            Pool.Managed);
                        triStripSizes[strip] = points.Length - 2;
                        CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])
                                                              triStrips[strip].Lock(0, LockFlags.None);
                        for (int v = 1; v < points.Length - 1; v++)
                        {
                            // positions
                            verts[v - 1].Position = points[v + 1];
                            verts[v].Position = points[v];
                            verts[v + 1].Position = points[v - 1];

                            // normals
                            Vector3 v0 = verts[v - 1].Position;
                            Vector3 v1 = verts[v].Position;
                            Vector3 v2 = verts[v + 1].Position;

                            Vector3 e1 = v1 - v0, e2 = v2 - v0;
                            Vector3 vNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                            verts[v - 1].Normal += vNormal;
                            verts[v].Normal += vNormal;
                            verts[v + 1].Normal += vNormal;

                            if (v != 1)
                                verts[v - 1].Normal *= 0.5f;
                            if (v == points.Length - 1)
                                verts[v].Normal *= 0.5f;
                        }
                        

                        // mirror for double-sided
                        triStripsMirrored[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormal),
                                                                    points.Length, device, Usage.None,
                                                                    CustomVertex.PositionNormal.Format,
                                                                    Pool.Managed);
                        // clone
                        triStripsMirrored[strip].SetData(verts, 0, LockFlags.None);
                        triStrips[strip].Unlock();
                        // flip
                        FlipNormalsVB(triStripsMirrored[strip], CustomVertex.PositionNormal.Format,
                                      triStripSizes[strip]);
                    }
                }
            }
            else if (shading.RibbonsShadingDesc.ShadingType == RibbonsShadingDesc.Shading.Edges)
            {
            }
        }
Ejemplo n.º 21
0
 public void DrawUserPrimitives(PrimitiveType primative_type, int primative_count, VertexFormats format, object vertex_data)
 {
     _Device_.VertexFormat = format;
     _Device_.DrawUserPrimitives(primative_type, primative_count, vertex_data);
 }
Ejemplo n.º 22
0
 public void SetFontRenderState()
 {
     mLastTexture  = null;
     mVertexFormat = VertexFormats.PointSize;
 }
Ejemplo n.º 23
0
 public static VertexElement[] DeclaratorFromFormat(VertexFormats vertexFormat)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 24
0
 public void DrawUserPrimitives(PrimitiveType primative_type, int primative_count, VertexFormats format, object vertex_data)
 {
     _Device_.VertexFormat = format;
     _Device_.DrawUserPrimitives(primative_type, primative_count, vertex_data);
 }
Ejemplo n.º 25
0
Archivo: Icons.cs Proyecto: Fav/testww
        /// <summary>
        /// This used to be done in the World class but moved here so folks could override a ROL's behavior.
        ///
        /// NOTE:  Everything under an Icons is rendered using RenderPriority.Icons.  If you put any other kind of
        /// ROL in here it (and it's children) probably wont render.
        /// </summary>
        /// <param name="drawArgs"></param>
        /// <param name="priority"></param>
        public override void RenderChildren(DrawArgs drawArgs, RenderPriority priority)
        {
            //Respect icon set temporal extents
            if (TimeKeeper.CurrentTimeUtc < EarliestTime || TimeKeeper.CurrentTimeUtc > LatestTime)
            {
                return;
            }

            if (!isOn)
            {
                return;
            }

            if (!isInitialized)
            {
                return;
            }

            if (priority != RenderPriority.Icons)
            {
                return;
            }

            // render ourselves
            this.Render(drawArgs);

            if (m_canUsePointSprites)
            {
                pointSprites.Clear();
            }

            // First render everything except icons - we need to do this twice since the other loop is INSIDE the
            // sprite.begin which can mess up the rendering of other ROs.  This is why prerender is in this loop.
            m_childrenRWLock.AcquireReaderLock(Timeout.Infinite);
            try
            {
                foreach (RenderableObject ro in m_children)
                {
                    if (!ro.IsOn)
                    {
                        continue;
                    }

                    if (ro is Icon) // && (priority == RenderPriority.Icons))
                    {
                        Icon icon = ro as Icon;
                        if (icon == null)
                        {
                            continue;
                        }

                        // do this once for both passes
                        icon.DistanceToIcon = Vector3.Length(icon.Position - drawArgs.WorldCamera.Position);

                        // do PreRender regardless of everything else
                        // note that mouseover actions happen one render cycle after mouse is over icon

                        icon.PreRender(drawArgs, (mouseOverIcon != null) && (icon == mouseOverIcon));
                    }
                    else if (ro is RenderableObjectList)
                    {
                        (ro as RenderableObjectList).RenderChildren(drawArgs, priority);
                    }
                    //// hack to render both surface images and terrain mapped images.
                    //else if (priority == RenderPriority.TerrainMappedImages)
                    //{
                    //    if (ro.RenderPriority == RenderPriority.SurfaceImages || ro.RenderPriority == RenderPriority.TerrainMappedImages)
                    //    {
                    //        ro.Render(drawArgs);
                    //    }
                    //}
                    else  // if (ro.RenderPriority == priority)
                    {
                        ro.Render(drawArgs);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                m_childrenRWLock.ReleaseReaderLock();
            }

            m_labelRectangles.Clear();

            int  closestIconDistanceSquared = int.MaxValue;
            Icon closestIcon = null;

            if (priority == RenderPriority.Icons)
            {
                try
                {
                    m_sprite.Begin(SpriteFlags.AlphaBlend);

                    // Now render just the icons
                    m_childrenRWLock.AcquireReaderLock(Timeout.Infinite);
                    try
                    {
                        foreach (RenderableObject ro in m_children)
                        {
                            if (!ro.IsOn)
                            {
                                continue;
                            }

                            Icon icon = ro as Icon;
                            if (icon == null)
                            {
                                continue;
                            }

                            // don't try to render if we aren't in view or too far away
                            if ((drawArgs.WorldCamera.ViewFrustum.ContainsPoint(icon.Position)) &&
                                (icon.DistanceToIcon <= icon.MaximumDisplayDistance) &&
                                (icon.DistanceToIcon >= icon.MinimumDisplayDistance))
                            {
                                Vector3 translationVector = new Vector3(
                                    (float)(icon.PositionD.X - drawArgs.WorldCamera.ReferenceCenter.X),
                                    (float)(icon.PositionD.Y - drawArgs.WorldCamera.ReferenceCenter.Y),
                                    (float)(icon.PositionD.Z - drawArgs.WorldCamera.ReferenceCenter.Z));

                                Vector3 projectedPoint = drawArgs.WorldCamera.Project(translationVector);

                                // check if inside bounding box of icon
                                int dx = DrawArgs.LastMousePosition.X - (int)projectedPoint.X;
                                int dy = DrawArgs.LastMousePosition.Y - (int)projectedPoint.Y;
                                if (icon.SelectionRectangle.Contains(dx, dy))
                                {
                                    // Mouse is over, check whether this icon is closest
                                    int distanceSquared = dx * dx + dy * dy;
                                    if (distanceSquared < closestIconDistanceSquared)
                                    {
                                        closestIconDistanceSquared = distanceSquared;
                                        closestIcon = icon;
                                    }
                                }

                                // mouseover is always one render cycle behind...we mark that an icon is the mouseover
                                // icon and render it normally.  On the NEXT pass it renders as a mouseover icon

                                if (icon != mouseOverIcon)
                                {
                                    // Note: Always render hooked icons as a real icon rather than a sprite.
                                    if (icon.UsePointSprite && (icon.DistanceToIcon > icon.PointSpriteDistance) && m_canUsePointSprites && !icon.IsHooked)
                                    {
                                        PointSpriteVertex pv = new PointSpriteVertex(translationVector.X, translationVector.Y, translationVector.Z, icon.PointSpriteSize, icon.PointSpriteColor.ToArgb());
                                        pointSprites.Add(pv);
                                    }
                                    else
                                    {
                                        // add pointsprite anyway
                                        if (icon.UsePointSprite && icon.AlwaysRenderPointSprite)
                                        {
                                            PointSpriteVertex pv = new PointSpriteVertex(translationVector.X, translationVector.Y, translationVector.Z, icon.PointSpriteSize, icon.PointSpriteColor.ToArgb());
                                            pointSprites.Add(pv);
                                        }

                                        icon.FastRender(drawArgs, m_sprite, projectedPoint, false, m_labelRectangles);
                                    }
                                }
                            }
                            else
                            {
                                // do whatever we're supposed to do if we're not in view or too far away
                                icon.NoRender(drawArgs);
                            }

                            // do post rendering even if we don't render
                            // note that mouseover actions happen one render cycle after mouse is over icon
                            icon.PostRender(drawArgs, icon == mouseOverIcon);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.Message.ToString());
                    }
                    finally
                    {
                        m_childrenRWLock.ReleaseReaderLock();
                    }

                    // Clear the rectangles so that mouseover label always appears
                    m_labelRectangles.Clear();

                    // Render the mouse over icon last (on top)
                    if (mouseOverIcon != null)
                    {
                        Vector3 translationVector = new Vector3(
                            (float)(mouseOverIcon.PositionD.X - drawArgs.WorldCamera.ReferenceCenter.X),
                            (float)(mouseOverIcon.PositionD.Y - drawArgs.WorldCamera.ReferenceCenter.Y),
                            (float)(mouseOverIcon.PositionD.Z - drawArgs.WorldCamera.ReferenceCenter.Z));

                        Vector3 projectedPoint = drawArgs.WorldCamera.Project(translationVector);

                        if (mouseOverIcon.UsePointSprite && mouseOverIcon.AlwaysRenderPointSprite && m_canUsePointSprites)
                        {
                            // add pointsprite anyway
                            PointSpriteVertex pv = new PointSpriteVertex(translationVector.X, translationVector.Y, translationVector.Z, mouseOverIcon.PointSpriteSize, mouseOverIcon.PointSpriteColor.ToArgb());
                            pointSprites.Add(pv);
                        }

                        mouseOverIcon.FastRender(drawArgs, m_sprite, projectedPoint, true, m_labelRectangles);
                    }

                    // set new mouseover icon
                    mouseOverIcon = closestIcon;
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message.ToString());
                }
                finally
                {
                    m_sprite.End();
                }

                // render point sprites if any in the list
                try
                {
                    if (pointSprites.Count > 0)
                    {
                        // save device state
                        Texture       origTexture           = drawArgs.device.GetTexture(0);
                        VertexFormats origVertexFormat      = drawArgs.device.VertexFormat;
                        float         origPointScaleA       = drawArgs.device.RenderState.PointScaleA;
                        float         origPointScaleB       = drawArgs.device.RenderState.PointScaleB;
                        float         origPointScaleC       = drawArgs.device.RenderState.PointScaleC;
                        bool          origPointSpriteEnable = drawArgs.device.RenderState.PointSpriteEnable;
                        bool          origPointScaleEnable  = drawArgs.device.RenderState.PointScaleEnable;
                        Blend         origSourceBlend       = drawArgs.device.RenderState.SourceBlend;
                        Blend         origDestBlend         = drawArgs.device.RenderState.DestinationBlend;

                        // set device to do point sprites
                        drawArgs.device.SetTexture(0, m_pointTexture.Texture);
                        drawArgs.device.VertexFormat            = VertexFormats.Position | VertexFormats.PointSize | VertexFormats.Diffuse;
                        drawArgs.device.RenderState.PointScaleA = 1f;
                        drawArgs.device.RenderState.PointScaleB = 0f;
                        drawArgs.device.RenderState.PointScaleC = 0f;
                        //drawArgs.device.RenderState.PointScaleA = 0f;
                        //drawArgs.device.RenderState.PointScaleB = 0f;
                        //drawArgs.device.RenderState.PointScaleC = .0000000000001f;
                        drawArgs.device.RenderState.PointSpriteEnable = true;
                        drawArgs.device.RenderState.PointScaleEnable  = true;
                        drawArgs.device.RenderState.SourceBlend       = Blend.One;
                        drawArgs.device.RenderState.DestinationBlend  = Blend.InvSourceAlpha;

                        drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorOperation, (int)TextureOperation.Modulate);
                        drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
                        drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorArgument2, (int)TextureArgument.Diffuse);

                        drawArgs.device.DrawUserPrimitives(PrimitiveType.PointList, pointSprites.Count, pointSprites.ToArray());

                        // restore device state
                        drawArgs.device.SetTexture(0, origTexture);
                        drawArgs.device.VertexFormat                  = origVertexFormat;
                        drawArgs.device.RenderState.PointScaleA       = origPointScaleA;
                        drawArgs.device.RenderState.PointScaleB       = origPointScaleB;
                        drawArgs.device.RenderState.PointScaleC       = origPointScaleC;
                        drawArgs.device.RenderState.PointSpriteEnable = origPointSpriteEnable;
                        drawArgs.device.RenderState.PointScaleEnable  = origPointScaleEnable;
                        drawArgs.device.RenderState.SourceBlend       = origSourceBlend;
                        drawArgs.device.RenderState.DestinationBlend  = origDestBlend;
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message.ToString());
                }
            }
        }
Ejemplo n.º 26
0
 public Mesh(int numFaces, int numVertices, MeshFlags options, VertexFormats vertexFormat, Device device) : base((IntPtr)null)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 27
0
		/// <summary>
		/// Create an AutoVertexBuffer
		/// </summary>
		public AutoVertexBuffer(Direct3d d3d, Type vertexType, int numVerts,
									Usage usage, VertexFormats format, Pool pool)
		{
			mVertexBuffer = new VertexBuffer(vertexType, numVerts == 0 ? 1 : numVerts, d3d.Dx, usage, format, pool);
			mD3d = d3d;
			mVertexType = vertexType;
			mVertexNumVertices = numVerts;
			mVertexUsage = usage;
			mVertexFormat = format;
			mVertexPool = pool;

			d3d.DxLost += new Direct3d.DxDirect3dDelegate(d3d_DxLost);
			d3d.DxRestore += new Direct3d.DxDirect3dDelegate(d3d_DxRestore);
		}
Ejemplo n.º 28
0
 /// <summary>
 /// Sets the desired vertex format for the mesh
 /// </summary>
 public void SetVertexFormat(VertexFormats format)
 {
     vf    = format;
     vfSet = true;
 }
Ejemplo n.º 29
0
        protected void RebuildStructure(Device device, NuSceneBuffer3D sceneBuffer,
                                        GeneralStructuresShadingDesc shading)
        {
            boundingBox = GenerateBoundingBoxFromPoints(sceneBuffer.triangleStrips);

            bool    useClrAxis = shading.RibbonsShadingDesc.PropertiesInUse.ContainsKey("UseClrAxis");
            Vector4 xClrStep = new Vector4(), yClrStep = new Vector4(), zClrStep = new Vector4();
            Vector4 xClrStart = new Vector4(), yClrStart = new Vector4(), zClrStart = new Vector4();

            if (useClrAxis)
            {
                // pre-calculate colour ranges scale from bounding box
                Color xClrA, xClrB;
                if (shading.RibbonsShadingDesc.Clrs.XEnabled)
                {
                    xClrA = shading.RibbonsShadingDesc.Clrs.Xa;
                    xClrB = shading.RibbonsShadingDesc.Clrs.Xb;
                }
                else
                {
                    xClrA = xClrB = shading.RibbonsShadingDesc.DefaultClr;
                }
                Color yClrA, yClrB;
                if (shading.RibbonsShadingDesc.Clrs.YEnabled)
                {
                    yClrA = shading.RibbonsShadingDesc.Clrs.Ya;
                    yClrB = shading.RibbonsShadingDesc.Clrs.Yb;
                }
                else
                {
                    yClrA = yClrB = shading.RibbonsShadingDesc.DefaultClr;
                }
                Color zClrA, zClrB;
                if (shading.RibbonsShadingDesc.Clrs.ZEnabled)
                {
                    zClrA = shading.RibbonsShadingDesc.Clrs.Za;
                    zClrB = shading.RibbonsShadingDesc.Clrs.Zb;
                }
                else
                {
                    zClrA = zClrB = shading.RibbonsShadingDesc.DefaultClr;
                }

                float difA = xClrB.A - xClrA.A;
                float difR = xClrB.R - xClrA.R;
                float difG = xClrB.G - xClrA.G;
                float difB = xClrB.B - xClrA.B;

                // calc steps
                xClrStep = new Vector4(difA / boundingBox.Dimensions.X,
                                       difR / boundingBox.Dimensions.X,
                                       difG / boundingBox.Dimensions.X,
                                       difB / boundingBox.Dimensions.X);

                difA     = yClrB.A - yClrA.A;
                difR     = yClrB.R - yClrA.R;
                difG     = yClrB.G - yClrA.G;
                difB     = yClrB.B - yClrA.B;
                yClrStep = new Vector4(difA / boundingBox.Dimensions.Y,
                                       difR / boundingBox.Dimensions.Y,
                                       difG / boundingBox.Dimensions.Y,
                                       difB / boundingBox.Dimensions.Y);

                difA     = zClrB.A - zClrA.A;
                difR     = zClrB.R - zClrA.R;
                difG     = zClrB.G - zClrA.G;
                difB     = zClrB.B - zClrA.B;
                zClrStep = new Vector4(difA / boundingBox.Dimensions.Z,
                                       difR / boundingBox.Dimensions.Z,
                                       difG / boundingBox.Dimensions.Z,
                                       difB / boundingBox.Dimensions.Z);
            }

            if (shading.RibbonsShadingDesc.ShadingType == RibbonsShadingDesc.Shading.Solid)
            {
                // make triangle strips
                List <Vector3[]> triStripsList = sceneBuffer.triangleStrips;

                triStrips         = new VertexBuffer[triStripsList.Count];
                triStripsMirrored = new VertexBuffer[triStripsList.Count];
                triStripSizes     = new int[triStripsList.Count];
                for (int strip = 0; strip < triStripsList.Count; strip++)
                {
                    Vector3[] points = triStripsList[strip];
                    if (useClrAxis)
                    {
                        vFormat          = CustomVertex.PositionNormalColored.Format;
                        triStrips[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormalColored),
                                                            points.Length, device, Usage.None,
                                                            CustomVertex.PositionNormalColored.Format,
                                                            Pool.Managed);
                        triStripSizes[strip] = points.Length - 2;
                        CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])
                                                                     triStrips[strip].Lock(0, LockFlags.None);
                        float xStart = boundingBox.Centre.X - (boundingBox.Dimensions.X / 2.0f);
                        float yStart = boundingBox.Centre.Y - (boundingBox.Dimensions.Y / 2.0f);
                        float zStart = boundingBox.Centre.Z - (boundingBox.Dimensions.Z / 2.0f);
                        for (int v = 1; v < points.Length - 1; v++)
                        {
                            // V1
                            float xSteps = points[v - 1].X - xStart;
                            float ySteps = points[v - 1].Y - yStart;
                            float zSteps = points[v - 1].Z - zStart;

                            // blend XYZ colours
                            int A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            int R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            int G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            int B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v - 1].Color    = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v - 1].Position = points[v - 1];

                            // V2
                            xSteps = points[v].X - xStart;
                            ySteps = points[v].Y - yStart;
                            zSteps = points[v].Z - zStart;

                            // blend XYZ colours
                            A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v].Color    = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v].Position = points[v];

                            // V3
                            xSteps = points[v + 1].X - xStart;
                            ySteps = points[v + 1].Y - yStart;
                            zSteps = points[v + 1].Z - zStart;

                            // blend XYZ colours
                            A = (int)((xClrStep.X * xSteps) + (yClrStep.X * ySteps) + (zClrStep.X * zSteps));
                            R = (int)((xClrStep.Y * xSteps) + (yClrStep.Y * ySteps) + (zClrStep.Y * zSteps));
                            G = (int)((xClrStep.Z * xSteps) + (yClrStep.Z * ySteps) + (zClrStep.Z * zSteps));
                            B = (int)((xClrStep.W * xSteps) + (yClrStep.W * ySteps) + (zClrStep.W * zSteps));

                            verts[v + 1].Color    = Color.FromArgb(A, R, G, B).ToArgb();
                            verts[v + 1].Position = points[v + 1];


                            // normals
                            Vector3 v0 = verts[v + 1].Position;
                            Vector3 v1 = verts[v].Position;
                            Vector3 v2 = verts[v - 1].Position;

                            Vector3 e1 = v1 - v0, e2 = v2 - v0;
                            Vector3 vNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                            verts[v - 1].Normal += vNormal;
                            verts[v].Normal     += vNormal;
                            verts[v + 1].Normal += vNormal;

                            if (v != 1)
                            {
                                verts[v - 1].Normal *= 0.5f;
                            }
                            if (v == points.Length - 1)
                            {
                                verts[v].Normal *= 0.5f;
                            }
                        }

                        // mirror for double-sided
                        triStripsMirrored[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormalColored),
                                                                    points.Length, device, Usage.None,
                                                                    CustomVertex.PositionNormalColored.Format,
                                                                    Pool.Managed);
                        // clone
                        triStripsMirrored[strip].SetData(verts, 0, LockFlags.None);
                        triStrips[strip].Unlock();
                        // flip
                        FlipNormalsVB(triStripsMirrored[strip], CustomVertex.PositionNormalColored.Format,
                                      triStripSizes[strip]);
                    }
                    else
                    {
                        vFormat          = CustomVertex.PositionNormal.Format;
                        triStrips[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormal),
                                                            points.Length, device, Usage.None,
                                                            CustomVertex.PositionNormal.Format,
                                                            Pool.Managed);
                        triStripSizes[strip] = points.Length - 2;
                        CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])
                                                              triStrips[strip].Lock(0, LockFlags.None);
                        for (int v = 1; v < points.Length - 1; v++)
                        {
                            // positions
                            verts[v - 1].Position = points[v + 1];
                            verts[v].Position     = points[v];
                            verts[v + 1].Position = points[v - 1];

                            // normals
                            Vector3 v0 = verts[v - 1].Position;
                            Vector3 v1 = verts[v].Position;
                            Vector3 v2 = verts[v + 1].Position;

                            Vector3 e1 = v1 - v0, e2 = v2 - v0;
                            Vector3 vNormal = Vector3.Normalize(Vector3.Cross(e1, e2));

                            verts[v - 1].Normal += vNormal;
                            verts[v].Normal     += vNormal;
                            verts[v + 1].Normal += vNormal;

                            if (v != 1)
                            {
                                verts[v - 1].Normal *= 0.5f;
                            }
                            if (v == points.Length - 1)
                            {
                                verts[v].Normal *= 0.5f;
                            }
                        }


                        // mirror for double-sided
                        triStripsMirrored[strip] = new VertexBuffer(typeof(CustomVertex.PositionNormal),
                                                                    points.Length, device, Usage.None,
                                                                    CustomVertex.PositionNormal.Format,
                                                                    Pool.Managed);
                        // clone
                        triStripsMirrored[strip].SetData(verts, 0, LockFlags.None);
                        triStrips[strip].Unlock();
                        // flip
                        FlipNormalsVB(triStripsMirrored[strip], CustomVertex.PositionNormal.Format,
                                      triStripSizes[strip]);
                    }
                }
            }
            else if (shading.RibbonsShadingDesc.ShadingType == RibbonsShadingDesc.Shading.Edges)
            {
            }
        }
    /// <summary>
    /// Set the flexible vertex format
    /// </summary>
    public void SetVertexFormat(Device device, VertexFormats format)
    {
        Mesh pTempSysMemMesh = null;
        Mesh pTempLocalMesh  = null;

        if (systemMemoryMesh != null) {
            pTempSysMemMesh = systemMemoryMesh.Clone(MeshFlags.SystemMemory, format, device);
        }
        if (localMemoryMesh != null) {
            try {
                pTempLocalMesh = localMemoryMesh.Clone(0, format, device);
            }
            catch (Exception e) {
                pTempSysMemMesh.Dispose();
                pTempSysMemMesh = null;
                throw e;
            }
        }

        if (systemMemoryMesh != null)
            systemMemoryMesh.Dispose();
        systemMemoryMesh = null;

        if (localMemoryMesh != null)
            localMemoryMesh.Dispose();
        localMemoryMesh = null;

        // Clean up any vertex/index buffers
        DisposeLocalBuffers(true, true);

        if (pTempSysMemMesh != null) systemMemoryMesh = pTempSysMemMesh;
        if (pTempLocalMesh != null) localMemoryMesh  = pTempLocalMesh;

        // Compute normals in case the meshes have them
        if (systemMemoryMesh != null)
            systemMemoryMesh.ComputeNormals();
        if (localMemoryMesh != null)
            localMemoryMesh.ComputeNormals();
    }
Ejemplo n.º 31
0
 public SkyBox(Device device3D, string xFileName,
     Vector3 scal, VertexFormats vertexFormat)
     : base(device3D, xFileName, scal, vertexFormat)
 {
 }
Ejemplo n.º 32
0
 protected void SetupTextured()
 {
     m_TexturedVertices = new CustomVertex.PositionTextured[4];
     m_VertexFormat = CustomVertex.PositionTextured.Format;
     m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured),
         4, D3DDevice, Usage.Dynamic | Usage.WriteOnly,
         m_VertexFormat, Pool.Default);
     m_TexturedVertices[0].Position = new Vector3(m_x1, m_y1, m_z1);
     m_TexturedVertices[0].Tu = 0f;
     m_TexturedVertices[0].Tv = 0f;
     m_TexturedVertices[1].Position = new Vector3(m_x2, m_y2, m_z2);
     m_TexturedVertices[1].Tu = 0f;
     m_TexturedVertices[1].Tv = 1f;
     m_TexturedVertices[2].Position = new Vector3(m_x3, m_y3, m_z3);
     m_TexturedVertices[2].Tu = 1f;
     m_TexturedVertices[2].Tv = 1f;
     m_TexturedVertices[3].Position = new Vector3(m_x4, m_y4, m_z4);
     m_TexturedVertices[3].Tu = 1f;
     m_TexturedVertices[3].Tv = 0f;
     m_VertexBuffer.SetData(m_TexturedVertices, 0, LockFlags.None);
 }
Ejemplo n.º 33
0
        public void Render()
        {
            lock (_OSDLock)
            {
                // Store current settings so they can be restored when we are done
                VertexFormats vertexFormat = GUIGraphicsContext.DX9Device.VertexFormat;

                try
                {
                    if (_OSDTexture == null || _OSDTexture.Disposed)
                    {
                        return;
                    }

                    int wx = 0, wy = 0, wwidth = 0, wheight = 0;
                    if (GUIGraphicsContext.IsFullScreenVideo)
                    {
                        wheight = PlaneScene.DestRect.Height;
                        wwidth  = PlaneScene.DestRect.Width;

                        wx = GUIGraphicsContext.OverScanLeft;
                        wy = GUIGraphicsContext.OverScanTop;

                        if (PlaneScene.DestRect.X == 0 || PlaneScene.DestRect.Y == 0)
                        {
                            wx += PlaneScene.DestRect.X;
                            wy += PlaneScene.DestRect.Y;
                        }
                    }
                    else // Video overlay
                    {
                        wheight = GUIGraphicsContext.VideoWindow.Height;
                        wwidth  = GUIGraphicsContext.VideoWindow.Width;

                        wx = GUIGraphicsContext.VideoWindow.Right - (GUIGraphicsContext.VideoWindow.Width);
                        wy = GUIGraphicsContext.VideoWindow.Top;
                    }

                    DXNative.FontEngineSetRenderState((int)D3DRENDERSTATETYPE.D3DRS_ALPHABLENDENABLE, 1);
                    CreateVertexBuffer(wx, wy, wwidth, wheight);

                    // Make sure D3D objects haven't been disposed for some reason. This would cause
                    // an access violation on native side, causing Skin Engine to halt rendering
                    if (!_OSDTexture.Disposed && !_vertexBuffer.Disposed)
                    {
                        GUIGraphicsContext.DX9Device.SetStreamSource(0, _vertexBuffer, 0);
                        GUIGraphicsContext.DX9Device.SetTexture(0, _OSDTexture);
                        GUIGraphicsContext.DX9Device.VertexFormat = CustomVertex.TransformedTextured.Format;
                        GUIGraphicsContext.DX9Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
                    }
                    else
                    {
                        Log.Debug("OSD renderer: D3D resource was disposed! Not trying to render the texture");
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                try
                {
                    // Restore device settings
                    GUIGraphicsContext.DX9Device.SetTexture(0, null);
                    GUIGraphicsContext.DX9Device.VertexFormat = vertexFormat;
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }
        }
Ejemplo n.º 34
0
		/// <summary>
		/// Cone the mesh (and textures that it contains), 
		/// optionally converting the vertex and texture format.
		/// </summary>
		public AutoMesh Clone(Direct3d d3d, MeshFlags flags, VertexFormats vertexFormat,
						Format textureFormat, Usage usage, Pool pool)
		{
			// Clone the mesh vertex info
			Mesh mesh = mMesh.Clone(flags, vertexFormat, d3d.Dx);
			AutoMesh autoMesh = new AutoMesh(d3d, mesh);
			
			// Clone AutoMesh variables
			autoMesh.Tag = Tag;
			
			// Clone textures and materials
			if (mTextures.Length != 0)
			{
				// Clone materials
				autoMesh.mMaterials = new Material[mMaterials.Length];
				for (int i = 0;  i < mMaterials.Length;  i++)
					autoMesh.mMaterials[i] = mMaterials[i];
				
				// Clone textures
				autoMesh.mTextures = new AutoTexture[mTextures.Length];
				for (int i = 0;  i < mTextures.Length;  i++)
					if (mTextures[i] != null)
					{
						// Already cloned this texture?
						bool alreadyConvertedTexture = false;
						for (int j = 0;  j < i;  j++)
							if (mTextures[i] == mTextures[j])
							{
								alreadyConvertedTexture = true;
								autoMesh.mTextures[i] = autoMesh.mTextures[j];
								break;
							}
						// Clone new texture
						if (!alreadyConvertedTexture)
							autoMesh.mTextures[i] = mTextures[i].Clone(d3d, textureFormat, usage, pool);
					}
			}
			return autoMesh;			
		}
Ejemplo n.º 35
0
        public void BuildBuffers(Device device)
        {
            if (Geometry.Normals != null)
            {
                if (Geometry.VertexClrs != null)
                {
                    vFormat = CustomVertex.PositionNormalColored.Format;
                    vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalColored), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionNormalColored.Format, Pool.Managed);
                    CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])vBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.Vertices.Length; i++)
                    {
                        verts[i].Position = Geometry.Vertices[i];
                        verts[i].Normal   = Geometry.Normals[i];
                        verts[i].Color    = Geometry.VertexClrs[i];
                    }
                    vBuffer.Unlock();
                }
                else
                {
                    if (Geometry.TexCoords != null)
                    {
                        vFormat = CustomVertex.PositionNormalTextured.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), Geometry.Vertices.Length, device,
                                                   Usage.None, CustomVertex.PositionNormalTextured.Format, Pool.Managed);
                        CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Normal   = Geometry.Normals[i];
                            verts[i].Tu       = Geometry.TexCoords[i].X;
                            verts[i].Tv       = Geometry.TexCoords[i].Y;
                        }
                        vBuffer.Unlock();
                    }
                    else
                    {
                        vFormat = CustomVertex.PositionNormal.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), Geometry.Vertices.Length, device,
                                                   Usage.None, CustomVertex.PositionNormal.Format, Pool.Managed);
                        CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Normal   = Geometry.Normals[i];
                        }
                        vBuffer.Unlock();
                    }
                }
            }
            else
            {
                if (Geometry.VertexClrs != null)
                {
                    vFormat = CustomVertex.PositionColored.Format;
                    vBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), Geometry.Vertices.Length, device,
                                               Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
                    CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.Vertices.Length; i++)
                    {
                        verts[i].Position = Geometry.Vertices[i];
                        verts[i].Color    = Geometry.VertexClrs[i];
                    }
                    vBuffer.Unlock();
                }
                else
                {
                    if (Geometry.TexCoords != null)
                    {
                        vFormat = CustomVertex.PositionTextured.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), Geometry.Vertices.Length, device,
                                                   Usage.None, CustomVertex.PositionTextured.Format, Pool.Managed);
                        CustomVertex.PositionTextured[] verts = (CustomVertex.PositionTextured[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                            verts[i].Tu       = Geometry.TexCoords[i].X;
                            verts[i].Tv       = Geometry.TexCoords[i].Y;
                        }
                        vBuffer.Unlock();
                    }
                    else
                    {
                        vFormat = CustomVertex.PositionOnly.Format;
                        vBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), Geometry.Vertices.Length, device,
                                                   Usage.None, CustomVertex.PositionOnly.Format, Pool.Managed);
                        CustomVertex.PositionOnly[] verts = (CustomVertex.PositionOnly[])vBuffer.Lock(0, LockFlags.None);
                        for (int i = 0; i < Geometry.Vertices.Length; i++)
                        {
                            verts[i].Position = Geometry.Vertices[i];
                        }
                        vBuffer.Unlock();
                    }
                }
            }

            if (Geometry.PrimIndices != null)
            {
                if (MaterialIndices != null)
                {
                    matIBuffers = new IndexBuffer[MaterialIndices.Length];
                    int mibIdx = 0;
                    foreach (ABMaterialIndex matIndex in MaterialIndices)
                    {
                        if (matIndex.Material.Texture == null && matIndex.Material.TextureName != null)
                        {
                            if (File.Exists(matIndex.Material.TextureName))
                            {
                                matIndex.Material.Texture = TextureLoader.FromFile(device, matIndex.Material.TextureName);
                            }
                        }

                        int         numIndices = matIndex.Indices.Length * 3;
                        IndexBuffer buffer     = matIBuffers[mibIdx++] = new IndexBuffer(typeof(int), numIndices, device, Usage.None, Pool.Managed);
                        int[]       indices    = (int[])buffer.Lock(0, LockFlags.None);
                        int         iIdx       = 0;
                        for (int i = 0; i < matIndex.Indices.Length; i++)
                        {
                            int index = matIndex.Indices[i] * 3;
                            indices[iIdx++] = Geometry.PrimIndices[index];
                            indices[iIdx++] = Geometry.PrimIndices[index + 1];
                            indices[iIdx++] = Geometry.PrimIndices[index + 2];
                        }
                        buffer.Unlock();

                        switch (Geometry.PrimType)
                        {
                        case PrimitiveType.LineList:
                            matIndex.PrimCount = numIndices / 2;
                            break;

                        case PrimitiveType.LineStrip:
                            matIndex.PrimCount = numIndices - 1;
                            break;

                        case PrimitiveType.PointList:
                            matIndex.PrimCount = numIndices;
                            break;

                        case PrimitiveType.TriangleList:
                            matIndex.PrimCount = numIndices / 3;
                            break;
                        }
                    }
                }
                else
                {
                    iBuffer = new IndexBuffer(typeof(int), Geometry.PrimIndices.Length, device, Usage.None, Pool.Managed);
                    //iBuffer.SetData(Geometry.PrimIndices, 0, LockFlags.None);
                    int[] indices = (int[])iBuffer.Lock(0, LockFlags.None);
                    for (int i = 0; i < Geometry.PrimIndices.Length; i++)
                    {
                        indices[i] = Geometry.PrimIndices[i];
                    }
                    iBuffer.Unlock();

                    switch (Geometry.PrimType)
                    {
                    case PrimitiveType.LineList:
                        numPrimitives = Geometry.PrimIndices.Length / 2;
                        break;

                    case PrimitiveType.LineStrip:
                        numPrimitives = Geometry.PrimIndices.Length - 1;
                        break;

                    case PrimitiveType.PointList:
                        numPrimitives = Geometry.PrimIndices.Length;
                        break;

                    case PrimitiveType.TriangleList:
                        numPrimitives = Geometry.PrimIndices.Length / 3;
                        break;
                    }
                }
            }
            else
            {
                switch (Geometry.PrimType)
                {
                case PrimitiveType.LineList:
                    numPrimitives = Geometry.Vertices.Length / 2;
                    break;

                case PrimitiveType.LineStrip:
                    numPrimitives = Geometry.Vertices.Length - 1;
                    break;

                case PrimitiveType.PointList:
                    numPrimitives = Geometry.Vertices.Length;
                    break;

                case PrimitiveType.TriangleList:
                    numPrimitives = Geometry.Vertices.Length / 3;
                    break;
                }
            }
        }
Ejemplo n.º 36
0
 public static float ComputeBoundingSphere(GraphicsStream pointsFvf, int numVertices, VertexFormats vertexFormat, out Vector3 center)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 37
0
 public static void ComputeBoundingBox(GraphicsStream pointsFvf, int numVertices, VertexFormats vertexFormat, out Vector3 min, out Vector3 max)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 38
0
 public Mesh3D(Device device3D, string xFileName,
     Vector3 scal, VertexFormats vertexFormat)
 {
     _device3d = device3D;
     Scal = scal;
     _vertexFormat = vertexFormat;
     string currentDirectory = Application.StartupPath;
     try
     {
         ExtendedMaterial[] materialArray;
         //
         // Check DirectX |*.x file name
         //
         if (!xFileName.EndsWith(".x", true, new System.Globalization.CultureInfo("en-US")))
         {
             throw new Exception(@"Your File is not a DirectX file |*.x" + "\n\rPlease Enter correct FileName.");
         }
         if (!System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + @"\myMeshs\" + xFileName))
         {
             throw new NotFoundException("Given path was not found.");
         }
         Directory.SetCurrentDirectory(Application.StartupPath + @"\myMeshs\");
         mesh = Mesh.FromFile(xFileName, MeshFlags.Managed, device3D, out materialArray);
         //
         // Compute Normals
         //
         if (vertexFormat == CustomVertex.PositionNormalTextured.Format)
         {
             mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device3D);
             mesh.ComputeNormals();
         }
         else if (vertexFormat == CustomVertex.PositionNormalColored.Format)
         {
             mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalColored.Format, device3D);
             mesh.ComputeNormals();
         }
         //
         // set data
         //
         if (materialArray != null && materialArray.Length > 0)
         {
             MeshMaterials = new Material[materialArray.Length];
             MeshTexturs = new Texture[materialArray.Length];
             for (int i = 0; i < materialArray.Length; i++)
             {
                 MeshMaterials[i] = materialArray[i].Material3D;
                 MeshMaterials[i].Ambient = MeshMaterials[i].Diffuse;
                 if (!string.IsNullOrEmpty(materialArray[i].TextureFilename))
                 {
                     MeshTexturs[i] = TextureLoader.FromFile(device3D, materialArray[i].TextureFilename);
                 }
             }
         }
         ComputeRadius();
         optimaize(Math.Max(Math.Max(scal.X, scal.Y), scal.Z));
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show(ex.Message, ex.Source,
             System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
         hasError = true;
     }
     finally
     {
         Directory.SetCurrentDirectory(currentDirectory);
     }
 }
Ejemplo n.º 39
0
 public VertexBuffer(Device device, int sizeOfBufferInBytes, Usage usage, VertexFormats vertexFormat, Pool pool) : base((IntPtr)null)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 40
0
        public override void Render(DrawArgs drawArgs)
        {
            if (!isOn)
            {
                return;
            }

            if (!isInitialized)
            {
                Initialize(drawArgs);
            }

            m_pointSprites.Clear();

            int       closestIconDistanceSquared = int.MaxValue;
            PointIcon closestIcon = null;

            // build list of all points in view
            foreach (PointIcon point in m_points.Values)
            {
                try
                {
                    // don't bother to do anything else if we aren't even in view
                    if (drawArgs.WorldCamera.ViewFrustum.ContainsPoint(point.Position))
                    {
                        Vector3 translationVector = new Vector3(
                            (float)(point.PositionD.X - drawArgs.WorldCamera.ReferenceCenter.X),
                            (float)(point.PositionD.Y - drawArgs.WorldCamera.ReferenceCenter.Y),
                            (float)(point.PositionD.Z - drawArgs.WorldCamera.ReferenceCenter.Z));

                        Vector3 projectedPoint = drawArgs.WorldCamera.Project(translationVector);

                        // check if inside bounding box of icon
                        int dx = DrawArgs.LastMousePosition.X - (int)projectedPoint.X;
                        int dy = DrawArgs.LastMousePosition.Y - (int)projectedPoint.Y;
                        if (SelectionRectangle.Contains(dx, dy))
                        {
                            // Mouse is over, check whether this icon is closest
                            int distanceSquared = dx * dx + dy * dy;
                            if (distanceSquared < closestIconDistanceSquared)
                            {
                                closestIconDistanceSquared = distanceSquared;
                                closestIcon = point;
                            }
                        }

                        PointSpriteVertex pv = new PointSpriteVertex(translationVector.X, translationVector.Y, translationVector.Z, point.Size, point.Color.ToArgb());
                        m_pointSprites.Add(pv);
                    }
                }
                catch
                {
                }
                finally
                {
                }
            }

            // render point sprites if any in the list
            try
            {
                if (m_pointSprites.Count > 0)
                {
                    // save device state
                    Texture       origTexture           = drawArgs.device.GetTexture(0);
                    VertexFormats origVertexFormat      = drawArgs.device.VertexFormat;
                    float         origPointScaleA       = drawArgs.device.RenderState.PointScaleA;
                    float         origPointScaleB       = drawArgs.device.RenderState.PointScaleB;
                    float         origPointScaleC       = drawArgs.device.RenderState.PointScaleC;
                    bool          origPointSpriteEnable = drawArgs.device.RenderState.PointSpriteEnable;
                    bool          origPointScaleEnable  = drawArgs.device.RenderState.PointScaleEnable;
                    Blend         origSourceBlend       = drawArgs.device.RenderState.SourceBlend;
                    Blend         origDestBlend         = drawArgs.device.RenderState.DestinationBlend;

                    // set device to do point sprites
                    drawArgs.device.SetTexture(0, m_pointTexture.Texture);
                    drawArgs.device.VertexFormat                  = VertexFormats.Position | VertexFormats.PointSize | VertexFormats.Diffuse;
                    drawArgs.device.RenderState.PointScaleA       = 1f;
                    drawArgs.device.RenderState.PointScaleB       = 0f;
                    drawArgs.device.RenderState.PointScaleC       = 0f;
                    drawArgs.device.RenderState.PointSpriteEnable = true;
                    drawArgs.device.RenderState.PointScaleEnable  = true;
                    //drawArgs.device.RenderState.SourceBlend = Blend.One;
                    //drawArgs.device.RenderState.DestinationBlend = Blend.BlendFactor;

                    drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorOperation, (int)TextureOperation.Modulate);
                    drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
                    drawArgs.device.SetTextureStageState(0, TextureStageStates.ColorArgument2, (int)TextureArgument.Diffuse);

                    // Draw all visible points
                    drawArgs.device.DrawUserPrimitives(PrimitiveType.PointList, m_pointSprites.Count, m_pointSprites.ToArray());

                    // Draw label and description of mouseover point
                    if (closestIcon != null)
                    {
                    }

                    // restore device state
                    drawArgs.device.SetTexture(0, origTexture);
                    drawArgs.device.VertexFormat                  = origVertexFormat;
                    drawArgs.device.RenderState.PointScaleA       = origPointScaleA;
                    drawArgs.device.RenderState.PointScaleB       = origPointScaleB;
                    drawArgs.device.RenderState.PointScaleC       = origPointScaleC;
                    drawArgs.device.RenderState.PointSpriteEnable = origPointSpriteEnable;
                    drawArgs.device.RenderState.PointScaleEnable  = origPointScaleEnable;
                    drawArgs.device.RenderState.SourceBlend       = origSourceBlend;
                    drawArgs.device.RenderState.DestinationBlend  = origDestBlend;
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 41
0
        protected void SetupColored()
        {
            m_ColoredVertices = new CustomVertex.PositionColored[4];
            m_VertexFormat = CustomVertex.PositionColored.Format;
            m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
                4, device, Usage.Dynamic | Usage.WriteOnly,
                m_VertexFormat, Pool.Default);

            m_ColoredVertices[0].Position = new Vector3(m_x1, m_y1, m_z1);
            m_ColoredVertices[0].Color = m_Color1.ToArgb();
            m_ColoredVertices[1].Position = new Vector3(m_x2, m_y2, m_z2);
            m_ColoredVertices[1].Color = m_Color2.ToArgb();
            m_ColoredVertices[2].Position = new Vector3(m_x3, m_y3, m_z3);
            m_ColoredVertices[2].Color = m_Color3.ToArgb();
            m_ColoredVertices[3].Position = new Vector3(m_x4, m_y4, m_z4);
            m_ColoredVertices[3].Color = m_Color4.ToArgb();
            m_VertexBuffer.SetData(m_ColoredVertices, 0, LockFlags.None);
        }
Ejemplo n.º 42
0
 public Mesh Clone(MeshFlags options, VertexFormats vertexFormat, Device device)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 43
0
 public DataFields(VertexFormats format, string usage)
 {
     this.Format = format;
     this.Usage = usage;
 }
Ejemplo n.º 44
0
 public static float ComputeBoundingSphere(Array pointsFvf, VertexFormats vertexFormat, out Vector3 center)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 45
0
 public static void FlipNormalsVB(VertexBuffer vb, VertexFormats vFormat, int size)
 {
     if (vFormat == CustomVertex.PositionNormal.Format)
     {
         CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])
                                               vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else if (vFormat == CustomVertex.PositionNormalColored.Format)
     {
         CustomVertex.PositionNormalColored[] verts = (CustomVertex.PositionNormalColored[])
                                                      vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else if (vFormat == CustomVertex.PositionNormalTextured.Format)
     {
         CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])
                                                       vb.Lock(0, LockFlags.None);
         for (int v = 0; v < verts.Length; v++)
         {
             verts[v].Normal = new Vector3(-verts[v].Normal.X, -verts[v].Normal.Y, -verts[v].Normal.Z);
         }
         vb.Unlock();
     }
     else
         throw new Exception("Unsupported format to flip normals with");
 }
Ejemplo n.º 46
0
 public static void ComputeBoundingBox(Array pointsFvf, VertexFormats vertexFormat, out Vector3 min, out Vector3 max)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 47
0
		/// <summary>
		/// Setup the form by loading the mesh
		/// </summary>
		private void FormViewMesh_Shown(object sender, EventArgs e)
		{
			// ToDo: Show a message form while loading			
			AutoMesh autoMesh = null;

			// Load the mesh (or fail and exit)
			try
			{
				autoMesh = AutoMesh.LoadFromXFile(mFileName, MeshFlags.SystemMemory, d3dModel);									
			}
			catch 
			{
				// Dispose whatever we have
				try { autoMesh.Dispose(); } catch { }
				autoMesh = null;
				
				MessageBox.Show(this, "Error loading mesh");
				Hide();
				return;
			}
			
			// Detect unsupported texture formats
			VertexFormats format = autoMesh.M.VertexFormat;
			if ( (format & VertexFormats.TextureCountMask) != VertexFormats.Texture0
					&& (format & VertexFormats.Texture1) != VertexFormats.Texture1 )
			{
				MessageBox.Show(this, "Multiple textures not supported");
				autoMesh.Dispose();
				Hide();
				return;
			}

			// Unsupported vertex formats
			VertexFormats unsupported = VertexFormats.Specular
										| VertexFormats.LastBetaD3DColor
										| VertexFormats.LastBetaUByte4
										| VertexFormats.PointSize
										| VertexFormats.Transformed;
			if ( (int)(format & unsupported) != 0)
			{
				MessageBox.Show(this, "Unsupported vertex format");
				autoMesh.Dispose();
				Hide();
				return;
			}
			
			// No position (sometimes this happens)
			if ( (format & VertexFormats.Position) != VertexFormats.Position )
			{
				MessageBox.Show(this, "Unsupported vertex format (no position detected!)");
				autoMesh.Dispose();
				Hide();
				return;
			}
			
			// Clone to new format, using 32 bit pixel format.
			try
			{
				mMesh = autoMesh.Clone(d3dModel, MeshFlags.Managed, autoMesh.M.VertexFormat,
										Format.A8R8G8B8, Usage.AutoGenerateMipMap, Pool.Managed);
			}
			catch
			{
				MessageBox.Show(this, "Error cloning mesh");
				try { autoMesh.Dispose(); } catch  { }
				try { mMesh.Dispose(); } catch { }
				Hide();
				return;
			}
			
			autoMesh.Dispose();
			autoMesh = null;
			
			// Display mesh info
			DisplayMeshInfo();						
		}