shithub: tinygl

Download patch

ref: c429386086c69185f833a5a554d3797bf78227ff
parent: 5dae18c473720df2a450fd7f708d32b0fe48637c
author: David <gek@katherine>
date: Tue Feb 16 22:53:18 EST 2021

Adding blending

--- a/README.md
+++ b/README.md
@@ -58,6 +58,8 @@
 
 * Removed unused functions which bloat binary size and lengthen compile times.
 
+* Added support for glDepthMask and glDisable(GL_DEPTH_TEST) as per-GL-spec
+
 * Fixed a myriad of bugs and... weirdnesses
 
 
@@ -72,7 +74,7 @@
 
 * The only supported texture size and format is RGB 256x256
 
-* there is no GL_BLEND compatibility whatsoever
+* Blending is in development.
 
 * A lot of prototypes are missing.
 
--- a/SDL_Examples/gears.c
+++ b/SDL_Examples/gears.c
@@ -207,10 +207,9 @@
 	// glLightfv( GL_LIGHT0, GL_AMBIENT, white);
 	// glLightfv( GL_LIGHT0, GL_SPECULAR, white);
 	glEnable(GL_CULL_FACE);
-	glEnable(GL_LIGHTING);
-	// glDisable( GL_LIGHTING );
+	
 	glEnable(GL_LIGHT0);
-	glEnable(GL_DEPTH_TEST);
+	//glEnable(GL_DEPTH_TEST);
 	// glShadeModel( GL_SMOOTH );
 
 	glEnable(GL_POLYGON_STIPPLE);
@@ -221,7 +220,7 @@
 	gear1 = glGenLists(1);
 	glNewList(gear1, GL_COMPILE);
 	glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);
-	// glColor3fv(red);
+	glColor3fv(blue);
 	gear(1.0, 4.0, 1.0, 20, 0.7); // The largest gear.
 	glEndList();
 
@@ -228,7 +227,7 @@
 	gear2 = glGenLists(1);
 	glNewList(gear2, GL_COMPILE);
 	glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
-	// glColor3fv(green);
+	glColor3fv(red);
 	gear(0.5, 2.0, 2.0, 10, 0.7); // The small gear with the smaller hole, to the right.
 	glEndList();
 
@@ -235,7 +234,7 @@
 	gear3 = glGenLists(1);
 	glNewList(gear3, GL_COMPILE);
 	glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
-	// glColor3fv(blue);
+	glColor3fv(green);
 	gear(1.3, 2.0, 0.5, 10, 0.7); // The small gear above with the large hole.
 	glEndList();
 	// glEnable( GL_NORMALIZE );
@@ -334,7 +333,20 @@
 	// initialize GL:
 	glClearColor(0.0, 0.0, 0.0, 0.0);
 	glViewport(0, 0, winSizeX, winSizeY);
+//TESTING BLENDING...
+	//glDisable(GL_DEPTH_TEST);
 	glEnable(GL_DEPTH_TEST);
+
+	//glEnable(GL_BLEND);
+	glDisable(GL_BLEND);
+	
+	//glDepthMask(GL_FALSE);
+	glDepthMask(GL_TRUE);
+
+	//glDisable( GL_LIGHTING );
+	glEnable(GL_LIGHTING);
+	glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_DST_COLOR);
+	glBlendEquation(GL_FUNC_ADD);
 	GLfloat h = (GLfloat)winSizeY / (GLfloat)winSizeX;
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
--- a/SDL_Examples/texture.c
+++ b/SDL_Examples/texture.c
@@ -90,7 +90,8 @@
 	// glLightfv( GL_LIGHT0, GL_SPECULAR, white);
 	glEnable(GL_CULL_FACE);
 	// glDisable( GL_CULL_FACE );
-	glEnable(GL_LIGHTING);
+	glDisable(GL_BLEND);
+	//glEnable(GL_LIGHTING);
 	glEnable(GL_TEXTURE_2D);
 	glDisable(GL_LIGHTING);
 	// glEnable( GL_LIGHT0 );
--- a/include/GL/gl.h
+++ b/include/GL/gl.h
@@ -857,6 +857,7 @@
 void glPolygonOffset(GLfloat factor, GLfloat units);
 void glBlendFunc(GLint, GLint);
 void glBlendEquation(GLenum mode);
+void glDepthMask(GLint);
 /* not implemented, just added to compile  */
   /*
 inline void glPointSize(GLfloat) {}
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -7,7 +7,8 @@
 
 #include "zfeatures.h"
 #include "GL/gl.h"
-
+//#warning stdio is left in!!! remove it!!!
+//#include <stdio.h>
 #define ZB_Z_BITS 16
 
 #define ZB_POINT_Z_FRAC_BITS 14
@@ -87,32 +88,125 @@
 #define RGB_MIX_FUNC(rr, gg, bb, tpix)(tpix)
 #endif
 
+
+
 #if TGL_FEATURE_BLEND == 1
-#define 
-#define TGL_BLEND_FUNC(source, dest){\
-	GLuint t = source;\
-	GLint sr = GET_REDDER(t), sg = GET_GREENER(t), sb = GET_BLUEER(t);\
-	GLint dr = GET_REDDER(dest), dg = GET_GREENER(dest), db = GET_BLUEER(dest);\
-	switch(zb->sfactor){		\
-		case GL_ONE:		\
-		default:		\
-		break;				\
-		case GL_ONE_MINUS_SRC_COLOR:\
-						\
-		break;\
-	}\
-	switch(zb->blendeq){\
-		case GL_ADD_FUNC:\
-		case GL_ADD_FUNC_EXT:\
-			dest = RGB_TO_PIXEL(sr+dr,sg+dg,sb+db);\
-		break;\
-		case GL_SUBTRACT_FUNC:
-		case GL_SUBTRACT_FUNC_EXT:
-			dest = RGB_TO_PIXEL(sr-dr,sg-dg,sb-db);\
-		break;
-	}\
-}
+#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);}
+#define TGL_CLAMPI(imp,min,max) ( (imp<min)?min:((imp>max)?max:imp) )
+
+#if TGL_FEATURE_EXPENSIVE_BLEND == 1
+#define TGL_BLEND_SWITCH_CASE(sr,sg,sb,dr,dg,db,dest) 									\
+		switch(zb->blendeq){															\
+			case GL_FUNC_ADD:															\
+			default:																	\
+				dest = RGB_TO_PIXEL(sr+dr, sg+dg, sb+db);								\
+			break;																		\
+			case GL_FUNC_SUBTRACT:														\
+				sr-=dr;sg-=dg;sb-=db;													\
+				dest = RGB_TO_PIXEL(sr * (sr),sg,sb);									\
+			break;																		\
+			case GL_FUNC_REVERSE_SUBTRACT:												\
+				sr=dr-sr;sg=dg-sg;sb=db-sb;												\
+				sr = TGL_CLAMPI(sr, 0, 65280);											\
+				sg = TGL_CLAMPI(sg, 0, 65280);											\
+				sb = TGL_CLAMPI(sb, 0, 65280);											\
+				dest = RGB_TO_PIXEL(sr,sg,sb);											\
+			break;																		\
+			  																			\
+		}																				
 #else
+#define TGL_BLEND_SWITCH_CASE(sr,sg,sb,dr,dg,db,dest) 									\
+		/*switch(zb->blendeq){*/														\
+		/*	case GL_FUNC_ADD:	*/														\
+		/*	default:			*/														\
+		\
+		dest = RGB_TO_PIXEL(sr+dr, sg+dg, sb+db);										\
+		/*	break;				*/														\
+			  																			\
+		/*}						*/														
+#endif
+
+
+#define TGL_BLEND_FUNC(source, dest){													\
+	if(zb->enable_blend != 1){															\
+		TGL_NO_BLEND_FUNC(source,dest)													\
+	} else {																			\
+	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!");*/									\
+		switch(zb->sfactor){															\
+			case GL_ONE:																\
+			default:																	\
+			break;																		\
+			case GL_ONE_MINUS_SRC_COLOR:												\
+			sr = ~sr & 0xffff;															\
+			sg = ~sg & 0xffff;															\
+			sb = ~sb & 0xffff;															\
+			break;																		\
+			case GL_ZERO:																\
+			sr=0;sg=0;sb=0;break;														\
+			break;																		\
+		}																				\
+		switch(zb->dfactor){															\
+				case GL_ONE:															\
+				default:																\
+				break;																	\
+				case GL_ONE_MINUS_DST_COLOR:											\
+				dr = ~dr & 0xffff;														\
+				dg = ~dg & 0xffff;														\
+				db = ~db & 0xffff;														\
+				break;																	\
+				case GL_ZERO:															\
+				dr=0;dg=0;db=0;break;													\
+				break;																	\
+			}																			\
+		TGL_BLEND_SWITCH_CASE(sr,sg,sb,dr,dg,db,dest)									\
+	}																					\
+} ///////////////////////////////////////////////////////////////////////////////////////
+
+#define TGL_BLEND_FUNC_RGB(rr, gg, bb, dest){											\
+	if(zb->enable_blend != 1){															\
+		TGL_NO_BLEND_FUNC_RGB(rr,gg,bb,dest)											\
+	} else {																			\
+		GLint sr = rr & 0xFFFF, sg = gg & 0xFFFF, sb = bb & 0xFFFF, dr, dg, db;			\
+		{GLuint t = dest;																\
+		dr = GET_REDDER(t); dg = GET_GREENER(t); db = GET_BLUEER(t);}					\
+	/*printf("\nShould never reach this point!");*/										\
+		switch(zb->sfactor){															\
+			case GL_ONE:																\
+			default:																	\
+			break;																		\
+			case GL_ONE_MINUS_SRC_COLOR:												\
+			sr = ~sr & 0xffff;															\
+			sg = ~sg & 0xffff;															\
+			sb = ~sb & 0xffff;															\
+			break;																		\
+			case GL_ZERO:																\
+			sr=0;sg=0;sb=0;break;														\
+			break;																		\
+		}																				\
+		switch(zb->dfactor){															\
+				case GL_ONE:															\
+				default:																\
+				break;																	\
+				case GL_ONE_MINUS_DST_COLOR:											\
+				dr = ~dr & 0xffff;														\
+				dg = ~dg & 0xffff;														\
+				db = ~db & 0xffff;														\
+				break;																	\
+				case GL_ZERO:															\
+				dr=0;dg=0;db=0;break;													\
+				break;																	\
+		}																				\
+		TGL_BLEND_SWITCH_CASE(sr,sg,sb,dr,dg,db,dest)									\
+	}																					\
+} ///////////////////////////////////////////////////////////////////////////////////////
+
+#else
 #define TGL_BLEND_FUNC(source, dest){dest = source;}
 #define TGL_BLEND_FUNC_RGB(rr, gg, bb, dest){dest = RGB_TO_PIXEL(rr,gg,bb);}
 #endif
@@ -126,8 +220,10 @@
     GLushort *zbuf;
     PIXEL *pbuf;
     GLint frame_buffer_allocated;
+    /* depth */
+    GLint depth_test;
+    GLint depth_write;
     
-     
     GLint nb_colors;
     unsigned char *dctable;
     GLint *ctable;
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -10,7 +10,8 @@
 //NOTE: Polygon Offset does nothing at the moment.
 #define TGL_FEATURE_POLYGON_OFFSET 0
 #define TGL_FEATURE_POLYGON_STIPPLE 0
-#define TGL_FEATURE_BLEND 0
+#define TGL_FEATURE_BLEND 			0
+#define TGL_FEATURE_EXPENSIVE_BLEND 1
 //A stipple pattern is 128 bytes in size.
 #define TGL_POLYGON_STIPPLE_BYTES 128
 //A stipple pattern is 2^5 (32) bits wide.
--- a/src/api.c
+++ b/src/api.c
@@ -165,6 +165,9 @@
 	gl_add_op(p);
 }
 
+void glDepthMask(GLint i){
+	gl_get_context()->zb->depth_write = (i==GL_TRUE);
+}
 /* glEnable / glDisable */
 
 void glEnable(GLint cap) {
--- a/src/clip.c
+++ b/src/clip.c
@@ -1,5 +1,5 @@
-#include "zgl.h"
 #include "msghandling.h"
+#include "zgl.h"
 /* fill triangle profile */
 /* #define PROFILE */
 
@@ -110,7 +110,7 @@
 		if (c->render_mode == GL_SELECT) {
 			gl_add_select1(c, p1->zp.z, p2->zp.z, p2->zp.z);
 		} else {
-			if (c->depth_test)
+			if (c->zb->depth_test)
 				ZB_line_z(c->zb, &p1->zp, &p2->zp);
 			else
 				ZB_line(c->zb, &p1->zp, &p2->zp);
@@ -137,7 +137,7 @@
 			gl_transform_to_viewport(c, &q1);
 			gl_transform_to_viewport(c, &q2);
 
-			if (c->depth_test)
+			if (c->zb->depth_test)
 				ZB_line_z(c->zb, &q1.zp, &q2.zp);
 			else
 				ZB_line(c->zb, &q1.zp, &q2.zp);
@@ -414,7 +414,7 @@
 /* Render a clipped triangle in line mode */
 
 void gl_draw_triangle_line(GLContext* c, GLVertex* p0, GLVertex* p1, GLVertex* p2) {
-	if (c->depth_test) {
+	if (c->zb->depth_test) {
 		if (p0->edge_flag)
 			ZB_line_z(c->zb, &p0->zp, &p1->zp);
 		if (p1->edge_flag)
--- a/src/get.c
+++ b/src/get.c
@@ -1,5 +1,5 @@
-#include "zgl.h"
 #include "msghandling.h"
+#include "zgl.h"
 void glGetIntegerv(GLint pname, GLint* params) {
 	GLContext* c = gl_get_context();
 
--- a/src/init.c
+++ b/src/init.c
@@ -7,7 +7,7 @@
 	s->lists = gl_zalloc(sizeof(GLList*) * MAX_DISPLAY_LISTS);
 	s->texture_hash_table = gl_zalloc(sizeof(GLTexture*) * TEXTURE_HASH_TABLE_SIZE);
 
-	alloc_texture(c, 0); //MEMORY LEAK
+	alloc_texture(c, 0); // MEMORY LEAK
 }
 
 void endSharedState(GLContext* c) {
@@ -15,46 +15,45 @@
 	GLint i;
 	GLList* l;
 	GLParamBuffer *pb, *pb1;
-	GLTexture* t, *n;
-	for (i = 0; i < MAX_DISPLAY_LISTS; i++) 
-	if(s->lists[i]){
-		l = s->lists[i];
-		pb = l->first_op_buffer;
-		while (pb != NULL) {
-			pb1 = pb->next;
-			gl_free(pb);
-			pb = pb1;
+	GLTexture *t, *n;
+	for (i = 0; i < MAX_DISPLAY_LISTS; i++)
+		if (s->lists[i]) {
+			l = s->lists[i];
+			pb = l->first_op_buffer;
+			while (pb != NULL) {
+				pb1 = pb->next;
+				gl_free(pb);
+				pb = pb1;
+			}
+			gl_free(l);
+			s->lists[i] = NULL;
 		}
-		gl_free(l);
-		s->lists[i] = NULL;
-	}
 	gl_free(s->lists);
-	for(i = 0; i < TEXTURE_HASH_TABLE_SIZE; i++)
-	{
+	for (i = 0; i < TEXTURE_HASH_TABLE_SIZE; i++) {
 		t = s->texture_hash_table[i];
-		while(t){
-			GLTexture **ht;
-				GLImage* im;
-				GLint inner_i;
-			
-				//t = find_texture(c, h);
-				if (t->prev == NULL) {
-					ht = &c->shared_state.texture_hash_table[t->handle % TEXTURE_HASH_TABLE_SIZE];
-					*ht = t->next;
-				} else {
-					t->prev->next = t->next;
-				}
-				n=t->next;
-				if (t->next != NULL)
-					t->next->prev = t->prev;
-			
-				for (inner_i = 0; inner_i < MAX_TEXTURE_LEVELS; inner_i++) {
-					im = &t->images[inner_i];
-					if (im->pixmap != NULL)
-						gl_free(im->pixmap);
-				}
-				gl_free(t);
-				t=n;
+		while (t) {
+			GLTexture** ht;
+			GLImage* im;
+			GLint inner_i;
+
+			// t = find_texture(c, h);
+			if (t->prev == NULL) {
+				ht = &c->shared_state.texture_hash_table[t->handle % TEXTURE_HASH_TABLE_SIZE];
+				*ht = t->next;
+			} else {
+				t->prev->next = t->next;
+			}
+			n = t->next;
+			if (t->next != NULL)
+				t->next->prev = t->prev;
+
+			for (inner_i = 0; inner_i < MAX_TEXTURE_LEVELS; inner_i++) {
+				im = &t->images[inner_i];
+				if (im->pixmap != NULL)
+					gl_free(im->pixmap);
+			}
+			gl_free(t);
+			t = n;
 		}
 	}
 	gl_free(s->texture_hash_table);
@@ -138,7 +137,7 @@
 	c->zb->sfactor = GL_ONE;
 	c->zb->dfactor = GL_ZERO;
 	c->zb->blendeq = GL_FUNC_ADD;
-	
+
 	/* default state */
 	c->current_color.X = 1.0;
 	c->current_color.Y = 1.0;
@@ -221,22 +220,22 @@
 	c->specbuf_num_buffers = 0;
 
 	/* depth test */
-	c->depth_test = 0;
+	c->zb->depth_test = 0;
+	c->zb->depth_write = 1;
 }
 
 void glClose(void) {
-	GLContext* c = gl_get_context();GLuint i;
+	GLContext* c = gl_get_context();
+	GLuint i;
 	gl_free(c->vertex);
 	for (i = 0; i < 3; i++) {
-		//c->matrix_stack[i] = gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(M4));
+		// c->matrix_stack[i] = gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(M4));
 		gl_free(c->matrix_stack[i]);
-		//c->matrix_stack_ptr[i] = c->matrix_stack[i];
+		// c->matrix_stack_ptr[i] = c->matrix_stack[i];
 	}
 	i = 0;
 	GLSpecBuf* n = NULL;
-	for(
-	GLSpecBuf* b = c->specbuf_first; b != NULL; b = n)
-	{
+	for (GLSpecBuf* b = c->specbuf_first; b != NULL; b = n) {
 		n = b->next;
 		gl_free(b);
 		i++;
--- a/src/list.c
+++ b/src/list.c
@@ -1,5 +1,5 @@
-#include "zgl.h"
 #include "msghandling.h"
+#include "zgl.h"
 
 /*
 static char* op_table_str[] = {
@@ -29,8 +29,10 @@
 	GLList* l;
 
 	l = find_list(c, list);
-	if (l == NULL)
-		{tgl_warning("\nAttempted to delete NULL list!!!!\n");return;}
+	if (l == NULL) {
+		tgl_warning("\nAttempted to delete NULL list!!!!\n");
+		return;
+	}
 	assert(l != NULL);
 
 	/* free param buffer */
@@ -137,7 +139,7 @@
 		gl_compile_op(c, p);
 	}
 	if (c->print_flag) {
-//		gl_print_op(stderr, p);
+		//		gl_print_op(stderr, p);
 	}
 }
 
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -1,5 +1,5 @@
-#include "zgl.h"
 #include "msghandling.h"
+#include "zgl.h"
 void gl_print_matrix(const GLfloat* m) {
 	GLint i;
 
--- a/src/misc.c
+++ b/src/misc.c
@@ -47,7 +47,7 @@
 		c->viewport.updated = 1;
 	}
 }
-void glBlendFunc(GLenum sfactor, GLenum dfactor){
+void glBlendFunc(GLenum sfactor, GLenum dfactor) {
 	GLParam p[3];
 	p[0].op = OP_BlendFunc;
 	p[1].i = sfactor;
@@ -55,19 +55,17 @@
 	gl_add_op(p);
 	return;
 }
-void glopBlendFunc(GLContext* c, GLParam* p){
+void glopBlendFunc(GLContext* c, GLParam* p) {
 	c->zb->sfactor = p[1].i;
 	c->zb->dfactor = p[2].i;
 }
-void glBlendEquation(GLenum mode){
+void glBlendEquation(GLenum mode) {
 	GLParam p[2];
 	p[0].op = OP_BlendEquation;
 	p[1].i = mode;
 	gl_add_op(p);
 }
-void glopBlendEquation(GLContext* c, GLParam* p){
-	c->zb->blendeq = p[1].i;
-}
+void glopBlendEquation(GLContext* c, GLParam* p) { c->zb->blendeq = p[1].i; }
 void glopEnableDisable(GLContext* c, GLParam* p) {
 	GLint code = p[1].i;
 	GLint v = p[2].i;
@@ -92,7 +90,7 @@
 		c->normalize_enabled = v;
 		break;
 	case GL_DEPTH_TEST:
-		c->depth_test = v;
+		c->zb->depth_test = v;
 		break;
 	case GL_POLYGON_OFFSET_FILL:
 		if (v)
--- a/src/msghandling.c
+++ b/src/msghandling.c
@@ -1,7 +1,7 @@
 #include "../include/GL/gl.h"
+#include "zgl.h"
 #include <stdarg.h>
 #include <stdio.h>
-#include "zgl.h"
 #define NDEBUG
 
 #ifdef NDEBUG
@@ -45,7 +45,6 @@
 	va_end(args);
 #endif /* !NO_DEBUG_OUTPUT */
 }
-
 
 void gl_fatal_error(char* format, ...) {
 #ifndef NO_DEBUG_OUTPUT
--- a/src/opinfo.h
+++ b/src/opinfo.h
@@ -75,6 +75,4 @@
 ADD_OP(PlotPixel, 2, "%d %d")
 ADD_OP(TextSize, 1, "%d")
 
-
-
 #undef ADD_OP
--- a/src/zdither.c
+++ b/src/zdither.c
@@ -34,7 +34,7 @@
 	GLint c, r, g, b, i, index, r1, g1, b1;
 
 	if (nb_colors < (_R * _G * _B)) {
-//		tgl_fixme("zdither: not enough colors\n");
+		//		tgl_fixme("zdither: not enough colors\n");
 		exit(1);
 	}
 
--- a/src/zgl.h
+++ b/src/zgl.h
@@ -261,8 +261,8 @@
 	GLint offset_states;
 
 	/* opengl blending */
-	//All the settings are in the Zbuffer!
-	
+	// All the settings are in the Zbuffer!
+
 	/* specular buffer. could probably be shared between contexts,
 	  but that wouldn't be 100% thread safe */
 	GLSpecBuf* specbuf_first;
@@ -275,7 +275,7 @@
 	GLint (*gl_resize_viewport)(struct GLContext* c, GLint* xsize, GLint* ysize);
 
 	/* depth test */
-	GLint depth_test;
+	//Moved to Zbuffer.
 } GLContext;
 
 extern GLContext* gl_ctx;
--- a/src/zline.c
+++ b/src/zline.c
@@ -1,23 +1,23 @@
 #include "../include/zbuffer.h"
 #include <stdlib.h>
 
-#define ZCMP(z, zpix) ((z) >= (zpix))
+#define ZCMP(z, zpix) (!(zb->depth_test) || z >= (zpix))
 
 /* TODO: Implement blending for lines.*/
 
-
 void ZB_plot(ZBuffer* zb, ZBufferPoint* p) {
 	GLushort* pz;
 	PIXEL* pp;
 	GLint zz;
-//	PIXEL col;
+	//	PIXEL col;
 	pz = zb->zbuf + (p->y * zb->xsize + p->x);
 	pp = (PIXEL*)((GLbyte*)zb->pbuf + zb->linesize * p->y + p->x * PSZB);
 	zz = p->z >> ZB_POINT_Z_FRAC_BITS;
 	if (ZCMP(zz, *pz)) {
-		//*pp = 
+		//*pp =
 		TGL_BLEND_FUNC_RGB(p->r, p->g, p->b, (*pp))
-		*pz = zz;
+		if(zb->depth_write)
+			*pz = zz;
 	}
 }
 
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -30,8 +30,8 @@
 #define NODRAWTEST(c) (1)
 #endif
 
-#define ZCMP(z, zpix, _a, c) ((z) >= (zpix) && STIPTEST(_a) && NODRAWTEST(c))
-#define ZCMPSIMP(z, zpix, _a, c) ((z) >= (zpix) && STIPTEST(_a))
+#define ZCMP(z, zpix, _a, c) ( ((!zb->depth_test) || (z) >= (zpix)) && STIPTEST(_a) && NODRAWTEST(c))
+#define ZCMPSIMP(z, zpix, _a, c) ( ((!zb->depth_test) || (z) >= (zpix)) && STIPTEST(_a))
 
 void ZB_fillTriangleFlat(ZBuffer* zb, ZBufferPoint* p0, ZBufferPoint* p1, ZBufferPoint* p2) {
 
@@ -46,8 +46,8 @@
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		if (ZCMPSIMP(zz, pz[_a], _a, color)) {                                                                                                                 \
-			TGL_BLEND_FUNC(color, (pp[_a]))/*pp[_a] = color;*/                                                                                                                                  \
-			pz[_a] = zz;                                                                                                                                       \
+			TGL_BLEND_FUNC(color, (pp[_a])) /*pp[_a] = color;*/                                                                                                \
+			if(zb->depth_write)pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 	}
@@ -78,10 +78,10 @@
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                           \
-			/*pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                             \
-			TGL_BLEND_FUNC_RGB(or1, og1, ob1,(pp[_a]));                                                                                                              \
-			pz[_a] = zz;                                                                                                                                       \
+		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
+			/*pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                          \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                       \
+			if(zb->depth_write)pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		og1 += dgdx;                                                                                                                                           \
@@ -92,11 +92,11 @@
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                       \
+		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                   \
 		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
-			/*pp[_a] = c;*/                                                                                                                                        \
-			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                                                        \
-			pz[_a] = zz;                                                                                                                                       \
+			/*pp[_a] = c;*/                                                                                                                                    \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                       \
+			if(zb->depth_write)pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		og1 += dgdx;                                                                                                                                           \
@@ -130,9 +130,10 @@
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
-			/*pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                             \
-			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));;                                                                                                              \
-			pz[_a] = zz;                                                                                                                                       \
+			/*pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                          \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                       \
+			                                                                                                                                                  \
+			if(zb->depth_write)pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		og1 += dgdx;                                                                                                                                           \
@@ -143,11 +144,11 @@
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                       \
+		/*c = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                                   \
 		if (ZCMPSIMP(zz, pz[_a], _a, c)) {                                                                                                                     \
-			/*pp[_a] = c;*/                                                                                                                                        \
-			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                             \
-			pz[_a] = zz;                                                                                                                                       \
+			/*pp[_a] = c;*/                                                                                                                                    \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                                       \
+			if(zb->depth_write) pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		og1 += dgdx;                                                                                                                                           \
@@ -263,9 +264,9 @@
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		if (ZCMP(zz, pz[_a], _a, 0)) {                                                                                                                         \
-			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, *(PIXEL*)((GLbyte*)texture + (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH))));*/                       \
-			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, *(PIXEL*)((GLbyte*)texture + (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH)))), pp[_a])		\
-			pz[_a] = zz;                                                                                                                                       \
+			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, *(PIXEL*)((GLbyte*)texture + (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH))));*/                   \
+			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, *(PIXEL*)((GLbyte*)texture + (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH)))), pp[_a])         \
+			if(zb->depth_write) pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		s += dsdx;                                                                                                                                             \
@@ -278,9 +279,9 @@
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		c = *(PIXEL*)((GLbyte*)texture + (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH)));                                                             \
 		if (ZCMP(zz, pz[_a], _a, c)) {                                                                                                                         \
-			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, c);*/                                                                                                           \
-			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, c), (pp[_a]));                                                                                                           \
-			pz[_a] = zz;                                                                                                                                       \
+			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, c);*/                                                                                                       \
+			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, c), (pp[_a]));                                                                                          \
+			if(zb->depth_write) pz[_a] = zz;                                                                                                                   \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
 		s += dsdx;                                                                                                                                             \
@@ -353,43 +354,5 @@
 
 #endif // if 1
 
+// Not maintained by Gek
 
-//Not maintained by Gek
-
-#if 0
-
-/* slow but exact version (only there for reference, incorrect for 24
-   bits) */
-
-void ZB_fillTriangleMappingPerspective(ZBuffer *zb,
-                            ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
-{
-    PIXEL *texture;
-
-#define INTERP_Z
-#define INTERP_STZ
-
-#define DRAW_INIT()                                                                                                                                            \
-	{ texture = zb->current_texture; }
-
-#define PUT_PIXEL(_a)                                                                                                                                          \
-	{                                                                                                                                                          \
-		GLfloat zinv;                                                                                                                                          \
-		GLint s, t;                                                                                                                                            \
-		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		if (ZCMP(zz, pz[_a], _a)) {                                                                                                                            \
-			zinv = 1.0 / (GLfloat)z;                                                                                                                           \
-			s = (GLint)(sz * zinv);                                                                                                                            \
-			t = (GLint)(tz * zinv);                                                                                                                            \
-			pp[_a] = texture[((t & 0x3FC00000) | s) >> 14];                                                                                                    \
-			pz[_a] = zz;                                                                                                                                       \
-		}                                                                                                                                                      \
-		z += dzdx;                                                                                                                                             \
-		sz += dszdx;                                                                                                                                           \
-		tz += dtzdx;                                                                                                                                           \
-	}
-
-#include "ztriangle.h"
-}
-
-#endif