VertexData CreateVertexData(Vec3[] vertices) { VertexData vertexData = new VertexData(); VertexDeclaration declaration = vertexData.VertexDeclaration; declaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); VertexBufferBinding bufferBinding = vertexData.VertexBufferBinding; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( 12, vertices.Length, HardwareBuffer.Usage.StaticWriteOnly); bufferBinding.SetBinding(0, vertexBuffer, true); vertexData.VertexCount = vertices.Length; unsafe { Vec3 *buffer = (Vec3 *)vertexBuffer.Lock(HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (Vec3 position in vertices) { *buffer = position; buffer++; } vertexBuffer.Unlock(); } return(vertexData); }
private void CreateBuffers(ushort[] indices, short[] vertices) { var numIndices = indices.Length; var numVertices = vertices.Length; _vertexDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration(); _vertexDeclaration.AddElement(0, 0, VertexElementType.Short2, VertexElementSemantic.Position); _ib = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, numIndices, BufferUsage.WriteOnly); _vb = HardwareBufferManager.Instance.CreateVertexBuffer(_vertexDeclaration, numVertices, BufferUsage.WriteOnly, false); _ib.WriteData(0, numIndices * sizeof(ushort), indices, true); _vb.WriteData(0, numVertices * sizeof(ushort), vertices, true); var binding = new VertexBufferBinding(); binding.SetBinding(0, _vb); VertexData = new VertexData(); VertexData.vertexDeclaration = _vertexDeclaration; VertexData.vertexBufferBinding = binding; VertexData.vertexCount = numVertices; VertexData.vertexStart = 0; IndexData = new IndexData(); IndexData.indexBuffer = _ib; IndexData.indexCount = numIndices; IndexData.indexStart = 0; }
private void CreatePrefabPlane() { Mesh mesh = (Mesh)Create("Prefab_Plane"); SubMesh subMesh = mesh.CreateSubMesh("Prefab_Plane_Submesh"); float[] vertices = { -100, -100, 0, // pos 0, 0, 1, // normal 0, 1, // texcoord 100, -100, 0, 0, 0, 1, 1, 1, 100, 100, 0, 0, 0, 1, 1, 0, -100, 100, 0, 0, 0, 1, 0, 0 }; mesh.SharedVertexData = new VertexData(); mesh.SharedVertexData.vertexCount = 4; VertexDeclaration vertexDeclaration = mesh.SharedVertexData.vertexDeclaration; VertexBufferBinding binding = mesh.SharedVertexData.vertexBufferBinding; int offset = 0; vertexDeclaration.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.Position); offset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexDeclaration.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.Normal); offset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexDeclaration.AddElement(0, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); offset += VertexElement.GetTypeSize(VertexElementType.Float2); // allocate vertex buffer HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer(offset, 4, BufferUsage.StaticWriteOnly); // set up the binding, one source only binding.SetBinding(0, vertexBuffer); vertexBuffer.WriteData(0, vertexBuffer.Size, vertices, true); subMesh.useSharedVertices = true; HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, 6, BufferUsage.StaticWriteOnly); short[] faces = { 0, 1, 2, 0, 2, 3 }; subMesh.indexData.indexBuffer = indexBuffer; subMesh.indexData.indexCount = 6; subMesh.indexData.indexStart = 0; indexBuffer.WriteData(0, indexBuffer.Size, faces, true); mesh.BoundingBox = new AxisAlignedBox(new Vector3(-100, -100, 0), new Vector3(100, 100, 0)); mesh.BoundingSphereRadius = MathUtil.Sqrt(100 * 100 + 100 * 100); resourceList.Add(mesh.Name, mesh); }
/// <summary> /// Default constructor. /// </summary> public WireBoundingBox() { vertexData = new VertexData(); vertexData.vertexCount = 24; vertexData.vertexStart = 0; // get a reference to the vertex declaration and buffer binding VertexDeclaration decl = vertexData.vertexDeclaration; VertexBufferBinding binding = vertexData.vertexBufferBinding; // add elements for position and color only decl.AddElement(POSITION, 0, VertexElementType.Float3, VertexElementSemantic.Position); decl.AddElement(COLOR, 0, VertexElementType.Color, VertexElementSemantic.Diffuse); // create a new hardware vertex buffer for the position data HardwareVertexBuffer buffer = HardwareBufferManager.Instance.CreateVertexBuffer( decl.GetVertexSize(POSITION), vertexData.vertexCount, BufferUsage.StaticWriteOnly); // bind the position buffer binding.SetBinding(POSITION, buffer); // create a new hardware vertex buffer for the color data buffer = HardwareBufferManager.Instance.CreateVertexBuffer( decl.GetVertexSize(COLOR), vertexData.vertexCount, BufferUsage.StaticWriteOnly); // bind the color buffer binding.SetBinding(COLOR, buffer); Material mat = MaterialManager.Instance.GetByName("Core/WireBB"); if (mat == null) { mat = MaterialManager.Instance.GetByName("BaseWhite"); mat = mat.Clone("Core/WireBB"); mat.Lighting = false; } this.Material = mat; }
void CreateMesh() { string meshName = MeshManager.Instance.GetUniqueName("DynamicMesh"); mesh = MeshManager.Instance.CreateManual(meshName); //set mesh gabarites Bounds bounds = new Bounds(new Vec3(-.1f, -.5f, -.5f), new Vec3(.1f, .5f, .5f)); mesh.SetBoundsAndRadius(bounds, bounds.GetRadius()); SubMesh subMesh = mesh.CreateSubMesh(); subMesh.UseSharedVertices = false; int maxVertices = (Type.GridSize.X + 1) * (Type.GridSize.Y + 1); int maxIndices = Type.GridSize.X * Type.GridSize.Y * 6; //init vertexData VertexDeclaration declaration = subMesh.VertexData.VertexDeclaration; declaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); declaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal); declaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TextureCoordinates, 0); VertexBufferBinding bufferBinding = subMesh.VertexData.VertexBufferBinding; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( 32, maxVertices, HardwareBuffer.Usage.DynamicWriteOnly); bufferBinding.SetBinding(0, vertexBuffer, true); //init indexData HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer( HardwareIndexBuffer.IndexType._16Bit, maxIndices, HardwareBuffer.Usage.DynamicWriteOnly); subMesh.IndexData.SetIndexBuffer(indexBuffer, true); //set material subMesh.MaterialName = Type.MaterialName; needUpdateVertices = true; needUpdateIndices = true; }
private void CreateBuffers(ushort[] indices, short[] vertices) { var numIndices = (uint)indices.Length; var numVertices = (uint)vertices.Length; _vertexDeclaration = HardwareBufferManager.Singleton.CreateVertexDeclaration(); _vertexDeclaration.AddElement(0, 0, VertexElementType.VET_SHORT2, VertexElementSemantic.VES_POSITION); _ib = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_16BIT, numIndices, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY); _vb = HardwareBufferManager.Singleton.CreateVertexBuffer(_vertexDeclaration.GetVertexSize(0), numVertices, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY, false); unsafe { fixed (ushort* x = indices) _ib.WriteData(0, numIndices * sizeof(ushort), x, true); fixed (short* x = vertices) _vb.WriteData(0, numVertices * sizeof(ushort), x, true); } var binding = new VertexBufferBinding(); binding.SetBinding(0, _vb); VertexData = new VertexData(_vertexDeclaration, binding); VertexData.vertexCount = numVertices; VertexData.vertexStart = 0; IndexData = new IndexData(); IndexData.indexBuffer = _ib; IndexData.indexCount = numIndices; IndexData.indexStart = 0; }
/// <summary> /// /// </summary> private unsafe void CreateGeometry() { // Vertex buffers _subMesh.vertexData = new VertexData(); _subMesh.vertexData.vertexStart = 0; _subMesh.vertexData.vertexCount = (uint)_vertexCount; VertexDeclaration vdecl = _subMesh.vertexData.vertexDeclaration; VertexBufferBinding vbind = _subMesh.vertexData.vertexBufferBinding; uint offset = 0; // Position vdecl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3); // 3D coords vdecl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_TEXTURE_COORDINATES, 0); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3); // Noise coords vdecl.AddElement(0, offset, VertexElementType.VET_FLOAT2, VertexElementSemantic.VES_TEXTURE_COORDINATES, 1); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT2); // Opacity vdecl.AddElement(0, offset, VertexElementType.VET_FLOAT1, VertexElementSemantic.VES_TEXTURE_COORDINATES, 2); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT1); _vertexBuffer = HardwareBufferManager.Singleton.CreateVertexBuffer( offset, (uint)_vertexCount, HardwareBuffer.Usage.HBU_DYNAMIC_WRITE_ONLY); vbind.SetBinding(0, _vertexBuffer); int[] indexbuffer = new int[_numberOfTriangles * 3]; int IndexOffset = 0; int VertexOffset = 0; // C for (int k = 0; k < _nc; k++) { // First triangle indexbuffer[IndexOffset] = VertexOffset; indexbuffer[IndexOffset + 1] = VertexOffset + 1; indexbuffer[IndexOffset + 2] = VertexOffset + 3; // Second triangle indexbuffer[IndexOffset + 3] = VertexOffset; indexbuffer[IndexOffset + 4] = VertexOffset + 3; indexbuffer[IndexOffset + 5] = VertexOffset + 2; IndexOffset += 6; VertexOffset += 4; } // B for (int k = 0; k < _nb; k++) { // First triangle indexbuffer[IndexOffset] = VertexOffset; indexbuffer[IndexOffset + 1] = VertexOffset + 1; indexbuffer[IndexOffset + 2] = VertexOffset + 3; // Second triangle indexbuffer[IndexOffset + 3] = VertexOffset; indexbuffer[IndexOffset + 4] = VertexOffset + 3; indexbuffer[IndexOffset + 5] = VertexOffset + 2; // Third triangle indexbuffer[IndexOffset + 6] = VertexOffset + 2; indexbuffer[IndexOffset + 7] = VertexOffset + 3; indexbuffer[IndexOffset + 8] = VertexOffset + 5; // Fourth triangle indexbuffer[IndexOffset + 9] = VertexOffset + 2; indexbuffer[IndexOffset + 10] = VertexOffset + 5; indexbuffer[IndexOffset + 11] = VertexOffset + 4; IndexOffset += 12; VertexOffset += 6; } // A for (int k = 0; k < _na; k++) { // First triangle indexbuffer[IndexOffset] = VertexOffset; indexbuffer[IndexOffset + 1] = VertexOffset + 1; indexbuffer[IndexOffset + 2] = VertexOffset + 3; // Second triangle indexbuffer[IndexOffset + 3] = VertexOffset; indexbuffer[IndexOffset + 4] = VertexOffset + 3; indexbuffer[IndexOffset + 5] = VertexOffset + 2; // Third triangle indexbuffer[IndexOffset + 6] = VertexOffset + 2; indexbuffer[IndexOffset + 7] = VertexOffset + 3; indexbuffer[IndexOffset + 8] = VertexOffset + 5; // Fourth triangle indexbuffer[IndexOffset + 9] = VertexOffset + 2; indexbuffer[IndexOffset + 10] = VertexOffset + 5; indexbuffer[IndexOffset + 11] = VertexOffset + 4; // Fifth triangle indexbuffer[IndexOffset + 12] = VertexOffset + 4; indexbuffer[IndexOffset + 13] = VertexOffset + 5; indexbuffer[IndexOffset + 14] = VertexOffset + 6; IndexOffset += 15; VertexOffset += 7; } // Prepare buffer for indices _indexBuffer = HardwareBufferManager.Singleton.CreateIndexBuffer(Mogre.HardwareIndexBuffer.IndexType.IT_32BIT, (uint)_numberOfTriangles * 3, HardwareBuffer.Usage.HBU_STATIC, true); fixed(int *addr = &indexbuffer[0]) { _indexBuffer.WriteData( 0, _indexBuffer.SizeInBytes, addr, true); } //indexbufferArr = null; // Set index buffer for this submesh _subMesh.indexData.indexBuffer = _indexBuffer; _subMesh.indexData.indexStart = 0; _subMesh.indexData.indexCount = (uint)_numberOfTriangles * 3; // Create our internal buffer for manipulations _vertices = new Vertex[_vertexCount]; // Update geometry UpdateGeometry(); }
private unsafe void createCube(string name, Mogre.Vector3 gpose, double d) { MeshPtr msh = MeshManager.Singleton.CreateManual(name, "General"); SubMesh sub1 = msh.CreateSubMesh("1"); const float sqrt13 = 0.577350269f; /* sqrt(1/3) */ const int nVertices = 8; const int vbufCount = 3 * 2 * nVertices; float[] vertices = new float[vbufCount] { (float)(gpose.x - d / 2), (float)(gpose.y + d / 2), (float)(gpose.z - d / 2), //0 position -sqrt13, sqrt13, -sqrt13, //0 normal A (float)(gpose.x + d / 2), (float)(gpose.y + d / 2), (float)(gpose.z - d / 2), //1 position sqrt13, sqrt13, -sqrt13, //1 normal B (float)(gpose.x + d / 2), (float)(gpose.y - d / 2), (float)(gpose.z - d / 2), //2 position sqrt13, -sqrt13, -sqrt13, //2 normal F (float)(gpose.x - d / 2), (float)(gpose.y - d / 2), (float)(gpose.z - d / 2), //3 position -sqrt13, -sqrt13, -sqrt13, //3 normal H (float)(gpose.x - d / 2), (float)(gpose.y + d / 2), (float)(gpose.z + d / 2), -sqrt13, sqrt13, sqrt13, //4 normal C (float)(gpose.x + d / 2), (float)(gpose.y + d / 2), (float)(gpose.z + d / 2), sqrt13, sqrt13, sqrt13, //5 normal D (float)(gpose.x + d / 2), (float)(gpose.y - d / 2), (float)(gpose.z + d / 2), sqrt13, -sqrt13, sqrt13, //6 normal E (float)(gpose.x - d / 2), (float)(gpose.y - d / 2), (float)(gpose.z + d / 2), -sqrt13, -sqrt13, sqrt13, //7 normal G }; const int ibufCount = 36; ushort[] faces = new ushort[ibufCount] { //back 0, 2, 3, 0, 1, 2, //right 1, 6, 2, 1, 5, 6, //front 4, 6, 5, 4, 7, 6, //left 0, 7, 4, 0, 3, 7, //top 0, 5, 1, 0, 4, 5, //bottom 2, 7, 3, 2, 6, 7 }; sub1.vertexData = new VertexData(); sub1.vertexData.vertexCount = nVertices; VertexDeclaration decl = sub1.vertexData.vertexDeclaration; uint offset = 0; //position decl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3); //normal decl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_NORMAL); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3); HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.Singleton.CreateVertexBuffer(offset, sub1.vertexData.vertexCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY); VertexBufferBinding bind = sub1.vertexData.vertexBufferBinding; void *pVertices; fixed(float *pFVertice = vertices) { pVertices = (void *)pFVertice; } vbuf.WriteData(0, vbuf.SizeInBytes, pVertices, true); bind.SetBinding(0, vbuf); void *pFaces; fixed(ushort *pUFaces = faces) { pFaces = (void *)pUFaces; } HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_16BIT, ibufCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY); ibuf.WriteData(0, ibuf.SizeInBytes, pFaces, true); sub1.useSharedVertices = false; sub1.indexData.indexBuffer = ibuf; sub1.indexData.indexCount = ibufCount; sub1.indexData.indexStart = 0; sub1.SetMaterialName("Examples/10PointBlock"); msh._setBounds(new AxisAlignedBox(-100, -100, -100, 100, 100, 100)); msh._setBoundingSphereRadius(Mogre.Math.Sqrt(3 * 100 * 100)); msh.Load(); }
/// <summary> /// /// </summary> protected void Initialize() { // Create geometry int nvertices = this.slices * 4; // n+1 planes int elemsize = 3 * 3; int dsize = elemsize * nvertices; int x; var indexData = new IndexData(); var vertexData = new VertexData(); var vertices = new float[dsize]; var coords = new float[4, 2] { { 0.0f, 0.0f }, { 0.0f, 1.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f } }; for (x = 0; x < this.slices; x++) { for (int y = 0; y < 4; y++) { float xcoord = coords[y, 0] - 0.5f; float ycoord = coords[y, 1] - 0.5f; float zcoord = -((float)x / (float)(this.slices - 1) - 0.5f); // 1.0f .. a/(a+1) // coordinate vertices[x * 4 * elemsize + y * elemsize + 0] = xcoord * (this.size / 2.0f); vertices[x * 4 * elemsize + y * elemsize + 1] = ycoord * (this.size / 2.0f); vertices[x * 4 * elemsize + y * elemsize + 2] = zcoord * (this.size / 2.0f); // normal vertices[x * 4 * elemsize + y * elemsize + 3] = 0.0f; vertices[x * 4 * elemsize + y * elemsize + 4] = 0.0f; vertices[x * 4 * elemsize + y * elemsize + 5] = 1.0f; // tex vertices[x * 4 * elemsize + y * elemsize + 6] = xcoord * Utility.Sqrt(3.0f); vertices[x * 4 * elemsize + y * elemsize + 7] = ycoord * Utility.Sqrt(3.0f); vertices[x * 4 * elemsize + y * elemsize + 8] = zcoord * Utility.Sqrt(3.0f); } } var faces = new short[this.slices * 6]; for (x = 0; x < this.slices; x++) { faces[x * 6 + 0] = (short)(x * 4 + 0); faces[x * 6 + 1] = (short)(x * 4 + 1); faces[x * 6 + 2] = (short)(x * 4 + 2); faces[x * 6 + 3] = (short)(x * 4 + 1); faces[x * 6 + 4] = (short)(x * 4 + 2); faces[x * 6 + 5] = (short)(x * 4 + 3); } //setup buffers vertexData.vertexStart = 0; vertexData.vertexCount = nvertices; VertexDeclaration decl = vertexData.vertexDeclaration; VertexBufferBinding bind = vertexData.vertexBufferBinding; int offset = 0; offset += decl.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position).Size; offset += decl.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.Normal).Size; offset += decl.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.TexCoords).Size; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer(decl, nvertices, BufferUsage.StaticWriteOnly); bind.SetBinding(0, vertexBuffer); HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, this.slices * 6, BufferUsage.StaticWriteOnly); indexData.indexBuffer = indexBuffer; indexData.indexCount = this.slices * 6; indexData.indexStart = 0; indexBuffer.WriteData(0, indexBuffer.Size, faces, true); vertexBuffer.WriteData(0, vertexBuffer.Size, vertices); vertices = null; faces = null; // Now make the render operation renderOperation.operationType = OperationType.TriangleList; renderOperation.indexData = indexData; renderOperation.vertexData = vertexData; renderOperation.useIndices = true; // Create a brand new private material if (!ResourceGroupManager.Instance.GetResourceGroups().Contains("VolumeRendable")) { ResourceGroupManager.Instance.CreateResourceGroup("VolumeRendable"); } var material = (Material)MaterialManager.Instance.Create(this.texture, "VolumeRendable"); // Remove pre-created technique from defaults material.RemoveAllTechniques(); // Create a techinique and a pass and a texture unit Technique technique = material.CreateTechnique(); Pass pass = technique.CreatePass(); TextureUnitState textureUnit = pass.CreateTextureUnitState(); // Set pass parameters pass.SetSceneBlending(SceneBlendType.TransparentAlpha); pass.DepthWrite = false; pass.CullingMode = CullingMode.None; pass.LightingEnabled = false; textureUnit.SetTextureAddressingMode(TextureAddressing.Clamp); textureUnit.SetTextureName(this.texture, TextureType.ThreeD); textureUnit.SetTextureFiltering(TextureFiltering.Trilinear); this.unit = textureUnit; base.material = material; }
public void Init(TerrainOptions options) { this.options = options; this.numMipMaps = options.maxMipmap; this.size = options.size; this.terrain = new VertexData(); this.terrain.vertexStart = 0; this.terrain.vertexCount = options.size * options.size; VertexDeclaration decl = this.terrain.vertexDeclaration; VertexBufferBinding binding = this.terrain.vertexBufferBinding; int offset = 0; // Position/Normal decl.AddElement(POSITION, 0, VertexElementType.Float3, VertexElementSemantic.Position); decl.AddElement(NORMAL, 0, VertexElementType.Float3, VertexElementSemantic.Normal); // TexCoords decl.AddElement(TEXCOORD, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); offset += VertexElement.GetTypeSize(VertexElementType.Float2); decl.AddElement(TEXCOORD, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 1); offset += VertexElement.GetTypeSize(VertexElementType.Float2); // TODO: Color HardwareVertexBuffer buffer = HardwareBufferManager.Instance.CreateVertexBuffer(decl.Clone(POSITION), this.terrain.vertexCount, BufferUsage.StaticWriteOnly, true); binding.SetBinding(POSITION, buffer); buffer = HardwareBufferManager.Instance.CreateVertexBuffer(decl.Clone(NORMAL), this.terrain.vertexCount, BufferUsage.StaticWriteOnly, true); binding.SetBinding(NORMAL, buffer); buffer = HardwareBufferManager.Instance.CreateVertexBuffer(decl.Clone(TEXCOORD), this.terrain.vertexCount, BufferUsage.StaticWriteOnly, true); binding.SetBinding(TEXCOORD, buffer); this.minLevelDistSqr = new float[this.numMipMaps]; int endx = options.startx + options.size; int endz = options.startz + options.size; // TODO: name buffers different so we can unlock HardwareVertexBuffer posBuffer = binding.GetBuffer(POSITION); var pos = posBuffer.Lock(BufferLocking.Discard); HardwareVertexBuffer texBuffer = binding.GetBuffer(TEXCOORD); var tex = texBuffer.Lock(BufferLocking.Discard); float min = 99999999, max = 0; #if !AXIOM_SAFE_ONLY unsafe #endif { var posPtr = pos.ToFloatPointer(); var texPtr = tex.ToFloatPointer(); int posCount = 0; int texCount = 0; for (int j = options.startz; j < endz; j++) { for (int i = options.startx; i < endx; i++) { float height = options.GetWorldHeight(i, j) * options.scaley; posPtr[posCount++] = i * options.scalex; posPtr[posCount++] = height; posPtr[posCount++] = j * options.scalez; texPtr[texCount++] = (float)i / options.worldSize; texPtr[texCount++] = (float)j / options.worldSize; texPtr[texCount++] = ((float)i / options.size) * options.detailTile; texPtr[texCount++] = ((float)j / options.size) * options.detailTile; if (height < min) { min = height; } if (height > max) { max = height; } } // for i } // for j } // unsafe // unlock the buffers posBuffer.Unlock(); texBuffer.Unlock(); this.box.SetExtents(new Vector3(options.startx * options.scalex, min, options.startz * options.scalez), new Vector3((endx - 1) * options.scalex, max, (endz - 1) * options.scalez)); this.center = new Vector3((options.startx * options.scalex + endx - 1) / 2, (min + max) / 2, (options.startz * options.scalez + endz - 1) / 2); float C = CalculateCFactor(); CalculateMinLevelDist2(C); }
/** Initializes the LandScapeRenderable with the given options and the starting coordinates of this block. */ public Renderable() : base() { info = null; materialLODIndex = 0; neighbors = new Renderable[4]; for (long i = 0; i < 4; i++) { neighbors[i] = null; } inUse = false; isLoaded = false; // Setup render op renderOp = new RenderOperation(); renderOp.vertexData = new VertexData(); renderOp.vertexData.vertexStart = 0; long tileSize = Options.Instance.TileSize; renderOp.vertexData.vertexCount = (int)((tileSize + 1) * (tileSize + 1)); box = new AxisAlignedBox(); // Vertex declaration VertexDeclaration decl = renderOp.vertexData.vertexDeclaration; VertexBufferBinding bind = renderOp.vertexData.vertexBufferBinding; HardwareVertexBuffer vbuf; // Vertex buffer #1, position // positions int offset = 0; decl.AddElement(POSITION, offset, VertexElementType.Float3, VertexElementSemantic.Position); offset += VertexElement.GetTypeSize(VertexElementType.Float3); if (Options.Instance.Lit) { decl.AddElement(POSITION, offset, VertexElementType.Float3, VertexElementSemantic.Normal); offset += VertexElement.GetTypeSize(VertexElementType.Float3); } decl.AddElement(POSITION, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); offset += VertexElement.GetTypeSize(VertexElementType.Float2); vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( decl.GetVertexSize(POSITION), renderOp.vertexData.vertexCount, BufferUsage.StaticWriteOnly, false); bind.SetBinding(POSITION, vbuf); // if (Options.Instance.Lit) // { // offset = 0; // decl.AddElement(NORMAL, offset, VertexElementType.Float3, VertexElementSemantic.Normal); // offset += VertexElement.GetTypeSize(VertexElementType.Float3); // vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( // decl.GetVertexSize(NORMAL), // renderOp.vertexData.vertexCount, // BufferUsage.StaticWriteOnly); // // bind.SetBinding(NORMAL, vbuf); // } // // offset = 0; // decl.AddElement(TEXCOORD, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); // offset += VertexElement.GetTypeSize(VertexElementType.Float2); // vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( // decl.GetVertexSize(TEXCOORD), // renderOp.vertexData.vertexCount, // BufferUsage.StaticWriteOnly); // // bind.SetBinding(TEXCOORD, vbuf); // // // if (Options.Instance.Colored || // Options.Instance.Coverage_Vertex_Color || // Options.Instance.Base_Vertex_Color) // { // offset = 0; // decl.AddElement(COLORS, offset, VertexElementType.Float3, VertexElementSemantic.Diffuse); // offset += VertexElement.GetTypeSize(VertexElementType.Color); // vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( // decl.GetVertexSize(COLORS), // renderOp.vertexData.vertexCount, // BufferUsage.StaticWriteOnly); // // bind.SetBinding(COLORS, vbuf); // } //No need to set the indexData since it is shared from IndexBuffer class renderOp.operationType = OperationType.TriangleList; renderOp.useIndices = true; renderOp.indexData = null; RenderLevel = Options.Instance.MaxRenderLevel / 2; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="plane"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="curvature"></param> /// <param name="xSegments"></param> /// <param name="ySegments"></param> /// <param name="normals"></param> /// <param name="numberOfTexCoordSets"></param> /// <param name="uTiles"></param> /// <param name="vTiles"></param> /// <param name="upVector"></param> /// <param name="orientation"></param> /// <param name="vertexBufferUsage"></param> /// <param name="indexBufferUsage"></param> /// <param name="vertexShadowBuffer"></param> /// <param name="indexShadowBuffer"></param> /// <returns></returns> public Mesh CreateCurvedIllusionPlane(string name, Plane plane, float width, float height, float curvature, int xSegments, int ySegments, bool normals, int numberOfTexCoordSets, float uTiles, float vTiles, Vector3 upVector, Quaternion orientation, BufferUsage vertexBufferUsage, BufferUsage indexBufferUsage, bool vertexShadowBuffer, bool indexShadowBuffer) { Mesh mesh = CreateManual(name); SubMesh subMesh = mesh.CreateSubMesh(name + "SubMesh"); // set up vertex data, use a single shared buffer mesh.SharedVertexData = new VertexData(); VertexData vertexData = mesh.SharedVertexData; // 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); // optional normals if (normals) { vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Float3, VertexElementSemantic.Normal); currentOffset += VertexElement.GetTypeSize(VertexElementType.Float3); } for (ushort i = 0; i < numberOfTexCoordSets; i++) { // assumes 2d texture coordinates vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Float2, VertexElementSemantic.TexCoords, i); currentOffset += VertexElement.GetTypeSize(VertexElementType.Float2); } vertexData.vertexCount = (xSegments + 1) * (ySegments + 1); // allocate vertex buffer HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer(vertexDeclaration.GetVertexSize(0), vertexData.vertexCount, vertexBufferUsage, vertexShadowBuffer); // set up the binding, one source only VertexBufferBinding binding = vertexData.vertexBufferBinding; binding.SetBinding(0, vertexBuffer); // work out the transform required, default orientation of plane is normal along +z, distance 0 Matrix4 xlate, xform, rot; Matrix3 rot3 = Matrix3.Identity; xlate = rot = Matrix4.Identity; // determine axes Vector3 zAxis, yAxis, xAxis; zAxis = plane.Normal; zAxis.Normalize(); yAxis = upVector; yAxis.Normalize(); xAxis = yAxis.Cross(zAxis); if (xAxis.Length == 0) { throw new AxiomException("The up vector for a plane cannot be parallel to the planes normal."); } rot3.FromAxes(xAxis, yAxis, zAxis); rot = rot3; // set up standard xform from origin xlate.Translation = plane.Normal * -plane.D; // concatenate xform = xlate * rot; // generate vertex data, imagine a large sphere with the camera located near the top, // the lower the curvature, the larger the sphere. use the angle from the viewer to the // points on the plane float cameraPosition; // camera position relative to the sphere center // derive sphere radius (unused) //float sphereDistance; // distance from the camera to the sphere along box vertex vector float sphereRadius; // actual values irrelevant, it's the relation between the sphere's radius and the camera's position which is important float SPHERE_RADIUS = 100; float CAMERA_DISTANCE = 5; sphereRadius = SPHERE_RADIUS - curvature; cameraPosition = sphereRadius - CAMERA_DISTANCE; // lock the whole buffer float xSpace = width / xSegments; float ySpace = height / ySegments; float halfWidth = width / 2; float halfHeight = height / 2; Vector3 vec = Vector3.Zero; Vector3 norm = Vector3.Zero; Vector3 min = Vector3.Zero; Vector3 max = Vector3.Zero; float maxSquaredLength = 0; bool firstTime = true; // generate vertex data GenerateCurvedIllusionPlaneVertexData(vertexBuffer, ySegments, xSegments, xSpace, halfWidth, ySpace, halfHeight, xform, firstTime, normals, orientation, cameraPosition, sphereRadius, uTiles, vTiles, numberOfTexCoordSets, ref min, ref max, ref maxSquaredLength); // generate face list subMesh.useSharedVertices = true; Tesselate2DMesh(subMesh, xSegments + 1, ySegments + 1, false, indexBufferUsage, indexShadowBuffer); // generate bounds for the mesh mesh.BoundingBox = new AxisAlignedBox(min, max); mesh.BoundingSphereRadius = MathUtil.Sqrt(maxSquaredLength); mesh.Load(); mesh.Touch(); return(mesh); }
public Mesh CreateBoneMesh(string name) { Mesh mesh = CreateManual(name); mesh.SkeletonName = name + ".skeleton"; SubMesh subMesh = mesh.CreateSubMesh("BoneSubMesh"); subMesh.useSharedVertices = true; subMesh.MaterialName = "BaseWhite"; // short[] faces = { 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 2, 1, 2, 5, 1, 5, 4, 1, 4, 3, 1, 3, 2 }; // short[] faces = { 0, 3, 2, 0, 4, 3, 0, 5, 4, 0, 2, 5, 1, 5, 2, 1, 4, 5, 1, 3, 4, 1, 2, 3 }; short[] faces = { 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 2, 1, 2, 5, 1, 5, 4, 1, 4, 3, 1, 3, 2, 0, 3, 2, 0, 4, 3, 0, 5, 4, 0, 2, 5, 1, 5, 2, 1, 4, 5, 1, 3, 4, 1, 2, 3 }; int faceCount = faces.Length / 3; // faces per bone int vertexCount = 6; // vertices per bone // set up vertex data, use a single shared buffer mesh.SharedVertexData = new VertexData(); VertexData vertexData = mesh.SharedVertexData; // 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); vertexDeclaration.AddElement(0, currentOffset, VertexElementType.Float3, VertexElementSemantic.Normal); currentOffset += VertexElement.GetTypeSize(VertexElementType.Float3); int boneCount = mesh.Skeleton.BoneCount; // I want 6 vertices per bone - exclude the root bone vertexData.vertexCount = boneCount * vertexCount; // 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); Vector3[] vertices = new Vector3[vertexData.vertexCount]; GetVertices(ref vertices, mesh.Skeleton.RootBone); // Generate vertex data unsafe { // lock the vertex buffer IntPtr data = vertexBuffer.Lock(BufferLocking.Discard); float *pData = (float *)data.ToPointer(); foreach (Vector3 vec in vertices) { // assign to geometry *pData++ = vec.x; *pData++ = vec.y; *pData++ = vec.z; // fake normals *pData++ = 0; *pData++ = 1; *pData++ = 0; } // unlock the buffer vertexBuffer.Unlock(); } // unsafe // Generate index data HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, faces.Length * boneCount, BufferUsage.StaticWriteOnly); subMesh.indexData.indexBuffer = indexBuffer; subMesh.indexData.indexCount = faces.Length * boneCount; subMesh.indexData.indexStart = 0; for (ushort boneIndex = 0; boneIndex < mesh.Skeleton.BoneCount; ++boneIndex) { Axiom.Animating.Bone bone = mesh.Skeleton.GetBone(boneIndex); short[] tmpFaces = new short[faces.Length]; for (int tmp = 0; tmp < faces.Length; ++tmp) { tmpFaces[tmp] = (short)(faces[tmp] + vertexCount * bone.Handle); } indexBuffer.WriteData(faces.Length * bone.Handle * sizeof(short), tmpFaces.Length * sizeof(short), tmpFaces, true); } for (ushort boneIndex = 0; boneIndex < mesh.Skeleton.BoneCount; ++boneIndex) { Axiom.Animating.Bone bone = mesh.Skeleton.GetBone(boneIndex); Axiom.Animating.Bone parentBone = bone; if (bone.Parent != null) { parentBone = (Axiom.Animating.Bone)bone.Parent; } for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex) { Axiom.Animating.VertexBoneAssignment vba = new Axiom.Animating.VertexBoneAssignment(); // associate the base of the joint display with the bone's parent, // and the rest of the points with the bone. vba.boneIndex = parentBone.Handle; vba.weight = 1.0f; vba.vertexIndex = vertexCount * bone.Handle + vertexIndex; mesh.AddBoneAssignment(vba); } } mesh.Load(); mesh.Touch(); return(mesh); }
/// <summary> /// /// </summary> /// <param name="name">Name of the plane mesh.</param> /// <param name="plane">Plane to use for distance and orientation of the mesh.</param> /// <param name="width">Width in world coordinates.</param> /// <param name="height">Height in world coordinates.</param> /// <param name="xSegments">Number of x segments for tesselation.</param> /// <param name="ySegments">Number of y segments for tesselation.</param> /// <param name="normals">If true, plane normals are created.</param> /// <param name="numTexCoordSets">Number of 2d texture coord sets to use.</param> /// <param name="uTile">Number of times the texture should be repeated in the u direction.</param> /// <param name="vTile">Number of times the texture should be repeated in the v direction.</param> /// <param name="upVec">The up direction of the plane.</param> /// <returns></returns> public Mesh CreatePlane(string name, Plane plane, float width, float height, int xSegments, int ySegments, bool normals, int numTexCoordSets, float uTile, float vTile, Vector3 upVec, BufferUsage vertexBufferUsage, BufferUsage indexBufferUsage, bool vertexShadowBuffer, bool indexShadowBuffer) { Mesh mesh = CreateManual(name); SubMesh subMesh = mesh.CreateSubMesh(name + "SubMesh"); mesh.SharedVertexData = new VertexData(); VertexData vertexData = mesh.SharedVertexData; VertexDeclaration decl = vertexData.vertexDeclaration; int currOffset = 0; // add position data decl.AddElement(0, currOffset, VertexElementType.Float3, VertexElementSemantic.Position); currOffset += VertexElement.GetTypeSize(VertexElementType.Float3); // normals are optional if (normals) { decl.AddElement(0, currOffset, VertexElementType.Float3, VertexElementSemantic.Normal); currOffset += VertexElement.GetTypeSize(VertexElementType.Float3); } // add texture coords for (ushort i = 0; i < numTexCoordSets; i++) { decl.AddElement(0, currOffset, VertexElementType.Float2, VertexElementSemantic.TexCoords, i); currOffset += VertexElement.GetTypeSize(VertexElementType.Float2); } vertexData.vertexCount = (xSegments + 1) * (ySegments + 1); // create a new vertex buffer (based on current API) HardwareVertexBuffer vbuf = HardwareBufferManager.Instance.CreateVertexBuffer(decl.GetVertexSize(0), vertexData.vertexCount, vertexBufferUsage, vertexShadowBuffer); // get a reference to the vertex buffer binding VertexBufferBinding binding = vertexData.vertexBufferBinding; // bind the first vertex buffer binding.SetBinding(0, vbuf); // transform the plane based on its plane def Matrix4 translate = Matrix4.Identity; Matrix4 transform = Matrix4.Zero; Matrix4 rotation = Matrix4.Identity; Matrix3 rot3x3 = Matrix3.Zero; Vector3 xAxis, yAxis, zAxis; zAxis = plane.Normal; zAxis.Normalize(); yAxis = upVec; yAxis.Normalize(); xAxis = yAxis.Cross(zAxis); if (xAxis.Length == 0) { throw new AxiomException("The up vector for a plane cannot be parallel to the planes normal."); } rot3x3.FromAxes(xAxis, yAxis, zAxis); rotation = rot3x3; // set up transform from origin translate.Translation = plane.Normal * -plane.D; transform = translate * rotation; float xSpace = width / xSegments; float ySpace = height / ySegments; float halfWidth = width / 2; float halfHeight = height / 2; float xTexCoord = (1.0f * uTile) / xSegments; float yTexCoord = (1.0f * vTile) / ySegments; Vector3 vec = Vector3.Zero; Vector3 min = Vector3.Zero; Vector3 max = Vector3.Zero; float maxSquaredLength = 0; bool firstTime = true; // generate vertex data GeneratePlaneVertexData(vbuf, ySegments, xSegments, xSpace, halfWidth, ySpace, halfHeight, transform, firstTime, normals, rotation, numTexCoordSets, xTexCoord, yTexCoord, subMesh, ref min, ref max, ref maxSquaredLength); // generate face list Tesselate2DMesh(subMesh, xSegments + 1, ySegments + 1, false, indexBufferUsage, indexShadowBuffer); // generate bounds for the mesh mesh.BoundingBox = new AxisAlignedBox(min, max); mesh.BoundingSphereRadius = MathUtil.Sqrt(maxSquaredLength); mesh.Load(); mesh.Touch(); return(mesh); }