示例#1
0
 private void glControl_Resize(object sender, EventArgs e)
 {
     if (_init)
     {
         GL1.Viewport(0, 0, GlControl.Width, GlControl.Height);
     }
 }
示例#2
0
 private void glControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
 {
     if (_init)
     {
         _w.UpdateUnitPositions();
         if (_w.Units.First().Position == new Position(_w.Field.Width / 2, _w.Field.Height / 2))
         {
             GlControl.Paint -= glControl_Paint;
             MessageBox.Show("Player 1 wins");
             Application.Current.Shutdown();
         }
         if (_w.Units.Last().Position == new Position(_w.Field.Width / 2, _w.Field.Height / 2))
         {
             GlControl.Paint -= glControl_Paint;
             MessageBox.Show("Player 2 wins");
             Application.Current.Shutdown();
         }
         _sw.Start();
         Graphics.DrawWorld(_w, _view, _w.Units.ToArray());
         GL1.Flush();
         //GL1.Finish();
         GL1.SwapBuffers(GL1.DC);
         _sw.Stop();
         mainWindow.Title = $"{_sw.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture)}";
         _sw.Reset();
     }
 }
示例#3
0
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     GL1.Init(GlControl.Handle);
     _drawTimer          = new Timer();
     _drawTimer.Elapsed += DrawTimer_Tick;
     _drawTimer.Interval = 10;
     _drawTimer.Start();
     _view = new Rectangle(0, 0, width, height);
     _w    = World.Generate(width, height, 1);
     OpenGLInit();
 }
示例#4
0
        public static void Init()
        {
            sceneShaped = false;
            GL1.GenVertexArray(1, out VAOId);
            GL1.BindVertexArray(VAOId);

            GL1.GenBuffer(1, ref VBOId);

            GL1.GenBuffer(1, ref VBOId2);

            GL1.BindBuffer(0x8892, VBOId);

            ShaderProgram = new ShaderProgram("shaders\\vertex.glsl", "shaders\\fragment.glsl");
            ShaderProgram.Use();
            matrixLocation = GL1.GetUniformLocation(ShaderProgram.ProgramID, "MVP");

            GL1.ClearColor(0.5f, 0, 1, 0);

            GL1.Clear(0x00000100 | 0x00004000);
        }
示例#5
0
        public ShaderProgram(string vertexPath, string fragPath)
        {
            var vID = GL1.CreateShader((uint)ShaderType.VertexShader);

            var fID        = GL1.CreateShader((uint)ShaderType.FragmentShader);
            var vertexText = File.ReadAllText(vertexPath);
            var fragText   = File.ReadAllText(fragPath);

            GL1.ShaderSource(vID, 1, new[] { vertexText }, new[] { vertexText.Length });
            GL1.ShaderSource(fID, 1, new[] { fragText }, new[] { fragText.Length });
            GL1.CompileShader(vID);
            GL1.CompileShader(fID);
            ProgramID = GL1.CreateProgram();
            GL1.AttachShader(ProgramID, vID);
            GL1.AttachShader(ProgramID, fID);
            GL1.LinkProgram(ProgramID);
            GL1.DetachShader(ProgramID, vID);
            GL1.DetachShader(ProgramID, fID);
            GL1.DeleteShader(vID);
            GL1.DeleteShader(fID);
        }
示例#6
0
 private static void DrawArray(Vector3[] pos, Vector3[] color, PrimitiveType type)
 {
     if (pos != null)
     {
         GL1.EnableVertexAttribArray(0);
         GL1.BindBuffer(0x8892, VBOId);
         GL1.BufferData(0x8892, (IntPtr)(Vector3.SizeInBytes * pos.Length), pos,
                        (uint)BufferUsageHint.DynamicDraw);
         GL1.VertexAttribPointer(0, 3, (uint)VertexAttribPointerType.Float, false, Vector3.SizeInBytes,
                                 (IntPtr)0);
     }
     if (color != null)
     {
         GL1.EnableVertexAttribArray(1);
         GL1.BindBuffer(0x8892, VBOId2);
         GL1.BufferData(0x8892, (IntPtr)(Vector3.SizeInBytes * color.Length), color,
                        (uint)BufferUsageHint.StreamDraw);
         GL1.VertexAttribPointer(1, 3, (uint)VertexAttribPointerType.Float, false, Vector3.SizeInBytes,
                                 (IntPtr)0);
     }
     GL1.DrawArrays((uint)type, 0, color.Length);
     GL1.BindBuffer(0x8892, 0);
 }
示例#7
0
        public static void DrawWorld(World w, Rectangle view, params Unit[] u)
        {
            GL1.DrawBuffer(0x0405);
            GL1.Clear(0x00000100 | 0x00004000);

            var mat = Matrix4.CreateOrthographicOffCenter((float)view.Left, (float)view.Right, (float)view.Bottom,
                                                          (float)view.Top, -1, 1);

            GL1.UniformMatrix4(matrixLocation, 1, false, ref mat);
            if (!sceneShaped)
            {
                _poses  = new Vector3[w.Field.Height * w.Field.Height * 2 * 3];
                _colors = new Vector3[w.Field.Height * w.Field.Height * 2 * 3];
            }
            var visionmap = new bool[w.Field.Width, w.Field.Height];

            foreach (var unit in u)
            {
                var tmp = w.Vision.VisionField(unit);
                for (var i = 0; i < w.Field.Width; i++)
                {
                    for (var j = 0; j < w.Field.Height; j++)
                    {
                        visionmap[i, j] = visionmap[i, j] || tmp[i, j];
                    }
                }
            }
            if (!sceneShaped)
            {
                for (var i = 0; i < w.Field.Width; i++)
                {
                    for (var j = 0; j < w.Field.Height; j++)
                    {
                        ShapeTile(i, j, w.Field[i, j].Passable);
                    }
                }
                DrawArray(_poses, _colors, PrimitiveType.Triangles);
                sceneShaped = true;
            }

            var cVisible = new Vector3(0, 0, 1);
            var cDefault = new Vector3(1, 1, 1);
            var cPlayer  = new Vector3(0, 1, 0);
            var cEnemy   = new Vector3(1, 0, 0);

            Parallel.For(0, w.Field.Width, i =>
            {
                for (var j = 0; j < w.Field.Height; j++)
                {
                    if (visionmap[i, j])
                    {
                        if (w.Field[i, j].Unit != null)
                        {
                            UpdateTile(6 * (i * w.Field.Height + j), w.Field[i, j].Passable, ref cEnemy, ref cVisible);
                        }
                        else
                        {
                            UpdateTile(6 * (i * w.Field.Height + j), w.Field[i, j].Passable, ref cDefault, ref cVisible);
                        }
                    }
                    else
                    {
                        UpdateTile(6 * (i * w.Field.Height + j), w.Field[i, j].Passable, ref cDefault, ref cDefault);
                    }
                }
            });
            foreach (var unit in u)
            {
                UpdateTile(6 * (unit.Position.X * w.Field.Height + unit.Position.Y), true, ref cPlayer, ref cVisible);
            }
            DrawArray(null, _colors, PrimitiveType.Triangles);
        }
示例#8
0
 public void Use()
 {
     GL1.UseProgram(ProgramID);
 }
示例#9
0
 public void Dispose()
 {
     GL1.UseProgram(0);
     GL1.DeleteProgram(ProgramID);
 }