private void TestToStringWithCulture(CultureInfo culture) { CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = culture; try { string listSeparator = culture.TextInfo.ListSeparator; string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; var o = new Vec4F(1.1f, 2.2f, 3.3f, 4.4f); string s = o.ToString(null, null); TestToStringResults(o, s, listSeparator, decimalSeparator); string s2 = o.ToString(); Assert.AreEqual(s, s2); s = o.ToString("G", culture); TestToStringResults(o, s, listSeparator, decimalSeparator); s = o.ToString("R", culture); TestToStringResults(o, s, listSeparator, decimalSeparator); } finally { Thread.CurrentThread.CurrentCulture = originalCulture; } }
public static void SetObjectProperty(uint typeid, ulong instanceId, uint propId, Color color) { Vec4F val = new Vec4F(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f); IntPtr ptr = new IntPtr(&val); int sz = Marshal.SizeOf(val); NativeSetObjectProperty(typeid, propId, instanceId, ptr, sz); }
private void TestToStringResults(Vec4F o, string s, string listSeparator, string decimalSeparator) { string[] results = s.Split(new[] { listSeparator }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual(results.Length, 4); foreach (string oneFloatString in results) Assert.True(oneFloatString.Contains(decimalSeparator)); Assert.AreEqual(float.Parse(results[0]), o.X); Assert.AreEqual(float.Parse(results[1]), o.Y); Assert.AreEqual(float.Parse(results[2]), o.Z); Assert.AreEqual(float.Parse(results[3]), o.W); }
/// <summary> /// Change text color /// </summary> /// <param name="vec">Vec3F containing the RGB color values.</param> /// <exception cref="ArgumentOutOfRangeException">Normalized color values must be /// between 0 and 1.</exception> public void SetColor(Vec4F vec) { if (vec.X < 0.0f || vec.X > 1.0f || vec.Y < 0.0f || vec.Y > 1.0f || vec.Z < 0.0f || vec.Z > 1.0f || vec.W < 0.0f || vec.W > 1.0f) { throw new ArgumentOutOfRangeException($"ARGB Color values must be between 0 and 1: {vec}"); } color = System.Drawing.Color.FromArgb((int)(vec.X * 255.0f), (int)(vec.Y * 255.0f), (int)(vec.Z * 255.0f), (int)(vec.W * 255.0f)); CreateBitmapTexture(); }
private void TestToStringResults(Vec4F o, string s, string listSeparator, string decimalSeparator) { string[] results = s.Split(new[] { listSeparator }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual(results.Length, 4); foreach (string oneFloatString in results) { Assert.True(oneFloatString.Contains(decimalSeparator)); } Assert.AreEqual(float.Parse(results[0]), o.X); Assert.AreEqual(float.Parse(results[1]), o.Y); Assert.AreEqual(float.Parse(results[2]), o.Z); Assert.AreEqual(float.Parse(results[3]), o.W); }
private static void CommitLighting(RenderState renderState, RenderState previousRenderState) { if (RenderStatesDiffer(previousRenderState, renderState, RenderMode.Lit)) { if ((renderState.RenderMode & RenderMode.Lit) != 0) { Gl.glEnable(Gl.GL_LIGHTING); Gl.glEnable(Gl.GL_LIGHT0); Gl.glEnable(Gl.GL_NORMALIZE); } else { Gl.glDisable(Gl.GL_LIGHTING); } Util3D.RenderStats.RenderStateChanges++; //Console.WriteLine("CommitLighting"); } if ((renderState.RenderMode & RenderMode.Lit) != 0) { { Vec4F colorVec = renderState.EmissionColor; float[] color = { colorVec.X, colorVec.Y, colorVec.Z, colorVec.W }; Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION, color); } { Vec4F colorVec = renderState.AmbientColor; float[] color = { colorVec.X, colorVec.Y, colorVec.Z, colorVec.W }; Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT, color); } { Vec4F colorVec = renderState.SolidColor; float[] color = { colorVec.X, colorVec.Y, colorVec.Z, colorVec.W }; Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE, color); } { Vec4F specularcolorVec = renderState.SpecularColor; float[] color = { specularcolorVec.X, specularcolorVec.Y, specularcolorVec.Z, specularcolorVec.W }; Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR, color); } { float Shininess = renderState.Shininess; Gl.glMaterialf(Gl.GL_FRONT_AND_BACK, Gl.GL_SHININESS, Shininess); } } }
private static void CommitSmooth(RenderState renderState, RenderState previousRenderState) { if ((renderState.RenderMode & RenderMode.Smooth) != 0) { if (RenderStatesDiffer(previousRenderState, renderState, RenderMode.Smooth)) { Gl.glShadeModel(Gl.GL_SMOOTH); Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL); Util3D.RenderStats.RenderStateChanges++; //Console.WriteLine("CommitSmooth"); } Vec4F color = renderState.SolidColor; Gl.glColor4f(color.X, color.Y, color.Z, color.W); } }
/// <summary> /// Transforms the given world or local point into viewport (Windows) coordinates</summary> /// <param name="localPoint">World or local point to be transformed</param> /// <param name="localToScreen">Tranformation matrix composed of object-to-world times /// world-to-view times view-to-projection</param> /// <param name="viewportWidth">The viewport width, for example Control.Width or /// IRenderAction.ViewportWidth</param> /// <param name="viewportHeight">The viewport height, for example Control.Height or /// IRenderAction.ViewportHeight</param> /// <returns>The viewport or Window coordinate in the range [0,Width] and [0,Height] /// where the origin is the upper left corner of the viewport. The coordinate could be /// outside of this range if localPoint is not visible.</returns> /// <example> /// To calculate localToScreen using an object's local-to-world and a Camera: /// localToScreen = Matrix4F.Multiply(localToWorld, camera.ViewMatrix); /// localToScreen.Mul(localToScreen, camera.ProjectionMatrix); /// </example> public static Vec2F TransformToViewport( Vec3F localPoint, Matrix4F localToScreen, float viewportWidth, float viewportHeight) { // transform to clip space and do perspective divide. Result is in range of [-1, 1] Vec4F xScreen = new Vec4F(localPoint); localToScreen.Transform(xScreen, out xScreen); xScreen = Vec4F.Mul(xScreen, 1.0f / xScreen.W); // get viewport coordinates. Convert [-1, 1] to [0, view size] Vec2F xViewport = new Vec2F( (xScreen.X + 1) * 0.5f * viewportWidth, (1 - (xScreen.Y + 1) * 0.5f) * viewportHeight); return(xViewport); }
private void ParseCustomAttributesForLightingInfo(RenderState rs) { IEnumerable attributes = CustomAttributes; if (attributes != null) { float[] alpha = null; foreach (DomNode attribute in attributes) { string name = attribute.GetAttribute(Schema.customDataAttributeType.nameAttribute) as string; if (name == "color") { float[] diffuse = new float[3]; PopulateCustomAttributeValueArray( attribute.GetAttribute(Schema.customDataAttributeType.valueAttribute) as string, diffuse); rs.SolidColor = new Vec4F(diffuse[0], diffuse[1], diffuse[2], 1.0f); } else if (name == "specularColor") { float[] specular = new float[3]; PopulateCustomAttributeValueArray( attribute.GetAttribute(Schema.customDataAttributeType.valueAttribute) as string, specular); } else if (name == "transparency") { alpha = new float[3]; PopulateCustomAttributeValueArray( attribute.GetAttribute(Schema.customDataAttributeType.valueAttribute) as string, alpha); } } if (alpha != null) { Vec4F color = new Vec4F(rs.SolidColor); color[3] = alpha[0]; rs.SolidColor = color; rs.RenderMode |= RenderMode.Alpha; } } }
private static void CommitWire(RenderState renderState, RenderState previousRenderState) { if ((renderState.RenderMode & RenderMode.Wireframe) != 0) { if (RenderStatesDiffer(previousRenderState, renderState, RenderMode.Wireframe)) { Gl.glDisable(Gl.GL_TEXTURE_2D); Gl.glDisable(Gl.GL_CULL_FACE); Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE); Gl.glPolygonOffset(-1.0f, -1.0f); Gl.glEnable(Gl.GL_POLYGON_OFFSET_LINE); Util3D.RenderStats.RenderStateChanges++; //Console.WriteLine("CommitWire"); } Vec4F color = renderState.WireframeColor; Gl.glColor4f(color.X, color.Y, color.Z, color.W); bool makeGlCall = false; if (previousRenderState == null) { makeGlCall = true; } else { bool previousRsWasSmooth = ((previousRenderState.RenderMode & RenderMode.Smooth) != 0); bool previousRsHadDifferentThickness = (previousRenderState.LineThickness != renderState.LineThickness); makeGlCall = previousRsWasSmooth || previousRsHadDifferentThickness; } if (makeGlCall) { Gl.glLineWidth(renderState.LineThickness); } } }
public static void SetObjectProperty(uint typeid, ulong instanceId, uint propId, Color color) { Vec4F val = new Vec4F(color.R/255.0f, color.G/255.0f, color.B/255.0f, color.A/255.0f); IntPtr ptr = new IntPtr(&val); int sz = Marshal.SizeOf(val); NativeSetObjectProperty(typeid, propId, instanceId, ptr, sz); }
public static void SetObjectProperty(uint typeid, ulong instanceId, uint propId, Vec4F val) { IntPtr ptr = new IntPtr(&val); int sizeInBytes = Marshal.SizeOf(val); NativeSetObjectProperty(typeid, propId, instanceId, ptr, sizeInBytes); }
/// <summary> /// Transforms the given world or local point into viewport (Windows) coordinates</summary> /// <param name="localPoint">World or local point to be transformed</param> /// <param name="localToScreen">Tranformation matrix composed of object-to-world times /// world-to-view times view-to-projection</param> /// <param name="viewportWidth">The viewport width, for example Control.Width or /// IRenderAction.ViewportWidth</param> /// <param name="viewportHeight">The viewport height, for example Control.Height or /// IRenderAction.ViewportHeight</param> /// <returns>The viewport or Window coordinate in the range [0,Width] and [0,Height] /// where the origin is the upper left corner of the viewport. The coordinate could be /// outside of this range if localPoint is not visible.</returns> /// <example> /// To calculate localToScreen using an object's local-to-world and a Camera: /// localToScreen = Matrix4F.Multiply(localToWorld, camera.ViewMatrix); /// localToScreen.Mul(localToScreen, camera.ProjectionMatrix); /// </example> public static Vec2F TransformToViewport( Vec3F localPoint, Matrix4F localToScreen, float viewportWidth, float viewportHeight) { // transform to clip space and do perspective divide. Result is in range of [-1, 1] Vec4F xScreen = new Vec4F(localPoint); localToScreen.Transform(xScreen, out xScreen); xScreen = Vec4F.Mul(xScreen, 1.0f / xScreen.W); // get viewport coordinates. Convert [-1, 1] to [0, view size] Vec2F xViewport = new Vec2F( (xScreen.X + 1) * 0.5f * viewportWidth, (1 - (xScreen.Y + 1) * 0.5f) * viewportHeight); return xViewport; }
public static float Dot(Vec4F v1, Vec4F v2) { return(v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z + v1.W * v2.W); }