Esempio n. 1
0
        public XnaHardwareVertexBuffer(HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration,
                                       int numVertices, BufferUsage usage, GraphicsDevice dev, bool useSystemMemory,
                                       bool useShadowBuffer)
            : base(manager, vertexDeclaration, numVertices, usage, useSystemMemory, useShadowBuffer)
        {
            _device = dev;
            if (!(vertexDeclaration is XnaVertexDeclaration))
            {
                throw new AxiomException(
                          "Invalid VertexDeclaration supplied, must be created by HardwareBufferManager.CreateVertexDeclaration()");
            }
            if (usage == BufferUsage.Dynamic || usage == BufferUsage.DynamicWriteOnly)
            {
                _buffer = new DynamicVertexBuffer(_device,
                                                  ((XnaVertexDeclaration)vertexDeclaration).XFGVertexDeclaration,
                                                  numVertices, XnaHelper.Convert(usage));
            }
            else
            {
                _buffer = new VertexBuffer(_device, ((XnaVertexDeclaration)vertexDeclaration).XFGVertexDeclaration,
                                           numVertices, XnaHelper.Convert(usage));
            }

            _bufferBytes = new byte[vertexDeclaration.GetVertexSize() * numVertices];
            _bufferBytes.Initialize();
        }
        public XnaHardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration,
                                        int numVertices, BufferUsage usage, GraphicsDevice dev, bool useSystemMemory,
                                        bool useShadowBuffer )
            : base( manager, vertexDeclaration, numVertices, usage, useSystemMemory, useShadowBuffer )
        {
            _device = dev;
            if ( !( vertexDeclaration is XnaVertexDeclaration ) )
            {
                throw new AxiomException(
                    "Invalid VertexDeclaration supplied, must be created by HardwareBufferManager.CreateVertexDeclaration()" );
            }
            if ( usage == BufferUsage.Dynamic || usage == BufferUsage.DynamicWriteOnly )
            {
                _buffer = new DynamicVertexBuffer( _device,
                                                   ( (XnaVertexDeclaration)vertexDeclaration ).XFGVertexDeclaration,
                                                   numVertices, XnaHelper.Convert( usage ) );
            }
            else
            {
                _buffer = new VertexBuffer( _device, ( (XnaVertexDeclaration)vertexDeclaration ).XFGVertexDeclaration,
                                            numVertices, XnaHelper.Convert( usage ) );
            }

            _bufferBytes = new byte[vertexDeclaration.GetVertexSize()*numVertices];
            _bufferBytes.Initialize();
        }
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration vertexDeclaration, int numVerts,
		                                                         BufferUsage usage, bool useShadowBuffer )
		{
			Contract.Requires( numVerts > 0 );

#if AXIOM_D3D_MANAGE_BUFFERS
			// Override shadow buffer setting; managed buffers are automatically
			// backed by system memory
			// Don't override shadow buffer if discardable, since then we use
			// unmanaged buffers for speed (avoids write-through overhead)
			if ( useShadowBuffer && ( usage & BufferUsage.Discardable ) == 0 )
			{
				useShadowBuffer = false;
				// Also drop any WRITE_ONLY so we can read direct
				if ( usage == BufferUsage.DynamicWriteOnly )
				{
					usage = BufferUsage.Dynamic;
				}

				else if ( usage == BufferUsage.StaticWriteOnly )
				{
					usage = BufferUsage.Static;
				}
			}
#endif
			var vbuf = new D3D9HardwareVertexBuffer( this, vertexDeclaration, numVerts, usage, false, useShadowBuffer );
			lock ( VertexBuffersMutex )
			{
				vertexBuffers.Add( vbuf );
			}

			return vbuf;
		}
        public override HardwareVertexBuffer CreateVertexBuffer(VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer)
        {
            var buffer = new XnaHardwareVertexBuffer(this, vertexDeclaration, numVerts, usage, _device, false, useShadowBuffer);

            lock (VertexBuffersMutex)
                vertexBuffers.Add(buffer);
            return(buffer);
        }
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer )
		{
			var vertexBuffer = new GLESHardwareVertexBuffer( this, vertexDeclaration, numVerts, usage, true );
			lock ( VertexBuffersMutex )
			{
				vertexBuffers.Add( vertexBuffer );
			}
			return vertexBuffer;
		}
Esempio n. 6
0
		public void Define( Array controlPointArray, VertexDeclaration declaration, int width, int height,
		                    int uMaxSubdivisionLevel, int vMaxSubdivisionLevel, VisibleSide visibleSide, BufferUsage vbUsage,
		                    BufferUsage ibUsage, bool vbUseShadow, bool ibUseShadow )
		{
			VertexBufferUsage = vbUsage;
			UseVertexShadowBuffer = vbUseShadow;
			IndexBufferUsage = ibUsage;
			UseIndexShadowBuffer = ibUseShadow;

			// Init patch builder
			// define the surface
			// NB clone the declaration to make it independent
			this.vertexDeclaration = (VertexDeclaration)declaration.Clone();
			this.patchSurface.DefineSurface( controlPointArray, this.vertexDeclaration, width, height, PatchSurfaceType.Bezier,
			                                 uMaxSubdivisionLevel, vMaxSubdivisionLevel, visibleSide );
		}
        static TerrainPage()
        {
            terrainVertexDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration();

            // set up the vertex declaration
            int vDecOffset = 0;
            terrainVertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float3, VertexElementSemantic.Position);
            vDecOffset += VertexElement.GetTypeSize(VertexElementType.Float3);

            terrainVertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float3, VertexElementSemantic.Normal);
            vDecOffset += VertexElement.GetTypeSize(VertexElementType.Float3);

            terrainVertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float2, VertexElementSemantic.TexCoords);

            vertexSize = terrainVertexDeclaration.GetVertexSize(0) / 4;
        }
        // ------------------------------------
        // ------------------------------------
        /// <summary>
        ///     Creates a new PatchMesh.
        /// </summary>
        /// <remarks>
        ///     As defined in <see cref="MeshManager.CreateBezierPatch" />.
        /// </remarks>
        public PatchMesh(string name, System.Array controlPointBuffer, VertexDeclaration declaration,
            int width, int height, int uMaxSubdivisionLevel, int vMaxSubdivisionLevel, VisibleSide visibleSide,
            BufferUsage vbUsage, BufferUsage ibUsage, bool vbUseShadow, bool ibUseShadow)
            : base(name)
        {
            vertexBufferUsage = vbUsage;
            useVertexShadowBuffer = vbUseShadow;
            indexBufferUsage = ibUsage;
            useIndexShadowBuffer = ibUseShadow;

            // Init patch builder
            // define the surface
            // NB clone the declaration to make it independent
            vertexDeclaration = (VertexDeclaration)declaration.Clone();
            patchSurface.DefineSurface(controlPointBuffer, vertexDeclaration, width, height,
                PatchSurfaceType.Bezier, uMaxSubdivisionLevel, vMaxSubdivisionLevel, visibleSide);
        }
Esempio n. 9
0
		public GLHardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration, int numVertices, BufferUsage usage, bool useShadowBuffer )
			: base( manager, vertexDeclaration, numVertices, usage, false, useShadowBuffer )
		{
			bufferID = 0;

			Gl.glGenBuffersARB( 1, out bufferID );

			if ( bufferID == 0 )
			{
				throw new Exception( "Cannot create GL vertex buffer" );
			}

			Gl.glBindBufferARB( Gl.GL_ARRAY_BUFFER_ARB, bufferID );

			// initialize this buffer.  we dont have data yet tho
			Gl.glBufferDataARB( Gl.GL_ARRAY_BUFFER_ARB, new IntPtr( sizeInBytes ), IntPtr.Zero, GLHelper.ConvertEnum( usage ) ); // TAO 2.0
			//Gl.glBufferDataARB( Gl.GL_ARRAY_BUFFER_ARB, sizeInBytes, IntPtr.Zero, GLHelper.ConvertEnum( usage ) );
		}
Esempio n. 10
0
		public HardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration, int numVertices,
		                             BufferUsage usage, bool useSystemMemory, bool useShadowBuffer )
			: base( usage, useSystemMemory, useShadowBuffer )
		{
			this.vertexDeclaration = vertexDeclaration;
			this.numVertices = numVertices;
			this.Manager = manager;

			// calculate the size in bytes of this buffer
			sizeInBytes = vertexDeclaration.GetVertexSize()*numVertices;

			// create a shadow buffer if required
			if ( useShadowBuffer )
			{
				shadowBuffer = new DefaultHardwareVertexBuffer( this.Manager, vertexDeclaration, numVertices, BufferUsage.Dynamic );
			}

			this.useCount = 0;
		}
		public GLESHardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration, int numVertices, BufferUsage usage, bool useShadowBuffer )
			: base( manager, vertexDeclaration, numVertices, usage, false, useShadowBuffer )
		{
			if ( !useShadowBuffer )
			{
				throw new AxiomException( "Only supported with shadowBuffer" );
			}
			
			var buffers = new int[ 1 ];
			GL.GenBuffers( 1, buffers );
			GLESConfig.GlCheckError( this );
			this._bufferID = buffers[ 0 ];
			
			if ( this._bufferID == 0 )
			{
				throw new AxiomException( "Cannot create GL ES vertex buffer" );
			}
			
			( Root.Instance.RenderSystem as GLESRenderSystem ).BindGLBuffer( GLenum.ArrayBuffer, this._bufferID );
			GL.BufferData( GLenum.ArrayBuffer, new IntPtr( sizeInBytes ), IntPtr.Zero, GLESHardwareBufferManager.GetGLUsage( usage ) );
			GLESConfig.GlCheckError( this );
		}
Esempio n. 12
0
		public override HardwareVertexBuffer CreateVertexBuffer(  VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer = false )
Esempio n. 13
0
		/// <summary>
		///     Creates a Bezier patch based on an array of control vertices.
		/// </summary>
		public PatchMesh CreateBezierPatch( string name, string group, Array controlPointBuffer, VertexDeclaration declaration,
			int width, int height, int uMaxSubdivisionLevel, int vMaxSubdivisionLevel, VisibleSide visibleSide,
			BufferUsage vbUsage, BufferUsage ibUsage, bool vbUseShadow, bool ibUseShadow )
		{
			if ( width < 3 || height < 3 )
			{
				throw new Exception( "Bezier patch requires at least 3x3 control points." );
			}
			PatchMesh mesh = (PatchMesh)this[ name ];

			if ( mesh != null )
			{
				throw new AxiomException( "A mesh with the name {0} already exists!", name );
			}

			mesh = new PatchMesh( this, name, nextHandle, group );

			mesh.Define( controlPointBuffer, declaration, width, height, uMaxSubdivisionLevel, vMaxSubdivisionLevel, visibleSide, vbUsage, ibUsage, vbUseShadow, ibUseShadow );

			mesh.Load();

			_add( mesh );

			return mesh;
		}
 /// <summary>
 ///     Sets up the surface by defining it's control points, type and initial subdivision level.
 /// </summary>
 /// <remarks>
 ///     This method initialises the surface by passing it a set of control points. The type of curves to be used
 ///     are also defined here, although the only supported option currently is a bezier patch. You can also
 ///     specify a global subdivision level here if you like, although it is recommended that the parameter
 ///     is left as AUTO_LEVEL, which means the system decides how much subdivision is required (based on the
 ///     curvature of the surface).
 /// </remarks>
 /// <param name="controlPoints">
 ///     A pointer to a buffer containing the vertex data which defines control points 
 ///     of the curves rather than actual vertices. Note that you are expected to provide not
 ///     just position information, but potentially normals and texture coordinates too. The
 ///     format of the buffer is defined in the VertexDeclaration parameter.
 /// </param>
 /// <param name="decl">
 ///     VertexDeclaration describing the contents of the buffer. 
 ///     Note this declaration must _only_ draw on buffer source 0!
 /// </param>
 /// <param name="width">Specifies the width of the patch in control points.</param>
 /// <param name="height">Specifies the height of the patch in control points.</param>
 /// <param name="type">The type of surface.</param>
 /// <param name="uMaxSubdivision">
 ///     If you want to manually set the top level of subdivision, 
 ///     do it here, otherwise let the system decide.
 /// </param>
 /// <param name="vMaxSubdivision">
 ///     If you want to manually set the top level of subdivision, 
 ///     do it here, otherwise let the system decide.
 /// </param>
 /// <param name="side">Determines which side of the patch (or both) triangles are generated for.</param>
 public void DefineSurface(System.Array controlPoints, VertexDeclaration decl, int width, int height)
 {
     DefineSurface(controlPoints, decl, width, height, PatchSurfaceType.Bezier, AUTO_LEVEL, AUTO_LEVEL, VisibleSide.Front);
 }
 public GLDefaultHardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration, int numVertices, BufferUsage usage, bool useSystemMemory, bool useShadowBuffer ) : base( manager, vertexDeclaration, numVertices, usage, useSystemMemory, useShadowBuffer )
 {
 }
Esempio n. 16
0
		protected void WriteGeometryVertexDeclaration( BinaryWriter writer, VertexDeclaration vertexDeclaration )
		{
			var start_offset = writer.Seek( 0, SeekOrigin.Current );
			WriteChunk( writer, MeshChunkID.GeometryVertexDeclaration, 0 );

			for ( var i = 0; i < vertexDeclaration.ElementCount; ++i )
			{
				WriteGeometryVertexElement( writer, vertexDeclaration.GetElement( i ) );
			}

			var end_offset = writer.Seek( 0, SeekOrigin.Current );
			writer.Seek( (int)start_offset, SeekOrigin.Begin );
			WriteChunk( writer, MeshChunkID.GeometryVertexDeclaration, (int)( end_offset - start_offset ) );
			writer.Seek( (int)end_offset, SeekOrigin.Begin );
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="vertexSize"></param>
		/// <param name="numVerts"></param>
		/// <param name="usage"></param>
		/// <param name="useShadowBuffer"></param>
		/// <returns></returns>
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration declaration, int numVerts, BufferUsage usage, bool useShadowBuffer )
		{
			// always use shadowBuffer
			GLESHardwareVertexBuffer buf = new GLESHardwareVertexBuffer( this, declaration, numVerts, usage, true );
			lock ( _vertexBufferLock )
			{
				vertexBuffers.Add( buf );
			}
			return buf;
		}
Esempio n. 18
0
 public override HardwareVertexBuffer CreateVertexBuffer(VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer = false)
Esempio n. 19
0
		public override void DestroyVertexDeclaration( VertexDeclaration decl )
		{
			_baseInstance.DestroyVertexDeclaration( decl );
		}
		public DefaultHardwareVertexBuffer( HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration,
											int numVertices, BufferUsage usage )
			: base( manager, vertexDeclaration, numVertices, usage, true, false ) // always software, never shadowed
		{
			_mpData = new byte[ base.sizeInBytes ];
		}
		protected override void DestroyVertexDeclarationImpl( VertexDeclaration decl )
		{
			decl.SafeDispose();
		}
        /// <summary>
        ///     Creates a Bezier patch based on an array of control vertices.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="controlPointBuffer"></param>
        /// <param name="declaration"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="uMaxSubdivisionLevel"></param>
        /// <param name="vMaxSubdivisionLevel"></param>
        /// <param name="visibleSide"></param>
        /// <param name="vbUsage"></param>
        /// <param name="ibUsage"></param>
        /// <param name="vbUseShadow"></param>
        /// <param name="ibUseShadow"></param>
        /// <returns></returns>
        public PatchMesh CreateBezierPatch(string name, System.Array controlPointBuffer, VertexDeclaration declaration,
            int width, int height, int uMaxSubdivisionLevel, int vMaxSubdivisionLevel, VisibleSide visibleSide,
            BufferUsage vbUsage, BufferUsage ibUsage, bool vbUseShadow, bool ibUseShadow)
        {
            PatchMesh mesh = (PatchMesh)GetByName(name);

            if(mesh != null) {
                throw new AxiomException("A mesh with the name {0} already exists!", name);
            }

            mesh = new PatchMesh(name, controlPointBuffer, declaration, width, height,
                uMaxSubdivisionLevel, vMaxSubdivisionLevel, visibleSide, vbUsage, ibUsage, vbUseShadow, ibUseShadow);

            mesh.IsManuallyDefined = true;

            base.Load(mesh, 0);

            return mesh;
        }
        // Just override the mandatory create scene method
        protected override void CreateScene()
        {
            // Set ambient light
            scene.AmbientLight = new ColorEx(0.2f, 0.2f, 0.2f);

            // Create point light
            Light light = scene.CreateLight("MainLight");

            // Accept default settings: point light, white diffuse, just set position.
            // I could attach the light to a SceneNode if I wanted it to move automatically with
            // other objects, but I don't.
            light.Type = LightType.Directional;
            light.Direction = new Vector3(-0.5f, -0.5f, 0);

            // Create patch with positions, normals, and 1 set of texcoords
            patchDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration();
            patchDeclaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position);
            patchDeclaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal);
            patchDeclaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0);

            // Patch data
            PatchVertex[] patchVertices = new PatchVertex[9];

            patchVertices[0].X = -500; patchVertices[0].Y = 200; patchVertices[0].Z = -500;
            patchVertices[0].Nx = -0.5f; patchVertices[0].Ny = 0.5f; patchVertices[0].Nz = 0;
            patchVertices[0].U = 0; patchVertices[0].V = 0;

            patchVertices[1].X = 0; patchVertices[1].Y = 500; patchVertices[1].Z = -750;
            patchVertices[1].Nx = 0; patchVertices[1].Ny = 0.5f; patchVertices[1].Nz = 0;
            patchVertices[1].U = 0.5f; patchVertices[1].V = 0;

            patchVertices[2].X = 500; patchVertices[2].Y = 1000; patchVertices[2].Z = -500;
            patchVertices[2].Nx = 0.5f; patchVertices[2].Ny = 0.5f; patchVertices[2].Nz = 0;
            patchVertices[2].U = 1; patchVertices[2].V = 0;

            patchVertices[3].X = -500; patchVertices[3].Y = 0; patchVertices[3].Z = 0;
            patchVertices[3].Nx = -0.5f; patchVertices[3].Ny = 0.5f; patchVertices[3].Nz = 0;
            patchVertices[3].U = 0; patchVertices[3].V = 0.5f;

            patchVertices[4].X = 0; patchVertices[4].Y = 500; patchVertices[4].Z = 0;
            patchVertices[4].Nx = 0; patchVertices[4].Ny = 0.5f; patchVertices[4].Nz = 0;
            patchVertices[4].U = 0.5f; patchVertices[4].V = 0.5f;

            patchVertices[5].X = 500; patchVertices[5].Y = -50; patchVertices[5].Z = 0;
            patchVertices[5].Nx = 0.5f; patchVertices[5].Ny = 0.5f; patchVertices[5].Nz = 0;
            patchVertices[5].U = 1; patchVertices[5].V = 0.5f;

            patchVertices[6].X = -500; patchVertices[6].Y = 0; patchVertices[6].Z = 500;
            patchVertices[6].Nx = -0.5f; patchVertices[6].Ny = 0.5f; patchVertices[6].Nz = 0;
            patchVertices[6].U = 0; patchVertices[6].V = 1;

            patchVertices[7].X = 0; patchVertices[7].Y = 500; patchVertices[7].Z = 500;
            patchVertices[7].Nx = 0; patchVertices[7].Ny = 0.5f; patchVertices[7].Nz = 0;
            patchVertices[7].U = 0.5f; patchVertices[7].V = 1;

            patchVertices[8].X = 500; patchVertices[8].Y = 200; patchVertices[8].Z = 800;
            patchVertices[8].Nx = 0.5f; patchVertices[8].Ny = 0.5f; patchVertices[8].Nz = 0;
            patchVertices[8].U = 1; patchVertices[8].V = 1;

            patch = MeshManager.Instance.CreateBezierPatch("Bezier1", patchVertices, patchDeclaration, 3, 3, 5, 5, VisibleSide.Both, BufferUsage.StaticWriteOnly, BufferUsage.DynamicWriteOnly, true, true);

            // Start patch a 0 detail
            patch.SetSubdivision(0);

            // Create entity based on patch
            patchEntity = scene.CreateEntity("Entity1", "Bezier1");

            Material material = (Material)MaterialManager.Instance.Create("TextMat");
            material.GetTechnique(0).GetPass(0).CreateTextureUnitState("BumpyMetal.jpg");
            patchEntity.MaterialName = "TextMat";

            // Attach the entity to the root of the scene
            scene.RootSceneNode.AttachObject(patchEntity);

            camera.Position = new Vector3(500, 500, 1500);
            camera.LookAt(new Vector3(0, 200, -300));
        }
Esempio n. 24
0
        public override HardwareVertexBuffer CreateVertexBuffer(VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer)
#endif
        {
            return(this._baseInstance.CreateVertexBuffer(vertexDeclaration, numVerts, usage, useShadowBuffer));
        }
Esempio n. 25
0
        public void InitQuake3Patches(Quake3Level q3lvl, VertexDeclaration decl)
        {
            int face;

            patchVertexCount = 0;
            patchIndexCount = 0;

            // We're just building the patch here to get a hold on the size of the mesh
            // although we'll reuse this information later
            face = q3lvl.NumFaces;

            while(face-- > 0)
            {
                InternalBspFace src = q3lvl.Faces[face];

                if(src.type == BspFaceType.Patch)
                {
                    // Seems to be some crap in the Q3 level where vertex count = 0 or num control points = 0?
                    if((src.vertCount == 0) || (src.meshCtrl[0] == 0))
                        continue;

                    PatchSurface ps = new PatchSurface();

                    // Set up control points & format.
                    // Reuse the vertex declaration.
                    // Copy control points into a buffer so we can convert their format.
                    BspVertex[] controlPoints = new BspVertex[src.vertCount];
                    TextureLightMap texLightMap;

                    for(int v = 0; v < src.vertCount; v++)
                        QuakeVertexToBspVertex(q3lvl.Vertices[src.vertStart + v], out controlPoints[v], out texLightMap);

                    // Define the surface, but don't build it yet (no vertex / index buffer)
                    ps.DefineSurface(
                        controlPoints,
                        decl,
                        src.meshCtrl[0],
                        src.meshCtrl[1]
                        );

                    // Get stats
                    patchVertexCount += ps.RequiredVertexCount;
                    patchIndexCount += ps.RequiredIndexCount;

                    // Save the surface for later
                    patches.Add(face, ps);
                }
            }
        }
Esempio n. 26
0
 public override void DestroyVertexDeclaration(VertexDeclaration decl)
 {
     this._baseInstance.DestroyVertexDeclaration(decl);
 }
		/// <summary>
		/// </summary>
		/// <param name="vertexSize"> </param>
		/// <param name="numVertices"> </param>
		/// <param name="usage"> </param>
		public GLDefaultHardwareVertexBuffer(VertexDeclaration declaration, int numVertices, BufferUsage usage)
			: base( null, declaration, numVertices, usage, true, false )
		{
			this._data = new byte[ declaration.GetVertexSize() * numVertices ];
			this._dataPtr = BufferBase.Wrap( this._data );
		}
Esempio n. 28
0
		protected override void SetupContent()
		{
			ResourceGroupManager.Instance.InitializeAllResourceGroups();
			// setup some basic lighting for our scene
			SceneManager.AmbientLight = new ColorEx( 0.5f, 0.5f, 0.5f );
			SceneManager.CreateLight( "BezierLight" ).Position = new Vector3( 100, 100, 100 );

			// define the control point vertices for our patch
			// Patch data
			PatchVertex[] patchVertices = new PatchVertex[ 9 ];

			patchVertices[ 0 ].X = -500;
			patchVertices[ 0 ].Y = 200;
			patchVertices[ 0 ].Z = -500;
			patchVertices[ 0 ].Nx = -0.5f;
			patchVertices[ 0 ].Ny = 0.5f;
			patchVertices[ 0 ].Nz = 0;
			patchVertices[ 0 ].U = 0;
			patchVertices[ 0 ].V = 0;

			patchVertices[ 1 ].X = 0;
			patchVertices[ 1 ].Y = 500;
			patchVertices[ 1 ].Z = -750;
			patchVertices[ 1 ].Nx = 0;
			patchVertices[ 1 ].Ny = 0.5f;
			patchVertices[ 1 ].Nz = 0;
			patchVertices[ 1 ].U = 0.5f;
			patchVertices[ 1 ].V = 0;

			patchVertices[ 2 ].X = 500;
			patchVertices[ 2 ].Y = 1000;
			patchVertices[ 2 ].Z = -500;
			patchVertices[ 2 ].Nx = 0.5f;
			patchVertices[ 2 ].Ny = 0.5f;
			patchVertices[ 2 ].Nz = 0;
			patchVertices[ 2 ].U = 1;
			patchVertices[ 2 ].V = 0;

			patchVertices[ 3 ].X = -500;
			patchVertices[ 3 ].Y = 0;
			patchVertices[ 3 ].Z = 0;
			patchVertices[ 3 ].Nx = -0.5f;
			patchVertices[ 3 ].Ny = 0.5f;
			patchVertices[ 3 ].Nz = 0;
			patchVertices[ 3 ].U = 0;
			patchVertices[ 3 ].V = 0.5f;

			patchVertices[ 4 ].X = 0;
			patchVertices[ 4 ].Y = 500;
			patchVertices[ 4 ].Z = 0;
			patchVertices[ 4 ].Nx = 0;
			patchVertices[ 4 ].Ny = 0.5f;
			patchVertices[ 4 ].Nz = 0;
			patchVertices[ 4 ].U = 0.5f;
			patchVertices[ 4 ].V = 0.5f;

			patchVertices[ 5 ].X = 500;
			patchVertices[ 5 ].Y = -50;
			patchVertices[ 5 ].Z = 0;
			patchVertices[ 5 ].Nx = 0.5f;
			patchVertices[ 5 ].Ny = 0.5f;
			patchVertices[ 5 ].Nz = 0;
			patchVertices[ 5 ].U = 1;
			patchVertices[ 5 ].V = 0.5f;

			patchVertices[ 6 ].X = -500;
			patchVertices[ 6 ].Y = 0;
			patchVertices[ 6 ].Z = 500;
			patchVertices[ 6 ].Nx = -0.5f;
			patchVertices[ 6 ].Ny = 0.5f;
			patchVertices[ 6 ].Nz = 0;
			patchVertices[ 6 ].U = 0;
			patchVertices[ 6 ].V = 1;

			patchVertices[ 7 ].X = 0;
			patchVertices[ 7 ].Y = 500;
			patchVertices[ 7 ].Z = 500;
			patchVertices[ 7 ].Nx = 0;
			patchVertices[ 7 ].Ny = 0.5f;
			patchVertices[ 7 ].Nz = 0;
			patchVertices[ 7 ].U = 0.5f;
			patchVertices[ 7 ].V = 1;

			patchVertices[ 8 ].X = 500;
			patchVertices[ 8 ].Y = 200;
			patchVertices[ 8 ].Z = 800;
			patchVertices[ 8 ].Nx = 0.5f;
			patchVertices[ 8 ].Ny = 0.5f;
			patchVertices[ 8 ].Nz = 0;
			patchVertices[ 8 ].U = 1;
			patchVertices[ 8 ].V = 1;
			// specify a vertex format declaration for our patch: 3 floats for position, 3 floats for normal, 2 floats for UV
			patchDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration();
			patchDeclaration.AddElement( 0, 0, VertexElementType.Float3, VertexElementSemantic.Position );
			patchDeclaration.AddElement( 0, 12, VertexElementType.Float3, VertexElementSemantic.Normal );
			patchDeclaration.AddElement( 0, 24, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0 );

			// create a patch mesh using vertices and declaration
			patch = MeshManager.Instance.CreateBezierPatch( "patch", ResourceGroupManager.DefaultResourceGroupName, patchVertices, patchDeclaration, 3, 3, 5, 5, VisibleSide.Both, BufferUsage.StaticWriteOnly, BufferUsage.DynamicWriteOnly, true, true );

			// Start patch at 0 detail
			patch.Subdivision = 0;

			// Create entity based on patch
			patchEntity = SceneManager.CreateEntity( "Entity1", "patch" );
			Material material = (Material)MaterialManager.Instance.Create( "TextMat", ResourceGroupManager.DefaultResourceGroupName, null );
			material.GetTechnique( 0 ).GetPass( 0 ).CreateTextureUnitState( "BumpyMetal.jpg" );

			patchEntity.MaterialName = "TextMat";
			patchPass = material.GetTechnique( 0 ).GetPass( 0 );

			// Attach the entity to the root of the scene
			SceneManager.RootSceneNode.AttachObject( patchEntity );

			// save the main pass of the material so we can toggle wireframe on it
			if ( material != null )
			{
				patchPass = material.GetTechnique( 0 ).GetPass( 0 );

				// use an orbit style camera
				CameraManager.setStyle( CameraStyle.Orbit );
				CameraManager.SetYawPitchDist( 0, 0, 250 );

				TrayManager.ShowCursor();

				// create slider to adjust detail and checkbox to toggle wireframe
				Slider slider = TrayManager.CreateThickSlider( TrayLocation.TopLeft, "Detail", "Detail", 120, 44, 0, 1, 6 );
				CheckBox box = TrayManager.CreateCheckBox( TrayLocation.TopLeft, "Wireframe", "Wireframe", 120 );
				slider.SliderMoved += new SliderMovedHandler( slider_SliderMoved );
				box.CheckChanged += new CheckChangedHandler( box_CheckChanged );

			}
		}
Esempio n. 29
0
		public override HardwareVertexBuffer CreateVertexBuffer(  VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer )
		{
			return _baseInstance.CreateVertexBuffer( vertexDeclaration, numVerts, usage, useShadowBuffer );
		}
        /// <summary>
        ///     Sets up the surface by defining it's control points, type and initial subdivision level.
        /// </summary>
        /// <remarks>
        ///     This method initialises the surface by passing it a set of control points. The type of curves to be used
        ///     are also defined here, although the only supported option currently is a bezier patch. You can also
        ///     specify a global subdivision level here if you like, although it is recommended that the parameter
        ///     is left as AUTO_LEVEL, which means the system decides how much subdivision is required (based on the
        ///     curvature of the surface).
        /// </remarks>
        /// <param name="controlPoints">
        ///     A pointer to a buffer containing the vertex data which defines control points 
        ///     of the curves rather than actual vertices. Note that you are expected to provide not
        ///     just position information, but potentially normals and texture coordinates too. The
        ///     format of the buffer is defined in the VertexDeclaration parameter.
        /// </param>
        /// <param name="decl">
        ///     VertexDeclaration describing the contents of the buffer. 
        ///     Note this declaration must _only_ draw on buffer source 0!
        /// </param>
        /// <param name="width">Specifies the width of the patch in control points.</param>
        /// <param name="height">Specifies the height of the patch in control points.</param>
        /// <param name="type">The type of surface.</param>
        /// <param name="uMaxSubdivisionLevel">
        ///     If you want to manually set the top level of subdivision, 
        ///     do it here, otherwise let the system decide.
        /// </param>
        /// <param name="vMaxSubdivisionLevel">
        ///     If you want to manually set the top level of subdivision, 
        ///     do it here, otherwise let the system decide.
        /// </param>
        /// <param name="side">Determines which side of the patch (or both) triangles are generated for.</param>
        public unsafe void DefineSurface(System.Array controlPointBuffer, VertexDeclaration declaration, int width, int height, 
            PatchSurfaceType type, int uMaxSubdivisionLevel, int vMaxSubdivisionLevel, VisibleSide visibleSide)
        {
            if (height == 0 || width == 0) {
                return; // Do nothing - garbage
            }

            this.type = type;
            this.controlWidth = width;
            this.controlHeight = height;
            this.controlCount = width * height;
            this.controlPointBuffer = controlPointBuffer;
            this.declaration = declaration;

            // Copy positions into Vector3 vector
            controlPoints.Clear();
            VertexElement elem = declaration.FindElementBySemantic(VertexElementSemantic.Position);
            int vertSize = declaration.GetVertexSize(0);
            byte *pVert = (byte*)Marshal.UnsafeAddrOfPinnedArrayElement(controlPointBuffer, 0);
            float* pReal = null;

            for (int i = 0; i < controlCount; i++) {
                pReal = (float*)(pVert + elem.Offset);
                controlPoints.Add(new Vector3(pReal[0], pReal[1], pReal[2]));
                pVert += vertSize;
            }

            this.side = visibleSide;

            // Determine max level
            // Initialise to 100% detail
            subdivisionFactor = 1.0f;

            if (uMaxSubdivisionLevel == AUTO_LEVEL) {
                uLevel = maxULevel = GetAutoULevel();
            }
            else {
                uLevel = maxULevel = uMaxSubdivisionLevel;
            }

            if (vMaxSubdivisionLevel == AUTO_LEVEL) {
                vLevel = maxVLevel = GetAutoVLevel();
            }
            else {
                vLevel = maxVLevel = vMaxSubdivisionLevel;
            }

            // Derive mesh width / height
            meshWidth  = (LevelWidth(maxULevel) - 1) * ((controlWidth-1) / 2) + 1;
            meshHeight = (LevelWidth(maxVLevel) - 1) * ((controlHeight-1) / 2) + 1;

            // Calculate number of required vertices / indexes at max resolution
            requiredVertexCount = meshWidth * meshHeight;
            int iterations = (side == VisibleSide.Both)? 2 : 1;
            requiredIndexCount = (meshWidth-1) * (meshHeight - 1) * 2 * iterations * 3;

            // Calculate bounds based on control points
            Vector3 min = Vector3.Zero;
            Vector3 max = Vector3.Zero;
            float maxSqRadius = 0.0f;
            bool first = true;

            for(int i = 0; i < controlPoints.Count; i++) {
                Vector3 vec = controlPoints[i];
                if (first) {
                    min = max = vec;
                    maxSqRadius = vec.LengthSquared;
                    first = false;
                }
                else {
                    min.Floor(vec);
                    max.Ceil(vec);
                    maxSqRadius = MathUtil.Max(vec.LengthSquared, maxSqRadius);
                }
            }

            // set the bounds of the patch
            aabb.SetExtents(min, max);
            boundingSphereRadius = MathUtil.Sqrt(maxSqRadius);
        }
Esempio n. 31
0
        public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer )
		{
			XnaHardwareVertexBuffer buffer = new XnaHardwareVertexBuffer( this, vertexDeclaration, numVerts, usage, _device, false, useShadowBuffer );
			vertexBuffers.Add( buffer );
			return buffer;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="vertexSize"></param>
		/// <param name="numVerts"></param>
		/// <param name="usage"></param>
		/// <returns></returns>
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration declaration, int numVerts, BufferUsage usage )
		{
			return CreateVertexBuffer( declaration, numVerts, usage, true );
		}
Esempio n. 33
0
		/// <summary>
		///		Default constructor.  Calls on the current buffer manager to initialize the bindings and declarations.
		/// </summary>
		public VertexData()
		{
			vertexDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration();
			vertexBufferBinding = HardwareBufferManager.Instance.CreateVertexBufferBinding();
		}
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage, bool useShadowBuffer )
		{
			return new GLESDefaultHardwareVertexBuffer( vertexDeclaration, numVerts, usage );
		}
Esempio n. 35
0
		public override HardwareVertexBuffer CreateVertexBuffer( VertexDeclaration vertexDeclaration, int numVerts, BufferUsage usage )
		{
			// call overloaded method with no shadow buffer
			return CreateVertexBuffer( vertexDeclaration, numVerts, usage, false );
		}
        protected void SetPointsImpl(List <Vector3> points, List <ColorEx> colors, List <List <VertexBoneAssignment> > boneAssignments)
        {
            if (colors != null && points.Count != colors.Count)
            {
                throw new Exception("Invalid parameters to SetPoints.  Point list length does not match colors list length");
            }
            Vector3 min = Vector3.Zero;
            Vector3 max = Vector3.Zero;

            // set up vertex data
            vertexData = new VertexData();

            // set up vertex declaration
            VertexDeclaration vertexDeclaration = vertexData.vertexDeclaration;
            int currentOffset = 0;

            // always need positions
            vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Float3, VertexElementSemantic.Position);
            currentOffset += VertexElement.GetTypeSize(VertexElementType.Float3);
            int colorOffset = currentOffset / sizeof(float);

            if (colors != null)
            {
                vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Color, VertexElementSemantic.Diffuse);
                currentOffset += VertexElement.GetTypeSize(VertexElementType.Color);
            }
            int boneIndexOffset = currentOffset / sizeof(float);

            if (boneAssignments != null)
            {
                vertexDeclaration.AddElement(0, currentOffset, VertexElementType.UByte4, VertexElementSemantic.BlendIndices);
                currentOffset += VertexElement.GetTypeSize(VertexElementType.UByte4);
            }
            int boneWeightOffset = currentOffset / sizeof(float);

            if (boneAssignments != null)
            {
                vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Float4, VertexElementSemantic.BlendWeights);
                currentOffset += VertexElement.GetTypeSize(VertexElementType.Float4);
            }
            int stride = currentOffset / sizeof(float);

            vertexData.vertexCount = points.Count;

            // allocate vertex buffer
            HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer(vertexDeclaration.GetVertexSize(0), vertexData.vertexCount, BufferUsage.StaticWriteOnly);

            // set up the binding, one source only
            VertexBufferBinding binding = vertexData.vertexBufferBinding;

            binding.SetBinding(0, vertexBuffer);

            // Generate vertex data
            unsafe {
                // lock the vertex buffer
                IntPtr data = vertexBuffer.Lock(BufferLocking.Discard);

                byte * pData  = (byte *)data.ToPointer();
                float *pFloat = (float *)pData;
                uint * pInt   = (uint *)pData;
                for (int i = 0; i < points.Count; ++i)
                {
                    Vector3 vec = points[i];
                    // assign to geometry
                    pFloat[stride * i]     = vec.x;
                    pFloat[stride * i + 1] = vec.y;
                    pFloat[stride * i + 2] = vec.z;
                    if (colors != null)
                    {
                        // assign to diffuse
                        pInt[stride * i + colorOffset] = Root.Instance.RenderSystem.ConvertColor(colors[i]);
                    }
                    if (boneAssignments != null)
                    {
                        for (int j = 0; j < 4; ++j)
                        {
                            pData[4 * (stride * i + boneIndexOffset) + j] = (byte)(boneAssignments[i][j].boneIndex);
                            pFloat[stride * i + boneWeightOffset + j]     = boneAssignments[i][j].weight;
                        }
                    }
                }
                // unlock the buffer
                vertexBuffer.Unlock();
            } // unsafe

            for (int i = 0; i < points.Count; ++i)
            {
                Vector3 vec = points[i];
                // Also update the bounding sphere radius
                float len = vec.Length;
                if (len > boundingSphereRadius)
                {
                    boundingSphereRadius = len;
                }
                // Also update the bounding box
                if (vec.x < min.x)
                {
                    min.x = vec.x;
                }
                if (vec.y < min.y)
                {
                    min.y = vec.y;
                }
                if (vec.z < min.z)
                {
                    min.z = vec.z;
                }
                if (vec.x > max.x)
                {
                    max.x = vec.x;
                }
                if (vec.y > max.y)
                {
                    max.y = vec.y;
                }
                if (vec.z > max.z)
                {
                    max.z = vec.z;
                }
            }

            // Set the SimpleRenderable bounding box
            box = new AxisAlignedBox(min, max);
        }