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 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(); }
public void TestPerspectiveFrustum() { PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(); projectionMatrix.SetPerspective(60.0f, 1.0f, 0.5f, 100.0f); IEnumerable <Plane> planes = Plane.GetFrustumPlanes(projectionMatrix); foreach (Plane plane in planes) { Assert.GreaterOrEqual(plane.GetDistance(-Vertex3f.UnitZ), 0.0f); } }
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++)); } } }
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); } }
public void TestPerspectiveFrustumNoModelView() { PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(60.0f, 1.0f, 1.0f, 10.0f); BoundingBox boundingBox; Vertex3f bboxPosition; IEnumerable <Plane> planes = Plane.GetFrustumPlanes(projectionMatrix); bboxPosition = new Vertex3f(-0.5f, -0.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsFalse(boundingBox.IsClipped(planes)); bboxPosition = new Vertex3f(-10.5f, -10.5f, -3.0f); boundingBox = new BoundingBox(bboxPosition, bboxPosition + Vertex3f.One); Assert.IsTrue(boundingBox.IsClipped(planes)); }
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)); }