/// <summary> /// Reloads the OpenGl Texture from the Surface. /// </summary> /// <param name="surface">The surface to load from.</param> /// <param name="isFlipped">States if the surface should be flipped when moved into the OpenGl Texture.</param> /// <param name="minifyingFilter">"The openGl filter used for minifying"</param> /// <param name="magnificationFilter">"The openGl filter used for magnification"</param> /// <param name="wrapS">The wrap parameter for texture coordinate S</param> /// <param name="wrapT">The wrap parameter for texture coordinate T</param> public void Refresh(Surface surface, bool isFlipped, MinifyingOption minifyingFilter, MagnificationOption magnificationFilter, WrapOption wrapS, WrapOption wrapT) { if (surface == null) { throw new ArgumentNullException("surface"); } this.surface = surface; this.Delete(); using (Surface textureSurface = TransformSurface(isFlipped)) { int[] textures = new int[1]; glGenTextures(1, textures); this.textureId = textures[0]; this.textureWidth = textureSurface.Width; this.textureHeight = textureSurface.Height; this.isFlipped = isFlipped; this.minifyingFilter = minifyingFilter; this.magnificationFilter = magnificationFilter; this.wrapS = wrapS; this.wrapT = wrapT; this.widthRatio = (float)surface.Width / textureWidth; this.heightRatio = (float)surface.Height / textureHeight; glBindTexture(GL_TEXTURE_2D, textureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)minifyingFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)magnificationFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)wrapS); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)wrapT); if (minifyingFilter == MinifyingOption.Linear || minifyingFilter == MinifyingOption.Nearest) { glTexImage2D(GL_TEXTURE_2D, 0, textureSurface.BytesPerPixel, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureSurface.Pixels); } else { NativeMethods.gluBuild2DMipmaps(GL_TEXTURE_2D, textureSurface.BytesPerPixel, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE, textureSurface.Pixels); } needRefresh = false; needSetOptions = false; } }
/// <summary> /// Creates a new Instance of SurfaceGl. /// </summary> /// <param name="surface">The surface to be copied into a OpenGl Texture.</param> /// <param name="isFlipped">States if the surface should be flipped when copied into a OpenGl Texture.</param> public SurfaceGl(Surface surface, bool isFlipped) { if (surface == null) { throw new ArgumentNullException("surface"); } this.surface = surface; this.isFlipped = isFlipped; this.textureId = -1; this.textureWidth = -1; this.textureHeight = -1; this.widthRatio = -1; this.heightRatio = -1; this.minifyingFilter = MinifyingOption.Linear; this.magnificationFilter = MagnificationOption.Linear; this.wrapS = WrapOption.Repeat; this.wrapT = WrapOption.Repeat; }