shithub: tinygl

Download patch

ref: 61100b1d5795d82168cd9d08b13dae5f26425dc8
parent: bb91438a3cca03683f0df6320454542e1907527d
author: David <gek@katherine>
date: Tue Feb 23 06:59:29 EST 2021

Essential bug fix update

--- a/SDL_Examples/gears.c
+++ b/SDL_Examples/gears.c
@@ -428,7 +428,7 @@
 		glDisable(GL_DEPTH_TEST);
 		glEnable(GL_BLEND);
 		glDepthMask(GL_FALSE);
-		glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
+		glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR);
 		glBlendEquation(GL_FUNC_ADD);
 	}else{
 		glEnable(GL_DEPTH_TEST);
--- a/SDL_Examples/model.c
+++ b/SDL_Examples/model.c
@@ -27,6 +27,7 @@
 #include <SDL/SDL.h>
 #include <time.h>
 int noSDL = 0;
+int doblend = 0;
 #ifndef M_PI
 #define M_PI 3.14159265
 #endif
@@ -315,6 +316,8 @@
 				testingModelArrays = 1;
 			if(!strcmp(argv[i],"-copy"))
 				testingCopyImage2D = 1;
+			if(!strcmp(argv[i],"-blend"))
+				doblend = 1;
 			larg = argv[i];
 		}
 	}
@@ -567,6 +570,15 @@
 		if (doTextures)
 			glBindTexture(GL_TEXTURE_2D, tex);
 		// glDisable(GL_BLEND);
+		// Testing blending for textured triangles.
+		//glDisable(GL_DEPTH_TEST);
+		if(doblend){
+			glEnable(GL_BLEND);
+			glDepthMask(GL_FALSE);
+			glDisable(GL_DEPTH_TEST);
+		}else  {glDisable(GL_BLEND);}
+		glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR);
+		glBlendEquation(GL_FUNC_ADD);
 		// glDisable(GL_TEXTURE_2D);
 		// printf("\nNew triangle!\n");
 		if (!dlExists) {
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -171,11 +171,11 @@
 #define TGL_BLEND_FUNC(source, dest){													\
 	{																					\
 	GLuint sr, sg, sb, dr, dg, db;														\
-	{	GLuint t = source;																\
-	sr = GET_REDDER(t); sg = GET_GREENER(t); sb = GET_BLUEER(t);						\
-	t = dest;																			\
-	dr = GET_REDDER(t); dg = GET_GREENER(t); db = GET_BLUEER(t);}						\
-		/*printf("\nShould never reach this point!");*/									\
+	{	GLuint temp = source;																\
+	sr = GET_REDDER(temp); sg = GET_GREENER(temp); sb = GET_BLUEER(temp);						\
+	temp = dest;																			\
+	dr = GET_REDDER(temp); dg = GET_GREENER(temp); db = GET_BLUEER(temp);}						\
+		/*printf("\nShould never reach this point!");*/									\
 		switch(sfactor){															\
 			case GL_ONE:																\
 			default:																	\
@@ -209,8 +209,8 @@
 #define TGL_BLEND_FUNC_RGB(rr, gg, bb, dest){											\
 	{																					\
 		GLint sr = rr & COLOR_MASK, sg = gg & COLOR_MASK, sb = bb & COLOR_MASK, dr, dg, db;			\
-		{GLuint t = dest;																\
-		dr = GET_REDDER(t); dg = GET_GREENER(t); db = GET_BLUEER(t);}					\
+		{GLuint temp = dest;																\
+		dr = GET_REDDER(temp); dg = GET_GREENER(temp); db = GET_BLUEER(temp);}					\
 	/*printf("\nShould never reach this point!");*/										\
 		switch(sfactor){																\
 			case GL_ONE:																\
@@ -217,9 +217,9 @@
 			default:																	\
 			break;																		\
 			case GL_ONE_MINUS_SRC_COLOR:												\
-			sr = ~sr & 0xffff;															\
-			sg = ~sg & 0xffff;															\
-			sb = ~sb & 0xffff;															\
+			sr = ~sr & COLOR_MASK;															\
+			sg = ~sg & COLOR_MASK;															\
+			sb = ~sb & COLOR_MASK;															\
 			break;																		\
 			case GL_ZERO:																\
 			sr=0;sg=0;sb=0;break;														\
@@ -230,9 +230,9 @@
 				default:																\
 				break;																	\
 				case GL_ONE_MINUS_DST_COLOR:											\
-				dr = ~dr & 0xffff;														\
-				dg = ~dg & 0xffff;														\
-				db = ~db & 0xffff;														\
+				dr = ~dr & COLOR_MASK;														\
+				dg = ~dg & COLOR_MASK;														\
+				db = ~db & COLOR_MASK;														\
 				break;																	\
 				case GL_ZERO:															\
 				dr=0;dg=0;db=0;break;													\
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -173,7 +173,7 @@
 	{                                                                                                                                                          \
 		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                   \
-		if (ZCMPSIMP(zz, pz[_a], _a, c)) {                                                                                                                     \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
 			/*pp[_a] = c;*/                                                                                                                                    \
 			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                       \
 			if(zbdw) pz[_a] = zz;                                                                                                                   \
@@ -264,7 +264,7 @@
 	{                                                                                                                                                          \
 		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                   \
-		if (ZCMPSIMP(zz, pz[_a], _a, c)) {                                                                                                                     \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
 			/*pp[_a] = c;*/                                                                                                                                    \
 			pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);                                                                                                       \
 			if(zbdw) pz[_a] = zz;                                                                                                                   \
@@ -352,51 +352,24 @@
 
 
 
-#define DRAW_LINE_TRI_TEXTURED()                                                                                                                               \
-	{                                                                                                                                                          \
-		register GLushort* pz;                                                                                                                                 \
-		register PIXEL* pp;                                                                                                                                    \
-		register GLuint s, t, z;                                                                                                                   		        \
-		register GLint n;                                                                                                                          	\
-		OR1OG1OB1DECL                                                                                                                                          \
-		GLfloat sz, tz, fz, zinv;                                                                                                                              \
-		n = (x2 >> 16) - x1;                                                                                                                                   \
-		fz = (GLfloat)z1;                                                                                                                                      \
-		zinv = 1.0 / fz;                                                                                                                                       \
-		pp = (PIXEL*)((GLbyte*)pp1 + x1 * PSZB);                                                                                                               \
-		pz = pz1 + x1;                                                                                                                                         \
-		z = z1;                                                                                                                                                \
-		sz = sz1;                                                                                                                                              \
-		tz = tz1;                                                                                                                                              \
-		while (n >= (NB_INTERP - 1)) {                                                                                                                         \
-			register GLint  dsdx, dtdx;																																\
-			{                                                                                                                                                  \
-				GLfloat ss, tt;                                                                                                                                \
-				ss = (sz * zinv);                                                                                                                              \
-				tt = (tz * zinv);                                                                                                                              \
-				s = (GLint)ss;                                                                                                                                 \
-				t = (GLint)tt;                                                                                                                                 \
-				dsdx = (GLint)((dszdx - ss * fdzdx) * zinv);                                                                                                   \
-				dtdx = (GLint)((dtzdx - tt * fdzdx) * zinv);																								\
-			}                                                                                                   										\
-				fz += fndzdx;                                                                                                                                  \
-				zinv = 1.0 / fz;                                                                                                                               \
-			PUT_PIXEL(0); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(1); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(2); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(3); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(4); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(5); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(6); /*the_x++;*/                                                                                                                         \
-			PUT_PIXEL(7); /*the_x-=7;*/                                                                                                                        \
-			pz += NB_INTERP;                                                                                                                                   \
-			/*pp = (PIXEL*)((GLbyte*)pp + NB_INTERP * PSZB);*/ /*the_x+=NB_INTERP * PSZB;*/                                                                        \
-			pp += NB_INTERP; /*the_x+=NB_INTERP * PSZB;*/                                                                        \
-			n -= NB_INTERP;                                                                                                                                    \
-			sz += ndszdx;                                                                                                                                      \
-			tz += ndtzdx;                                                                                                                                      \
-		}                                                                                                                                                      \
-		{ register GLint  dsdx, dtdx;																														\
+#define DRAW_LINE_TRI_TEXTURED()                                                                                                                                   \
+	{                                                                                                                                                              \
+		register GLushort* pz;                                                                                                                                     \
+		register PIXEL* pp;                                                                                                                                        \
+		register GLuint s, t, z;                                                                                                                   		           \
+		register GLint n;                                                                                                                          	               \
+		OR1OG1OB1DECL                                                                                                                                              \
+		GLfloat sz, tz, fz, zinv;                                                                                                                                  \
+		n = (x2 >> 16) - x1;                                                                                                                                       \
+		fz = (GLfloat)z1;                                                                                                                                          \
+		zinv = 1.0 / fz;                                                                                                                                           \
+		pp = (PIXEL*)((GLbyte*)pp1 + x1 * PSZB);                                                                                                                   \
+		pz = pz1 + x1;                                                                                                                                             \
+		z = z1;                                                                                                                                                    \
+		sz = sz1;                                                                                                                                                  \
+		tz = tz1;                                                                                                                                                  \
+		while (n >= (NB_INTERP - 1)) {                                                                                                                             \
+			register GLint  dsdx, dtdx;																															   \
 			{                                                                                                                                                      \
 				GLfloat ss, tt;                                                                                                                                    \
 				ss = (sz * zinv);                                                                                                                                  \
@@ -404,6 +377,32 @@
 				s = (GLint)ss;                                                                                                                                     \
 				t = (GLint)tt;                                                                                                                                     \
 				dsdx = (GLint)((dszdx - ss * fdzdx) * zinv);                                                                                                       \
+				dtdx = (GLint)((dtzdx - tt * fdzdx) * zinv);																								       \
+			}                                                                                                   										           \
+				fz += fndzdx;                                                                                                                                      \
+				zinv = 1.0 / fz;                                                                                                                                   \
+			PUT_PIXEL(0); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(1); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(2); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(3); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(4); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(5); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(6); /*the_x++;*/                                                                                                                             \
+			PUT_PIXEL(7); /*the_x-=7;*/                                                                                                                            \
+			pz += NB_INTERP;                                                                                                                                       \
+			pp += NB_INTERP; /*the_x+=NB_INTERP * PSZB;*/                                                                        							       \
+			n -= NB_INTERP;                                                                                                                                    	   \
+			sz += ndszdx;                                                                                                                                          \
+			tz += ndtzdx;                                                                                                                                          \
+		}                                                                                                                                                 		   \
+		{ register GLint  dsdx, dtdx;																															   \
+			{                                                                                                                                                      \
+				GLfloat ss, tt;                                                                                                                                    \
+				ss = (sz * zinv);                                                                                                                                  \
+				tt = (tz * zinv);                                                                                                                                  \
+				s = (GLint)ss;                                                                                                                                     \
+				t = (GLint)tt;                                                                                                                                     \
+				dsdx = (GLint)((dszdx - ss * fdzdx) * zinv);                                                                                                       \
 				dtdx = (GLint)((dtzdx - tt * fdzdx) * zinv);                                                                                                       \
 			}                                                                                                                                                      \
 			while (n >= 0) {                                                                                                                                       \
@@ -427,6 +426,7 @@
 #endif
 #define INTERP_Z
 #define INTERP_STZ
+#define INTERP_RGB
 
 //NB_INTERP refers to some sort of texture mapping speedup enhancement.
 #define NB_INTERP 8
@@ -440,7 +440,6 @@
 		ndtzdx = NB_INTERP * dtzdx;                                                                                                                            \
 	}
 #if TGL_FEATURE_LIT_TEXTURES == 1
-#define INTERP_RGB
 #define OR1OG1OB1DECL                                                                                                                                          \
 	register GLint or1, og1, ob1;                                                                                                                             \
 	or1 = r1;                                                                                                                                                  \
@@ -449,8 +448,7 @@
 #define OR1G1B1INCR                                                                                                                                   \
 		og1 += dgdx;                                                                                                                      \
 		or1 += drdx;                                                                                                                      \
-		ob1 += dbdx;                                                                                                                  \
-	
+		ob1 += dbdx;                                                                                                                  
 #else
 #define OR1OG1OB1DECL /*A comment*/
 #define OR1G1B1INCR   /*Another comment*/
@@ -457,13 +455,27 @@
 #define or1 COLOR_MULT_MASK
 #define og1 COLOR_MULT_MASK
 #define ob1 COLOR_MULT_MASK
-#undef INTERP_RGB
 #endif
 #if TGL_FEATURE_NO_DRAW_COLOR != 1
+
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
+		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                         \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
+			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, TEXTURE_SAMPLE(texture, s, t));*/             															   \
+			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, (TEXTURE_SAMPLE(texture, s, t))), (pp[_a]) );                   															   \
+			if(zbdw) pz[_a] = zz;                                                                                                                   		   \
+		}                                                                                                                                                      \
+		z += dzdx;                                                                                                                                             \
+		s += dsdx;                                                                                                                                             \
+		t += dtdx;                                                                                                                                             \
+		OR1G1B1INCR                                                                                                                                            \
+	}
+/*
+#define PUT_PIXEL(_a)                                                                                                                                          \
+	{                                                                                                                                                          \
 		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		if (ZCMP(zz, pz[_a], _a, 0)) {                                                                                                                         \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                         \
 			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, TEXTURE_SAMPLE(texture, s, t)), pp[_a]);      						   								\
 			if(zbdw) pz[_a] = zz;                                                                                                                   		\
 		}                                                                                                                                                      \
@@ -472,6 +484,7 @@
 		t += dtdx;                                                                                                                                             \
 		OR1G1B1INCR                                                                                                                                            \
 	}
+*/
 #else
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
@@ -541,10 +554,10 @@
 #if TGL_FEATURE_NO_DRAW_COLOR != 1
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
-		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		if (ZCMP(zz, pz[_a], _a, 0)) {                                                                                                                         \
-			pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, TEXTURE_SAMPLE(texture, s, t));                   				\
-			if(zbdw) pz[_a] = zz;                                                                                                                   			\
+		register GLuint zz =z >> ZB_POINT_Z_FRAC_BITS;                                                                                                         \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
+			pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, TEXTURE_SAMPLE(texture, s, t));                   															   \
+			if(zbdw) pz[_a] = zz;                                                                                                                   		   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		s += dsdx;                                                                                                                                             \