public void RenderTexture(RTexture texture, Math.Rectangle bounds, RColor color, Matrix matrix, bool font) { RViewport viewport = REngine.Instance._viewport; UpdateQuad(bounds); blendState.PlatformApplyState(); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); defaultShader.Bind(); defaultShader.SetSamplerValue(RTextureLayer.DIFFUSE, texture); vertexQuad2D.Bind(); vertexQuad2D.BindVertexArray(); indexQuad2D.Bind(); defaultShader.SetUniformValue("projection", camera2d.Projection); defaultShader.SetUniformValue("view", camera2d.View); defaultShader.SetUniformValue("diffuse_color", color.ToVector4()); defaultShader.SetUniformValue("model", matrix); defaultShader.SetUniformValue("font", font); vertexQuad2D.VertexDeclaration.Apply(defaultShader, IntPtr.Zero); GL.DrawElements(PrimitiveType.Triangles, indexQuad2D.IndexCount, DrawElementsType.UnsignedShort, IntPtr.Zero); REngine.CheckGLError(); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.DstAlpha); GL.Disable(EnableCap.Blend); indexQuad2D.Unbind(); vertexQuad2D.UnbindVertexArray(); vertexQuad2D.Unbind(); defaultShader.Unbind(); }
public void RenderFullscreenQuad(RShader shader) { RViewport viewport = REngine.Instance._viewport; quadVerts[0].Position = new Vector2(-1, -1); quadVerts[0].TexCoord = new Vector2(0, 0); quadVerts[1].Position = new Vector2(1, -1); quadVerts[1].TexCoord = new Vector2(1, 0); quadVerts[2].Position = new Vector2(1, 1); quadVerts[2].TexCoord = new Vector2(1, 1); quadVerts[3].Position = new Vector2(-1, 1); quadVerts[3].TexCoord = new Vector2(0, 1); vertexQuad2D.SetData <RVertexData2D>(quadVerts); shader.Bind(); GL.Disable(EnableCap.DepthTest); GL.Disable(EnableCap.CullFace); vertexQuad2D.Bind(); vertexQuad2D.BindVertexArray(); indexQuad2D.Bind(); vertexQuad2D.VertexDeclaration.Apply(shader, IntPtr.Zero); GL.DrawElements(PrimitiveType.Triangles, indexQuad2D.IndexCount, DrawElementsType.UnsignedShort, IntPtr.Zero); REngine.CheckGLError(); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.DstAlpha); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.CullFace); indexQuad2D.Unbind(); vertexQuad2D.UnbindVertexArray(); vertexQuad2D.Unbind(); shader.Unbind(); }
public void RenderText(RFont font, Vector2 penPoint, string text, RColor color) { blendState.PlatformApplyState(); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.Disable(EnableCap.CullFace); defaultShader.Bind(); defaultShader.SetSamplerValue(RTextureLayer.DIFFUSE, font.Texture); defaultShader.SetUniformValue("projection", camera2d.Projection); defaultShader.SetUniformValue("view", camera2d.View); defaultShader.SetUniformValue("diffuse_color", color.ToVector4()); defaultShader.SetUniformValue("model", Matrix.Identity); font.Render(ref defaultShader, ref vertexQuad2D, ref indexQuad2D, text, penPoint, color, Matrix.Identity); REngine.CheckGLError(); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.DstAlpha); GL.Disable(EnableCap.Blend); defaultShader.Unbind(); /*text = text.Replace("\r\n", "\n"); * char lastChar = '\0'; * Vector2 originalPoint = penPoint; * foreach(char c in text) * { * if(c == ' ') * { * penPoint.X += font.Kerning(lastChar, c).X+font.SpaceWidth; * lastChar = ' '; * continue; * } * if(c == '\t') * { * penPoint.X += (font.Kerning(lastChar, c).X+(font.SpaceWidth * 2)); * continue; * } * if(c == '\r' || c=='\n') * { * penPoint.Y += font.LineHeight + (font.font.Height>>6); * penPoint.X = originalPoint.X+font.Kerning(lastChar, c).X; * continue; * } * penPoint.X += font.Kerning(lastChar, c).X; * RTextureGlyph glyph = font.GetGlyph(c); * int x0 = (int)(penPoint.X + (glyph.bitmapLeft)); * int y0 = (int)(penPoint.Y - (glyph.bitmapTop)); * //penPoint.X += glyph.Offset.X; * * RenderTexture(glyph, new Rectangle(x0, y0, (int)glyph.Bounds.Width, (int)glyph.Bounds.Height), color, Matrix.Identity, true); * penPoint.X += glyph.advance.X; * lastChar = c; * } * //font.RenderText(defaultShader, text, penPoint.X, penPoint.Y, size, size); */ }
public void End() { REngine.Instance.SetCamera(oldCamera); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); //GL.FrontFace(FrontFaceDirection.Ccw); GL.CullFace(CullFaceMode.Back); GL.DepthFunc(DepthFunction.Less); GL.Disable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.Zero); REngine.CheckGLError(); GL.DepthMask(true); REngine.CheckGLError(); }
public void Begin() { oldCamera = REngine.Instance.GetCamera(); REngine.Instance.SetCamera(camera2d); GL.Disable(EnableCap.DepthTest); //GL.Viewport(0, (int)viewport.Width, 0, (int)viewport.Height); //blendState.ColorWriteChannels = RColorWriteChannels.All; GL.Enable(EnableCap.Blend); blendState.PlatformApplyState(); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); //GL.FrontFace(FrontFaceDirection.Ccw); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); REngine.CheckGLError(); GL.Disable(EnableCap.CullFace); camera2d.Update(); }
internal void Init() { REngine.CheckGLError(); defaultShader = new RShader(); defaultShader.Load(RShaderResources.Basic2dEffectVert, RShaderResources.Basic2dEffectFrag, null); Fonts.Add(RFont.Default); quad = new RMeshBuilder(); quad.CreateQuad(new Vector2(0, 0), new Vector2(1, 1), true); quadVerts = new RVertexData2D[4]; quadVerts[0] = new RVertexData2D(new Vector2(-1, -1), new Vector2(0, 0)); quadVerts[1] = new RVertexData2D(new Vector2(1, -1), new Vector2(1, 0)); quadVerts[2] = new RVertexData2D(new Vector2(1, 1), new Vector2(1, 1)); quadVerts[3] = new RVertexData2D(new Vector2(-1, 1), new Vector2(0, 1)); vertexQuad2D = new RVertexBuffer(quadVerts[0].Declaration, 4, RBufferUsage.WriteOnly); vertexQuad2D.SetData <RVertexData2D>(quadVerts); indexQuad2D = new RIndexBuffer(typeof(short), 6, RBufferUsage.WriteOnly); indexQuad2D.SetData <short>(new short[6] { 0, 1, 2, 0, 2, 3 }, 0, 6); initialized = true; }
public static void LoadFromBitmap(ref Bitmap bitmap, out uint texturehandle, out TextureTarget dimension, out RPixelFormat format, out PixelType type) { dimension = (TextureTarget)0; texturehandle = TextureLoaderParameters.OpenGLDefaultTexture; ErrorCode GLError = ErrorCode.NoError; Bitmap CurrentBitmap = null; try // Exceptions will be thrown if any Problem occurs while working on the file. { CurrentBitmap = bitmap; if (TextureLoaderParameters.FlipImages) { CurrentBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); } if (CurrentBitmap.Height > 1) { dimension = TextureTarget.Texture2D; } else { dimension = TextureTarget.Texture1D; } GL.GenTextures(1, out texturehandle); GL.BindTexture(dimension, texturehandle); #region Load Texture OpenTK.Graphics.OpenGL.PixelInternalFormat pif; OpenTK.Graphics.OpenGL.PixelFormat pf; OpenTK.Graphics.OpenGL.PixelType pt; switch (CurrentBitmap.PixelFormat) { case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: // misses glColorTable setup pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.R8; pf = OpenTK.Graphics.OpenGL.PixelFormat.Red; pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte; break; case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555: case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: // does not work pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb5A1; pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgr; pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedShort5551Ext; break; /* case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: * pif = Reactor.Graphics.OpenGL.PixelInternalFormat.R5G6B5IccSgix; * pf = Reactor.Graphics.OpenGL.PixelFormat.R5G6B5IccSgix; * pt = Reactor.Graphics.OpenGL.PixelType.UnsignedByte; * break; */ case System.Drawing.Imaging.PixelFormat.Format24bppRgb: // works pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb; pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgr; pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte; break; case System.Drawing.Imaging.PixelFormat.Format32bppRgb: // has alpha too? wtf? case System.Drawing.Imaging.PixelFormat.Canonical: case System.Drawing.Imaging.PixelFormat.Format32bppArgb: // works pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba; pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgra; pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte; break; default: throw new ArgumentException("ERROR: Unsupported Pixel Format " + CurrentBitmap.PixelFormat); } format = (RPixelFormat)pf; type = pt; BitmapData Data = CurrentBitmap.LockBits(new System.Drawing.Rectangle(0, 0, CurrentBitmap.Width, CurrentBitmap.Height), ImageLockMode.ReadOnly, CurrentBitmap.PixelFormat); if (Data.Height > 1) { // image is 2D if (TextureLoaderParameters.BuildMipmapsForUncompressed) { throw new Exception("Cannot build mipmaps, Glu is deprecated."); // Glu.Build2DMipmap(dimension, (int)pif, Data.Width, Data.Height, pf, pt, Data.Scan0); } else { GL.TexImage2D(dimension, 0, pif, Data.Width, Data.Height, TextureLoaderParameters.Border, pf, pt, Data.Scan0); } } else { // image is 1D if (TextureLoaderParameters.BuildMipmapsForUncompressed) { throw new Exception("Cannot build mipmaps, Glu is deprecated."); // Glu.Build1DMipmap(dimension, (int)pif, Data.Width, pf, pt, Data.Scan0); } else { GL.TexImage1D(dimension, 0, pif, Data.Width, TextureLoaderParameters.Border, pf, pt, Data.Scan0); } } //GL.Finish( ); REngine.CheckGLError(); GLError = GL.GetError( ); if (GLError != ErrorCode.NoError) { throw new ArgumentException("Error building TexImage. GL Error: " + GLError); } CurrentBitmap.UnlockBits(Data); #endregion Load Texture Setup(dimension); return; // success } catch (Exception e) { dimension = (TextureTarget)0; texturehandle = TextureLoaderParameters.OpenGLDefaultTexture; throw new ArgumentException("Texture Loading Error: Failed to read data.\n" + e); // return; // failure } finally { CurrentBitmap = null; } }