فهرست منبع

Add support for blending to OpenGL/OpenGL|ES renderer

Daniele Bartolini 12 سال پیش
والد
کامیت
a13788f2dd
2فایلهای تغییر یافته به همراه64 افزوده شده و 5 حذف شده
  1. 22 1
      engine/renderers/RenderContext.h
  2. 42 4
      engine/renderers/gl/GLRenderer.cpp

+ 22 - 1
engine/renderers/RenderContext.h

@@ -59,9 +59,30 @@ namespace crown
 #define STATE_PRIMITIVE_TRIANGLES	0x0000000000000000
 #define STATE_PRIMITIVE_POINTS		0x0000000000001000
 #define STATE_PRIMITIVE_LINES		0x0000000000002000
-#define STATE_PRIMITIVE_MASK		0x000000000000f000
+#define STATE_PRIMITIVE_MASK		0x000000000000F000
 #define STATE_PRIMITIVE_SHIFT		12
 
+#define STATE_BLEND_FUNC_ZERO					0x0000000000010000			
+#define STATE_BLEND_FUNC_ONE					0x0000000000020000
+#define STATE_BLEND_FUNC_SRC_COLOR				0x0000000000030000
+#define STATE_BLEND_FUNC_ONE_MINUS_SRC_COLOR	0x0000000000040000
+#define STATE_BLEND_FUNC_DST_COLOR				0x0000000000050000
+#define STATE_BLEND_FUNC_ONE_MINUS_DST_COLOR	0x0000000000060000
+#define STATE_BLEND_FUNC_SRC_ALPHA				0x0000000000070000
+#define STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA	0x0000000000080000
+#define STATE_BLEND_FUNC_DST_ALPHA				0x0000000000090000
+#define STATE_BLEND_FUNC_ONE_MINUS_DST_ALPHA	0x00000000000A0000
+#define STATE_BLEND_FUNC_MASK					0x0000000000FF0000
+#define STATE_BLEND_FUNC_SHIFT					16
+
+#define STATE_BLEND_EQUATION_ADD				0x0000000001000000
+#define STATE_BLEND_EQUATION_SUBTRACT			0x0000000002000000
+#define STATE_BLEND_EQUATION_REVERSE_SUBTRACT	0x0000000003000000
+#define STATE_BLEND_EQUATION_MASK				0x0000000003000000
+#define STATE_BLEND_EQUATION_SHIFT				24
+
+#define STATE_BLEND_FUNC(src, dst) (uint64_t(src << 4) | uint64_t(dst))
+
 #define CLEAR_COLOR					0x1
 #define CLEAR_DEPTH					0x2
 

+ 42 - 4
engine/renderers/gl/GLRenderer.cpp

@@ -93,6 +93,31 @@ const GLTextureFormatInfo TEXTURE_FORMAT_TABLE[PixelFormat::COUNT] =
 	{ GL_RGBA, GL_RGBA}
 };
 
+//-----------------------------------------------------------------------------
+const GLenum BLEND_FUNCTION_TABLE[] =
+{
+	0, // Unused
+	GL_ZERO,
+	GL_ONE,
+	GL_SRC_COLOR,
+	GL_ONE_MINUS_SRC_COLOR,
+	GL_DST_COLOR,
+	GL_ONE_MINUS_DST_COLOR,
+	GL_SRC_ALPHA,
+	GL_ONE_MINUS_SRC_ALPHA,
+	GL_DST_ALPHA,
+	GL_ONE_MINUS_DST_ALPHA
+};
+
+//-----------------------------------------------------------------------------
+const GLenum BLEND_EQUATION_TABLE[] =
+{
+	0, // Unused
+	GL_FUNC_ADD,
+	GL_FUNC_SUBTRACT,
+	GL_FUNC_REVERSE_SUBTRACT
+};
+
 // Keep in sync with ShaderAttrib
 const char* const SHADER_ATTRIB_NAMES[ShaderAttrib::COUNT] =
 {
@@ -194,10 +219,6 @@ public:
 		Log::d("Max Vertex Indices   : %d", m_max_vertex_indices);
 		Log::d("Max Vertex Vertices  : %d", m_max_vertex_vertices);
 
-		GL_CHECK(glDisable(GL_BLEND));
-		GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
-		GL_CHECK(glBlendEquation(GL_FUNC_ADD));
-
 		#if defined(LINUX) || defined(WINDOWS)
 			// Point sprites enabled by default
 			GL_CHECK(glEnable(GL_POINT_SPRITE));
@@ -298,6 +319,23 @@ public:
 				GL_CHECK(glDisable(GL_CULL_FACE));
 			}
 
+			// Blending
+			if (flags & (STATE_BLEND_FUNC_MASK | STATE_BLEND_EQUATION_MASK))
+			{
+				uint32_t function = (flags & STATE_BLEND_FUNC_MASK) >> STATE_BLEND_FUNC_SHIFT;
+				uint32_t equation = (flags & STATE_BLEND_EQUATION_MASK) >> STATE_BLEND_EQUATION_SHIFT;
+				uint32_t src = (function >> 4) & 0xF;
+				uint32_t dst = function & 0xF;
+
+				GL_CHECK(glEnable(GL_BLEND));
+				GL_CHECK(glBlendFunc(BLEND_FUNCTION_TABLE[src], BLEND_FUNCTION_TABLE[dst]));
+				GL_CHECK(glBlendEquation(BLEND_EQUATION_TABLE[equation]));
+			}
+			else
+			{
+				GL_CHECK(glDisable(GL_BLEND));
+			}
+
 			// Bind GPU program
 			if (cur_state.program.id != INVALID_ID)
 			{