private Vector2 ScreenToWorld(Vector2 v, bool hack = false) { Point formPosition = new Point((int)v.X, (int)v.Y); if (!hack) { formPosition = PointToClient(new Point((int)v.X, (int)v.Y)); Point controlPosition = new Point(); Control ctrl = worldView; while (ctrl != this) { controlPosition.X += ctrl.Location.X; controlPosition.Y += ctrl.Location.Y; ctrl = ctrl.Parent; } formPosition.X -= controlPosition.X; formPosition.Y -= controlPosition.Y; } //formPosition.X += worldView.Width / 2; //formPosition.Y += worldView.Height / 2; Matrix4 fakeProjection = Matrix4.CreateOrthographic(Tile.SIZE_F * 7, Tile.SIZE_F * 6 / camera.AspectRatio, 1f, Tile.SIZE_F * 4); return(ExtraMath.Unproject(fakeProjection, camera.View, worldView.Size, formPosition).Xy); }
public static void ApplyWindowRenderSize(int width, int height) { Log.Debug($"Width {Width} -> {width} , Height {Height} -> {height}"); //裁剪View Width = width; Height = height; GL.Viewport(0, 0, Width, Height); float radio = (float)Width / Height; ViewHeight = SB_HEIGHT; ViewWidth = SB_HEIGHT * radio; ProjectionMatrix = Matrix4.Identity * Matrix4.CreateOrthographic(ViewWidth, ViewHeight, -1, 1); CameraViewMatrix = Matrix4.Identity; if (PlayerSetting.FrameHeight != Height || PlayerSetting.FrameWidth != Width) { PostProcessesManager.Resize(PlayerSetting.FrameWidth, PlayerSetting.FrameHeight); } else { PostProcessesManager.Resize(Width, Height); } SetupClipPostProcesses(); }
public static void DrawTexturePixeled(Texture2D tex, float x, float y, Color4 color, float angle = 0.0f, float scale = 1.0f, int layer = -1, bool flipY = false) { if (tex == null) { return; } if (PixelCeiling) { x = (int)x; y = (int)y; } if (layer < 0) { GL.Disable(EnableCap.DepthTest); } tex2Shader.UseShader(); tex2Shader.SetParameter(tex2Shader.loc_layer, layer < 0 ? 0f : (float)layer); tex2Shader.SetParameter(tex2Shader.loc_flipY, flipY ? 1.0f : 0.0f); tex2Shader.SetParameter(tex2Shader.loc_color, ref color); tex2Shader.SetParameter(TextureUnit.Texture0, tex); //var ortho = Matrix4.CreateOrthographicOffCenter(0, MMW.ClientSize.Width, MMW.ClientSize.Height, 0, 0, 1.0f); var ortho = Matrix4.CreateOrthographic(MMW.ClientSize.Width, MMW.ClientSize.Height, 0, 1.0f); var mat = MatrixHelper.CreateTransform(new Vector3(x, y, 0), new Vector3(0, 0, angle), new Vector3(tex.Size.Width * 0.5f * scale, tex.Size.Height * 0.5f * scale, 1.0f)); tex2Shader.SetParameter(tex2Shader.loc_mvp, mat * ortho, false); DrawSubMesh(texMesh.subMeshes[0]); tex2Shader.UnuseShader(); GL.BindTexture(TextureTarget.Texture2D, 0); if (layer < 0) { GL.Enable(EnableCap.DepthTest); } }
private static void RenderFrame() { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); #region PROJECTION MATRIX var projMat = Matrix4.CreateOrthographic(battlefieldSize.X, battlefieldSize.Y, 0.1f, 500); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projMat); #endregion #region CAMERA MATRIX GL.MatrixMode(MatrixMode.Modelview); var lookMat = Matrix4.LookAt(new Vector3(0, 0, -10), new Vector3(0, 0, 1), Vector3.UnitY); var modelMat = Matrix4.CreateRotationY(0); lookMat = modelMat * lookMat; GL.LoadMatrix(ref lookMat); #endregion for (int i = 0; i < m_meshList.Count; ++i) { m_meshList[i].Draw(); } for (int i = 0; i < m_debugMeshList.Count; ++i) { m_debugMeshList[i].Draw(); } m_debugMeshList.Clear(); game.SwapBuffers(); }
public Shader(string fragShader, string vertShader) { _id = GL.CreateProgram(); int frag = GL.CreateShader(ShaderType.FragmentShader); int vert = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(frag, fragShader); GL.ShaderSource(vert, vertShader); GL.CompileShader(frag); var log = GL.GetShaderInfoLog(frag); if (!string.IsNullOrEmpty(log)) { Console.WriteLine(log); } GL.CompileShader(vert); log = GL.GetShaderInfoLog(vert); if (!string.IsNullOrEmpty(log)) { Console.WriteLine(log); } GL.AttachShader(_id, frag); GL.AttachShader(_id, vert); GL.LinkProgram(_id); GL.ValidateProgram(_id); LoadUniformMat4f("pr_matrix", Matrix4.CreateOrthographic(16.0f, 9.0f, -1.0f, 1.0f)); }
/// <summary> /// Called every time a viewport is changed. /// </summary> public override void GLsetupViewport(int width, int height, float near = 0.01f, float far = 1000.0f) { // 1. set ViewPort transform: GL.Viewport(0, 0, width, height); // 2. set projection matrix // 2a. perspective if (float.IsPositiveInfinity(far)) { float viewAngleVertical = (float)(Fov * 180 / Math.PI); float f = (float)(1.0 / Math.Tan(viewAngleVertical / 2.0)); float aspect = width / (float)height; //perspectiveProjection = new Matrix4 ( focalLength, 0, 0, 0, 0, focalLength / ratio, 0, 0, 0, 0, -1, -2 * near, 0, 0, -1, 0 ); perspectiveProjection = new Matrix4(f / aspect, 0.0f, 0.0f, 0.0f, 0.0f, f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f, -2.0f * near, 0.0f); } else { perspectiveProjection = Matrix4.CreatePerspectiveFieldOfView(Fov, width / (float)height, near, far); } // 2b. orthographic float minSize = 2.0f * Math.Min(width, height); ortographicProjection = Matrix4.CreateOrthographic(Diameter * width / minSize, Diameter * height / minSize, near, far); GLsetProjection(); setEllipse(width, height); }
public override Matrix4 GetViewportMatrix() { const float near = -1000000; const float far = 1000000; return(Matrix4.CreateOrthographic(Width, Height, near, far)); }
/// <summary> /// Called during the initial load and sets up the matrix and view /// </summary> /// <param name="sender">sender</param> /// <param name="e">eventarg</param> private void Form1_Load(object sender, EventArgs e) { GL.Enable(EnableCap.DepthTest); GL.MatrixMode(MatrixMode.Projection); projMat = Matrix4.CreateOrthographic(10.0f, 10.0f, 0.5f, 50.0f); GL.LoadMatrix(ref projMat); }
/// <summary> /// This is our main rendering function, which executes on the rendering thread. /// </summary> public void Render(double time) { lock (update_lock) { if (viewport_changed) { GL.Viewport(0, 0, viewport_width, viewport_height); viewport_changed = false; } } Matrix4 perspective = Matrix4.CreateOrthographic(2, 2, -1, 1); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref perspective); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Begin(BeginMode.Points); foreach (Particle p in Particles) { GL.Color4(p.Color); GL.Vertex2(p.Position); } GL.End(); }
private void SetCamera() { float height = ClientRectangle.Height; float width = ClientRectangle.Width; if (mShader != null) { Matrix4 proj; if (height > width) { if (width == 0) { width = 1; } proj = Matrix4.CreateOrthographic(10, 10 * height / width, 0, 10); } else { if (height == 0) { height = 1; } proj = Matrix4.CreateOrthographic(10 * width / height, 10, 0, 10); } int uProjectionLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uProjection"); GL.UniformMatrix4(uProjectionLocation, true, ref proj); } }
public void Draw() { EchoShader.Instance.CurrentTexture = EchoTexture; EchoRect.Scale = new Vector2(Window.Width, Window.Height); Status.Projection = Matrix4.CreateOrthographic(Window.Width, Window.Height, 0.1f, 100); EchoRect.Draw(this); }
protected override void OnResize(EventArgs e) { base.OnResize(e); if (mShader != null) { int uProjectionLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uProjection"); int windowHeight = this.ClientRectangle.Height; int windowWidth = this.ClientRectangle.Width; if (windowHeight > windowWidth) { if (windowWidth < 1) { windowWidth = 1; } float ratio = windowHeight / windowWidth; Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(1, (float)ClientRectangle.Width / ClientRectangle.Height, 0.5f, 5); GL.UniformMatrix4(uProjectionLocation, true, ref projection); } else { if (windowHeight < 1) { windowHeight = 1; } float ratio = windowWidth / windowHeight; Matrix4 projection = Matrix4.CreateOrthographic(10, ratio * 10, -1, 1); GL.UniformMatrix4(uProjectionLocation, true, ref projection); } } GL.Viewport(this.ClientRectangle); }
private static void OnRenderFrame() { // calculate how much time has elapsed since the last frame watch.Stop(); float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency; watch.Restart(); Matrix4 modelMatrix = CreateModelMatrix(time_); time_ += deltaTime; effect_.update(deltaTime, Neutrino._math.vec3_(modelMatrix[3].x, modelMatrix[3].y, modelMatrix[3].z), null); // set up the OpenGL viewport and clear both the color and depth bits Gl.Viewport(0, 0, width, height); //Gl.ClearColor(0.5F, 0.5F, 0.5F, 0.0F); Gl.ClearColor(0.4F, 0.4F, 0.4F, 0.0F); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 projMatrix = Matrix4.CreateOrthographic(width, height, 1F, 10000F); //.CreatePerspectiveFieldOfView(60F * (float)Math.PI / 180F, (float)width / height, 1F, 10000F); Matrix4 viewMatrix = Matrix4.LookAt(new Vector3(0, 0, 1000), Vector3.Zero, Vector3.Up); effect_.render(ref projMatrix, ref viewMatrix, ref modelMatrix); Glut.glutSwapBuffers(); }
protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); shader.Use(); controller.NewFrame(this); ImGui.SliderFloat("Scale", ref scale, 0, 10); ImGui.SliderFloat("Angle", ref angle, -3.14f, 3.14f); ImGui.SliderFloat("Angle_z", ref angle_z, -3.14f, 3.14f); ImGui.SliderFloat("Distance", ref dist, 0, 10.0f); ImGui.Checkbox("Perspective", ref persp); shader.SetUniform("scaleFactor", scale); var model = Matrix4.CreateRotationY(angle) * Matrix4.CreateRotationX(angle_z) * Matrix4.CreateTranslation(0, 0, -dist); shader.SetUniform("model", model); var projection = persp ? Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 2), (float)Width / Height, 0.1f, 100.0f) : Matrix4.CreateOrthographic(10, 10, -7, 7); shader.SetUniform("projection", projection); GL.DrawElements(PrimitiveType.Triangles, mesh.Indeces.Length, DrawElementsType.UnsignedInt, 0); controller.Render(); Context.SwapBuffers(); base.OnRenderFrame(e); }
protected void ResizeViewTo(GLTextuer2D inc, GLTextuer2D o, int owidth, int oheight, int nwidth, int nheight) { float wp = (float)nwidth / (float)owidth; float hp = (float)nheight / (float)oheight; float fp = wp < hp ? wp : hp; Matrix4 proj = Matrix4.CreateOrthographic(nwidth, nheight, 0.03f, 1000f); Matrix4 translation = Matrix4.CreateTranslation(0, 0, 0); //half width/height for scale as it is centered based Matrix4 sm = Matrix4.CreateScale(fp * (float)(owidth * 0.5f), fp * (float)(oheight * 0.5f), 1); Matrix4 model = sm * translation; Matrix4 view = Matrix4.LookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.UnitY); resizeProcessor.Model = model; resizeProcessor.View = view; resizeProcessor.Projection = proj; resizeProcessor.Luminosity = Luminosity; resizeProcessor.Bind(inc); if (renderQuad != null) { renderQuad.Draw(); } resizeProcessor.Unbind(); Blit(o, nwidth, nheight); }
/// <summary>Calculate the Projection matrix - projects the 3d model space to the 2D screen</summary> public void CalculateProjectionMatrix() // calculate and return znear. { Size scr = ViewPort.Size; if (InPerspectiveMode) { // Fov, perspective, znear, zfar ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView(Fov, (float)scr.Width / scr.Height, PerspectiveNearZDistance, PerspectiveFarZDistance); ZNear = PerspectiveNearZDistance; } else { ZNear = -OrthographicDistance; float orthoheight = (OrthographicDistance / 5.0f) * scr.Height / scr.Width; ProjectionMatrix = Matrix4.CreateOrthographic(OrthographicDistance * 2.0f / 5.0f, orthoheight * 2.0F, -OrthographicDistance, OrthographicDistance); Matrix4 zoffset = Matrix4.CreateTranslation(0, 0, 0.5f); // we ensure all z's are based around 0.5f. W = 1 in clip space, so Z must be <= 1 to be visible ProjectionMatrix = Matrix4.Mult(ProjectionMatrix, zoffset); // doing this means that a control can display with a Z around low 0. } //System.Diagnostics.Debug.WriteLine("PM\r\n{0}", ProjectionMatrix); if (ModelAxisPositiveZAwayFromViewer) { ProjectionMatrix = ProjectionMatrix * Matrix4.CreateScale(new Vector3(1, -1, 1)); // flip y to make y+ up } ProjectionModelMatrix = Matrix4.Mult(ModelMatrix, ProjectionMatrix); CountMatrixCalcs++; }
public void Setup(GLControl glControl) { GL.Viewport(0, 0, glControl.Width, glControl.Height); viewport[0] = 0; viewport[1] = 0; viewport[2] = glControl.Width; viewport[3] = glControl.Height; var aspect = glControl.Width / (float)glControl.Height; var o = Matrix4.CreateOrthographic(OrthoWidth, OrthoWidth / aspect, ZNear, ZFar); Matrix4 mp = Matrix4.CreatePerspectiveFieldOfView((float)(Fov * Math.PI / 180) * zoom, glControl.Width / (float)glControl.Height, 1, 25e4f); GL.MatrixMode(MatrixMode.Projection); if (IsOrtho) { ProjectionMatrix = o; GL.LoadMatrix(ref o); } else { ProjectionMatrix = mp; GL.LoadMatrix(ref mp); } Matrix4 modelview = Matrix4.LookAt(CamFrom, CamTo, CamUp); //modelview = WorldMatrix * modelview; GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); ViewMatrix = modelview; GL.MultMatrix(ref WorldMatrix); }
public LightSource() { shadowBuffer = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, shadowBuffer); shadowMap = Texture2D.CreateShadowMap(width, heigth); //shadowMap.BindTexture(); GL.DrawBuffer(DrawBufferMode.None); GL.ReadBuffer(ReadBufferMode.None); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); //load shader shdr = ShaderManager.GetInstance.GetShader("shadow"); //locate source pos = new Vector3(-1f, 2f, 1f); look = new Vector3(0f, 0f, 0f); projection = Matrix4.CreateOrthographic(5f, 5f, 0.1f, 5f); direction = Matrix4.LookAt(pos, look, new Vector3(0f, 1f, 0f)); UniformBufferManager ubm = UniformBufferManager.GetInstance; ubo = (UniformBufferSkeleton)ubm.GetBuffer("skeleton"); ubs = (UniformBufferSpace)ubm.GetBuffer("space"); ubl = (UniformBufferLight)ubm.GetBuffer("light"); }
protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); var w = 10f; var d = w; _proj3D = Matrix4.CreateOrthographic(w, w * Height / Width, -d / 2, d / 2); if (_pause) { return; } _t += e.Time; var pln = MVec4D.UnitXy + MVec4D.UnitXz + MVec4D.UnitYw; var r = MVec4D.Rotor(e.Time / 10, pln.Normalized); foreach (var pent in _simplexes) { for (var i = 0; i < pent.Verts.Length; i++) { pent.Verts[i] |= r; } } var blade = MVec4D.UnitXyz; var pivot = MVec4D.Zero; for (var i = 0; i < _simplexes.Length; i++) { _intersections[i] = _simplexes[i].Intersect(blade, pivot); } }
//Create Projection Matrix public static void CreateProjectionMatrix(int height, int width) { int xaspect = width / 100; int yaspect = (height) / 100; ProjectionMatrix = Matrix4.CreateOrthographic(xaspect, yaspect, 0.01f, 100f); }
protected override Matrix4 GetProjectionMatrix() { // float near_plane = 0.01f; // float FarPlane = 7.5f; //return Matrix4.CreateOrthographicOffCenter(-Size.X / 2, Size.X / 2, -Size.Y / 2, Size.Y / 2, NearPlane, FarPlane); return(Matrix4.CreateOrthographic(Size.X, Size.Y, NearPlane, FarPlane)); }
private void Form1_Load(object sender, EventArgs e) { GL.Enable(EnableCap.DepthTest); GL.MatrixMode(MatrixMode.Projection); Matrix4 projMat = Matrix4.CreateOrthographic(glControl.Width, glControl.Height, 0.5f, 100.0f); GL.LoadMatrix(ref projMat); }
protected WindowBase() : base(800, 640, new OpenTK.Graphics.GraphicsMode(new OpenTK.Graphics.ColorFormat(8, 8, 8, 8), 3, 3, 4), "WINDOW", GameWindowFlags.Default, DisplayDevice.Default, 3, 0, OpenTK.Graphics.GraphicsContextFlags.ForwardCompatible) { Updated = false; InputService = new InputService(); AudioService = new AudioService(); CurrentView.Size = new SizeF(ClientSize.Width, ClientSize.Height); _ortho = Matrix4.CreateOrthographic(ClientSize.Width, ClientSize.Height, -2.0f, 50.0f); }
/// <summary> /// The OnLoad method gets called as soon as the window is created. Anything that needs to be /// created or initialized at the start of the game should go here. /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { _game = new Game(); Matrix4.CreateOrthographic((float)Width, (float)Height, -1f, 1f, out ORTHO_MATRIX); CursorVisible = true; Console.WriteLine(GL.GetString(StringName.Renderer)); }
public void Resize(int width, int height) { _width = width; _height = height; _proj = _camera.ComputeProjectionMatrix(_width / (float)_height); _guiProj = Matrix4.CreateOrthographic(_width, _height, 0, 1); }
public ViewManager(float width, float height) { VisibleSize = new Vector2(width, height); // XXX: Right now these values are pretty arbitrary. ZNear = 0.0f; ZFar = 10.0f; ProjectionMatrix = Matrix4.CreateOrthographic(width, height, ZNear, ZFar); }
public Matrix4 GetProjectionMat() { if (orthographic) { return(Matrix4.CreateOrthographic(orthographicSize * 2, orthographicSize * 2 / aspect, nearClipPlane, farClipPlane)); } return(Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 180.0f * fieldOfView, aspect, nearClipPlane, farClipPlane)); }
public void UpdateProjectionMatrix() { //WorldProjectionMatrix = Matrix4.CreateOrthographic(PreferredWidth * (Scale.X), PreferredHeight * (Scale.Y), //-1000.0f, 1000.0f); WorldZOffset = -1000 - (float)(System.Math.Exp(Scale.X * 10) * 0.01); WorldProjectionMatrix = Matrix4.Mult(Matrix4.CreateTranslation(0, 0, WorldZOffset), Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, WindowWidth / WindowHeight, 0.1f, 1000000f)); ScreenProjectionMatrix = Matrix4.CreateOrthographic(WindowWidth, WindowHeight, -10.0f, 10.0f); }
public override void Awake() { projection = Matrix4.CreateOrthographic(Transform.Size.X, -Transform.Size.Y, 0f, 100f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); GL.Translate(Transform.Position); }
public Matrix4 GetProjectionMatrix() { if (IsOrthographic) { return(Matrix4.CreateOrthographic(OrthographicSize * 2, OrthographicSize * 2 / AspectRatio, NearClipPlane, FarClipPlane)); } return(Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 180.0f * FieldOfView, AspectRatio, NearClipPlane, FarClipPlane)); }