public static void SetTexturingMode(int samplerIndex, TexturingModes mode) { var previousMode = samplerTexturingModes[samplerIndex]; if (previousMode == mode) { return; } if (samplerTextureIds[samplerIndex] != 0) { UnbindTexture(samplerTextureIds[samplerIndex]); } // Only matters for the fixed pipeline if (samplerIndex < maxFpTextureUnits) { ActiveTextureUnit = samplerIndex; if (previousMode != TexturingModes.None) { SetCapability((EnableCap)ToTextureTarget(previousMode), false); } if (mode != TexturingModes.None) { SetCapability((EnableCap)ToTextureTarget(mode), true); } } samplerTexturingModes[samplerIndex] = mode; }
public static TextureTarget ToTextureTarget(TexturingModes mode) { switch (mode) { case TexturingModes.Texturing2d: return(TextureTarget.Texture2D); case TexturingModes.Texturing3d: return(TextureTarget.Texture3D); default: throw new InvalidOperationException("Not texture target matches the texturing mode " + mode); } }
public static void Initialize(ResourceManager resourceManager, int width, int height) { retrieveRendererInfo(); setupDebugOutput(); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); SetCapability(EnableCap.Lighting, false); if (UseSrgb && HasCapabilities(3, 0, "GL_ARB_framebuffer_object")) { int defaultFramebufferColorEncoding; GL.GetFramebufferAttachmentParameter(FramebufferTarget.Framebuffer, FramebufferAttachment.BackLeft, FramebufferParameterName.FramebufferAttachmentColorEncoding, out defaultFramebufferColorEncoding); if (defaultFramebufferColorEncoding == (int)0x8C40) { SetCapability(EnableCap.FramebufferSrgb, true); colorCorrected = true; } else { Trace.WriteLine("Warning: The default framebuffer isn't sRgb"); } } // glActiveTexture requires opengl 1.3 maxFpTextureUnits = HasCapabilities(1, 3) ? GL.GetInteger(GetPName.MaxTextureUnits) : 1; maxTextureImageUnits = GL.GetInteger(GetPName.MaxTextureImageUnits); maxVertexTextureImageUnits = GL.GetInteger(GetPName.MaxVertexTextureImageUnits); maxGeometryTextureImageUnits = HasCapabilities(3, 2, "GL_ARB_geometry_shader4") ? GL.GetInteger(GetPName.MaxGeometryTextureImageUnits) : 0; maxCombinedTextureImageUnits = GL.GetInteger(GetPName.MaxCombinedTextureImageUnits); maxTextureCoords = GL.GetInteger(GetPName.MaxTextureCoords); // glDrawBuffers requires opengl 2.0 maxDrawBuffers = HasCapabilities(2, 0) ? GL.GetInteger(GetPName.MaxDrawBuffers) : 1; Trace.WriteLine($"texture units available: fp:{maxFpTextureUnits} ps:{maxTextureImageUnits} vs:{maxVertexTextureImageUnits} gs:{maxGeometryTextureImageUnits} combined:{maxCombinedTextureImageUnits} coords:{maxTextureCoords}"); samplerTextureIds = new int[maxTextureImageUnits]; samplerTexturingModes = new TexturingModes[maxTextureImageUnits]; CheckError("initializing openGL context"); whitePixel = Texture2d.Create(Color4.White, "whitepixel"); normalPixel = Texture2d.Create(new Color4(0.5f, 0.5f, 1, 1), "normalpixel", 1, 1, new TextureOptions() { Srgb = false, }); textGenerator = new TextGenerator(resourceManager); textFontManager = new TextFontManager(); Viewport = new Rectangle(0, 0, width, height); }
/// <summary> /// Bind the texture to a specific texture unit using its textureId and activate it /// </summary> public static void BindTexture(int textureId, int samplerIndex = 0, TexturingModes mode = TexturingModes.Texturing2d) { if (textureId == 0) { throw new ArgumentException("Use UnbindTexture instead"); } SetTexturingMode(samplerIndex, mode); ActiveTextureUnit = samplerIndex; if (samplerTextureIds[samplerIndex] != textureId) { GL.BindTexture(ToTextureTarget(mode), textureId); samplerTextureIds[samplerIndex] = textureId; //Debug.Print("Bound texture " + textureId + " (" + mode + ") to unit " + samplerIndex); TextureBinds++; } }
/// <summary> /// Bind the texture in the first texture unit by its textureId and activate it /// </summary> public static void BindPrimaryTexture(int textureId, TexturingModes mode = TexturingModes.Texturing2d) { BindTexture(textureId, 0, mode); }