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 UpdateGeometry() { // Update zone C for (int k = 0; k < _nc; k++) { UpdateZoneCSlice(k); } //Update zone B for (int k = 0; k < _nb; k++) { UpdateZoneBSlice(k); } // Update zone A for (int k = 0; k < _na; k++) { UpdateZoneASlice(k); } //Upload changes fixed(Vertex *addr = &_vertices[0]) { _vertexBuffer. WriteData(0, _vertexBuffer.SizeInBytes, addr, true); } }
/// <summary> /// Convert Obj Model File to Ogre Mesh format /// </summary> /// <param name="fileStream">Obj File Stream</param> /// <returns>Ogre Mesh</returns> public MeshPtr ConvertObjToMesh(Stream fileStream) { List <Vector3> vertexObj = new List <Vector3>(); List <Vector3> faceObj = new List <Vector3>(); float[] vertices; float[] faces; StreamReader reader = new StreamReader(fileStream); string line; Regex floatNumber = new Regex(@"[\d\-][\d\.]*"); Regex ushortNumber = new Regex(@"\d+"); MatchCollection matchList; while ((line = reader.ReadLine()) != null) { //Read vertices if (line.Substring(0, 2) == "v ") { matchList = floatNumber.Matches(line); float x = Convert.ToSingle(matchList[0].ToString()); float y = Convert.ToSingle(matchList[1].ToString()); float z = Convert.ToSingle(matchList[2].ToString()); vertexObj.Add(new Vector3(x, y, z)); } //Read faces else if (line.Substring(0, 2) == "f ") { //Error here where invalid indices were given. This is because the OBJ file started indexing the verts from 1 instead of 0. matchList = ushortNumber.Matches(line); int v1 = -1 + Convert.ToUInt16(matchList[0].ToString()); int v2 = -1 + Convert.ToUInt16(matchList[1].ToString()); int v3 = -1 + Convert.ToUInt16(matchList[2].ToString()); faceObj.Add(new Vector3((ushort)v1, (ushort)v2, (ushort)v3)); } } int vertexNum = vertexObj.Count; vertices = new float[vertexNum * 3]; for (int i = 0; i < vertexNum; i++) { vertices[i * 3 + 0] = vertexObj[i].x; vertices[i * 3 + 1] = vertexObj[i].y; vertices[i * 3 + 2] = vertexObj[i].z; } int faceNum = faceObj.Count; faces = new float[faceNum * 3]; for (int i = 0; i < faceNum; i++) { faces[i * 3 + 0] = faceObj[i].x; faces[i * 3 + 1] = faceObj[i].y; faces[i * 3 + 2] = faceObj[i].z; } MeshPtr mesh = MeshManager.Singleton.CreateManual("mesh1", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); SubMesh subMesh = mesh.CreateSubMesh(); mesh.sharedVertexData = new VertexData(); mesh.sharedVertexData.vertexCount = (uint)vertexObj.Count; VertexDeclaration vdecl = mesh.sharedVertexData.vertexDeclaration; VertexBufferBinding vbind = mesh.sharedVertexData.vertexBufferBinding; vdecl.AddElement(0, 0, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION); HardwareVertexBufferSharedPtr vertexBuff = HardwareBufferManager.Singleton.CreateVertexBuffer((uint)(3 * System.Runtime.InteropServices.Marshal.SizeOf(typeof(float))), (uint)vertexObj.Count, HardwareBuffer.Usage.HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE); unsafe { GCHandle handle = GCHandle.Alloc(vertices, GCHandleType.Pinned); void * pVertex = (void *)handle.AddrOfPinnedObject(); vertexBuff.WriteData(0, vertexBuff.SizeInBytes, pVertex, true); handle.Free(); vbind.SetBinding(0, vertexBuff); } HardwareIndexBufferSharedPtr indexBuff = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_32BIT, (uint)(3 * faceObj.Count), HardwareBuffer.Usage.HBU_STATIC, true); unsafe { GCHandle handle = GCHandle.Alloc(faces, GCHandleType.Pinned); void * pFaces = (void *)handle.AddrOfPinnedObject(); indexBuff.WriteData(0, indexBuff.SizeInBytes, pFaces, true); handle.Free(); } subMesh.useSharedVertices = true; subMesh.indexData.indexBuffer = indexBuff; subMesh.indexData.indexStart = 0; subMesh.indexData.indexCount = (uint)(3 * faceObj.Count); mesh._setBounds(new AxisAlignedBox(-100, -100, -100, 100, 100, 100)); mesh.Touch(); reader.Close(); return(mesh); }
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; }
internal unsafe void UpdateGeometry() { if (!this.IsCreated) { return; } // 95% of camera far clip distance float TODO_Radius = this.SkyX.Camera.FarClipDistance * 0.95f; _vertices[0].X = 0; _vertices[0].Z = 0; _vertices[0].Y = TODO_Radius; _vertices[0].NX = 0; _vertices[0].NZ = 0; _vertices[0].NY = 1; _vertices[0].U = 4; _vertices[0].V = 4; _vertices[0].Opacity = 1; float angleStep = (Utility.PI / 2.0f) / (this.Circles - 1.0f); float r, uvr, c, s, sc; int x, y; for (y = 0; y < this.Circles - 1; y++) { r = Utility.Cos(Utility.PI / 2.0f - angleStep * (y + 1.0f)); uvr = (y + 1.0f) / (this.Circles - 1.0f); for (x = 0; x < this.Steps; x++) { c = Utility.Cos(Utility.TWO_PI * x / (float)this.Steps) * r; s = Utility.Sin(Utility.TWO_PI * x / (float)this.Steps) * r; sc = Utility.Sin(Utility.ACos(r)); _vertices[1 + y * this.Steps + x].X = c * TODO_Radius; _vertices[1 + y * this.Steps + x].Z = s * TODO_Radius; _vertices[1 + y * this.Steps + x].Y = sc * TODO_Radius; _vertices[1 + y * this.Steps + x].NX = c; _vertices[1 + y * this.Steps + x].NZ = s; _vertices[1 + y * this.Steps + x].NY = sc; _vertices[1 + y * this.Steps + x].U = (1.0f + c * uvr / r) * 4.0f; _vertices[1 + y * this.Steps + x].V = (1.0f + s * uvr / r) * 4.0f; _vertices[1 + y * this.Steps + x].Opacity = 1; } } r = Utility.Cos(angleStep); uvr = (this.Circles + 1.0f) / (this.Circles - 1.0f); for (x = 0; x < this.Steps; x++) { c = Utility.Cos(Utility.TWO_PI * x / (float)this.Steps) * r; s = Utility.Sin(Utility.TWO_PI * x / (float)this.Steps) * r; _vertices[1 + (this.Circles - 1) * this.Steps + x].X = _vertices[1 + (this.Circles - 2) * this.Steps + x].X; _vertices[1 + (this.Circles - 1) * this.Steps + x].Z = _vertices[1 + (this.Circles - 2) * this.Steps + x].Z; _vertices[1 + (this.Circles - 1) * this.Steps + x].Y = _vertices[1 + (this.Circles - 2) * this.Steps + x].Y - TODO_Radius * this.SkydomeFadingPercent; _vertices[1 + (this.Circles - 1) * this.Steps + x].NX = _vertices[1 + (this.Circles - 2) * this.Steps + x].NX; _vertices[1 + (this.Circles - 1) * this.Steps + x].NZ = _vertices[1 + (this.Circles - 2) * this.Steps + x].NZ; _vertices[1 + (this.Circles - 1) * this.Steps + x].NY = _vertices[1 + (this.Circles - 2) * this.Steps + x].NY; _vertices[1 + (this.Circles - 1) * this.Steps + x].U = (1.0f + c * uvr / 4) * 4.0f; _vertices[1 + (this.Circles - 1) * this.Steps + x].V = (1.0f + s * uvr / 4) * 4.0f; _vertices[1 + (this.Circles - 1) * this.Steps + x].Opacity = this.SmoothSkydomeFading ? 0 : 1; } // Update data fixed(PosUVVertex *addr = &_vertices[0]) { _vertexBuffer.WriteData(0, _vertexBuffer.SizeInBytes, addr, true); } // Update bounds AxisAlignedBox meshBounds = new AxisAlignedBox(new Vector3(-TODO_Radius, 0, -TODO_Radius), new Vector3(TODO_Radius, TODO_Radius, TODO_Radius)); //_mesh.BoundingBox = meshBounds; _mesh._setBounds(meshBounds); _sceneNode.NeedUpdate(); //for (int i = 0; i < _vertices.Length; i++) // LogVertices(_vertices[i]); }
public unsafe void createMesh() { /// Create the mesh via the MeshManager MeshPtr msh = MeshManager.Singleton.CreateManual("ColourCube", "General"); /// Create one submesh SubMesh sub = msh.CreateSubMesh(); /// Define the vertices (8 vertices, each consisting of 2 groups of 3 floats //const int nVertices = 8; //int row = recordno; // int col = demcol; int row = recordno; int col = 1100; int step = 10; int vbufCount; uint nVertices = (uint)(col * row); float[] vertices = CreateVertices(row, col, step, out vbufCount); /// Define 12 triangles (two triangles per cube face) /// The values in this table refer to vertices in the above table uint ibufCount; ushort[] faces = CreateFaces(row, col, out ibufCount); /// Create vertex data structure for 8 vertices shared between submeshes msh.sharedVertexData = new VertexData(); msh.sharedVertexData.vertexCount = nVertices; /// Create declaration (memory format) of vertex data VertexDeclaration decl = msh.sharedVertexData.vertexDeclaration; uint offset = 0; // 1st buffer decl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3); decl.AddElement(0, offset, VertexElementType.VET_FLOAT2, VertexElementSemantic.VES_TEXTURE_COORDINATES); offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT2); /// Allocate vertex buffer of the requested number of vertices (vertexCount) /// and bytes per vertex (offset) HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.Singleton.CreateVertexBuffer(offset, msh.sharedVertexData.vertexCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY); /// Upload the vertex data to the card fixed(void *p = vertices) { vbuf.WriteData(0, vbuf.SizeInBytes, p, true);// writeData(0, vbuf->getSizeInBytes(), vertices, true); } /// Set vertex buffer binding so buffer 0 is bound to our vertex buffer VertexBufferBinding bind = msh.sharedVertexData.vertexBufferBinding;// msh->sharedVertexData->vertexBufferBinding; bind.SetBinding(0, vbuf); /// Allocate index buffer of the requested number of vertices (ibufCount) HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_16BIT, ibufCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY); /// Upload the index data to the card fixed(void *p = faces) { ibuf.WriteData(0, ibuf.SizeInBytes, p, true); } /// Set parameters of the submesh sub.useSharedVertices = true; sub.indexData.indexBuffer = ibuf; sub.indexData.indexCount = ibufCount; sub.indexData.indexStart = 0; /// Set bounding information (for culling) msh._setBounds(new AxisAlignedBox(min, max)); // msh._setBoundingSphereRadius(Mogre.Math.Sqrt(3 * 100 * 100)); /// Notify Mesh object that it has been loaded msh.Load(); }
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(); }