public static warp_Object BOX(float xsize, float ysize, float zsize) { float x = Math.Abs(xsize / 2); float y = Math.Abs(ysize / 2); float z = Math.Abs(zsize / 2); float xx, yy, zz; warp_Object n = new warp_Object(); int[] xflag = new int[6]; int[] yflag = new int[6]; int[] zflag = new int[6]; xflag [0] = 10; yflag [0] = 3; zflag [0] = 0; xflag [1] = 10; yflag [1] = 15; zflag [1] = 3; xflag [2] = 15; yflag [2] = 3; zflag [2] = 10; xflag [3] = 10; yflag [3] = 0; zflag [3] = 12; xflag [4] = 0; yflag [4] = 3; zflag [4] = 5; xflag [5] = 5; yflag [5] = 3; zflag [5] = 15; for (int side = 0; side < 6; side++) { for (int i = 0; i < 4; i++) { xx = ((xflag [side] & (1 << i)) > 0) ? x : -x; yy = ((yflag [side] & (1 << i)) > 0) ? y : -y; zz = ((zflag [side] & (1 << i)) > 0) ? z : -z; n.addVertex(new warp_Vector(xx, yy, zz), i & 1, (i & 2) >> 1); } int t = side << 2; n.addTriangle(t, t + 2, t + 3); n.addTriangle(t, t + 3, t + 1); } return(n); }
public static warp_Object ROTATIONOBJECT(warp_Vector[] path, int sides) { int steps = sides + 1; warp_Object newObject = new warp_Object(); double alpha = 2 * pi / ((double)steps - 1); float qx, qz; int nodes = path.GetLength(0); warp_Vertex vertex = null; float u, v; // Texture coordinates for (int j = 0; j < steps; j++) { u = (float)(steps - j - 1) / (float)(steps - 1); for (int i = 0; i < nodes; i++) { v = (float)i / (float)(nodes - 1); qx = (float)(path [i].x * Math.Cos(j * alpha) + path [i].z * Math.Sin(j * alpha)); qz = (float)(path [i].z * Math.Cos(j * alpha) - path [i].x * Math.Sin(j * alpha)); vertex = new warp_Vertex(new warp_Vector(qx, path [i].y, qz)); vertex.u = u; vertex.v = v; newObject.addVertex(vertex); } } for (int j = 0; j < steps - 1; j++) { for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle(i + nodes * j, i + nodes * (j + 1), i + 1 + nodes * j); newObject.addTriangle(i + nodes * (j + 1), i + 1 + nodes * (j + 1), i + 1 + nodes * j); } } for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle(i + nodes * (steps - 1), i, i + 1 + nodes * (steps - 1)); newObject.addTriangle(i, i + 1, i + 1 + nodes * (steps - 1)); } return(newObject); }
public static warp_Object SIMPLEPLANE(float size, bool doubleSided) { warp_Object newObject = new warp_Object(); newObject.addVertex(new warp_Vertex(new warp_Vector(-size, 0f, size), warp_Vector.UnitY, 0, 0)); newObject.addVertex(new warp_Vertex(new warp_Vector(size, 0f, size), warp_Vector.UnitY, 4f, 0)); newObject.addVertex(new warp_Vertex(new warp_Vector(size, 0f, -size), warp_Vector.UnitY, 4f, 4f)); newObject.addVertex(new warp_Vertex(new warp_Vector(-size, 0f, -size), warp_Vector.UnitY, 0, 4f)); newObject.addTriangle(0, 3, 2); newObject.addTriangle(0, 2, 1); if (doubleSided) { newObject.addTriangle(0, 2, 3); newObject.addTriangle(0, 1, 2); } return(newObject); }
public static warp_Object SIMPLEPLANE (float size, bool doubleSided) { warp_Object newObject = new warp_Object (); newObject.addVertex (new warp_Vertex (new warp_Vector (-size, 0f, size), warp_Vector.UnitY, 0, 0)); newObject.addVertex (new warp_Vertex (new warp_Vector (size, 0f, size), warp_Vector.UnitY, 4f, 0)); newObject.addVertex (new warp_Vertex (new warp_Vector (size, 0f, -size), warp_Vector.UnitY, 4f, 4f)); newObject.addVertex (new warp_Vertex (new warp_Vector (-size, 0f, -size), warp_Vector.UnitY, 0, 4f)); newObject.addTriangle (0, 3, 2); newObject.addTriangle (0, 2, 1); if (doubleSided) { newObject.addTriangle (0, 2, 3); newObject.addTriangle (0, 1, 2); } return newObject; }
void readPointList(BinaryReader inStream) { int v1, v2, v3; int triangles = readShort(inStream); for (int i = 0; i < triangles; i++) { v1 = readShort(inStream); v2 = readShort(inStream); v3 = readShort(inStream); readShort(inStream); currentObject.addTriangle(currentObject.vertex(v1), currentObject.vertex(v2), currentObject.vertex(v3)); } }
void CreatePrim (WarpRenderer renderer, ISceneChildEntity prim) { try { if ((PCode)prim.Shape.PCode != PCode.Prim) return; if (prim.Scale.LengthSquared () < MIN_PRIM_SIZE * MIN_PRIM_SIZE) return; Primitive omvPrim = prim.Shape.ToOmvPrimitive (prim.OffsetPosition, prim.GetRotationOffset ()); FacetedMesh renderMesh = null; // Are we dealing with a sculptie or mesh? if (omvPrim.Sculpt != null && omvPrim.Sculpt.SculptTexture != UUID.Zero) { // Try fetching the asset byte [] sculptAsset = m_scene.AssetService.GetData (omvPrim.Sculpt.SculptTexture.ToString ()); if (sculptAsset != null) { // Is it a mesh? if (omvPrim.Sculpt.Type == SculptType.Mesh) { AssetMesh meshAsset = new AssetMesh (omvPrim.Sculpt.SculptTexture, sculptAsset); FacetedMesh.TryDecodeFromAsset (omvPrim, meshAsset, DetailLevel.Highest, out renderMesh); meshAsset = null; } else // It's sculptie { Image sculpt = m_imgDecoder.DecodeToImage (sculptAsset); if (sculpt != null) { renderMesh = m_primMesher.GenerateFacetedSculptMesh (omvPrim, (Bitmap)sculpt, DetailLevel.Medium); sculpt.Dispose (); } } sculptAsset = null; } else { // missing sculpt data... replace with something renderMesh = m_primMesher.GenerateFacetedMesh (omvPrim, DetailLevel.Medium); } } else // Prim { renderMesh = m_primMesher.GenerateFacetedMesh (omvPrim, DetailLevel.Medium); } if (renderMesh == null) return; warp_Vector primPos = ConvertVector (prim.GetWorldPosition ()); warp_Quaternion primRot = ConvertQuaternion (prim.GetRotationOffset ()); warp_Matrix m = warp_Matrix.quaternionMatrix (primRot); if (prim.ParentID != 0) { ISceneEntity group = m_scene.GetGroupByPrim (prim.LocalId); if (group != null) m.transform (warp_Matrix.quaternionMatrix (ConvertQuaternion (group.RootChild.GetRotationOffset ()))); } warp_Vector primScale = ConvertVector (prim.Scale); string primID = prim.UUID.ToString (); // Create the prim faces for (int i = 0; i < renderMesh.Faces.Count; i++) { Face renderFace = renderMesh.Faces [i]; string meshName = primID + "-Face-" + i; warp_Object faceObj = new warp_Object (renderFace.Vertices.Count, renderFace.Indices.Count / 3); foreach (Vertex v in renderFace.Vertices) { warp_Vector pos = ConvertVector (v.Position); warp_Vector norm = ConvertVector (v.Normal); if (prim.Shape.SculptTexture == UUID.Zero) norm = norm.reverse (); warp_Vertex vert = new warp_Vertex (pos, norm, v.TexCoord.X, v.TexCoord.Y); faceObj.addVertex (vert); } for (int j = 0; j < renderFace.Indices.Count;) { faceObj.addTriangle ( renderFace.Indices [j++], renderFace.Indices [j++], renderFace.Indices [j++]); } Primitive.TextureEntryFace teFace = prim.Shape.Textures.GetFace ((uint)i); string materialName; Color4 faceColor = GetFaceColor (teFace); if (m_texturePrims && (prim.Scale.LengthSquared () > m_texturePrimSize)) { materialName = GetOrCreateMaterial (renderer, faceColor, teFace.TextureID); } else { materialName = GetOrCreateMaterial (renderer, faceColor); } faceObj.transform (m); faceObj.setPos (primPos); faceObj.scaleSelf (primScale.x, primScale.y, primScale.z); renderer.Scene.addObject (meshName, faceObj); renderer.SetObjectMaterial (meshName, materialName); faceObj = null; } renderMesh.Faces.Clear (); renderMesh = null; } catch (Exception ex) { MainConsole.Instance.Warn ("[Warp3D]: Exception creating prim, " + ex); } }
warp_Object CreateTerrain (WarpRenderer renderer, bool textureTerrain) { ITerrainChannel terrain = m_scene.RequestModuleInterface<ITerrainChannel> (); float diffX = 1.0f; //(float) m_scene.RegionInfo.RegionSizeX/(float) Constants.RegionSize; float diffY = 1.0f; //(float) m_scene.RegionInfo.RegionSizeY/(float) Constants.RegionSize; int newRsX = m_scene.RegionInfo.RegionSizeX / (int)diffX; int newRsY = m_scene.RegionInfo.RegionSizeY / (int)diffY; warp_Object obj = new warp_Object (newRsX * newRsY, ((newRsX - 1) * (newRsY - 1) * 2)); for (float y = 0; y < m_scene.RegionInfo.RegionSizeY; y += diffY) { for (float x = 0; x < m_scene.RegionInfo.RegionSizeX; x += diffX) { float t_height = terrain [(int)x, (int)y]; float waterHeight = (float)m_scene.RegionInfo.RegionSettings.WaterHeight; //clamp to eliminate artifacts t_height = Utils.Clamp (t_height, waterHeight - 0.5f, waterHeight + 0.5f); if (t_height < 0.0f) t_height = 0.0f; warp_Vector pos = ConvertVector (x / diffX, y / diffY, t_height); obj.addVertex ( new warp_Vertex (pos, x / m_scene.RegionInfo.RegionSizeX, (m_scene.RegionInfo.RegionSizeY - y) / (m_scene.RegionInfo.RegionSizeY))); } } const float normal_map_reduction = 2.0f; //2.0f-2.5f is the sweet spot for (float y = 0; y < m_scene.RegionInfo.RegionSizeY; y += diffY) { for (float x = 0; x < m_scene.RegionInfo.RegionSizeX; x += diffX) { float newX = x / diffX; float newY = y / diffY; if (newX < newRsX - 1 && newY < newRsY - 1) { int v = (int)(newY * newRsX + newX); // Normal Vector3 v1 = new Vector3 (newX, newY, (terrain [(int)x, (int)y]) / normal_map_reduction); Vector3 v2 = new Vector3 (newX + 1, newY, (terrain [(int)x + 1, (int)y]) / normal_map_reduction); Vector3 v3 = new Vector3 (newX, newY + 1, (terrain [(int)x, (int)(y + 1)]) / normal_map_reduction); warp_Vector norm = ConvertVector (SurfaceNormal (v1, v2, v3)); norm = norm.reverse (); obj.vertex (v).n = norm; // Triangle 1 obj.addTriangle ( v, v + 1, v + newRsX); // Triangle 2 obj.addTriangle ( v + newRsX + 1, v + newRsX, v + 1); } } } renderer.Scene.addObject ("Terrain", obj); renderer.Scene.sceneobject ("Terrain").setPos (0.0f, 0.0f, 0.0f); UUID [] textureIDs = new UUID [4]; float [] startHeights = new float [4]; float [] heightRanges = new float [4]; RegionSettings regionInfo = m_scene.RegionInfo.RegionSettings; textureIDs [0] = regionInfo.TerrainTexture1; textureIDs [1] = regionInfo.TerrainTexture2; textureIDs [2] = regionInfo.TerrainTexture3; textureIDs [3] = regionInfo.TerrainTexture4; startHeights [0] = (float)regionInfo.Elevation1SW; startHeights [1] = (float)regionInfo.Elevation1NW; startHeights [2] = (float)regionInfo.Elevation1SE; startHeights [3] = (float)regionInfo.Elevation1NE; heightRanges [0] = (float)regionInfo.Elevation2SW; heightRanges [1] = (float)regionInfo.Elevation2NW; heightRanges [2] = (float)regionInfo.Elevation2SE; heightRanges [3] = (float)regionInfo.Elevation2NE; uint globalX, globalY; Utils.LongToUInts (m_scene.RegionInfo.RegionHandle, out globalX, out globalY); Bitmap image = TerrainSplat.Splat (terrain, textureIDs, startHeights, heightRanges, new Vector3d (globalX, globalY, 0.0), m_scene.AssetService, textureTerrain); warp_Texture texture = new warp_Texture (image); warp_Material material = new warp_Material (texture); material.setReflectivity (0); // reduces tile seams a bit thanks lkalif renderer.Scene.addMaterial ("TerrainColor", material); renderer.SetObjectMaterial ("Terrain", "TerrainColor"); image.Dispose (); return obj; }
public static warp_Object TUBE(warp_Vector[] path, float r, int steps, bool closed) { warp_Vector[] circle = new warp_Vector[steps]; float angle; for (int i = 0; i < steps; i++) { angle = 2 * 3.14159265f * (float)i / (float)steps; circle [i] = new warp_Vector(r * warp_Math.cos(angle), r * warp_Math.sin(angle), 0f); } warp_Object newObject = new warp_Object(); int segments = path.GetLength(0); warp_Vector forward, up, right; warp_Matrix frenetmatrix; warp_Vertex tempvertex; float relx, rely; int a, b, c, d; for (int i = 0; i < segments; i++) { // Calculate frenet frame matrix if (i != segments - 1) { forward = warp_Vector.sub(path [i + 1], path [i]); } else { if (!closed) { forward = warp_Vector.sub(path [i], path [i - 1]); } else { forward = warp_Vector.sub(path [1], path [0]); } } forward.normalize(); up = new warp_Vector(0f, 0f, 1f); right = warp_Vector.getNormal(forward, up); up = warp_Vector.getNormal(forward, right); frenetmatrix = new warp_Matrix(right, up, forward); frenetmatrix.shift(path [i].x, path [i].y, path [i].z); // Add nodes relx = (float)i / (float)(segments - 1); for (int k = 0; k < steps; k++) { rely = (float)k / (float)steps; tempvertex = new warp_Vertex(circle [k].transform(frenetmatrix)); tempvertex.u = relx; tempvertex.v = rely; newObject.addVertex(tempvertex); } } for (int i = 0; i < segments - 1; i++) { for (int k = 0; k < steps - 1; k++) { a = i * steps + k; b = a + 1; c = a + steps; d = b + steps; newObject.addTriangle(a, c, b); newObject.addTriangle(b, c, d); } a = (i + 1) * steps - 1; b = a + 1 - steps; c = a + steps; d = b + steps; newObject.addTriangle(a, c, b); newObject.addTriangle(b, c, d); } return(newObject); }
public static warp_Object BOX (float xsize, float ysize, float zsize) { float x = (float)Math.Abs (xsize / 2); float y = (float)Math.Abs (ysize / 2); float z = (float)Math.Abs (zsize / 2); float xx, yy, zz; warp_Object n = new warp_Object (); int[] xflag = new int[6]; int[] yflag = new int[6]; int[] zflag = new int[6]; xflag [0] = 10; yflag [0] = 3; zflag [0] = 0; xflag [1] = 10; yflag [1] = 15; zflag [1] = 3; xflag [2] = 15; yflag [2] = 3; zflag [2] = 10; xflag [3] = 10; yflag [3] = 0; zflag [3] = 12; xflag [4] = 0; yflag [4] = 3; zflag [4] = 5; xflag [5] = 5; yflag [5] = 3; zflag [5] = 15; for (int side = 0; side < 6; side++) { for (int i = 0; i < 4; i++) { xx = ((xflag [side] & (1 << i)) > 0) ? x : -x; yy = ((yflag [side] & (1 << i)) > 0) ? y : -y; zz = ((zflag [side] & (1 << i)) > 0) ? z : -z; n.addVertex (new warp_Vector (xx, yy, zz), i & 1, (i & 2) >> 1); } int t = side << 2; n.addTriangle (t, t + 2, t + 3); n.addTriangle (t, t + 3, t + 1); } return n; }
public static warp_Object TUBE (warp_Vector[] path, float r, int steps, bool closed) { warp_Vector[] circle = new warp_Vector[steps]; float angle; for (int i = 0; i < steps; i++) { angle = 2 * 3.14159265f * (float)i / (float)steps; circle [i] = new warp_Vector (r * warp_Math.cos (angle), r * warp_Math.sin (angle), 0f); } warp_Object newObject = new warp_Object (); int segments = path.GetLength (0); warp_Vector forward, up, right; warp_Matrix frenetmatrix; warp_Vertex tempvertex; float relx, rely; int a, b, c, d; for (int i = 0; i < segments; i++) { // Calculate frenet frame matrix if (i != segments - 1) { forward = warp_Vector.sub (path [i + 1], path [i]); } else { if (!closed) { forward = warp_Vector.sub (path [i], path [i - 1]); } else { forward = warp_Vector.sub (path [1], path [0]); } } forward.normalize (); up = new warp_Vector (0f, 0f, 1f); right = warp_Vector.getNormal (forward, up); up = warp_Vector.getNormal (forward, right); frenetmatrix = new warp_Matrix (right, up, forward); frenetmatrix.shift (path [i].x, path [i].y, path [i].z); // Add nodes relx = (float)i / (float)(segments - 1); for (int k = 0; k < steps; k++) { rely = (float)k / (float)steps; tempvertex = new warp_Vertex (circle [k].transform (frenetmatrix)); tempvertex.u = relx; tempvertex.v = rely; newObject.addVertex (tempvertex); } } for (int i = 0; i < segments - 1; i++) { for (int k = 0; k < steps - 1; k++) { a = i * steps + k; b = a + 1; c = a + steps; d = b + steps; newObject.addTriangle (a, c, b); newObject.addTriangle (b, c, d); } a = (i + 1) * steps - 1; b = a + 1 - steps; c = a + steps; d = b + steps; newObject.addTriangle (a, c, b); newObject.addTriangle (b, c, d); } return newObject; }
public static warp_Object ROTATIONOBJECT (warp_Vector[] path, int sides) { int steps = sides + 1; warp_Object newObject = new warp_Object (); double alpha = 2 * pi / ((double)steps - 1); float qx, qz; int nodes = path.GetLength (0); warp_Vertex vertex = null; float u, v; // Texture coordinates for (int j = 0; j < steps; j++) { u = (float)(steps - j - 1) / (float)(steps - 1); for (int i = 0; i < nodes; i++) { v = (float)i / (float)(nodes - 1); qx = (float)(path [i].x * Math.Cos (j * alpha) + path [i].z * Math.Sin (j * alpha)); qz = (float)(path [i].z * Math.Cos (j * alpha) - path [i].x * Math.Sin (j * alpha)); vertex = new warp_Vertex (new warp_Vector (qx, path [i].y, qz)); vertex.u = u; vertex.v = v; newObject.addVertex (vertex); } } for (int j = 0; j < steps - 1; j++) { for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle (i + nodes * j, i + nodes * (j + 1), i + 1 + nodes * j); newObject.addTriangle (i + nodes * (j + 1), i + 1 + nodes * (j + 1), i + 1 + nodes * j); } } for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle (i + nodes * (steps - 1), i, i + 1 + nodes * (steps - 1)); newObject.addTriangle (i, i + 1, i + 1 + nodes * (steps - 1)); } return newObject; }