/// <summary> /// Update shadow map. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocating resources. /// </param> public override void UpdateShadowMap(GraphicsContext ctx, SceneGraph shadowGraph) { CheckCurrentContext(ctx); if (shadowGraph == null) { throw new ArgumentNullException("shadowGraph"); } // Compute light matrix IModelMatrix viewMatrix = new ModelMatrix(); // LocalModel.Multiply(LightMatrix.GetInverseMatrix()).GetInverseMatrix(); viewMatrix.LookAtDirection((Vertex3d)LocalModel.Position, (Vertex3f)Direction, Vertex3d.UnitY); // Set light scene view shadowGraph.ProjectionMatrix = new PerspectiveProjectionMatrix(FalloffAngle * 2.0f, 1.0f, 0.1f, 100.0f); shadowGraph.ViewMatrix = viewMatrix; _ShadowFramebuffer.BindDraw(ctx); // Reset viewport new ViewportState(0, 0, (int)_ShadowMap.Width, (int)_ShadowMap.Height).Apply(ctx, null); _ShadowFramebuffer.Clear(ctx, ClearBufferMask.DepthBufferBit); shadowGraph.Draw(ctx, _ShadowProgram); _ShadowFramebuffer.UnbindDraw(ctx); // Cache view matrix _ShadowViewMatrix = _BiasMatrix.Multiply(shadowGraph.ProjectionMatrix.Multiply(viewMatrix)); }
public void TestGetFrustumLeftPlane() { ProjectionMatrix projectionMatrix; ModelMatrix modelMatrix; Matrix4x4 mvp; Plane plane; // Orthographic projection projectionMatrix = new OrthoProjectionMatrix(-2.0f, +2.0f, -2.0f, +2.0f); plane = Plane.GetFrustumLeftPlane(projectionMatrix); Assert.AreEqual(Vertex3f.UnitX, plane.Normal); Assert.AreEqual(-2.0f, plane.Distance); // Rotate by 90 deg on Y axis modelMatrix = new ModelMatrix(); modelMatrix.RotateY(90.0f); mvp = projectionMatrix * modelMatrix; plane = Plane.GetFrustumLeftPlane(mvp); Assert.AreEqual(Vertex3f.UnitZ, plane.Normal); Assert.AreEqual(-2.0f, plane.Distance); // Rotate by 180 deg on Y axis modelMatrix = new ModelMatrix(); modelMatrix.RotateY(180.0f); mvp = projectionMatrix * modelMatrix; plane = Plane.GetFrustumLeftPlane(mvp); Assert.AreEqual(-Vertex3f.UnitX, plane.Normal); Assert.AreEqual(-2.0f, plane.Distance); }
private void RenderControl_Render_GLSL(object sender, GlControlEventArgs e) { Control control = (Control)sender; PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(45.0f, (float)control.Width / (float)control.Height, 0.1f, 100.0f); ModelMatrix viewMatrix = new ModelMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); modelMatrix.Translate(new Vertex3f(modelPosition.X, modelPosition.Y, modelPosition.Z)); modelMatrix.Scale(new Vertex3f(0.2f, 0.2f, 0.2f)); Gl.Viewport(0, 0, control.Width, control.Height); Gl.ClearColor(0.05f, 0.05f, 0.05f, 1.0f); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); modelShader.Use(); modelMatrix.RotateX(modelAngle.X); modelMatrix.RotateY(modelAngle.Y); modelMatrix.RotateZ(modelAngle.Z); //viewMatrix.Translate(new Vertex3f(-2.0f * (float)Math.Sin(modelAngle.Y * PI_OVER_180), 0.0f, -2.0f*(float)Math.Cos(modelAngle.Y*PI_OVER_180))); Gl.UniformMatrix4(modelShader.uLocation_Projection, 1, false, projectionMatrix.ToArray()); Gl.UniformMatrix4(modelShader.uLocation_View, 1, false, viewMatrix.ToArray()); Gl.UniformMatrix4(modelShader.uLocation_Model, 1, false, modelMatrix.ToArray()); modelNanosuit.Draw(modelShader); }
private float[] calcMVP(float w, float h) { OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, w, 0.0f, h, 0.0f, 1000000.0f); ModelMatrix modelMatrix = new ModelMatrix(); return((projectionMatrix * modelMatrix).ToArray()); }
/// <summary> /// Draw a character sequence. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for drawing. /// </param> /// <param name="modelview"> /// The <see cref="Matrix4x4"/> the model-view-projection matrix for the first character of <paramref name="s"/>. /// </param> /// <param name="color"> /// The <see cref="ColorRGBAF"/> that specifies the glyph color. /// </param> /// <param name="s"> /// A <see cref="String"/> that specifies the characters for be drawn. /// </param> public override void DrawString(GraphicsContext ctx, Matrix4x4 modelview, ColorRGBAF color, string s) { ModelMatrix charModel = new ModelMatrix(modelview); ctx.Bind(_FontProgram); _FontProgram.SetUniform(ctx, "glo_UniformColor", color); foreach (char c in s) { Glyph glyph; if (_Glyphs.TryGetValue(c, out glyph) == false) { continue; } if (glyph.ElementIndex >= 0) { // Set model-view _FontProgram.SetUniform(ctx, "glo_ModelViewProjection", charModel); // Rasterize it _VertexArrays.Draw(ctx, _FontProgram, glyph.ElementIndex); } // Move next charModel.Translate(glyph.GlyphSize.Width, 0.0f); } }
private void SampleGraphicsControl_Render(object sender, GraphicsControlEventArgs e) { GraphicsContext ctx = e.Context; GraphicsSurface framebuffer = e.Framebuffer; if (_AnimationBegin == DateTime.MinValue) { _AnimationBegin = DateTime.UtcNow; } PerspectiveProjectionMatrix matrixProjection = new PerspectiveProjectionMatrix(); Matrix4x4 matrixView; // Set projection matrixProjection.SetPerspective(60.0f, (float)ClientSize.Width / (float)ClientSize.Height, 1.0f, 1000.0f); // Set view ModelMatrix matrixViewModel = new ModelMatrix(); matrixViewModel.RotateX(_ViewElevation); matrixViewModel.RotateY(_ViewAzimuth); matrixViewModel.Translate(0.0f, 0.0f, _ViewDistance); matrixView = matrixViewModel.GetInverseMatrix(); _NewtonProgram.Bind(ctx); _NewtonProgram.SetUniform(ctx, "hal_ModelViewProjection", matrixProjection * matrixView); _NewtonProgram.SetUniform(ctx, "hal_FrameTimeInterval", (float)(DateTime.UtcNow - _AnimationBegin).TotalSeconds); _NewtonVertexArray.Draw(ctx, _NewtonProgram); SwapNewtonVertexArrays(); // Issue another rendering SampleGraphicsControl.Invalidate(); }
private void RenderControl_Render_ES(object sender, GlControlEventArgs e) { Control control = (Control)sender; OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f); ModelMatrix modelMatrix = new ModelMatrix(); // Animate triangle modelMatrix.RotateZ(_Angle); Gl.Viewport(0, 0, control.Width, control.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4(_Es2_Program_Location_uMVP, false, (projectionMatrix * modelMatrix).ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
public void Execute(int index) { float3 position = Positions[index].Value; Matrices[index] = new ModelMatrix { Value = math.float4x4(quaternion.identity, position) }; }
public override Base GetBase() { Base B = ModelMatrix.toBase(); B.BaseO = Point; return(B); }
/// <summary> /// Draw a character sequence. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for drawing. /// </param> /// <param name="modelview"> /// The <see cref="Matrix4x4"/> the model-view-projection matrix for the first character of <paramref name="s"/>. /// </param> /// <param name="color"> /// The <see cref="ColorRGBAF"/> that specifies the glyph color. /// </param> /// <param name="s"> /// A <see cref="String"/> that specifies the characters for be drawn. /// </param> public override void DrawString(GraphicsContext ctx, Matrix4x4 modelview, ColorRGBAF color, string s) { ModelMatrix charModel = new ModelMatrix(modelview); ctx.Bind(_FontProgram); // Set uniforms _FontProgram.SetUniform(ctx, "glo_UniformColor", color); _FontProgram.SetUniform(ctx, "glo_FontGlyph", _FontTexture); // Set instances char[] fontChars = s.ToCharArray(); uint instances = 0; _GlyphInstances.Map(ctx, BufferAccessARB.WriteOnly); try { for (int i = 0; i < fontChars.Length; i++) { Glyph glyph; if (_GlyphDb.TryGetValue(fontChars[i], out glyph) == false) { continue; } // Set instance information Matrix4x4f modelViewProjection = new Matrix4x4f( new Vertex4f(charModel.GetColumn(0)), new Vertex4f(charModel.GetColumn(1)), new Vertex4f(charModel.GetColumn(2)), new Vertex4f(charModel.GetColumn(3)) ); Vertex3f glyphVertexParams = new Vertex3f( glyph.GlyphSize.Width, glyph.GlyphSize.Height, glyph.Layer ); Vertex2f glyphTexParams = new Vertex2f( glyph.TexScale.Width, glyph.TexScale.Height ); _GlyphInstances.SetElement(modelViewProjection, instances, 0); _GlyphInstances.SetElement(glyphVertexParams, instances, 1); _GlyphInstances.SetElement(glyphTexParams, instances, 2); // Count the instance instances++; // Move next charModel.Translate(glyph.GlyphSize.Width, 0.0f); } } finally { _GlyphInstances.Unmap(ctx); } // Rasterize it using (State.BlendState stateBlend = State.BlendState.AlphaBlending) { stateBlend.ApplyState(ctx, _FontProgram); _VertexArrays.DrawInstanced(ctx, _FontProgram, instances); } }
/// <summary> /// Static constructor. /// </summary> static SceneObjectLight() { _BiasMatrix = new ModelMatrix(new float[] { 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, }); }
private IMatrix4x4 CreateViewMatrix(Camera camera) { Matrix <double> objectToWorldTransform = camera.GetObjectToWorldTransform(); IModelMatrix modelMat = new ModelMatrixDouble(objectToWorldTransform.AsColumnMajorArray() ?? objectToWorldTransform.ToColumnMajorArray()); Vector <double> worldPosition = objectToWorldTransform.Multiply(Vector <double> .Build.Dense(new double[] { 0, 0, 0, 1 })); IModelMatrix viewMat = new ModelMatrix(); viewMat.Rotate(modelMat.Rotation); viewMat.Translate(-worldPosition[0], -worldPosition[1], -worldPosition[2]); return(viewMat); }
public override Base GetBase() { Base B = ModelMatrix.toBase(); B.BaseO = Point; if (Lam >= 0) { xyz D = ModelMatrix * Poly.Direction(Lam) - ModelMatrix * new xyz(0, 0, 0); B = Base.DoComplete(Point, D, new xyz(0, 0, 1) & D); } return(B); }
public virtual Base GetBase() { Base B = ModelMatrix.toBase(); B.BaseO = Point; if ((TriangleInfo != null)) { Plane Triangle = new Plane(ModelMatrix * TriangleInfo.Points[TriangleInfo.Indices[PrimId * 3]], ModelMatrix * TriangleInfo.Points[TriangleInfo.Indices[PrimId * 3 + 1]], ModelMatrix * TriangleInfo.Points[TriangleInfo.Indices[PrimId * 3 + 2]]); return(Base.DoComplete(Point, Triangle.NormalUnit * (-1))); } return(B); }
private static void NativeWindow_Render(object sender, NativeWindowEventArgs e) { NativeWindow nativeWindow = (NativeWindow)sender; Gl.Viewport(0, 0, (int)nativeWindow.Width, (int)nativeWindow.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_CubeEdgeProgram); // Compute MVP PerspectiveProjectionMatrix proj = new PerspectiveProjectionMatrix(60.0f, (float)nativeWindow.Width / nativeWindow.Height, 0.5f, 1e6f); ModelMatrix view = new ModelMatrix(); ModelMatrix model = new ModelMatrix(); model.RotateY(_Angle); model.RotateZ(_Angle); view.LookAtTarget(Vertex3f.One * 7.0f, Vertex3f.Zero, Vertex3f.UnitY); Gl.BindVertexArray(_CubeVao); Gl.UniformMatrix4(_CubeEdgeProgram_Location_uMVP, false, (proj * view * model).ToArray()); foreach (float scale4d in new float[] { 64.0f, 32.0f, 16.0f, 8.0f, 4.0f, 2.0f, 1.0f, 0.5f, 0.25f, 0.125f }) { Gl.Uniform1(_CubeEdgeProgram_Location_uScale4D, scale4d * _Zooom); Gl.Uniform4(_CubeEdgeProgram_Location_uColor, 0.0f, 0.3f, 1.0f, Math.Min(1.0f, scale4d * _Zooom / 2.0f)); Gl.DrawElements(PrimitiveType.Lines, _CubeEdges.Length, DrawElementsType.UnsignedShort, IntPtr.Zero); } _Angle += 360.0f / (25.0f * 5); _Zooom -= 0.025f; if (_Zooom < 0.5f) { _Zooom = 1.0f; } // Save PNG frame if (_FrameNo < 125) { using (Bitmap bitmap = new Bitmap((int)nativeWindow.Width, (int)nativeWindow.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Gl.ReadPixels(0, 0, (int)nativeWindow.Width, (int)nativeWindow.Height, OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, bitmapData.Scan0); bitmap.Save(String.Format("Frame_{0:D3}.png", _FrameNo++)); } } }
public override Base GetBase() { xyzArray A = ModelMatrix * CurveArray.getxyArray().ToxyzArray(); Base B = ModelMatrix.toBase(); B.BaseO = Point; if (Lam >= 0) { xyz D = ModelMatrix * CurveArray.Direction(Lam).toXYZ() - ModelMatrix * new xyz(0, 0, 0); B = Base.DoComplete(Point, D, new xyz(0, 0, 1) & D); } return(B); }
public override Base GetBase() { Base B = ModelMatrix.toBase(); B.BaseO = Point; if (Lam >= 0) { xyz D = ModelMatrix * Curve.Derivation(Lam).toXYZ() - ModelMatrix * new xyz(0, 0, 0); B = Base.DoComplete(Point, D, new xyz(0, 0, 1) & D); } return(B); }
public void MatrixMul2() { ProjectionMatrix proj = new OrthoProjectionMatrix(-1.0f, 0.5f, 0.0f, 1.0f, 0.1f, 3.0f); ModelMatrix model = new ModelMatrix(); model.Translate(2.0f, -4.0f, 1.125f); model.RotateX(30.0f); Matrix r1 = proj * model; Matrix m = new Matrix(proj.ToArray(), 4, 4), n = new Matrix(model.ToArray(), 4, 4); Matrix r2 = m * n; }
/// <summary> /// Draw a character sequence. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for drawing. /// </param> /// <param name="modelview"> /// The <see cref="Matrix4x4"/> the model-view-projection matrix for the first character of <paramref name="s"/>. /// </param> /// <param name="color"> /// The <see cref="ColorRGBAF"/> that specifies the glyph color. /// </param> /// <param name="s"> /// A <see cref="String"/> that specifies the characters for be drawn. /// </param> public override void DrawString(GraphicsContext ctx, Matrix4x4 modelview, ColorRGBAF color, string s) { // Draw the string DrawStringCore(ctx, modelview, color, s); // Shadow effect if (_FxShadow != null) { ModelMatrix shadowModel = new ModelMatrix(modelview); shadowModel.Translate(_FxShadow.Offset); DrawStringCore(ctx, shadowModel, _FxShadow.Color, s); } }
private void RenderControl_Render_ES(object sender, GlControlEventArgs e) { Control control = (Control)sender; PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(45.0f, (float)control.Width / (float)control.Height, 0.1f, 100.0f); ModelMatrix viewMatrix = new ModelMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); // Move camera viewMatrix.Translate(new Vertex3f(0.0f, 0.0f, -2.0f)); // Animate triangle /*modelMatrix.LookAtDirection( * new Vertex3f(0.0f, 0.0f, 0.0f), * new Vertex3f( * (float)Math.Sin(angle_rad), * 0.0f, * (float)Math.Cos(angle_rad) * ), * new Vertex3f(0.0f, 1.0f, 0.0f) * );*/ //Quaternion Q = new Quaternion(new Vertex3f(0.0f, 1.0f, 0.0f), angle); modelMatrix.RotateZ(angle); modelMatrix.RotateY(angle); //modelMatrix.Translate(Math.Cos(theta), Math.Sin(theta)); //modelMatrix.RotateY(theta); Gl.UseProgram(Program_Shader); Gl.Viewport(0, 0, control.Width, control.Height); Gl.Enable(EnableCap.DepthTest); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)Program_Location_aPosition, 3, Gl.FLOAT, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aPosition); Gl.VertexAttribPointer((uint)Program_Location_aColor, 3, Gl.FLOAT, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aColor); Gl.UniformMatrix4(Program_Location_uProjection, 1, false, projectionMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uView, 1, false, viewMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uModel, 1, false, modelMatrix.ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 36); } }
static Matrix4x4Test() { Random random = new Random(); _MatArray = new Matrix4x4[MulArraySize]; for (int i = 0; i < _MatArray.Length; i++) { ModelMatrix modelMatrix = new ModelMatrix(); modelMatrix.RotateX(random.NextDouble() * 360.0); modelMatrix.RotateY(random.NextDouble() * 360.0); modelMatrix.RotateZ(random.NextDouble() * 360.0); modelMatrix.Translate(random.NextDouble(), random.NextDouble(), random.NextDouble()); _MatArray[i] = modelMatrix; } }
public override Base GetBase() { Base B = Base.UnitBase; B.BaseO = Point; int id = -1; xyArray A = null; LineType ViewLine = Device.FromScr(Device.MousePos); xyz P = ModelMatrix * ViewLine.P; xyz Q = ModelMatrix * ViewLine.Q; ViewLine = new LineType(P, Q); for (int i = 1; i < Loca.Count; i++) { A = Loca[i].getxyArray(); A = ModelMatrix * A; double Dummy = -1; double di = A.Distance(ViewLine, Snapdist * 8, out Lam, out Dummy); if (di <= Snapdist) { id = i; // break; } } if (id >= 0) { if (id == 1) { } if (Lam >= 0) { double L = Lam; Lam = Loca[id].xyArrayIndexToCurveArrayIndex(Lam); xyz D = Loca[id].Direction(Lam).toXYZ(); B = Base.DoComplete(A.Value(L).toXYZ(), D, new xyz(0, 0, 1) & D); } } else { B = ModelMatrix.toBase(); B.BaseO = Point; } return(B); }
public override double2 GetTexCoord(IntersectData data) { double3 n = GetNormal(data); n = ModelMatrix.Transform(n); double asin = Math.Asin(n.y); double ty = (asin + Math.PI / 2) / Math.PI; double atan = Math.Atan2(n.z, n.x); double tx = (atan + Math.PI) / (2 * Math.PI); double2 t = new double2(tx, ty); // Scaling //t.x *= 0.5; //t.y *= 1; return(t); }
/// <summary> /// Get a list of <see cref="GlyphModelType"/> for a specific string and model-view-projection matrix. /// </summary> /// <param name="modelview"></param> /// <param name="s"></param> /// <returns></returns> private List <GlyphModelType> GetGlyphsInstances(Matrix4x4 modelview, string s) { ModelMatrix charModel = new ModelMatrix(modelview); List <GlyphModelType> glyphsInstances = new List <GlyphModelType>(); char[] fontChars = s.ToCharArray(); for (int i = 0; i < fontChars.Length; i++) { Glyph glyph; if (_GlyphMetadata.TryGetValue(fontChars[i], out glyph) == false) { continue; } // Set instance information Matrix4x4f modelViewProjection = new Matrix4x4f( new Vertex4f(charModel.GetColumn(0)), new Vertex4f(charModel.GetColumn(1)), new Vertex4f(charModel.GetColumn(2)), new Vertex4f(charModel.GetColumn(3)) ); Vertex3f glyphVertexParams = new Vertex3f( glyph.GlyphSize.Width, glyph.GlyphSize.Height, glyph.Layer ); Vertex2f glyphTexParams = new Vertex2f( glyph.TexScale.Width, glyph.TexScale.Height ); GlyphModelType glyphModel = new GlyphModelType(); glyphModel.ModelViewProjection = modelViewProjection; glyphModel.VertexParams = glyphVertexParams; glyphModel.TexParams = glyphTexParams; glyphsInstances.Add(glyphModel); // Move next charModel.Translate(glyph.GlyphSize.Width, 0.0f); } return(glyphsInstances); }
public void Execute(int index) { float3 position = Positions[index].Value; quaternion quat = Rotations[index].Value; float3 scale = Scales[index].Value; float4x4 matrix = math.mul(math.float4x4(scale.x, 0.0f, 0.0f, 0.0f, 0.0f, scale.y, 0.0f, 0.0f, 0.0f, 0.0f, scale.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f), math.float4x4(quat, new float3 { x = 0, y = 0, z = 0 })); matrix = math.mul(math.float4x4(quaternion.identity, position), matrix); Matrices[index] = new ModelMatrix { Value = matrix }; }
public void TestProjectionFrustum() { PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(90.0f, 1.0f, 1.0f, 10.0f); ModelMatrix modelMatrix = new ModelMatrix(); IMatrix4x4 frustumMatrix = projectionMatrix * modelMatrix; Plane planeL = Plane.GetFrustumLeftPlane(frustumMatrix); Plane planeR = Plane.GetFrustumRightPlane(frustumMatrix); Plane planeB = Plane.GetFrustumBottomPlane(frustumMatrix); Plane planeT = Plane.GetFrustumTopPlane(frustumMatrix); Plane planeN = Plane.GetFrustumNearPlane(frustumMatrix); Assert.IsTrue(planeN.GetDistance(new Vertex3f(0.0f, 0.0f, -5.0f)) > 0.0f); Plane planeF = Plane.GetFrustumFarPlane(frustumMatrix); Assert.IsTrue(planeF.GetDistance(new Vertex3f(0.0f, 0.0f, -5.0f)) > 0.0f); }
public void TestPerspectiveFrustum() { PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); BoundingBox boundingBox; Vertex3f bboxPosition; projectionMatrix.SetPerspective(60.0f, 1.0f, 0.5f, 15.0f); modelMatrix.Translate(new Vertex3f(-1000.0f, 00.0f, 0.0f)); IEnumerable <Plane> planes = Plane.GetFrustumPlanes(projectionMatrix * modelMatrix); bboxPosition = new Vertex3f(-0.5f, -0.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsTrue(boundingBox.IsClipped(planes)); bboxPosition = new Vertex3f(-10.5f, -10.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsTrue(boundingBox.IsClipped(planes)); bboxPosition = new Vertex3f(999.5f, -0.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsFalse(boundingBox.IsClipped(planes)); modelMatrix.SetIdentity(); modelMatrix.RotateY(180.0f); planes = Plane.GetFrustumPlanes(projectionMatrix * modelMatrix); bboxPosition = new Vertex3f(-10.5f, -10.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsTrue(boundingBox.IsClipped(planes)); bboxPosition = new Vertex3f(999.5f, -0.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsTrue(boundingBox.IsClipped(planes)); bboxPosition = new Vertex3f(-0.5f, -0.5f, +3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsFalse(boundingBox.IsClipped(planes)); }
private void DisplayShadowMaps(GraphicsContext ctx) { State.ViewportState viewportState = new State.ViewportState(ctx); OrthoProjectionMatrix orthoProjection = new OrthoProjectionMatrix(0.0f, viewportState.Width, 0.0f, viewportState.Height); ModelMatrix model = new ModelMatrix(); // No depth test State.DepthTestState.DefaultState.Apply(ctx, null); ctx.Bind(_ShadowMapDebugProgram); for (int i = 0; i < _LightManager.ShadowLights.Count; i++) { SceneObjectLight shadowLight = _LightManager.ShadowLights[i]; SceneObjectLightSpot shadowSpotLight = shadowLight as SceneObjectLightSpot; if (shadowSpotLight == null) { continue; } Texture2d shadowTex = shadowSpotLight._ShadowMap; shadowTex.SamplerParams.CompareMode = false; ModelMatrix quadModel = new ModelMatrix(model); quadModel.Scale(shadowTex.Width / 4, shadowTex.Height / 4); _ShadowMapDebugProgram.SetUniform(ctx, "glo_ModelViewProjection", orthoProjection * quadModel); _ShadowMapDebugProgram.SetUniform(ctx, "glo_NearFar", new Vertex2f(0.1f, 100.0f)); _ShadowMapDebugProgram.SetUniform(ctx, "glo_Texture", shadowTex); _ShadowMapQuad.Draw(ctx, _ShadowMapDebugProgram); shadowTex.SamplerParams.CompareMode = true; // Stride right model.Translate(shadowTex.Width, 0.0f); } }
public override Base GetBase() { Base B = ModelMatrix.toBase();; B.BaseO = Point; int id = -1; for (int i = 0; i < PolyPoly.Count; i++) { xyArray A = PolyPoly[i]; LineType ViewLine = Device.FromScr(Device.MousePos); double Dummy = -1; double di = A.Distance(ViewLine, 2 * Snapdist, out Lam, out Dummy); if (di <= 2 * Snapdist) { id = i; break; } } if (id >= 0) { xyzArray A = ModelMatrix * PolyPoly[id].ToxyzArray(); if (Lam >= 0) { xyz D = ModelMatrix * PolyPoly[id].Direction(Lam).toXYZ() - ModelMatrix * new xyz(0, 0, 0); B = Base.DoComplete(Point, D, new xyz(0, 0, 1) & D); } } else { xyzArray A = ModelMatrix * PolyPoly[0].ToxyzArray(); xyz D = A.cross(); B = Base.DoComplete(Point, D, new xyz(0, 0, 1) & D); } return(B); }
/// <summary> /// Draw a character sequence. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for drawing. /// </param> /// <param name="modelview"> /// The <see cref="Matrix4x4"/> the model-view-projection matrix for the first character of <paramref name="s"/>. /// </param> /// <param name="color"> /// The <see cref="ColorRGBAF"/> that specifies the glyph color. /// </param> /// <param name="s"> /// A <see cref="String"/> that specifies the characters for be drawn. /// </param> private void DrawStringCore(GraphicsContext ctx, Matrix4x4 modelview, ColorRGBAF color, string s) { ModelMatrix charModel = new ModelMatrix(modelview); ctx.Bind(_FontProgram); _FontProgram.SetUniform(ctx, "glo_UniformColor", color); if (ctx.Extensions.ShaderDrawParameters_ARB) { List <VertexArrayObject.IElement> glyphElements = new List <VertexArrayObject.IElement>(); int drawInstanceId = 0; foreach (char c in s) { Glyph glyph; if (_Glyphs.TryGetValue(c, out glyph) == false) { continue; } if (glyph.ElementIndex >= 0) { // Collect draw instance element glyphElements.Add(_VertexArrays.GetElementArray(glyph.ElementIndex)); // Set model-view _FontProgram.SetUniform(ctx, "glo_CharModelViewProjection[" + drawInstanceId + "]", charModel); // Next instance drawInstanceId++; } // Move next charModel.Translate(glyph.GlyphSize.Width, 0.0f); } // Draw using Multi-Draw primitive VertexArrayObject.IElement multiElement = _VertexArrays.CombineArrayElements(glyphElements); _VertexArrays.Draw(ctx, _FontProgram, multiElement); } else { foreach (char c in s) { Glyph glyph; if (_Glyphs.TryGetValue(c, out glyph) == false) { continue; } if (glyph.ElementIndex >= 0) { // Set model-view _FontProgram.SetUniform(ctx, "glo_ModelViewProjection", charModel); // Rasterize it _VertexArrays.Draw(ctx, _FontProgram, glyph.ElementIndex); } // Move next charModel.Translate(glyph.GlyphSize.Width, 0.0f); } } }