shithub: tinygl

Download patch

ref: 008dd8584d1af5944190b6f67aa98883114243ce
parent: 75d8d14bd96170dd3395a2e1329ba2dd2b920e6e
author: David <gek@katherine>
date: Mon Feb 15 11:31:20 EST 2021

Love for TinyGL

--- a/README.md
+++ b/README.md
@@ -101,6 +101,64 @@
 
 Plot pixel directly to the buffer.
 
+## TOGGLEABLE FEATURES
+
+See `include/zfeatures.h`
+
+Standard OpenGL features that you can disable for extra performance or smaller binary size.
+```c
+#define TGL_FEATURE_ARRAYS         1
+#define TGL_FEATURE_DISPLAYLISTS   1
+#define TGL_FEATURE_POLYGON_OFFSET 1
+#define TGL_FEATURE_POLYGON_STIPPLE 1
+```
+
+Change the dimensions of a polygon stipple pattern, and how it's interpreted.
+
+If you're only ever going to use very small stipple patterns, it's recommended you alter these settings.
+```c
+//A stipple pattern is 128 bytes in size.
+#define TGL_POLYGON_STIPPLE_BYTES 128
+//A stipple pattern is 2^5 (32) bits wide.
+#define TGL_POLYGON_STIPPLE_POW2_WIDTH 5
+//The stipple pattern mask (the last bits of the screen coordinates used for indexing)
+//The default pattern is 32 bits wide and 32 bits tall, or 4 bytes per row and 32 tall, 4 * 32 = 128 bytes.
+#define TGL_POLYGON_STIPPLE_MASK_X 31
+#define TGL_POLYGON_STIPPLE_MASK_Y 31
+```
+
+These features enable you to achieve the effect of `discard` in GLSL shaders.
+
+You can use this to draw objects which only ever need discard-type alpha transparency.
+
+Simply specify a color to use for that purpose. My favorite choice is hideous magenta (0xff00ff)
+
+The color mask is "and'd" with the color before the test, in case you wanted to only test one color (red, for instance)
+
+Note that when changing between bit depths, you will need to alter the NO_COPY_COLOR and NO_DRAW_COLOR if you use those
+features.
+```c
+#define TGL_FEATURE_NO_COPY_COLOR 0
+#define TGL_FEATURE_NO_DRAW_COLOR 0
+#define TGL_FEATURE_FORCE_CLEAR_NO_COPY_COLOR 0
+#define TGL_NO_COPY_COLOR 0xff00ff
+#define TGL_NO_DRAW_COLOR 0xff00ff
+//^ solid debug pink.
+#define TGL_COLOR_MASK 0x00ffffff
+```
+
+Alter the bit depth of rendering. Note that all textures loaded are assumed to be R8G8B8 (in that order, with no Alpha or padding)
+
+Textures are converted internally (see image_util.c) to the renderer's output format.
+
+at the current time, only 16 and 32 bit rendering are maintained and tested regularly.
+```c
+#define TGL_FEATURE_8_BITS         0
+#define TGL_FEATURE_24_BITS        0
+//These are the only maintained modes.
+#define TGL_FEATURE_16_BITS        0
+#define TGL_FEATURE_32_BITS        1
+```
 
 ## FIXED BUGS FROM THE ORIGINAL!
 
binary files a/SDL_Examples/texture.png b/SDL_Examples/texture.png differ
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -54,9 +54,11 @@
 
 #else
 
-#error wrong TGL_FEATURE_RENDER_BITS buddy
+#error "wrong TGL_FEATURE_RENDER_BITS buddy"
 
 #endif
+
+
 
 typedef struct {
     int xsize,ysize;
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -7,7 +7,6 @@
 #define TGL_FEATURE_ARRAYS         1
 #define TGL_FEATURE_DISPLAYLISTS   1
 #define TGL_FEATURE_POLYGON_OFFSET 1
-
 #define TGL_FEATURE_POLYGON_STIPPLE 1
 //A stipple pattern is 128 bytes in size.
 #define TGL_POLYGON_STIPPLE_BYTES 128
@@ -14,18 +13,19 @@
 //A stipple pattern is 2^5 (32) bits wide.
 #define TGL_POLYGON_STIPPLE_POW2_WIDTH 5
 //The stipple pattern mask (the last bits of the screen coordinates used for indexing)
-//The default pattern is 32 bits wide and 32 bits tall, or 4 bytes per row and 32 tall, 4 * 32 128 bytes.
+//The default pattern is 32 bits wide and 32 bits tall, or 4 bytes per row and 32 tall, 4 * 32 = 128 bytes.
 #define TGL_POLYGON_STIPPLE_MASK_X 31
 #define TGL_POLYGON_STIPPLE_MASK_Y 31
 
 //These are features useful for integrating TinyGL with other renderers.
 #define TGL_FEATURE_NO_COPY_COLOR 0
+#define TGL_FEATURE_NO_DRAW_COLOR 0
 #define TGL_FEATURE_FORCE_CLEAR_NO_COPY_COLOR 0
 #define TGL_NO_COPY_COLOR 0xff00ff
-//NOTE: fc02fc is what you'll get if you set glColor3f to 1,0,1. 
+#define TGL_NO_DRAW_COLOR 0xff00ff
 //^ solid debug pink.
-#define TGL_COLOR_MASK 0xffffffff
-//^ mask to check for copybuffer. This is configured for the default mode.
+#define TGL_COLOR_MASK 0x00ffffff
+//^ mask to check for while drawing/copying.
 
 /*
  * Matrix of internal and external pixel formats supported. 'Y' means
--- a/src/msghandling.c
+++ b/src/msghandling.c
@@ -1,7 +1,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include "../include/GL/gl.h"
-#define NDEBUG
+//#define NDEBUG
 
 #ifdef NDEBUG
 #define NO_DEBUG_OUTPUT
--- a/src/zline.c
+++ b/src/zline.c
@@ -8,18 +8,27 @@
     GLushort *pz;
     PIXEL *pp;
     GLint zz;
-
+	#if TGL_FEATURE_NO_DRAW_COLOR == 1
+	PIXEL col;
+	#endif
     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)) {
-#if TGL_FEATURE_RENDER_BITS == 24 
-        pp[0]=p->r>>8;
-        pp[1]=p->g>>8;
-        pp[2]=p->b>>8;
+//#if TGL_FEATURE_RENDER_BITS == 24 
+        //pp[0]=p->r>>8;
+        //pp[1]=p->g>>8;
+        //pp[2]=p->b>>8;
+//#else
+#if TGL_FEATURE_NO_DRAW_COLOR == 1
+#define NODRAWTEST(color) ( (color & TGL_COLOR_MASK) != TGL_NO_DRAW_COLOR)
+		col = RGB_TO_PIXEL(p->r, p->g, p->b);
+		if(NODRAWTEST(col))
+			*pp = RGB_TO_PIXEL(p->r, p->g, p->b);
 #else
-	*pp = RGB_TO_PIXEL(p->r, p->g, p->b);
+		*pp = RGB_TO_PIXEL(p->r, p->g, p->b);
 #endif
+//#endif
 	*pz = zz;
     }
 }
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include "msghandling.h"
 #include "../include/zbuffer.h"
 
 
@@ -5,14 +6,9 @@
 
 
 #if TGL_FEATURE_RENDER_BITS == 32
-
-
 #elif TGL_FEATURE_RENDER_BITS == 16
-
 #else
-
-#error "USE 32 BIT MODE!!!"
-
+#error "WRONG MODE!!!"
 #endif
 
 
@@ -25,15 +21,22 @@
 //NOTES                                                           Divide by 8 to get the byte        Get the actual bit
 #define STIPBIT(_a) (zb->stipplepattern[(XSTIP(_a) | (YSTIP<<TGL_POLYGON_STIPPLE_POW2_WIDTH))>>3] & (1<<(XSTIP(_a) & 7)))
 #define STIPTEST(_a) !(zb->dostipple && !STIPBIT(_a))
-#define ZCMP(z,zpix,_a) ((z) >= (zpix) && STIPTEST(_a))
 
+
 #else
 
-#define ZCMP(z,zpix,_a) ((z) >= (zpix))
+#define STIPTEST(_a) (1)
+//#define ZCMP(z,zpix,_a) ((z) >= (zpix))
 
 #endif
 
+#if TGL_FEATURE_NO_DRAW_COLOR == 1
+#define NODRAWTEST(c) ( (c & TGL_COLOR_MASK) != TGL_NO_DRAW_COLOR)
+#else
+#define NODRAWTEST(c) (1)
+#endif
 
+#define ZCMP(z,zpix,_a,c) ((z) >= (zpix) && STIPTEST(_a) && NODRAWTEST(c))
 
 void ZB_fillTriangleFlat(ZBuffer *zb,
 			 ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
@@ -56,7 +59,7 @@
 #define PUT_PIXEL(_a)				\
 {						\
     zz=z >> ZB_POINT_Z_FRAC_BITS;		\
-    if (ZCMP(zz,pz[_a],_a)) {				\
+    if (ZCMP(zz,pz[_a],_a,color)) {				\
       pp[_a]=color;				\
       pz[_a]=zz;				\
     }						\
@@ -75,8 +78,8 @@
 void ZB_fillTriangleSmooth(ZBuffer *zb,
 			   ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
-#if TGL_FEATURE_RENDER_BITS == 16
-//	GLint _drgbdx;
+#if TGL_FEATURE_NO_DRAW_COLOR == 1
+	PIXEL c;
 #endif
 //GLuint color;
 #define INTERP_Z
@@ -91,33 +94,39 @@
   	\
 }
 
-#define PUT_PIXEL(_a)				\
-{						\
-    zz=z >> ZB_POINT_Z_FRAC_BITS;		\
-    if (ZCMP(zz,pz[_a],_a)) {				\
-      pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);\
-      pz[_a]=zz;				\
-    }\
-    z+=dzdx;					\
-    og1+=dgdx;					\
-    or1+=drdx;					\
-    ob1+=dbdx;					\
+#if TGL_FEATURE_NO_DRAW_COLOR != 1
+#define PUT_PIXEL(_a)													\
+{																		\
+    zz=z >> ZB_POINT_Z_FRAC_BITS;										\
+    if (ZCMP(zz,pz[_a],_a,RGB_TO_PIXEL(or1, og1, ob1))) {				\
+      pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);								\
+      pz[_a]=zz;								\
+    }											\
+    z+=dzdx;									\
+    og1+=dgdx;									\
+    or1+=drdx;									\
+    ob1+=dbdx;									\
 }
-
+#else 
+#define PUT_PIXEL(_a)						\
+{											\
+    zz=z >> ZB_POINT_Z_FRAC_BITS;			\
+    c = RGB_TO_PIXEL(or1, og1, ob1);		\
+    if (ZCMP(zz,pz[_a],_a,c)) {				\
+      pp[_a] = c;							\
+      pz[_a]=zz;							\
+    }										\
+    z+=dzdx;								\
+    og1+=dgdx;								\
+    or1+=drdx;								\
+    ob1+=dbdx;								\
+}
+#endif
 //END OF 32 bit mode 
 #elif TGL_FEATURE_RENDER_BITS == 16
 
 
-/*
-#define DRAW_INIT()                                       \
-{                                                         \
-_drgbdx=(SAR_RND_TO_ZERO(drdx, 6) << 22) & 0xFFC00000;     \
-_drgbdx|=SAR_RND_TO_ZERO(dgdx,5) & 0x000007FF;             \
-_drgbdx|=(SAR_RND_TO_ZERO(dbdx,7) << 12) & 0x001FF000;     \
-}
-*/
 
-
 #define DRAW_INIT()				\
 {						\
   	\
@@ -124,20 +133,8 @@
 }
 
 
-/*#define PUT_PIXEL(_a)				\
-{						\
-    zz=z >> ZB_POINT_Z_FRAC_BITS;		\
-    if (ZCMP(zz,pz[_a],_a)) {				\
-      tmp=rgb&0xF81F07E0;				\
-      pp[_a] = tmp | (tmp >> 16);\
-      pz[_a]=zz;				\
-    }\
-    z+=dzdx;					\
-    rgb=(rgb+drgbdx) & ( ~ 0x00200800);\
-}
-*/
 
-
+/*
 #define PUT_PIXEL(_a)				\
 {						\
     zz=z >> ZB_POINT_Z_FRAC_BITS;		\
@@ -150,44 +147,54 @@
     or1+=drdx;					\
     ob1+=dbdx;					\
 }
-/*
-#define DRAW_LINE()							   \
-{									   \
-  register GLushort *pz;					   \
-  register PIXEL *pp;					   \
-  register GLuint tmp,z,zz,rgb,drgbdx;				   \
-  register GLint n;							   \
-  n=(x2 >> 16) - x1;							   \
-  pp=pp1+x1;								   \
-  pz=pz1+x1;								   \
-  z=z1;									   \
-  rgb=(r1 << 16) & 0xFFC00000;						   \
-  rgb|=(g1 >> 5) & 0x000007FF;						   \
-  rgb|=(b1 << 5) & 0x001FF000;						   \
-  drgbdx=_drgbdx;							   \
-  while (n>=3) {							   \
-    PUT_PIXEL(0);							   \
-    PUT_PIXEL(1);							   \
-    PUT_PIXEL(2);							   \
-    PUT_PIXEL(3);							   \
-    pz+=4;								   \
-    pp+=4;								   \
-    n-=4;								   \
-  }									   \
-  while (n>=0) {							   \
-    PUT_PIXEL(0);							   \
-    pz+=1;								   \
-    pp+=1;								   \
-    n-=1;								   \
-  }									   \
+*/
+
+#if TGL_FEATURE_NO_DRAW_COLOR != 1
+#define PUT_PIXEL(_a)													\
+{																		\
+    zz=z >> ZB_POINT_Z_FRAC_BITS;										\
+    if (ZCMP(zz,pz[_a],_a,0)) {											\
+      pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);								\
+      pz[_a]=zz;								\
+    }											\
+    z+=dzdx;									\
+    og1+=dgdx;									\
+    or1+=drdx;									\
+    ob1+=dbdx;									\
 }
+#else 
+#define PUT_PIXEL(_a)						\
+{											\
+    zz=z >> ZB_POINT_Z_FRAC_BITS;			\
+    c = RGB_TO_PIXEL(or1, og1, ob1);		\
+    if (ZCMP(zz,pz[_a],_a,c)) {				\
+      pp[_a] = c;							\
+      pz[_a]=zz;							\
+    }										\
+    z+=dzdx;								\
+    og1+=dgdx;								\
+    or1+=drdx;								\
+    ob1+=dbdx;								\
+}
+#endif
 
-*/
 #endif 
 //^ End of 16 bit mode stuff
 #include "ztriangle.h"
 } //EOF smooth fill triangle
 
+
+//
+//
+//			TEXTURE MAPPED TRIANGLES
+//               Section_Header
+//
+//
+//
+//
+
+
+
 void ZB_setTexture(ZBuffer *zb,PIXEL *texture)
 {
     zb->current_texture=texture;
@@ -198,6 +205,9 @@
 {
     PIXEL *texture;
 
+#if TGL_FEATURE_NO_DRAW_COLOR == 1
+	PIXEL c;
+#endif
 #define INTERP_Z
 #define INTERP_ST
 
@@ -207,11 +217,11 @@
 }
 
 
-
+#if TGL_FEATURE_NO_DRAW_COLOR != 1
 #define PUT_PIXEL(_a)				\
 {						\
    zz=z >> ZB_POINT_Z_FRAC_BITS;		\
-     if (ZCMP(zz,pz[_a],_a)) {				\
+     if (ZCMP(zz,pz[_a],_a,0)) {				\
        pp[_a]=texture[((t & 0x3FC00000) | s) >> 14];	\
        pz[_a]=zz;				\
     }						\
@@ -219,7 +229,20 @@
     s+=dsdx;					\
     t+=dtdx;					\
 }
-
+#else
+#define PUT_PIXEL(_a)				\
+{						\
+   zz=z >> ZB_POINT_Z_FRAC_BITS;		\
+   c = texture[((t & 0x3FC00000) | s) >> 14];\
+     if (ZCMP(zz,pz[_a],_a,c)) {				\
+       pp[_a]=c;	\
+       pz[_a]=zz;				\
+    }						\
+    z+=dzdx;					\
+    s+=dsdx;					\
+    t+=dtdx;					\
+}
+#endif
 #include "ztriangle.h"
 }
 
@@ -237,7 +260,9 @@
 {
     PIXEL *texture;
     GLfloat fdzdx,fndzdx,ndszdx,ndtzdx;
-
+#if TGL_FEATURE_NO_DRAW_COLOR == 1
+	PIXEL c;
+#endif
 #define INTERP_Z
 #define INTERP_STZ
 
@@ -252,11 +277,11 @@
   ndtzdx=NB_INTERP * dtzdx;\
 }
 
-
+#if TGL_FEATURE_NO_DRAW_COLOR != 1
 #define PUT_PIXEL(_a)				\
 {						\
    zz=z >> ZB_POINT_Z_FRAC_BITS;		\
-     if (ZCMP(zz,pz[_a],_a)) {				\
+     if (ZCMP(zz,pz[_a],_a,0)) {				\
        pp[_a]=*(PIXEL *)((GLbyte *)texture+ \
                (((t & 0x3FC00000) | (s & 0x003FC000)) >> (17 - PSZSH)));\
        pz[_a]=zz;				\
@@ -265,7 +290,21 @@
     s+=dsdx;					\
     t+=dtdx;					\
 }
-
+#else
+#define PUT_PIXEL(_a)				\
+{						\
+	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]=c;                      \
+		pz[_a]=zz;				\
+    }						\
+    z+=dzdx;					\
+    s+=dsdx;					\
+    t+=dtdx;					\
+}
+#endif
 
 
 #define DRAW_LINE()				\