shithub: tinygl

Download patch

ref: bb91438a3cca03683f0df6320454542e1907527d
parent: 991af3667599e78b0aa9253f6b9ff57ea6f0371e
author: David <gek@katherine>
date: Mon Feb 22 20:39:56 EST 2021

Color interpolation sooped up

--- a/SDL_Examples/gears.c
+++ b/SDL_Examples/gears.c
@@ -422,7 +422,7 @@
 
 	//glDisable( GL_LIGHTING );
 	glEnable(GL_LIGHTING);
-	glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ZERO);
+	//glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ZERO);
 	glBlendEquation(GL_FUNC_ADD);
 	if(blending){
 		glDisable(GL_DEPTH_TEST);
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -32,6 +32,28 @@
 #define ST_TO_TEXTURE_BYTE_OFFSET(s,t) ( ((s & ZB_S_MASK)>>(ZB_POINT_S_VALUE-PSZSH)) | ((t & ZB_T_MASK)>>(ZB_POINT_T_VALUE-PSZSH))  )
 #endif
 
+//The corrected mult mask prevents a bug relating to color interp. it's also why the color bit depth is so damn high.
+#define COLOR_MULT_MASK (0xff0000)
+#define COLOR_CORRECTED_MULT_MASK (0xfe0000)
+#define COLOR_MASK 		(0xffffff)
+#define COLOR_MIN_MULT (COLOR_MASK & ~COLOR_MULT_MASK)
+#define COLOR_SHIFT		16
+
+#define COLOR_R_GET32(r) ((r) & 0xff0000)
+#define COLOR_G_GET32(g) ((g>>8) & 0xff00)
+#define COLOR_B_GET32(b) ((b >>16)&0xff)
+
+#define COLOR_R_GET16(r) ((r>>8) & 0xF800)
+#define COLOR_G_GET16(g) ((((g)) >> 13) & 0x07E0)
+#define COLOR_B_GET16(b) (((b) >> 19) & 31)
+
+#if TGL_FEATURE_RENDER_BITS == 32
+#define RGB_TO_PIXEL(r,g,b) \
+  ( COLOR_R_GET32(r) | COLOR_G_GET32(g) | COLOR_B_GET32(b) )
+#elif TGL_FEATURE_RENDER_BITS == 16
+#define RGB_TO_PIXEL(r,g,b) \
+	( COLOR_R_GET16(r) | COLOR_G_GET16(g) | COLOR_B_GET16(b)  )
+#endif
 //This is how textures are sampled. if you want to do some sort of fancy texture filtering,
 //you do it here.
 #define TEXTURE_SAMPLE(texture, s, t)														\
@@ -46,8 +68,8 @@
 #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   )
+//#define TGL_CLAMPI(imp) ( (imp>0) * (COLOR_MASK * (imp>COLOR_MASK) + imp * (!(imp>COLOR_MASK)) )      )
+#define TGL_CLAMPI(imp) ( (imp>0)?((imp>COLOR_MASK)?COLOR_MASK:imp):0   )
 
 //#if TGL_FEATURE_BETTER_COLOR_INTERP == 1
 //#define (imp) imp = TGL_CLAMPI2((imp));
@@ -59,15 +81,14 @@
 #if TGL_FEATURE_RENDER_BITS == 32
 
 /* 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))
+
+#define GET_REDDER(p) ((p & COLOR_MULT_MASK))
+#define GET_GREENER(p) ((p & 0xff00)<<8)
+#define GET_BLUEER(p) ((p & 0xff)<<16)
+//These never change, DO NOT CHANGE THESE!!!
 #define GET_RED(p) ((p & 0xff0000)>>16)
-#define GET_REDDER(p) ((p & 0xff0000)>>8)
 #define GET_GREEN(p) ((p & 0xff00)>>8)
-#define GET_GREENER(p) ((p & 0xff00))
 #define GET_BLUE(p) (p & 0xff)
-#define GET_BLUEER(p) ((p & 0xff)<<8)
 typedef GLuint PIXEL;
 #define PSZB 4
 #define PSZSH 5
@@ -75,17 +96,15 @@
 #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 GET_REDDER(p) ((p & 0xF800)<<8)
+#define GET_GREENER(p) ((p & 0x07E0)<<13)
+#define GET_BLUEER(p) ((p & 31)<<19)
+//DO NOT CHANGE THESE BASED ON COLOR INTERP BITDEPTH
 #define GET_RED(p) ((p & 0xF800)>>8)
-#define GET_REDDER(p) ((p & 0xF800))
-
 #define GET_GREEN(p) ((p & 0x07E0)>>3)
-#define GET_GREENER(p) ((p & 0x07E0)<<5)
-
 #define GET_BLUE(p) ((p & 31)<<3)
-#define GET_BLUEER(p) ((p & 31)<<11)
 
+
 typedef GLushort PIXEL;
 #define PSZB 2 
 #define PSZSH 4 
@@ -110,10 +129,12 @@
 
 
 
-#if TGL_FEATURE_BLEND == 1
-#define TGL_BLEND_VARS GLuint zbblendeq = zb->blendeq; GLuint sfactor = zb->sfactor; GLuint dfactor = zb->dfactor;
 #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);}
+
+#if TGL_FEATURE_BLEND == 1
+#define TGL_BLEND_VARS GLuint zbblendeq = zb->blendeq; GLuint sfactor = zb->sfactor; GLuint dfactor = zb->dfactor;
+
 //SORCERY to achieve 32 bit signed integer clamping
 
 
@@ -160,9 +181,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;														\
@@ -173,9 +194,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;													\
@@ -187,7 +208,7 @@
 
 #define TGL_BLEND_FUNC_RGB(rr, gg, bb, dest){											\
 	{																					\
-		GLint sr = rr & 0xFFFF, sg = gg & 0xFFFF, sb = bb & 0xFFFF, dr, dg, db;			\
+		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);}					\
 	/*printf("\nShould never reach this point!");*/										\
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -5,7 +5,7 @@
 
 //Enables setting the error flags when there's an error, so you can check it with glGetError
 //Disabling this has slight performance gains.
-#define TGL_FEATURE_ERROR_CHECK 1
+#define TGL_FEATURE_ERROR_CHECK 0
 //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.
--- a/src/api.c
+++ b/src/api.c
@@ -56,9 +56,9 @@
 	p[7].ui = (GLuint) (b * (ZB_POINT_BLUE_MAX - ZB_POINT_BLUE_MIN) +
 							  ZB_POINT_BLUE_MIN);
 	*/
-	p[5].ui = (((GLuint)(r * 65535)) & 65535);
-	p[6].ui = (((GLuint)(g * 65535)) & 65535);
-	p[7].ui = (((GLuint)(b * 65535)) & 65535);
+	p[5].ui = (((GLuint)(r * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
+	p[6].ui = (((GLuint)(g * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
+	p[7].ui = (((GLuint)(b * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
 	gl_add_op(p);
 }
 
@@ -70,18 +70,9 @@
 	p[2].f = v[1];
 	p[3].f = v[2];
 	p[4].f = v[3];
-	/* direct convertion to GLinteger to go faster if no shading */
-	/*
-	p[5].ui = (GLuint) (v[0] * (ZB_POINT_RED_MAX - ZB_POINT_RED_MIN) +
-							  ZB_POINT_RED_MIN);
-	p[6].ui = (GLuint) (v[1] * (ZB_POINT_GREEN_MAX - ZB_POINT_GREEN_MIN) +
-							  ZB_POINT_GREEN_MIN);
-	p[7].ui = (GLuint) (v[2] * (ZB_POINT_BLUE_MAX - ZB_POINT_BLUE_MIN) +
-							  ZB_POINT_BLUE_MIN);
-	*/
-	p[5].ui = (((GLuint)(v[0] * 65535)) & 65535);
-	p[6].ui = (((GLuint)(v[1] * 65535)) & 65535);
-	p[7].ui = (((GLuint)(v[2] * 65535)) & 65535);
+	p[5].ui = (((GLuint)(v[0] * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
+	p[6].ui = (((GLuint)(v[1] * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
+	p[7].ui = (((GLuint)(v[2] * COLOR_CORRECTED_MULT_MASK) + COLOR_MIN_MULT) & COLOR_MASK);
 
 	gl_add_op(p);
 }
--- a/src/arrays.c
+++ b/src/arrays.c
@@ -1,7 +1,7 @@
 #include "zgl.h"
 #include <assert.h>
 //#include <stdio.h>
-
+#include "msghandling.h"
 
 
 //Code for buffers is here too!
--- a/src/clear.c
+++ b/src/clear.c
@@ -11,9 +11,9 @@
 void glopClear(GLContext* c, GLParam* p) {
 	GLint mask = p[1].i;
 	GLint z = 0;
-	GLint r = (GLint)(c->clear_color.v[0] * 65535);
-	GLint g = (GLint)(c->clear_color.v[1] * 65535);
-	GLint b = (GLint)(c->clear_color.v[2] * 65535);
+	GLint r = (GLint)(c->clear_color.v[0] * COLOR_MULT_MASK);
+	GLint g = (GLint)(c->clear_color.v[1] * COLOR_MULT_MASK);
+	GLint b = (GLint)(c->clear_color.v[2] * COLOR_MULT_MASK);
 
 	/* TODO : correct value of Z */
 
--- a/src/clip.c
+++ b/src/clip.c
@@ -20,14 +20,17 @@
 	v->zp.z = (GLint)(v->pc.Z * winv * c->viewport.scale.Z + c->viewport.trans.Z);
 	}
 	/* color */
-	{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;
-	}
+	//{
+	//GLuint val;
+	v->zp.r = (GLuint)(v->color.v[0] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
+	//v->zp.r = (val<COLOR_MIN_MULT)?COLOR_MIN_MULT:val;
+	//v->zp.r = val;
+	v->zp.g = (GLuint)(v->color.v[1] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
+	//v->zp.g = (val<COLOR_MIN_MULT)?COLOR_MIN_MULT:val;
+	//v->zp.g = val;
+	v->zp.b = (GLuint)(v->color.v[2] * COLOR_CORRECTED_MULT_MASK + COLOR_MIN_MULT) & COLOR_MASK;
+	//v->zp.b = val;
+	//}
 	/* texture */
 
 	if (c->texture_2d_enabled) {
--- a/src/get.c
+++ b/src/get.c
@@ -242,7 +242,7 @@
 		break;
 	case GL_POLYGON_STIPPLE:
 #if TGL_FEATURE_POLYGON_STIPPLE == 1
-		params[0] = c->zb->GLuint dostipple;
+		params[0] = c->zb->dostipple;
 #else
 		params[0] = GL_FALSE;
 #endif
--- a/src/light.c
+++ b/src/light.c
@@ -77,7 +77,7 @@
 #define ERROR_FLAG GL_INVALID_ENUM
 #include "error_check.h"
 #else
-	//assert(0);
+	return;
 #endif
 	}
 }
--- a/src/ztext.c
+++ b/src/ztext.c
@@ -66,7 +66,7 @@
 
 	if (x > 0 && x < w && y > 0 && y < h) {
 #if TGL_FEATURE_RENDER_BITS == 16
-		pix = RGB_TO_PIXEL(((pix & 255) << 8), (pix & 65280), ((pix >> 16) << 8));
+		pix = RGB_TO_PIXEL((pix & COLOR_MULT_MASK), ((pix & 0xFF00)<<(COLOR_SHIFT - 8)), ((pix & 255) << COLOR_SHIFT) );
 #endif
 		p[1].i = x + y * w;
 		p[2].ui = pix;
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -454,9 +454,9 @@
 #else
 #define OR1OG1OB1DECL /*A comment*/
 #define OR1G1B1INCR   /*Another comment*/
-#define or1 0xffff
-#define og1 0xffff
-#define ob1 0xffff
+#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
@@ -534,9 +534,9 @@
 #else
 #define OR1OG1OB1DECL /*A comment*/
 #define OR1G1B1INCR   /*Another comment*/
-#define or1 0xffff
-#define og1 0xffff
-#define ob1 0xffff
+#define or1 COLOR_MULT_MASK
+#define og1 COLOR_MULT_MASK
+#define ob1 COLOR_MULT_MASK
 #endif
 #if TGL_FEATURE_NO_DRAW_COLOR != 1
 #define PUT_PIXEL(_a)                                                                                                                                          \