Bladeren bron

Added function SetBlendMode()

Useful to enable additive blend mode for particles
raysan5 10 jaren geleden
bovenliggende
commit
b8b0247043
3 gewijzigde bestanden met toevoegingen van 40 en 8 verwijderingen
  1. 5 0
      src/raylib.h
  2. 30 8
      src/rlgl.c
  3. 5 0
      src/rlgl.h

+ 5 - 0
src/raylib.h

@@ -353,6 +353,9 @@ typedef enum {
     COMPRESSED_ASTC_8x8_RGBA        // 2 bpp
 } TextureFormat;
 
+// Color blending modes (pre-defined)
+typedef enum { BLEND_ALPHA = 0, BLEND_ADDITIVE, BLEND_MULTIPLIED } BlendMode;
+
 // Gestures type
 // NOTE: It could be used as flags to enable only some gestures
 typedef enum {
@@ -448,6 +451,8 @@ void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D textu
 void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture);  // Specular map texture shader assignment
 void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment
 
+void SetBlendMode(int mode);                                        // Set blending mode (alpha, additive, multiplied)
+
 //------------------------------------------------------------------------------------
 // Input Handling Functions (Module: core)
 //------------------------------------------------------------------------------------

+ 30 - 8
src/rlgl.c

@@ -244,8 +244,8 @@ static bool enabledPostpro = false;
 #endif
 
 // Compressed textures support flags
-static bool texCompDXTSupported = false;     // DDS texture compression support
-static bool npotSupported = false;  // NPOT textures full support
+static bool texCompDXTSupported = false;   // DDS texture compression support
+static bool npotSupported = false;         // NPOT textures full support
 
 #if defined(GRAPHICS_API_OPENGL_ES2)
 // NOTE: VAO functionality is exposed through extensions (OES)
@@ -255,13 +255,15 @@ static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
 //static PFNGLISVERTEXARRAYOESPROC glIsVertexArray;        // NOTE: Fails in WebGL, omitted
 #endif
 
+// Save screen size data (render size), required for postpro quad
+static int screenWidth, screenHeight;
+
+static int blendMode = 0;
+
 // White texture useful for plain color polys (required by shader)
 // NOTE: It's required in shapes and models modules!
 unsigned int whiteTexture;
 
-// Save screen size data (render size), required for postpro quad
-static int screenWidth, screenHeight;
-
 //----------------------------------------------------------------------------------
 // Module specific Functions Declaration
 //----------------------------------------------------------------------------------
@@ -2043,6 +2045,8 @@ void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
 #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
     int width, height;
     
+    glBindTexture(GL_TEXTURE_2D, textureId);
+    
     glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
     glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
     //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
@@ -2063,15 +2067,13 @@ void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
         case UNCOMPRESSED_R8G8B8A8: pixels = (unsigned char *)malloc(size*4); glFormat = GL_RGBA; glType = GL_UNSIGNED_BYTE; break;                // 32 bpp
         default: TraceLog(WARNING, "Texture format not suported"); break;
     }
-
-    glBindTexture(GL_TEXTURE_2D, textureId);
     
     // NOTE: Each row written to or read from by OpenGL pixel operations like glGetTexImage are aligned to a 4 byte boundary by default, which may add some padding.
     // Use glPixelStorei to modify padding with the GL_[UN]PACK_ALIGNMENT setting. 
     // GL_PACK_ALIGNMENT affects operations that read from OpenGL memory (glReadPixels, glGetTexImage, etc.) 
     // GL_UNPACK_ALIGNMENT affects operations that write to OpenGL memory (glTexImage, etc.)
     glPixelStorei(GL_PACK_ALIGNMENT, 1);
-    
+
     glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels);
     
     glBindTexture(GL_TEXTURE_2D, 0);
@@ -2513,6 +2515,26 @@ void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textur
 */
 }
 
+// Set blending mode (alpha, additive, multiplied)
+// NOTE: Only 3 blending modes predefined
+void SetBlendMode(int mode)
+{
+    if ((blendMode != mode) && (mode < 3))
+    {
+        rlglDraw();
+        
+        switch (mode)
+        {
+            case BLEND_ALPHA: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break;
+            case BLEND_ADDITIVE: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; // Alternative: glBlendFunc(GL_ONE, GL_ONE);
+            case BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); break;
+            default: break;
+        }
+        
+        blendMode = mode;
+    }
+}
+
 #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
 void PrintProjectionMatrix(void)
 {

+ 5 - 0
src/rlgl.h

@@ -182,6 +182,9 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
         Texture2D texture;
         Shader shader;
     } Model;
+	
+    // Color blending modes (pre-defined)
+    typedef enum { BLEND_ALPHA = 0, BLEND_ADDITIVE, BLEND_MULTIPLIED } BlendMode;
 #endif
 
 #ifdef __cplusplus
@@ -282,6 +285,8 @@ void SetShaderMapDiffuse(Shader *shader, Texture2D texture);
 void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture);    // Normal map texture shader assignment
 void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture);  // Specular map texture shader assignment
 void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment
+
+void SetBlendMode(int mode);                                        // Set blending mode (alpha, additive, multiplied)
 #endif
 
 #ifdef __cplusplus