public MGUI(string sName = "GUI") : base(sName) { _instance = this; CreateGeometry = false; _projectionMatrix = Matrix4d.CreateOrthographicOffCenter(0, 100, 0, 100, 0.01, 50); _viewMatrix = Matrix4d.LookAt(new Vector3d(0, 0, 10), new Vector3d(0, 0, 0), new Vector3d(0, 1, 0)); }
public bool UpdateMatrices() { if (hasChanged == false) { return(false); } hasChanged = false; ViewMatrix = Matrix4d.LookAt(Position, LookAt, Vector3d.UnitY); switch (ProjectionType) { case ProjectionType.Perspective: ProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView(FovRads, Width / (float)Height, NearZ, FarZ); break; case ProjectionType.Ortographic: var halfWidth = Width / 2f; var halfHeight = Height / 2f; ProjectionMatrix = Matrix4d.CreateOrthographicOffCenter(-halfWidth, halfWidth, -halfHeight, halfHeight, NearZ, FarZ); break; default: throw new ArgumentOutOfRangeException(); } return(true); }
public void PreRender(Model model, AABB box) { // TODO: Accelerate this: one big texture rather than 6 small ones? int[] ints = new int[6]; // TODO: Normals too! int fbo = GL.GenFramebuffer(); ints[0] = RenderSide(0, model, box, fbo, Vector3.UnitX, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Min.Y, box.Max.Y, box.Min.Z, box.Max.Z, box.Min.X, box.Max.X)); ints[1] = RenderSide(1, model, box, fbo, -Vector3.UnitX, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Max.Y, box.Min.Y, box.Min.Z, box.Max.Z, box.Min.X, box.Max.X)); ints[2] = RenderSide(2, model, box, fbo, Vector3.UnitY, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Min.X, box.Max.X, box.Min.Z, box.Max.Z, box.Min.Y, box.Max.Y)); ints[3] = RenderSide(3, model, box, fbo, -Vector3.UnitY, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Max.X, box.Min.X, box.Min.Z, box.Max.Z, box.Min.Y, box.Max.Y)); ints[4] = RenderSide(4, model, box, fbo, Vector3.UnitZ, Vector3.UnitX, Matrix4d.CreateOrthographicOffCenter(box.Min.Y, box.Max.Y, box.Min.X, box.Max.X, box.Min.Z, box.Max.Z)); ints[5] = RenderSide(5, model, box, fbo, -Vector3.UnitZ, Vector3.UnitX, Matrix4d.CreateOrthographicOffCenter(box.Max.Y, box.Min.Y, box.Min.X, box.Max.X, box.Min.Z, box.Max.Z)); GL.DeleteFramebuffer(fbo); model.LODHelper = ints; }
public void PreRender(Model model, AABB box, Matrix4 oTrans) { // TODO: Accelerate this: one big texture rather than 6 small ones? KeyValuePair <int, int>[] ints = new KeyValuePair <int, int> [6]; // TODO: Normals too! int fbo = GL.GenFramebuffer(); GL.Viewport(0, 0, TWIDTH, TWIDTH); ints[0] = RenderSide(0, model, box, fbo, Vector3.UnitX, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Min.Y, box.Max.Y, box.Min.Z, box.Max.Z, box.Min.X, box.Max.X), oTrans); ints[1] = RenderSide(1, model, box, fbo, -Vector3.UnitX, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Max.Y, box.Min.Y, box.Min.Z, box.Max.Z, box.Min.X, box.Max.X), oTrans); ints[2] = RenderSide(2, model, box, fbo, Vector3.UnitY, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Min.X, box.Max.X, box.Min.Z, box.Max.Z, box.Min.Y, box.Max.Y), oTrans); ints[3] = RenderSide(3, model, box, fbo, -Vector3.UnitY, Vector3.UnitZ, Matrix4d.CreateOrthographicOffCenter(box.Max.X, box.Min.X, box.Min.Z, box.Max.Z, box.Min.Y, box.Max.Y), oTrans); ints[4] = RenderSide(4, model, box, fbo, Vector3.UnitZ, Vector3.UnitX, Matrix4d.CreateOrthographicOffCenter(box.Min.Y, box.Max.Y, box.Min.X, box.Max.X, box.Min.Z, box.Max.Z), oTrans); ints[5] = RenderSide(5, model, box, fbo, -Vector3.UnitZ, Vector3.UnitX, Matrix4d.CreateOrthographicOffCenter(box.Max.Y, box.Min.Y, box.Min.X, box.Max.X, box.Min.Z, box.Max.Z), oTrans); GL.DeleteFramebuffer(fbo); model.LODHelper = ints; GL.DrawBuffer(DrawBufferMode.Back); TheClient.MainWorldView.OSetViewport(); }
public override void SetViewSize(double w, double h) { mViewWidth = w; mViewHeight = h; mViewOrg.X = w / 2.0; mViewOrg.Y = h / 2.0; mViewCenter.X = w / 2.0; mViewCenter.Y = h / 2.0; DeviceScaleX = w / 2.0; DeviceScaleY = -h / 2.0; GL.Viewport(0, 0, (int)mViewWidth, (int)mViewHeight); CalcProjectionMatrix(); CalcProjectionZW(); Matrix2D = Matrix4d.CreateOrthographicOffCenter( 0, mViewWidth, mViewHeight, 0, 0, mProjectionFar); }
public void Render(double delta_time) { #region Depth var light = new Vector3d(10, 10, 0); var light_target = new Vector3d(0, 0, 0); var depth_render_context = new MartinZottmann.Game.Graphics.RenderContext() { Window = RenderContext.Window, Projection = Matrix4d.CreateOrthographicOffCenter(-10, 10, -10, 10, 0, 100), //Projection = Matrix4d.Perspective(MathHelper.PiOver2, Camera.Aspect, 0.1, 30), View = Matrix4d.LookAt(light, light_target, Vector3d.UnitY), Model = Matrix4d.Identity, Program = ResourceManager.Programs["depth"] }; var depth_bias = Matrix4d.Scale(0.5, 0.5, 0.5) * Matrix4d.CreateTranslation(0.5, 0.5, 0.5); // Map [-1, 1] to [0, 1] var depth_bias_MVP = RenderContext.InvertedView * depth_render_context.ProjectionViewModel * depth_bias; ResourceManager.Programs["standard"].UniformLocations["in_DepthBiasMVP"].Set(depth_bias_MVP); ResourceManager.Programs["standard"].UniformLocations["in_Texture"].Set(0); ResourceManager.Programs["standard"].UniformLocations["in_ShadowTexture"].Set(1); ResourceManager.Programs["standard"].UniformLocations["in_LightPosition"].Set(light); //GL.Enable(EnableCap.PolygonOffsetFill); //GL.PolygonOffset(2.0f, 4.0f); //GL.CullFace(CullFaceMode.Front); using (new Bind(frame_buffer)) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Viewport(0, 0, 1024, 1024); Draw(delta_time, depth_render_context); } //GL.CullFace(CullFaceMode.Back); //GL.Disable(EnableCap.PolygonOffsetFill); #endregion #region Render GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Viewport(0, 0, RenderContext.Window.Width, RenderContext.Window.Height); Draw(delta_time, RenderContext); #endregion //screen.Render(delta_time, screen_render_context); #region Debug if (game_state_nodes.First.GameState.Debug) { GL.Viewport(0, 0, RenderContext.Window.Width, RenderContext.Window.Height); var debug_render_context = new MartinZottmann.Game.Graphics.RenderContext() { Window = RenderContext.Window, Projection = Matrix4d.CreateOrthographicOffCenter(-1, 1, -1, 1, -1, 1), View = Matrix4d.Identity, Model = Matrix4d.Scale(0.25) * Matrix4d.CreateTranslation(-0.75, -0.75, 0) }; var debug_screen = new Model(); var shape = new MartinZottmann.Engine.Graphics.Shapes.Quad(); shape.Vertices[0].Texcoord.Y = 0; shape.Vertices[1].Texcoord.Y = 0; shape.Vertices[2].Texcoord.Y = 1; shape.Vertices[3].Texcoord.Y = 1; debug_screen.Mesh(shape); debug_screen.Program = ResourceManager.Programs["plain_texture"]; debug_screen.Program.UniformLocations["in_ModelViewProjection"].Set(debug_render_context.ProjectionViewModel); debug_screen.Program.UniformLocations["in_Texture"].Set(0); debug_screen.Texture = RenderContext.DepthTexture; debug_screen.Draw(); } #endregion }
private void DrawCompassOrtho() { PushMatrixes(); double size = 40; double vw = DC.ViewWidth; double vh = DC.ViewHeight; double cx = size / 2 + 24; double cy = size / 2 + 40; double left = -cx; double right = vw - cx; double top = cy; double bottom = -(vh - cy); double arrowLen = 10; double arrowW2 = 5; Matrix4d prjm = Matrix4d.CreateOrthographicOffCenter(left, right, bottom, top, 100, 10000); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref prjm); GL.MatrixMode(MatrixMode.Modelview); Vector3d lookAt = Vector3d.Zero; Vector3d eye = -DC.ViewDir * 300; Matrix4d mdlm = Matrix4d.LookAt(eye, lookAt, DC.UpVector); GL.LoadMatrix(ref mdlm); Vector3d p0; Vector3d p1; GL.LineWidth(2); p0 = Vector3d.UnitX * -size; p1 = Vector3d.UnitX * size; DrawArrow(DC.GetPen(DrawTools.PEN_AXIS_X), p0, p1, ArrowTypes.CROSS, ArrowPos.END, arrowLen, arrowW2); p0 = Vector3d.UnitY * -size; p1 = Vector3d.UnitY * size; DrawArrow(DC.GetPen(DrawTools.PEN_AXIS_Y), p0, p1, ArrowTypes.CROSS, ArrowPos.END, arrowLen, arrowW2); p0 = Vector3d.UnitZ * -size; p1 = Vector3d.UnitZ * size; DrawArrow(DC.GetPen(DrawTools.PEN_AXIS_Z), p0, p1, ArrowTypes.CROSS, ArrowPos.END, arrowLen, arrowW2); GL.LineWidth(1); FontTex tex; Vector3d xv = CadMath.Normal(DC.ViewDir, DC.UpVector); Vector3d yv = CadMath.Normal(DC.ViewDir, DC.UpVector); double fs = 0.6; tex = mFontFaceW.CreateTexture("X"); p1 = Vector3d.UnitX * size; GL.Color4(DC.GetBrush(DrawTools.BRUSH_COMPASS_LABEL_X).Color4()); mFontRenderer.Render(tex, p1, xv * tex.ImgW * fs, DC.UpVector * tex.ImgH * fs); tex = mFontFaceW.CreateTexture("Y"); p1 = Vector3d.UnitY * size; GL.Color4(DC.GetBrush(DrawTools.BRUSH_COMPASS_LABEL_Y).Color4()); mFontRenderer.Render(tex, p1, xv * tex.ImgW * fs, DC.UpVector * tex.ImgH * fs); tex = mFontFaceW.CreateTexture("Z"); p1 = Vector3d.UnitZ * size; GL.Color4(DC.GetBrush(DrawTools.BRUSH_COMPASS_LABEL_Z).Color4()); mFontRenderer.Render(tex, p1, xv * tex.ImgW * fs, DC.UpVector * tex.ImgH * fs); PopMatrixes(); }