internal static Texture2D Create(int width, int height, int bytesPerPixel) { byte[] bytearray = new byte[width * height * bytesPerPixel]; // is there a faster way to create white? for (int i = 0; i < bytearray.Length; i++) { bytearray[i] = 255; } GCHandle pinnedArray = GCHandle.Alloc(bytearray, GCHandleType.Pinned); IntPtr pixels = pinnedArray.AddrOfPinnedObject(); var rv = new Texture2D(); { GlBindings.GenTextures(1, out rv.m_TextureId); GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels); } pinnedArray.Free(); return(rv); }
internal static Texture2D Create(IntPtr pixels, int width, int height, int bytesPerPixel) { var rv = new Texture2D(); GlBindings.GenTextures(1, out rv.m_TextureId); GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels); return(rv); }
internal static Texture2D CreateFromFile(string filename, bool generateMipmaps = false) { using (TracedStopwatch.Start("Loading Texture: " + filename)) { IntPtr intPtr = SDL2.SDL_image.IMG_Load(filename); var surface = Marshal.PtrToStructure <SDL.SDL_Surface>(intPtr); var pixelFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format); var mode = OpenGL.PixelFormat.Rgba; if (pixelFormat.BytesPerPixel == 3) { mode = OpenGL.PixelFormat.Rgb; } if (pixelFormat.BytesPerPixel == 1) { mode = OpenGL.PixelFormat.Red; } var rv = new Texture2D(); // TODO Pass in wraping styles GlBindings.GenTextures(1, out rv.m_TextureId); GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR); var internalFormat = OpenGL.PixelInternalFormat.Rgba; if (mode == PixelFormat.Luminance) { internalFormat = PixelInternalFormat.Red; } GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, internalFormat, surface.w, surface.h, 0, mode, PixelType.UnsignedByte, surface.pixels); if (generateMipmaps) { GlBindings.GenerateMipmap(TextureType.GL_TEXTURE_2D); } SDL2.SDL.SDL_free(intPtr); return(rv); } }
// TODO Will probably need to handle resizing the framebuffer // TODO Make internal and store public static RenderTarget Create(int width, int height) { var rv = new RenderTarget(); // Create the frame buffer GlBindings.GenFramebuffers(1, out rv.m_RanderTargetId); GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, rv.m_RanderTargetId); // Attach the texture component GlBindings.GenTextures(1, out rv.m_TextureColorBuffer); GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureColorBuffer); GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, OpenGL.PixelInternalFormat.Rgb, width, height, 0, OpenGL.PixelFormat.Rgb, OpenGL.PixelType.UnsignedByte, IntPtr.Zero); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.glFramebufferTexture2D(FrameBuffer.GL_FRAMEBUFFER, Attachment.GL_COLOR_ATTACHMENT0, TextureType.GL_TEXTURE_2D, rv.m_TextureColorBuffer, 0); // TODO Depth buffer! GlBindings.glGenRenderbuffers(1, out rv.m_RenderBufferObjectId); GlBindings.glBindRenderbuffer(RenderBuffer.GL_RENDERBUFFER, rv.m_RenderBufferObjectId); GlBindings.glRenderbufferStorage(RenderBuffer.GL_RENDERBUFFER, InternalFormat.GL_DEPTH24_STENCIL8, width, height); GlBindings.glFramebufferRenderbuffer(RenderBuffer.GL_FRAMEBUFFER, Attachment.GL_DEPTH_STENCIL_ATTACHMENT, RenderBuffer.GL_RENDERBUFFER, rv.RenderBufferObjectId); if (GlBindings.glCheckFramebufferStatus(FrameBuffer.GL_FRAMEBUFFER) != (int)ReturnValue.GL_FRAMEBUFFER_COMPLETE) { throw new Exception("Error with framebuffer"); } // Store previous GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, 0); return(rv); }
public void SetData(IntPtr pixels, int width, int height, int bytesPerPixel) { // TODO What if this texture is not bound? GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels); }
internal static TextureCubeMap CreateFromFile(string front, string back, string bottom, string top, string left, string right, bool generateMipmaps = false) { using (TracedStopwatch.Start("Loading Cubemap: " + front)) { var rv = new TextureCubeMap(); // Generate the cube map GlBindings.GenTextures(1, out rv.m_TextureId); GlBindings.BindTexture(TextureType.GL_TEXTURE_CUBE_MAP, rv.m_TextureId); var items = new List <string> { right, left, top, bottom, front, back }; var i = 0; foreach (var item in items) { var fi = new FileInfo(item); if (!fi.Exists) { throw new Exception("File not found"); } // Load each image from disk IntPtr intPtr = SDL2.SDL_image.IMG_Load(item); var surface = Marshal.PtrToStructure <SDL.SDL_Surface>(intPtr); var pixelFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format); var mode = OpenGL.PixelFormat.Rgba; if (pixelFormat.BytesPerPixel == 3) { mode = OpenGL.PixelFormat.Rgb; } if (pixelFormat.BytesPerPixel == 1) { mode = OpenGL.PixelFormat.Red; } var internalFormat = OpenGL.PixelInternalFormat.Rgba; if (mode == PixelFormat.Luminance) { internalFormat = PixelInternalFormat.Red; } GlBindings.TexImage2D(TextureType.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, surface.w, surface.h, 0, mode, PixelType.UnsignedByte, surface.pixels); // Free the managed resource SDL2.SDL.SDL_free(intPtr); i++; } // Texture Sampling and Filtering GlBindings.TexParameteri(TextureType.GL_TEXTURE_CUBE_MAP, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_CLAMP_TO_EDGE); GlBindings.TexParameteri(TextureType.GL_TEXTURE_CUBE_MAP, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_CLAMP_TO_EDGE); GlBindings.TexParameteri(TextureType.GL_TEXTURE_CUBE_MAP, TextureAttribute.GL_TEXTURE_WRAP_R, TextureAttributeValue.GL_CLAMP_TO_EDGE); GlBindings.TexParameteri(TextureType.GL_TEXTURE_CUBE_MAP, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR); GlBindings.TexParameteri(TextureType.GL_TEXTURE_CUBE_MAP, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR); return(rv); } }