protected override void DestroyTextureBuffer() { if (!HasDxContext) { base.DestroyTextureBuffer(); return; } UnlockTexture(); UnregisterDxTexture(); GLObject.TryDispose(ref _textureBuffer); GLObject.TryDispose(ref _dxTexture); }
protected override void OnLoad(EventArgs e) { // Set up window VSync = VSyncMode.On; this.WindowBorder = OpenTK.WindowBorder.Fixed; GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.AlphaTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.Enable(EnableCap.CullFace); _Object = MakeQuad(0.8f, 0.8f); }
protected override void OnLoad(EventArgs e) { VSync = VSyncMode.On; this.WindowBorder = OpenTK.WindowBorder.Fixed; GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.Enable(EnableCap.Multisample); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); Cube = PrepareCube(); Rect = PrepareRect(); InitMatrices(); LoadFrameBuffer(); }
protected override void OnLoad(EventArgs e) { // Configure VSync = VSyncMode.On; WindowBorder = OpenTK.WindowBorder.Fixed; GL.Enable(EnableCap.DepthTest); // Init scene image = Image.FromStream(Helper.GetResourceStream("Assets/cover.jpg")); myObject = MakeObject(image); // Wire up handlers this.Mouse.WheelChanged += (s, e2) => { Matrix4 translation = Matrix4.CreateTranslation(0, 0, e2.DeltaPrecise*5); //Matrix4.Mult(ref camera, ref translation, out camera); Matrix4.Mult(ref translation, ref camera, out camera); }; bool rotating = false; bool panning = false; this.Mouse.ButtonDown += (s, e2) => { if (e2.Button == OpenTK.Input.MouseButton.Left) rotating = true; if (e2.Button == OpenTK.Input.MouseButton.Right) panning = true; }; Mouse.ButtonUp += (s, e2) => { if (e2.Button == OpenTK.Input.MouseButton.Left) rotating = false; if (e2.Button == OpenTK.Input.MouseButton.Right) panning = false; }; this.Mouse.Move += (s, e2) => { if (rotating) { Matrix4 rotation = Matrix4.CreateRotationY(-e2.XDelta / 30f); Matrix4 rotation2 = Matrix4.CreateRotationX(-e2.YDelta / 30f); Matrix4.Mult(ref camera, ref rotation, out camera); Matrix4.Mult(ref camera, ref rotation2, out camera); } if (panning) { Matrix4 translation = Matrix4.CreateTranslation(e2.XDelta, 0, e2.YDelta); Matrix4.Mult(ref camera, ref translation, out camera); } }; }
public GLObject GetNearestBonusItem(GLPoint p) { var minDist = double.MaxValue; GLObject res = null; foreach (var item in BonusItems) { var dist = p.DistanceToPoint(item.Value.Position); if (dist < minDist) { minDist = dist; res = item.Value; } } return(res); }
static unsafe void Main(string[] args) { var Options = WindowOptions.Default; Options.Size = new Silk.NET.Maths.Vector2D <int>(800, 600); Options.Title = "LearnOpenGL with Silk.NET"; var window = Window.Create(Options); window.Render += (obj) => { GL.GetApi(window).ClearColor(Color.Black); GL.GetApi(window).Clear((uint)ClearBufferMask.ColorBufferBit); globject.Render(); }; window.Load += () => { globject = new GLObject(GL.GetApi(window), Vertices, Indices, "shader.vert", "shader.frag", false, 1); }; window.Closing += () => { globject.Dispose(); }; window.Run(); }
internal static void Render(IGenericWindow window, float deltatime) { if (window.CurrentScene == null) { return; } if (window.CurrentRenderPipeline == null) { window.SetRenderPipeline(SMRenderer.DefaultRenderPipeline); } SMRenderer.CurrentFrame++; GLObject.DisposeMarkedObjects(); Deltatime.RenderDelta = deltatime; var drawContext = new DrawContext { Window = window, Scene = window.CurrentScene, RenderPipeline = window.CurrentRenderPipeline, Mesh = Plate.Object, Material = window.CurrentRenderPipeline.DefaultMaterial, ModelMatrix = Matrix4.Identity, TextureMatrix = Matrix3.Identity, Instances = new Instance[1] { new Instance { ModelMatrix = Matrix4.Identity, TextureMatrix = Matrix3.Identity } } }; drawContext.SetCamera(window.ViewportCamera); GL.DepthFunc(DepthFunction.Lequal); window.CurrentRenderPipeline?.Render(ref drawContext); }
public void PickUpBonusItem(GLObject item) { var found = false; Point indexToRemove = new Point(); foreach (var kvp in BonusItems) { if (kvp.Value == item) { indexToRemove = kvp.Key; found = true; break; } } if (found) { BonusItems.Remove(indexToRemove); FinishCount++; string sound; if (FinishCount >= BonusItemsCount) { sound = "lastdiamond.mp3"; Locked = false; } else { sound = "diamond.mp3"; } Task.Run(() => { var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; player.Load(sound); player.Play(); }); } }
static void Main(string[] args) { GameWindow w = new GameWindow(); ShaderProgram sp = null; Vertexarray vao = null; Buffer <float> vbo_pos = null; Buffer <float> vbo_uv = null; Buffer <uint> ebo = null; Texture2D texture = null; w.Load += (s, a) => { GL.ClearColor(0, 0, 0, 1); // setting up shader var frag = new Shader(ShaderType.FragmentShader, File.ReadAllText("frag.glsl")); var vert = new Shader(ShaderType.VertexShader, File.ReadAllText("vert.glsl")); sp = new ShaderProgram(frag, vert); frag.Dispose(); vert.Dispose(); // setting up buffers vbo_pos = Glow.Buffer.create(new float[] { -.5f, -.5f, 0, 0, .5f, 0, .5f, -.5f, 0 }); vbo_uv = Glow.Buffer.create(new float[] { -0.5f, -0.5f, .5f, 1.5f, 1.5f, -0.5f }); ebo = Glow.Buffer.create(new uint[] { 0, 1, 2 }); // setting up vao int pos_loc = sp.get_attrib_location("pos"); int uv_loc = sp.get_attrib_location("uv"); vao = new Vertexarray(); vao.set_elementbuffer(ebo); vao.set_attribute_pointer(pos_loc, AttribType.Vec3, vbo_pos, sizeof(float) * 3); vao.set_attribute_pointer(uv_loc, AttribType.Vec2, vbo_uv, sizeof(float) * 2); GLObject.check_glerror(); // setting up texture /* * var r = new Random(); * var pixels = new Color32bit[16, 16]; * for (int x = 0; x < 16; x++) { * for (int y = 0; y < 16; y++) { * if (x == 0 || y == 0 || x == 15 || y == 15) { * pixels[x, y] = new Color32bit(1.0f); * } else { * pixels[x, y] = new Color32bit((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()); * } * } * } * texture = new Texture2D(pixels) { * Filter = Filter.Nearest, * };*/ var sw = new Stopwatch(); sw.Start(); texture = new Texture2D("Skybox_back.png"); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); foreach (var item in GLObject.Instances) { Console.WriteLine(item); } }; w.RenderFrame += (s, e) => { GL.Clear(ClearBufferMask.ColorBufferBit); sp.use(); texture.bind(TextureUnit.Texture0); //vao.DrawArrays(PrimitiveType.Triangles, 0, 9); vao.draw_elements(PrimitiveType.Triangles, 3, DrawElementsType.UnsignedInt); GL.Flush(); w.SwapBuffers(); }; var r = new Random(); w.UpdateFrame += (s, e) => { //texture.pixels[r.Next(texture.width), r.Next(texture.height)] = new color((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()); var h = r.Next(texture.height); for (int i = 0; i < texture.width; i++) { texture.pixels[i, h] = new color(1); } texture.apply(); }; w.Resize += (s, e) => { GL.Viewport(0, 0, w.Width, w.Height); }; w.Run(); }
private GLObject PrepareRect() { float[] vertices = new float[] { //X Y -1f, -1f, 0, 0, 1f, -1f, 1, 0, 1f, 1f, 1, 1, 1f, 1f, 1, 1, -1f, 1f, 0, 1, -1f, -1f, 0, 0 }; var program = new ShaderProgram(); program.AddShader(new Shader(ShaderType.VertexShader, LoadShader("Shaders/2d.vert"))); program.AddShader(new Shader(ShaderType.FragmentShader, LoadShader("Shaders/2d.frag"))); var obj = new GLObject(vertices, new List<PartitionRule>() { new PartitionRule("position", 0, 2), new PartitionRule("texcoord", 2, 2) }, program); obj.Program.Uniform1("texFramebuffer", 0); obj.Activate += (sender) => { GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, ColorBuffer); }; return obj; }
private GLObject PrepareCube() { float[] vertices = new float[] { //X Y Z R G B U V -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -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, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; // Make shader var program1 = new ShaderProgram(); program1.AddShader(new Shader(ShaderType.VertexShader, LoadShader("Shaders/basic.vert"))); program1.AddShader(new Shader(ShaderType.FragmentShader, LoadShader("Shaders/basic.frag"))); // Make textures var tex1 = new Texture(new Bitmap(GetResourceStream("Assets/info.png")), TextureTarget.Texture2D, PixelInternalFormat.Rgba, 0); tex1.TexParameter(TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); tex1.TexParameter(TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); tex1.TexParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapLinear); tex1.TexParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); // Create mipmap (pre-rendered thumb) GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); var tex2 = new Texture(new Bitmap(GetResourceStream("Assets/play.png")), TextureTarget.Texture2D, PixelInternalFormat.Rgba, 1); tex2.TexParameter(TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); tex2.TexParameter(TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); tex2.TexParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapLinear); tex2.TexParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); // Create mipmap (pre-rendered thumb) GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); List<PartitionRule> rules = new List<PartitionRule>() { new PartitionRule("position", 0, 3), new PartitionRule("color", 3, 3), new PartitionRule("texcoord", 6, 2) }; GLObject obj = new GLObject(vertices, rules, program1); obj.AddTexture("tex1", tex1); obj.AddTexture("tex2", tex2); return obj; }
private GLObject MakeObject(Image img) { int width = img.Width; int height = img.Height; int stride = 4; Vector2[] vertices = MakePlane(width, height, width /2, height / 2); float[] data = new float[vertices.Length * stride]; for (int i = 0; i < vertices.Length; i++) { int j = i * stride; Vector2 v = vertices[i]; // X / Z data[j] = v.X; data[j+1] = v.Y; // U / V data[j + 2] = v.X / width; data[j + 3] = -v.Y / height; } Matrix4 model = Matrix4.Identity;//Matrix4.CreateRotationX(3.41f); camera = Matrix4.LookAt(new Vector3(700, 700, -400), new Vector3(600, 0, -300), new Vector3(0, 1, 0)); Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(3.41f / 4, Width / (float)Height, 0.1f, 10000); // Calc shader ShaderProgram program = new ShaderProgram(); program.AddShader(new Shader(ShaderType.VertexShader, Helper.LoadShader("Shaders/picture.vert"))); program.AddShader(new Shader(ShaderType.FragmentShader, Helper.LoadShader("Shaders/picture.frag"))); if (!program.Compile()) { throw new Exception(program.Log); } program.UniformMatrix4("model", false, ref model); program.UniformMatrix4("view", false, ref camera); program.UniformMatrix4("projection", false, ref proj); Texture image = new Texture(img, TextureTarget.Texture2D, PixelInternalFormat.Rgb, 0); image.TexParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); image.TexParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); image.TexParameter(TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); image.TexParameter(TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); // Make instance GLObject obj = new GLObject(data, new List<PartitionRule>() { new PartitionRule("position", 0, 2), new PartitionRule("texcoord", 2, 2) }, program); obj.AddTexture("image", image); return obj; }
public void Generate(Context context, int moves, int itemsCount) { Clear(); State = AutoPilotStateEnum.Stoppped; StartPos = new Point(Rnd.Next(20, 80), Rnd.Next(20, 80)); _bonusItemsCount = itemsCount; var actPos = new Point(StartPos.X, StartPos.Y); EndPos = new Point(actPos.X, actPos.Y); LabMatrix[actPos.X, actPos.Y] = 1; var direction = 1; for (var move = 0; move <= moves; move++) { var steps = Rnd.Next(1, 4); if (move == 0) { steps = 4; // 4 steps on beginning } for (var step = 0; step < steps; step++) { if (CanMove(actPos.X, actPos.Y, direction)) { switch (direction) { case 0: actPos.X += 1; break; case 1: actPos.Y += 1; break; case 2: actPos.X -= 1; break; case 3: actPos.Y -= 1; break; } Path.Add(direction); EndPos = new Point(actPos.X, actPos.Y); LabMatrix[actPos.X, actPos.Y] = 1; } } direction = Rnd.Next(0, 3); } var generatedPositions = new List <Point>(); for (var j = 0; j < LabyrinthHeight; j++) { for (var i = 0; i < LabyrinthWidth; i++) { if (LabMatrix[i, j] == 1) { generatedPositions.Add(new Point(i, j)); } if ((i == StartPos.X) && (j == StartPos.Y)) { GeneratePosition(i, j, "labWallS", "labBottomS", "labTopS"); } else if ((i == EndPos.X) && (j == EndPos.Y)) { // locked: LockedFinishPolygons.AddRange(GeneratePosition(i, j, "labWallL", "labBottomL", "labTopL")); // unlocked: UnLockedFinishPolygons.AddRange(GeneratePosition(i, j, "labWallF", "labBottomF", "labTopF")); } else { GeneratePosition(i, j); } } } // bonus items if (generatedPositions.Count > BonusItemsCount + 4) { while (BonusItems.Count < BonusItemsCount) { var r = Rnd.Next(2, generatedPositions.Count - 2); if (BonusItems.Count == 0) { // first bonus item always 2 steps before observer r = 2; } var p = new Point(generatedPositions[r].X, generatedPositions[r].Y); if (BonusItems.ContainsKey(p) || ((StartPos.X == p.X && StartPos.Y == p.Y)) || ((EndPos.X == p.X && EndPos.Y == p.Y)) ) { continue; } var item = new GLObject(); item.LoadFromAndroidAsset(context, "diamond.xml"); item.Position = new GLPoint((p.X + 0) * TileWidth, -2, (p.Y + 0) * TileWidth + TileWidth / 2); BonusItems.Add(new Point(p.X, p.Y), item); } } // generating map var mapLines = new StringBuilder(); for (var j = 0; j < LabyrinthHeight; j++) { string line = null; for (var i = 0; i < LabyrinthWidth; i++) { if (LabMatrix[i, j] == 1) { if ((i == StartPos.X) && (j == StartPos.Y)) { line += 'S'; } else if ((i == EndPos.X) && (j == EndPos.Y)) { line += 'F'; } else { var bonusAtThisPos = false; foreach (var kvp in BonusItems) { if ((i == kvp.Key.X) && (j == kvp.Key.Y)) { bonusAtThisPos = true; break; } } line += bonusAtThisPos ? 'o' :'#'; } } else { line += ' '; } } if (line != null && line.Trim() != String.Empty) { mapLines.Append(line); mapLines.Append(Environment.NewLine); } } Logger.Info("Map:" + Environment.NewLine + mapLines.ToString()); Locked = true; FinishCount = 0; Move(-TileWidth / 2, 0, 0); }
public void DestroyBuffers() { DestroyBuffersOverride(); GLObject.TryDispose(ref _framebuffer); }
protected override void DestroyBuffersOverride() { base.DestroyBuffersOverride(); GLObject.TryDispose(ref _depthBuffer); }
public virtual void Destroy() { GLObject.TryDispose(ref _program); GLObject.TryDispose(ref _vertexArrayObject); }
/// <summary> /// Find OpenGL textures. /// </summary> /// <param name="existing">List of objects already existent in the scene.</param> /// <param name="range">Range in which to search for external objects (starting with 0).</param> /// <returns></returns> public static IEnumerable<GLObject> FindTextures(GLObject[] existing, int range = 64) => FindObjects(existing, new[] { typeof(GLImage), typeof(GLTexture) }, GLIsTexMethod, GLTexLabel, range);
/// <summary> /// Find OpenGL buffers. /// </summary> /// <param name="existing">List of objects already existent in the scene.</param> /// <param name="range">Range in which to search for external objects (starting with 0).</param> /// <returns></returns> public static IEnumerable<GLObject> FindBuffers(GLObject[] existing, int range = 64) => FindObjects(existing, new[] { typeof(GLBuffer) }, GLIsBufMethod, GLBufLabel, range);
public static void RemoveObject(GLObject o) { if(glObjects != null) { glObjects.Remove(o); } }
public static void AddObject(GLObject o) { if(glRenderer == null) { glRenderer = Camera.main.gameObject.AddComponent<GLRenderer>(); } if(glObjects == null) { glObjects = new List<GLObject>(); } if(!glObjects.Contains(o)) { glObjects.Add(o); } }
protected virtual void DestroyTextureBuffer() { GLObject.TryDispose(ref _textureBuffer); }