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_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); }
/// <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); } }
/// <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); } }
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 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; } }
/// <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 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); } }
/// <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); } } }
/// <summary> /// Update framebuffer. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ObjectsControl_Render(object sender, GlControlEventArgs e) { GlControl senderControl = (GlControl)sender; float senderAspectRatio = (float)senderControl.Width / senderControl.Height; // Clear Gl.Viewport(0, 0, senderControl.Width, senderControl.Height); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); _CubeScene.CurrentView.LocalModel.SetIdentity(); _CubeScene.CurrentView.LocalModel.Translate(_ViewStrideLat, _ViewStrideAlt, 0.0f); _CubeScene.CurrentView.LocalModel.RotateY(_ViewAzimuth); _CubeScene.CurrentView.LocalModel.RotateX(_ViewElevation); _CubeScene.CurrentView.LocalModel.Translate(0.0f, 0.0f, _ViewLever); _CubeScene.CurrentView.ProjectionMatrix = new PerspectiveProjectionMatrix(45.0f, senderAspectRatio, 0.5f, 10000.0f); _CubeScene.Draw(_Context); // Overlay ProjectionMatrix overlayProjection = new OrthoProjectionMatrix(0.0f, ClientSize.Width, 0.0f, ClientSize.Height); ModelMatrix overlayModel = new ModelMatrix(); overlayModel.Translate(0.375f, 0.375f); Gl.Clear(ClearBufferMask.DepthBufferBit); ColorRGBAF fpsColor = ColorRGBAF.ColorGreen; int fps = senderControl.Fps; if (fps >= 59) { fpsColor = ColorRGBAF.ColorGreen; } else if (fps >= 29) { fpsColor = ColorRGBAF.ColorBlue; } else if (fps >= 24) { fpsColor = ColorRGBAF.ColorYellow; } else { fpsColor = ColorRGBAF.ColorRed; } fontPatch.DrawString( _Context, overlayProjection * overlayModel, fpsColor, String.Format("FPS: {0}", senderControl.Fps) ); overlayModel.SetIdentity(); overlayModel.Translate(0.375f, ClientSize.Height - 64 + 0.375f); fontTitle.DrawString( _Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen, "Hello Objects example ~(^.^)~" ); overlayModel.SetIdentity(); overlayModel.Translate(0.375f, ClientSize.Height - 128 + 0.375f); fontTitleV.DrawString(_Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen, "Hello Objects example ~(^.^)~" ); }
/// <summary> /// Update framebuffer. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ObjectsControl_Render(object sender, GlControlEventArgs e) { GlControl senderControl = (GlControl)sender; float senderAspectRatio = (float)senderControl.Width / senderControl.Height; KhronosApi.LogEnabled = false; KhronosApi.LogComment("--------------------------------------------------"); KhronosApi.LogComment("*** Draw"); // Clear Gl.Viewport(0, 0, senderControl.Width, senderControl.Height); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); _CubeScene.CurrentView.ProjectionMatrix = new PerspectiveProjectionMatrix(45.0f, senderAspectRatio, 0.1f, 100.0f); _CubeScene.CurrentView.LocalModel.SetIdentity(); _CubeScene.CurrentView.LocalModel.Translate(_ViewStrideLat, _ViewStrideAlt, 0.0f); _CubeScene.CurrentView.LocalModel.RotateY(_ViewAzimuth); _CubeScene.CurrentView.LocalModel.RotateX(_ViewElevation); _CubeScene.CurrentView.LocalModel.Translate(0.0f, 0.0f, _ViewLever); _CubeScene.UpdateViewMatrix(); _CubeScene.Draw(_Context); KhronosApi.LogEnabled = false; return; // Overlay ProjectionMatrix overlayProjection = new OrthoProjectionMatrix(0.0f, ClientSize.Width, 0.0f, ClientSize.Height); ModelMatrix overlayModel = new ModelMatrix(); overlayModel.Translate(0.375f, 0.375f); Gl.Clear(ClearBufferMask.DepthBufferBit); ColorRGBAF fpsColor = ColorRGBAF.ColorGreen; int fps = (int)Math.Floor(1000.0 / senderControl.FrameSwapTime.TotalMilliseconds); if (fps >= 59) { fpsColor = ColorRGBAF.ColorGreen; } else if (fps >= 29) { fpsColor = ColorRGBAF.ColorBlue; } else if (fps >= 24) { fpsColor = ColorRGBAF.ColorYellow; } else { fpsColor = ColorRGBAF.ColorRed; } fontPatch.DrawString( _Context, overlayProjection * overlayModel, fpsColor, String.Format("FPS: {0}", fps) ); overlayModel.SetIdentity(); overlayModel.Translate(0.375f, ClientSize.Height - 64 + 0.375f); fontTitle.DrawString( _Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen, "Hello Objects example ~(^.^)~" ); overlayModel.SetIdentity(); overlayModel.Translate(0.375f, ClientSize.Height - 128 + 0.375f); fontTitleV.DrawString(_Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen, "Hello Objects example ~(^.^)~" ); // Release resources _Context.DisposeResources(); }