public Puzzle(int _nbx, int _nby, string _imgPath, int _Sides = 4) { Image = new GGL.Texture (_imgPath); nbPieceX = _nbx; nbPieceY = _nby; nbSides = _Sides; List<Vector3> tmp = new List<Vector3>(); cutter = new Cutter (CutType.Curvy); Vector3[] HBorder = MakeHorizontalBorder (nbPieceX, largP); Vector3[] VBorder = MakeHorizontalBorder (nbPieceY, hautP); tmp.AddRange (HBorder); for (int i = 1; i < nbPieceY; i++) { Matrix4 mat = Matrix4.CreateTranslation(0, i * hautP, 0); tmp.AddRange( transform (MakeHorizontalCut (nbPieceX, largP), mat)); } tmp.AddRange (transform (HBorder, Matrix4.CreateTranslation (0, Image.Height, 0))); Matrix4 rot = Matrix4.CreateRotationZ (MathHelper.PiOver2); tmp.AddRange ( transform (VBorder, rot)); for (int i = 1; i < nbPieceX; i++) { Matrix4 mat = rot * Matrix4.CreateTranslation(i * largP, 0, 0); tmp.AddRange( transform (MakeHorizontalCut (nbPieceY, hautP), mat)); } tmp.AddRange ( transform (VBorder, rot * Matrix4.CreateTranslation (Image.Width, 0, 0))); tmp.Add(new Vector3(Image.Width,Image.Height,0)); positions = new Vector3[tmp.Count*2]; Vector3[] bord = tmp.ToArray (); transform (ref bord, Matrix4.CreateTranslation (0, 0, -PieceThickness)); BorderOffset = (uint)tmp.Count; Array.Copy (tmp.ToArray (), 0, positions, 0, tmp.Count); Array.Copy (bord, 0, positions, BorderOffset, tmp.Count); CreateVBOs (); CreateVAOs (); createPieces (); List<uint> indicesList = new List<uint>(); foreach (Piece p in ZOrderedPieces) { p.IndFillPtr = indicesList.Count * sizeof(uint); p.indFillLength = p.IndFill.Length + 1; indicesList.AddRange (p.IndFill); indicesList.Add (uint.MaxValue); p.IndBorderPtr = new int[nbSides]; p.indBorderLength = new int[nbSides]; for (int i = 0; i < nbSides; i++) { p.IndBorderPtr[i] = indicesList.Count * sizeof(uint); p.indBorderLength [i] = p.IndBorder [i].Length; indicesList.AddRange (p.IndBorder [i]); indicesList.Add (uint.MaxValue); } } indices = indicesList.ToArray (); eboHandle = GL.GenBuffer (); GL.BindVertexArray(vaoHandle); GL.BindBuffer (BufferTarget.ElementArrayBuffer, eboHandle); GL.BufferData (BufferTarget.ElementArrayBuffer, new IntPtr (sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw); GL.BindVertexArray(0); Ready = true; }
void createProfileTexture(uint[] indProfile) { int fbo; profileTexture = new GGL.Texture (Image.Width, Image.Height); GL.ActiveTexture (TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, profileTexture); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Image.Width, Image.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.BindTexture(TextureTarget.Texture2D, 0); GL.GenFramebuffers(1, out fbo); GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, profileTexture, 0); if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete) { throw new Exception(GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer).ToString()); } GL.Disable (EnableCap.CullFace); GameLib.EffectShader RedFillShader = new GameLib.EffectShader ("Opuz2015.shaders.red"); RedFillShader.ProjectionMatrix = Matrix4.CreateOrthographicOffCenter (0, Image.Width, 0, Image.Height, 0, 1); RedFillShader.ModelViewMatrix = Matrix4.Identity; RedFillShader.ModelMatrix = Matrix4.Identity; RedFillShader.Enable (); GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo); float[] cc = new float[4]; int[] viewport = new int[4]; GL.GetInteger (GetPName.Viewport, viewport); GL.Viewport (0, 0, Image.Width, Image.Height); GL.GetFloat (GetPName.ColorClearValue, cc); GL.ClearColor (0, 0, 0, 0); GL.Clear (ClearBufferMask.ColorBufferBit); GL.BindVertexArray(vaoHandle); GL.LineWidth(3); GL.DrawElements (PrimitiveType.LineLoop, indProfile.Length, DrawElementsType.UnsignedInt, indProfile); GL.BindVertexArray (0); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Enable (EnableCap.CullFace); GL.UseProgram (0); GL.ClearColor (cc[0],cc[1],cc[2],cc[3]); GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]); GL.DeleteFramebuffer(fbo); RedFillShader.Dispose (); }