Ver Fonte

Corrected bug on GenTextureMipmaps()

texture.mipmaps value needs to be updated, so, texture must be passed by
reference instead of by value
Ray há 8 anos atrás
pai
commit
f1bcfc1352
4 ficheiros alterados com 24 adições e 18 exclusões
  1. 1 1
      src/raylib.h
  2. 18 12
      src/rlgl.c
  3. 1 1
      src/rlgl.h
  4. 4 4
      src/textures.c

+ 1 - 1
src/raylib.h

@@ -802,7 +802,7 @@ RLAPI void ImageColorInvert(Image *image);
 RLAPI void ImageColorGrayscale(Image *image);                                                            // Modify image color: grayscale
 RLAPI void ImageColorContrast(Image *image, float contrast);                                             // Modify image color: contrast (-100 to 100)
 RLAPI void ImageColorBrightness(Image *image, int brightness);                                           // Modify image color: brightness (-255 to 255)
-RLAPI void GenTextureMipmaps(Texture2D texture);                                                         // Generate GPU mipmaps for a texture
+RLAPI void GenTextureMipmaps(Texture2D *texture);                                                        // Generate GPU mipmaps for a texture
 RLAPI void SetTextureFilter(Texture2D texture, int filterMode);                                          // Set texture scaling filter mode
 RLAPI void SetTextureWrap(Texture2D texture, int wrapMode);                                              // Set texture wrapping mode
 

+ 18 - 12
src/rlgl.c

@@ -1719,15 +1719,15 @@ void rlglUpdateTexture(unsigned int id, int width, int height, int format, void
 }
 
 // Generate mipmap data for selected texture
-void rlglGenerateMipmaps(Texture2D texture)
+void rlglGenerateMipmaps(Texture2D *texture)
 {
-    glBindTexture(GL_TEXTURE_2D, texture.id);
+    glBindTexture(GL_TEXTURE_2D, texture->id);
     
     // Check if texture is power-of-two (POT)
     bool texIsPOT = false;
    
-    if (((texture.width > 0) && ((texture.width & (texture.width - 1)) == 0)) && 
-        ((texture.height > 0) && ((texture.height & (texture.height - 1)) == 0))) texIsPOT = true;
+    if (((texture->width > 0) && ((texture->width & (texture->width - 1)) == 0)) && 
+        ((texture->height > 0) && ((texture->height & (texture->height - 1)) == 0))) texIsPOT = true;
 
     if ((texIsPOT) || (npotSupported))
     {
@@ -1737,13 +1737,13 @@ void rlglGenerateMipmaps(Texture2D texture)
         
         // NOTE: data size is reallocated to fit mipmaps data
         // NOTE: CPU mipmap generation only supports RGBA 32bit data
-        int mipmapCount = GenerateMipmaps(data, texture.width, texture.height);
+        int mipmapCount = GenerateMipmaps(data, texture->width, texture->height);
 
-        int size = texture.width*texture.height*4;  // RGBA 32bit only
+        int size = texture->width*texture->height*4;  // RGBA 32bit only
         int offset = size;
 
-        int mipWidth = texture.width/2;
-        int mipHeight = texture.height/2;
+        int mipWidth = texture->width/2;
+        int mipHeight = texture->height/2;
 
         // Load the mipmaps
         for (int level = 1; level < mipmapCount; level++)
@@ -1757,23 +1757,29 @@ void rlglGenerateMipmaps(Texture2D texture)
             mipHeight /= 2;
         }
         
-        TraceLog(WARNING, "[TEX ID %i] Mipmaps generated manually on CPU side", texture.id);
+        TraceLog(WARNING, "[TEX ID %i] Mipmaps generated manually on CPU side", texture->id);
         
         // NOTE: Once mipmaps have been generated and data has been uploaded to GPU VRAM, we can discard RAM data
         free(data);
         
+        texture->mipmaps = mipmapCount + 1;
 #endif
 
 #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
         //glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);   // Hint for mipmaps generation algorythm: GL_FASTEST, GL_NICEST, GL_DONT_CARE
         glGenerateMipmap(GL_TEXTURE_2D);    // Generate mipmaps automatically
-        TraceLog(INFO, "[TEX ID %i] Mipmaps generated automatically", texture.id);
+        TraceLog(INFO, "[TEX ID %i] Mipmaps generated automatically", texture->id);
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);   // Activate Trilinear filtering for mipmaps (must be available)
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);   // Activate Trilinear filtering for mipmaps
+        
+        #define MIN(a,b) (((a)<(b))?(a):(b))
+        #define MAX(a,b) (((a)>(b))?(a):(b))
+        
+        texture->mipmaps =  1 + floor(log2(MAX(texture->width, texture->height)));
 #endif
     }
-    else TraceLog(WARNING, "[TEX ID %i] Mipmaps can not be generated", texture.id);
+    else TraceLog(WARNING, "[TEX ID %i] Mipmaps can not be generated", texture->id);
 
     glBindTexture(GL_TEXTURE_2D, 0);
 }

+ 1 - 1
src/rlgl.h

@@ -371,7 +371,7 @@ void rlglLoadExtensions(void *loader);          // Load OpenGL extensions
 unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount);    // Load texture in GPU
 RenderTexture2D rlglLoadRenderTexture(int width, int height);   // Load a texture to be used for rendering (fbo with color and depth attachments)
 void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data);         // Update GPU texture with new data
-void rlglGenerateMipmaps(Texture2D texture);                             // Generate mipmap data for selected texture
+void rlglGenerateMipmaps(Texture2D *texture);                       // Generate mipmap data for selected texture
 
 void rlglLoadMesh(Mesh *mesh, bool dynamic);                        // Upload vertex data into GPU and provided VAO/VBO ids
 void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex);          // Update vertex data on GPU (upload new data to one buffer)

+ 4 - 4
src/textures.c

@@ -1516,14 +1516,14 @@ void ImageColorBrightness(Image *image, int brightness)
 }
 
 // Generate GPU mipmaps for a texture
-void GenTextureMipmaps(Texture2D texture)
+void GenTextureMipmaps(Texture2D *texture)
 {
 #if PLATFORM_WEB
-    int potWidth = GetNextPOT(texture.width);
-    int potHeight = GetNextPOT(texture.height);
+    int potWidth = GetNextPOT(texture->width);
+    int potHeight = GetNextPOT(texture->height);
 
     // Check if texture is POT
-    if ((potWidth != texture.width) || (potHeight != texture.height))
+    if ((potWidth != texture->width) || (potHeight != texture->height))
     {
         TraceLog(WARNING, "Limited NPOT support, no mipmaps available for NPOT textures");
     }