@@ -37,10 +37,21 @@ public sealed class GlResourceManager : IDisposable
3737 /// </summary>
3838 private bool _disposed ;
3939
40+ /// <summary>
41+ /// Gets or sets a value indicating whether [use matrices].
42+ /// </summary>
43+ /// <value>
44+ /// <c>true</c> if [use matrices]; otherwise, <c>false</c>.
45+ /// </value>
4046 public bool UseMatrices { get ; set ; } = true ;
4147
4248 // --- Textures ---
4349
50+ /// <summary>
51+ /// The fallback texture identifier
52+ /// </summary>
53+ private int _fallbackTextureId = - 1 ;
54+
4455 /// <summary>
4556 /// Gets the texture.
4657 /// Be careful, this can overwrite existing textures.
@@ -62,6 +73,44 @@ public int GetTexture(string filePath)
6273 return texId ;
6374 }
6475
76+ /// <summary>
77+ /// Gets the fallback texture.
78+ /// </summary>
79+ /// <returns>Id of Texture, fallback, checkerboard</returns>
80+ public int GetFallbackTexture ( )
81+ {
82+ if ( _fallbackTextureId >= 0 ) return _fallbackTextureId ;
83+
84+ var pixels = new byte [ ] { 0 , 0 , 0 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 0 , 0 , 0 , 255 } ;
85+ _fallbackTextureId = GL . GenTexture ( ) ;
86+ GL . BindTexture ( TextureTarget . Texture2D , _fallbackTextureId ) ;
87+ GL . TexImage2D ( TextureTarget . Texture2D , 0 , PixelInternalFormat . Rgba , 2 , 2 , 0 ,
88+ PixelFormat . Rgba , PixelType . UnsignedByte , pixels ) ;
89+
90+ // 1. CLAMP TO EDGE: Stops the edges from wrapping around and bleeding colors at the borders
91+ GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapS , ( int ) TextureWrapMode . ClampToEdge ) ;
92+ GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapT , ( int ) TextureWrapMode . ClampToEdge ) ;
93+
94+ // 2. MIPMAPS: Keep it sharp up close, but blend it in the distance to stop the diagonal zippering
95+ GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMinFilter , ( int ) TextureMinFilter . NearestMipmapLinear ) ;
96+ GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMagFilter , ( int ) TextureMagFilter . Nearest ) ;
97+
98+ // 3. ANISOTROPIC FILTERING: The ultimate fix for viewing textures at sharp "roof" angles.
99+ // (Using hex codes safely bypasses any OpenTK version differences for this extension)
100+ GL . GetFloat ( ( GetPName ) 0x84FF , out float maxAniso ) ; // GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
101+ if ( maxAniso > 0.0f )
102+ {
103+ GL . TexParameter ( TextureTarget . Texture2D , ( TextureParameterName ) 0x84FE , maxAniso ) ; // GL_TEXTURE_MAX_ANISOTROPY_EXT
104+ }
105+
106+ // Generate the smaller mipmap versions so the MinFilter works
107+ GL . GenerateMipmap ( GenerateMipmapTarget . Texture2D ) ;
108+
109+ GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
110+
111+ return _fallbackTextureId ;
112+ }
113+
65114 /// <summary>
66115 /// Creates the master textures.
67116 /// </summary>
@@ -270,7 +319,13 @@ public void Dispose()
270319 _textureCache . Clear ( ) ;
271320 _programCache . Clear ( ) ;
272321
322+ if ( _fallbackTextureId >= 0 )
323+ {
324+ GL . DeleteTexture ( _fallbackTextureId ) ;
325+ _fallbackTextureId = - 1 ;
326+ }
327+
273328 GC . SuppressFinalize ( this ) ;
274329 }
275330 }
276- }
331+ }
0 commit comments