shithub: tinygl

Download patch

ref: bf8e3ef4af895452320d2502e274541e92102460
parent: 757ecd0728d4a249ddf72ce0ac0e0acd1df76744
author: David <gek@katherine>
date: Mon Feb 22 18:52:46 EST 2021

Color interp bug fix

--- a/README.md
+++ b/README.md
@@ -148,8 +148,6 @@
 * Lit triangles will use the current material properties, even if they are textured. If the diffuse color is black, then your
 textured triangles will appear black.
 
-* Lit textured triangles are smoothly shaded, irrespective of glShadeModel (Untextured triangles do not have this bug)
-
 * the X dimension of the rendering window with must be a multiple of 4.
 
 * Line rendering is not blended
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -46,7 +46,14 @@
 #define ZB_NB_COLORS    225 /* number of colors for 8 bit display */
 
 
+#define TGL_CLAMPI(imp) ( (imp>0) * (65535 * (imp>65535) + imp * (!(imp>65535)) )      )
+#define TGL_CLAMPI2(imp) ( (imp>0)?((imp>65535)?65535:imp):0   )
 
+//#if TGL_FEATURE_BETTER_COLOR_INTERP == 1
+//#define (imp) imp = TGL_CLAMPI2((imp));
+//#else
+//#define (imp) /*a comment*/
+//#endif
 
 
 #if TGL_FEATURE_RENDER_BITS == 32
@@ -54,7 +61,7 @@
 /* 32 bit mode */
 //#define RGB_TO_PIXEL(r,g,b) ( ((b&65280)<<8) | ((g&65280)) | ((r&65280)>>8) )
 #define RGB_TO_PIXEL(r,g,b) \
-  ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b & 0xff00) >> 8))
+  ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | (((b) & 0xff00) >> 8))
 #define GET_RED(p) ((p & 0xff0000)>>16)
 #define GET_REDDER(p) ((p & 0xff0000)>>8)
 #define GET_GREEN(p) ((p & 0xff00)>>8)
@@ -68,7 +75,7 @@
 #elif TGL_FEATURE_RENDER_BITS == 16
 
 /* 16 bit mode */
-#define RGB_TO_PIXEL(r,g,b) (((r) & 0xF800) | (((g & 0xff00) >> 5) & 0x07E0) | ((b & 0xff00) >> 11))
+#define RGB_TO_PIXEL(r,g,b) (((r) & 0xF800) | ((((g) & 0xff00) >> 5) & 0x07E0) | (((b) & 0xff00) >> 11))
 
 #define GET_RED(p) ((p & 0xF800)>>8)
 #define GET_REDDER(p) ((p & 0xF800))
@@ -108,7 +115,7 @@
 #define TGL_NO_BLEND_FUNC(source, dest){dest = source;}
 #define TGL_NO_BLEND_FUNC_RGB(rr, gg, bb, dest){dest = RGB_TO_PIXEL(rr,gg,bb);}
 //SORCERY to achieve 32 bit signed integer clamping
-#define TGL_CLAMPI(imp) ( (imp>0) * (65535 * (imp>65535) + imp * (!(imp>65535)) )      )
+
 
 #define TGL_BLEND_SWITCH_CASE(sr,sg,sb,dr,dg,db,dest) 									\
 		switch(zbblendeq){															\
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -9,7 +9,7 @@
 //Strict out-of-memory checking. All OpenGL function calls are invalidated (ALL OF THEM) if a GL_OUT_OF_MEMORY error occurs.
 //The checks slow down the renderer so it is not recommended , but
 //it's in the GL spec that this should occur.
-#define TGL_FEATURE_STRICT_OOM_CHECKS 1
+#define TGL_FEATURE_STRICT_OOM_CHECKS 0
 
 //Swap between using the inline'd malloc(), calloc(), and free() in zbuffer.h, or
 //a replacement gl_malloc(), gl_zalloc(), and gl_free() in memory.c
--- a/src/clip.c
+++ b/src/clip.c
@@ -20,10 +20,14 @@
 	v->zp.z = (GLint)(v->pc.Z * winv * c->viewport.scale.Z + c->viewport.trans.Z);
 	}
 	/* color */
-	v->zp.r = (GLuint)(v->color.v[0] * 65535) & 65535;
-	v->zp.g = (GLuint)(v->color.v[1] * 65535) & 65535;
-	v->zp.b = (GLuint)(v->color.v[2] * 65535) & 65535;
-
+	{GLuint val;
+	val = (GLuint)(v->color.v[0] * 0xff00) & 65535;
+	v->zp.r = (val<0xff)?0xff:val;
+	val = (GLuint)(v->color.v[1] * 0xff00) & 65535;
+	v->zp.g = (val<0xff)?0xff:val;
+	val = (GLuint)(v->color.v[2] * 0xff00) & 65535;
+	v->zp.b = (val<0xff)?0xff:val;
+	}
 	/* texture */
 
 	if (c->texture_2d_enabled) {
@@ -426,6 +430,17 @@
 
 	if (c->texture_2d_enabled) {
 		//if(c->current_texture)
+#if TGL_FEATURE_LIT_TEXTURES == 1
+		if(c->current_shade_model != GL_SMOOTH){
+			p1->zp.r = p2->zp.r;
+			p1->zp.g = p2->zp.g;
+			p1->zp.b = p2->zp.b;
+
+			p0->zp.r = p2->zp.r;
+			p0->zp.g = p2->zp.g;
+			p0->zp.b = p2->zp.b;
+		}
+#endif
 		{
 #ifdef PROFILE
 			count_triangles_textured++;
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -125,9 +125,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                               \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #else
 #define PUT_PIXEL(_a)                                                                                                                                          \
@@ -140,9 +140,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #endif
 // END OF 32 bit mode
@@ -164,9 +164,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                   				\
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #else
 #define PUT_PIXEL(_a)                                                                                                                                          \
@@ -179,9 +179,9 @@
 			if(zbdw) pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #endif
 
@@ -219,9 +219,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                               \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #else
 #define PUT_PIXEL(_a)                                                                                                                                          \
@@ -233,9 +233,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                               \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #endif
 // END OF 32 bit mode
@@ -255,9 +255,9 @@
 			if(zbdw)pz[_a] = zz;                                                                                                                   				\
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #else
 #define PUT_PIXEL(_a)                                                                                                                                          \
@@ -270,9 +270,9 @@
 			if(zbdw) pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
-		og1 += dgdx;                                                                                                                                           \
-		or1 += drdx;                                                                                                                                           \
-		ob1 += dbdx;                                                                                                                                           \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
 	}
 #endif
 
@@ -442,14 +442,15 @@
 #if TGL_FEATURE_LIT_TEXTURES == 1
 #define INTERP_RGB
 #define OR1OG1OB1DECL                                                                                                                                          \
-	register GLuint or1, og1, ob1;                                                                                                                             \
+	register GLint or1, og1, ob1;                                                                                                                             \
 	or1 = r1;                                                                                                                                                  \
 	og1 = g1;                                                                                                                                                  \
 	ob1 = b1;
-#define OR1G1B1INCR                                                                                                                                           \
-	og1 += dgdx;                                                                                                                                               \
-	or1 += drdx;                                                                                                                                               \
-	ob1 += dbdx;
+#define OR1G1B1INCR                                                                                                                                   \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  \
+	
 #else
 #define OR1OG1OB1DECL /*A comment*/
 #define OR1G1B1INCR   /*Another comment*/
@@ -522,14 +523,14 @@
 	}
 #if TGL_FEATURE_LIT_TEXTURES == 1
 #define OR1OG1OB1DECL                                                                                                                                          \
-	register GLuint or1, og1, ob1;                                                                                                                             \
+	register GLint or1, og1, ob1;                                                                                                                             \
 	or1 = r1;                                                                                                                                                  \
 	og1 = g1;                                                                                                                                                  \
 	ob1 = b1;
-#define OR1G1B1INCR                                                                                                                                            \
-	og1 += dgdx;                                                                                                                                               \
-	or1 += drdx;                                                                                                                                               \
-	ob1 += dbdx;
+#define OR1G1B1INCR                                                                                                                                   \
+		og1 += dgdx;                                                                                                                      \
+		or1 += drdx;                                                                                                                      \
+		ob1 += dbdx;                                                                                                                  
 #else
 #define OR1OG1OB1DECL /*A comment*/
 #define OR1G1B1INCR   /*Another comment*/
--- a/src/ztriangle.h
+++ b/src/ztriangle.h
@@ -282,7 +282,7 @@
 				register GLuint z;
 #endif
 #ifdef INTERP_RGB
-				register GLuint or1, og1, ob1;
+				register GLint or1, og1, ob1;
 #endif
 #ifdef INTERP_ST
 				register GLuint s, t;