public float Distance(float2 v) { float2 delta = v - this; float len = delta.Dot(delta); return (float)System.Math.Sqrt((double)len); }
// math functions public float Dot(float2 v) { return (x * v.x) + (y * v.y); }
public void MakeLatLonSphere2(int latDivs, int lonDivs) { List<VertexPNT> verts = new List<VertexPNT>(); List<int> faceTemp = new List<int>(); int index = 0; for (int lat = -latDivs / 2; lat <= latDivs / 2; lat++) { for (int lon = 0; lon <= lonDivs; lon++) { float2 pos = new float2((float)lon / lonDivs, (float)lat / latDivs); float2 radScale = new float2((float)(2 * Math.PI), (float)(Math.PI)); if ((lon != lonDivs) && (lat != latDivs /2)) { int row = lonDivs + 1; faceTemp.Add(index); faceTemp.Add(index + row); faceTemp.Add(index + 1); faceTemp.Add(index + row); faceTemp.Add(index + row + 1); faceTemp.Add(index + 1); } float3 posXYZ = new float3(); VertexPNT v0 = new VertexPNT(); v0._pos = SphericalCoord(pos * radScale, out posXYZ); v0.normal = posXYZ.Normalize(); v0.uv0 = new float2(1.0f-pos.x, pos.y + 0.5f); verts.Add(v0); index++; } } int[] faceList = faceTemp.ToArray();// GenerateDefaultFaceList(verts); indexBuffer = Buffer.Create(Global._G.device, BindFlags.IndexBuffer, faceList); numIndexes = faceList.Length; //public EffectPass pass; //EffectTechnique technique = effect.GetTechniqueByIndex(0); shaderName = "EarthRender"; technique = Global._G.effect.GetTechniqueByName(shaderName); EffectPass pass = technique.GetPassByIndex(0); var passSignature = pass.Description.Signature; layout = new InputLayout(Global._G.device, passSignature, new[] //ShaderSignature sig = ShaderSignature.GetInputSignature(Global._G.vertexShaderByteCode); //layout = new InputLayout(Global._G.device, sig, new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12+12, 0) }); vertices = Buffer.Create(Global._G.device, BindFlags.VertexBuffer, verts.ToArray()); //SharpDX.Direct3D11.Resource tex = Texture2D.FromFile<Texture2D>(Global._G.device, @"Content\world.200401.3x540x270.jpg"); ////SharpDX.Direct3D11.Resource tex = Texture2D.FromFile<Texture2D>(Global._G.device, @"C:\dev\Globe\Content\Custom\world.200401.3x5400x2700.jpg"); //textureView = new ShaderResourceView(Global._G.device, tex); //tex.Dispose(); textureView = new ShaderResourceView[] { Global._G.texMan.texDict["world.200401.3x540x270"].textureView, //Global._G.texMan.texDict["world.200407.3x5400x2700"].textureView, //Global._G.texMan.texDict["world.200408.3x8192x8192"].textureView, Global._G.texMan.texDict["noiseRGBA"].textureView, Global._G.texMan.texDict["cloud_combined_1024"].textureView }; // This is handled the the shader effects file. Maybe someday we can make an override if needed. //sampler = new SamplerState(Global._G.device, new SamplerStateDescription() //{ // Filter = Filter.Anisotropic, // AddressU = TextureAddressMode.Wrap, // AddressV = TextureAddressMode.Wrap, // AddressW = TextureAddressMode.Wrap, // BorderColor = Color.Black, // ComparisonFunction = Comparison.Never, // MaximumAnisotropy = 16, // MipLodBias = 0, // MinimumLod = 0, // MaximumLod = 16, //}); // CreateVertexBuffer(faces); // m_shader = "simpleGlobeShade"; }
public static float2 Lerp(float2 a, float2 b, float alpha) { return (a * (1.0f - alpha)) + (b * alpha); }
public float2 Rotate(float rot) { float2 result = new float2(); result.x = (float)(System.Math.Cos(rot) * x - System.Math.Sin(rot) * y); result.y = (float)(System.Math.Sin(rot) * x + System.Math.Cos(rot) * y); return result; }
public float3(float2 v) { x = v.x; y = v.y; z = 0.0f; }
public void MergePoint(float2 point1) { m_min = m_min.Min(point1); m_max = m_max.Max(point1); }
public float2 PowSign(float v) { float2 ret = new float2(); ret.x = System.Math.Sign(x) * (float)System.Math.Pow(System.Math.Abs(x), v); ret.y = System.Math.Sign(y) * (float)System.Math.Pow(System.Math.Abs(y), v); return ret; }
public bool Contains(float2 point) { if (!IsDefined()) return false; if (m_min.x > point.x) return false; if (m_min.y > point.y) return false; if (m_max.x < point.x) return false; if (m_max.y < point.y) return false; return true; }
public void Intersection(Box2 box) { if (!Intersects(box)) { SetUndefined(); return; } m_min = m_min.Max(box.m_min); m_max = m_max.Min(box.m_max); }
public Box2(float2 min, float2 max) { m_min = min.Min(max); m_max = min.Max(max); }
public int2(float2 f) { x = (int)f.x; y = (int)f.y; }
public float4(float2 xy, float2 zw) { x = xy.x; y = xy.y; z = zw.x; w = zw.y; }
public float2 Max(float2 v) { float2 ret = new float2(); ret.x = x > v.x ? x : v.x; ret.y = y > v.y ? y : v.y; return ret; }
public void SetUndefined() { m_min = new float2(float.MaxValue, float.MaxValue); m_max = new float2(float.MinValue, float.MinValue); }
public float2 Min(float2 v) { float2 ret = new float2(); ret.x = x < v.x ? x : v.x; ret.y = y < v.y ? y : v.y; return ret; }
public float2(float2 f) { x = f.x; y = f.y; }
public float2 Reflect(float2 normal) { return this - normal * this.Dot(normal) * 2.0f; //v = i - 2 * dot(i, n) * n. }
// Special functions public static bool IntersectCircleWithLine(float2 circlePos, float radius, float2 posA, float2 posB, out float distance) { // Use dot product along line to find closest point on line float dot = (posB - posA).Normalize().Dot(circlePos - posA); float2 pointOnLine = (posB - posA).Normalize() * dot + posA; // Clamp that point to line end points if outside //if ((dot - radius) < 0) pointOnLine = posA; if (dot < 0) pointOnLine = posA; //if ((dot + radius) > (posB - posA).Length()) pointOnLine = posB; if ((dot) > (posB - posA).Length()) pointOnLine = posB; // Distance formula from that point to sphere center, compare with radius. distance = pointOnLine.Distance(circlePos); if (distance > radius) return false; return true; }
public float2 Saturate(float2 v) { return v.Min(new float2(1, 1)).Max(new float2(0, 0)); }
public static bool IntersectSweptCircleWithLine(float2 circlePos, float circleRadius, float2 circleVel, float2 posA, float2 posB, out float t) { for (t = 0.0f; t <= 1.0f; t += 0.1f) { float dist; if (IntersectCircleWithLine(circlePos + circleVel * t, circleRadius, posA, posB, out dist)) return true; } return false; }
public void UpdateMouseControls() { if (leftButton || rightButton) { float2 delta = new float2((mousePos - dragStart) * 2); delta.x *= aspectRatio; delta /= viewWidth; if (leftButton && rightButton) { Dolly(delta.x * 1000); } else { if (leftButton) { // rotation camera float sensitivity = 1.5f; float3x3 roty = float3x3.RotateY(delta.x * sensitivity); float3 lookAtDelta = m_cameraPos - m_cameraLookat; lookAtDelta = roty.Mul(lookAtDelta); float3 rightVector = lookAtDelta.Cross(m_cameraUp).Normalize(); float3x3 upRot = float3x3.AxisAngle(rightVector, delta.y * sensitivity); lookAtDelta = upRot.Mul(lookAtDelta); m_cameraPos = lookAtDelta + m_cameraLookat; float3 newLookAtDelta = m_cameraPos - m_cameraLookat; float3 newRightVector = newLookAtDelta.Cross(m_cameraUp).Normalize(); if (newRightVector.Dot(rightVector) < 0) m_cameraUp.y = -m_cameraUp.y; } else { // move camera float3 lookAtDelta = m_cameraPos - m_cameraLookat; float3 rightVector = lookAtDelta.Cross(m_cameraUp).Normalize(); float3 upVector = rightVector.Cross(lookAtDelta).Normalize(); float moveScale = 0.5f * lookAtDelta.Length(); m_cameraPos -= rightVector * delta.x * moveScale; m_cameraLookat -= rightVector * delta.x * moveScale; m_cameraPos += upVector * delta.y * moveScale; m_cameraLookat += upVector * delta.y * moveScale; } } } SetupCamera(); }
/* public void GenCube() { //int indexBufSizeInBytes = 4*3; int[] indexes = new int[] { 0, 1, 2 }; // SharpDX.Direct3D11.BufferDescription indexBufDesc = new BufferDescription(indexBufSizeInBytes, ResourceUsage.Default, // BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0); indexBuffer = Buffer.Create(Global._G.device, BindFlags.IndexBuffer, indexes); // Layout from VertexShader input signature //ShaderBytecode passSignature = pass.Description.Signature; ShaderSignature sig = ShaderSignature.GetInputSignature(Global._G.vertexShaderByteCode); layout = new InputLayout(Global._G.device, sig, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16+12, 0) //new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0), //new InputElement("TEXCOORD", 0, Format.R32G32B32A32_Float, 32, 0) }); // Instantiate Vertex buiffer from vertex data //Buffer vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[] // { // new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 0.0f), // new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 0.0f), // new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 0.0f), // }); // Instantiate Vertex buiffer from vertex data vertices = Buffer.Create(Global._G.device, BindFlags.VertexBuffer, new[] { // 3D coordinates UV Texture coordinates -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Front -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // BACK 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f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f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Right 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }); SharpDX.Direct3D11.Resource tex = Texture2D.FromFile<Texture2D>(Global._G.device, "noiseRGBA.bmp"); textureView = new ShaderResourceView(Global._G.device, tex); sampler = new SamplerState(Global._G.device, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = 0, MaximumLod = 16, }); numIndexes = 3; }*/ public float3 SphericalCoord(float2 latLon, out float3 pos) { pos = new float3( -(float)(Math.Cos(latLon.x) * Math.Cos(latLon.y)), -(float)Math.Sin(latLon.y), (float)(Math.Sin(latLon.x) * Math.Cos(latLon.y)) ); float3 normal = pos.Normalize(); float angleXY = (float)Math.Atan2(normal.x, normal.y) * 0.0f; float angleYZ = (float)Math.Atan2(normal.y, normal.z) * 1.0f; //float angleZX = Math.Atan2(normal.z, normal.x)*8; float finalR = ((float)Math.Cos(angleXY)) * (1.0f - Math.Abs(normal.z)); finalR *= ((float)Math.Cos(angleYZ)) * (1.0f - Math.Abs(normal.x)); //finalR += (sin(angleZX)) * (1.0-abs(normal.y)); return pos;// *finalR; }