This can also be useful with the Camera2D/Camera3D classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public void DrawGrid(System.Drawing.Color color, float X, float Y, int cell_size = 16, int grid_size = 256) { int dX = (int)Math.Round(X / cell_size) * cell_size; int dZ = (int)Math.Round(Z / cell_size) * cell_size; int ratio = grid_size / cell_size; GL.PushMatrix(); GL.Translate(dX - grid_size / 2, 0, dZ - grid_size / 2); int i; GL.Color3(color); GL.Begin(BeginMode.Lines); for (i = 0; i < ratio + 1; i++) { int current = i * cell_size; GL.Vertex3(current, 0, 0); GL.Vertex3(current, 0, grid_size); GL.Vertex3(0, 0, current); GL.Vertex3(grid_size, 0, current); } GL.End(); GL.PopMatrix(); } |
-Update-
Made a little example of a grid using fog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | namespace Tutorial { class Main : GameWindow { private Camera3D camera; private int cell_size = 32; private int grid_size = 4096; public Main() : base(800, 600) { Title = "Grid"; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL.Enable(EnableCap.Fog); GL.Fog(FogParameter.FogColor, new float[4] { 0.0f, 0.0f, 0.0f, 1.0f });//Same color as clear. GL.Fog(FogParameter.FogMode, (int)FogMode.Linear); GL.Fog(FogParameter.FogEnd, (float)(grid_size / 3.0f)); camera = new Camera3D(0, 1, 0); this.Mouse.Move += new EventHandler<OpenTK.Input.MouseMoveEventArgs>(Mouse_Move); } private void Mouse_Move(object sender, OpenTK.Input.MouseMoveEventArgs e) { int dX = e.XDelta; int dY = e.YDelta; camera.RotationX += (float)dY / 5.0f; camera.RotationY += (float)dX / 5.0f; } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, Width, Height); OpenTK.Matrix4 matrix = OpenTK.Matrix4.CreatePerspectiveFieldOfView(1.0f, (Width / (float)Height), 1.0f, 10000.0f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref matrix); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); if (Keyboard[OpenTK.Input.Key.W]) camera.Move(Camera3D.Direction.Forward); if (Keyboard[OpenTK.Input.Key.S]) camera.Move(Camera3D.Direction.Backward); if (Keyboard[OpenTK.Input.Key.A]) camera.Move(Camera3D.Direction.Left); if (Keyboard[OpenTK.Input.Key.D]) camera.Move(Camera3D.Direction.Right); if (Keyboard[OpenTK.Input.Key.Q]) camera.Move(Camera3D.Direction.Up); if (Keyboard[OpenTK.Input.Key.E]) camera.Move(Camera3D.Direction.Down); if (Keyboard[OpenTK.Input.Key.Number1]) { GL.Enable(EnableCap.Fog); } if (Keyboard[OpenTK.Input.Key.Number2]) { GL.Disable(EnableCap.Fog); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); camera.Update(); camera.DrawGrid(System.Drawing.Color.LimeGreen, cell_size, grid_size); SwapBuffers(); } } } |
I like it
ReplyDeleteHi, how to capture the mouse in the game?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, i will ask you:
ReplyDeleteCan be used for GLControl also?
Sorry for my english ;)
It complained at Camera3D
ReplyDeleteI've got the following USINGS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenTK;