public static int loadCubeMap() { int texID = GL.GenTexture(); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.TextureCubeMap, texID); var cubeMapTextures = loadCubeMapTextures(); foreach (var dictValues in cubeMapTextures) { var target = TextureTarget.Texture2D; switch (dictValues.Key) { case EnumFacing.NORTH: target = TextureTarget.TextureCubeMapNegativeZ; break; case EnumFacing.SOUTH: target = TextureTarget.TextureCubeMapPositiveZ; break; case EnumFacing.EAST: target = TextureTarget.TextureCubeMapPositiveX; break; case EnumFacing.WEST: target = TextureTarget.TextureCubeMapNegativeX; break; case EnumFacing.UP: target = TextureTarget.TextureCubeMapPositiveY; break; case EnumFacing.DOWN: target = TextureTarget.TextureCubeMapNegativeY; break; } var bmp = (Bitmap)dictValues.Value.Clone(); var size = bmp.Size; BitmapData data = bmp.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(target, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); } GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); return(texID); }
static int LoadTexture(string filename) { if (String.IsNullOrEmpty(filename)) { throw new ArgumentException(filename); } int id = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, id); // We will not upload mipmaps, so disable mipmapping (otherwise the texture will not appear). // We can use GL.GenerateMipmaps() or GL.Ext.GenerateMipmaps() to create // mipmaps automatically. In that case, use TextureMinFilter.LinearMipmapLinear to enable them. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(filename); BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0); bmp.UnlockBits(bmp_data); return(id); }
/// <summary> /// Constructs a new instance. /// </summary> /// <param name="width">The width of the backing store in pixels.</param> /// <param name="height">The height of the backing store in pixels.</param> public TextRenderer(int width, int height) { if (width <= 0) { throw new ArgumentOutOfRangeException("width"); } if (height <= 0) { throw new ArgumentOutOfRangeException("height "); } if (GraphicsContext.CurrentContext == null) { throw new InvalidOperationException("No GraphicsContext is current on the calling thread."); } bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); gfx = Graphics.FromImage(bmp); gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; texture = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); }
public static int loadTexture(Bitmap textureMap, bool smooth) { GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); int texID = GL.GenTexture(); textures.Add(texID); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = textureMap.LockBits(new Rectangle(0, 0, textureMap.Width, textureMap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); textureMap.UnlockBits(data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)(smooth ? TextureMinFilter.Linear : TextureMinFilter.Nearest)); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)(smooth ? TextureMagFilter.Linear : TextureMagFilter.Nearest)); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); return(texID); }
protected override void _WriteDataToTexture(COGLTexture texture, IntPtr data) { Debug.Assert(texture.Name > 0); _ClearTexture(texture); GL.BindTexture(TextureTarget.Texture2D, texture.Name); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texture.DataSize.Width, texture.DataSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.BindTexture(TextureTarget.Texture2D, 0); }
public CTextureRef CopyScreen() { //TODO: Check if _W,_H needs to be used or not Size size = new Size(GetScreenWidth(), GetScreenHeight()); COGLTexture texture = _CreateTexture(size); GL.BindTexture(TextureTarget.Texture2D, texture.Name); GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, size.Width, size.Height); //TODO: Use _X,_Y and _W,_H? GL.BindTexture(TextureTarget.Texture2D, 0); return(_GetTextureReference(size, texture)); }
protected override void _DrawTexture(COGLTexture texture, SDrawCoords dc, SColorF color, bool isReflection = false) { // Align textures to full pixels to reduce artefacts dc.Wx1 = (float)Math.Round(dc.Wx1); dc.Wy1 = (float)Math.Round(dc.Wy1); dc.Wx2 = (float)Math.Round(dc.Wx2); dc.Wy2 = (float)Math.Round(dc.Wy2); GL.BindTexture(TextureTarget.Texture2D, texture.Name); GL.Enable(EnableCap.Blend); GL.MatrixMode(MatrixMode.Texture); GL.PushMatrix(); if (Math.Abs(dc.Rotation) > float.Epsilon) { GL.Translate(0.5f, 0.5f, 0); GL.Rotate(-dc.Rotation, 0f, 0f, 1f); GL.Translate(-0.5f, -0.5f, 0); } GL.Begin(PrimitiveType.Quads); GL.Color4(color.R, color.G, color.B, color.A * CGraphics.GlobalAlpha); GL.TexCoord2(dc.Tx1, dc.Ty1); GL.Vertex3(dc.Wx1, dc.Wy1, dc.Wz); if (isReflection) { GL.Color4(color.R, color.G, color.B, 0); } GL.TexCoord2(dc.Tx1, dc.Ty2); GL.Vertex3(dc.Wx1, dc.Wy2, dc.Wz); GL.TexCoord2(dc.Tx2, dc.Ty2); GL.Vertex3(dc.Wx2, dc.Wy2, dc.Wz); if (isReflection) { GL.Color4(color.R, color.G, color.B, color.A * CGraphics.GlobalAlpha); } GL.TexCoord2(dc.Tx2, dc.Ty1); GL.Vertex3(dc.Wx2, dc.Wy1, dc.Wz); GL.End(); GL.PopMatrix(); GL.Disable(EnableCap.Blend); GL.BindTexture(TextureTarget.Texture2D, 0); }
public static TextureHandle Create(Size <Pixels> size, IOSurface surface) { GL.ActiveTexture(TextureUnit.Texture0); var texture = new TextureHandle(GL.GenTexture()); GL.BindTexture(TextureTarget.TextureRectangle, texture); GL.TexParameter(TextureTarget.TextureRectangle, TextureParameterName.TextureMagFilter, (int)(TextureMagFilter.Nearest)); GL.TexParameter(TextureTarget.TextureRectangle, TextureParameterName.TextureMinFilter, (int)(TextureMinFilter.Linear)); GL.TexParameter(TextureTarget.TextureRectangle, TextureParameterName.TextureWrapS, (int)(TextureWrapMode.ClampToEdge)); GL.TexParameter(TextureTarget.TextureRectangle, TextureParameterName.TextureWrapT, (int)(TextureWrapMode.ClampToEdge)); Resize(texture, size, surface); return(texture); }
public static void Resize(TextureHandle texture, Size <Pixels> size, IOSurface surface) { GL.BindTexture(TextureTarget.TextureRectangle, texture); CGLTexImageIOSurface2D( CGLContext.CurrentContext.Handle, (int)TextureTarget.TextureRectangle, (int)PixelInternalFormat.Rgba, (int)size.Width, (int)size.Height, (int)PixelFormat.Bgra, (int)PixelType.UnsignedInt8888Reversed, surface.Handle, 0); GL.BindTexture(TextureTarget.TextureRectangle, 0); }
public void CopyScreen(ref CTextureRef textureRef) { COGLTexture texture; //Check for actual texture sizes as it may be downsized compared to OrigSize if (!_GetTexture(textureRef, out texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight()) { RemoveTexture(ref textureRef); textureRef = CopyScreen(); } else { GL.BindTexture(TextureTarget.Texture2D, texture.Name); GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, GetScreenWidth(), GetScreenHeight()); GL.BindTexture(TextureTarget.Texture2D, 0); } }
// Uploads the dirty regions of the backing store to the OpenGL texture. void UploadBitmap() { if (dirty_region != RectangleF.Empty) { System.Drawing.Imaging.BitmapData data = bmp.LockBits(dirty_region, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexSubImage2D(TextureTarget.Texture2D, 0, dirty_region.X, dirty_region.Y, dirty_region.Width, dirty_region.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); dirty_region = Rectangle.Empty; } }
public COGLTexture(int name, Size dataSize, int texWidth = 0, int texHeight = 0) : base(dataSize, new Size(texWidth, texHeight)) { Name = name; if (name == 0) { return; } GL.BindTexture(TextureTarget.Texture2D, Name); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texWidth, texHeight, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)OpenTK.Graphics.TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)OpenTK.Graphics.TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.BindTexture(TextureTarget.Texture2D, 0); }
public void BindTexture(GLTextureTarget target, GLTextureHandle texture) { GL.BindTexture((TextureTarget)target, (int)texture); }