shithub: tinygl

Download patch

ref: 5dae18c473720df2a450fd7f708d32b0fe48637c
parent: de7f5d4b7eca59b2f06e4b7ff131a74c72b9515a
author: David <gek@katherine>
date: Tue Feb 16 20:30:33 EST 2021

Preparing to add blending

--- a/include/GL/gl.h
+++ b/include/GL/gl.h
@@ -591,8 +591,11 @@
 	GL_MIN_EXT			= 0x8007,
 	GL_MAX_EXT			= 0x8008,
 	GL_FUNC_ADD_EXT			= 0x8006,
+	GL_FUNC_ADD			= 0x8006,
 	GL_FUNC_SUBTRACT_EXT		= 0x800A,
+	GL_FUNC_SUBTRACT		= 0x800A,
 	GL_FUNC_REVERSE_SUBTRACT_EXT	= 0x800B,
+	GL_FUNC_REVERSE_SUBTRACT	= 0x800B,
 	GL_BLEND_COLOR_EXT		= 0x8005,
 
 	/* GL_EXT_polygon_offset */
@@ -672,12 +675,13 @@
 
 /* some types */
 
-typedef int		GLenum;
+
 typedef void		GLvoid;
 typedef unsigned char	GLboolean;
 typedef signed char	GLbyte;		/* 1-byte signed */
 typedef short		GLshort;	/* 2-byte signed */
 typedef int		GLint;		/* 4-byte signed */
+typedef GLint	GLenum; /* Same as GLint */
 typedef unsigned char	GLubyte;	/* 1-byte unsigned */
 typedef unsigned short	GLushort;	/* 2-byte unsigned */
 typedef unsigned int	GLuint;		/* 4-byte unsigned */
@@ -851,14 +855,14 @@
 
 /* opengl 1.2 polygon offset */
 void glPolygonOffset(GLfloat factor, GLfloat units);
-
+void glBlendFunc(GLint, GLint);
+void glBlendEquation(GLenum mode);
 /* not implemented, just added to compile  */
   /*
 inline void glPointSize(GLfloat) {}
 inline void glLineWidth(GLfloat) {}
-inline void glDeleteLists(GLint, GLint) {}
 inline void glDepthFunc(GLint) {}
-inline void glBlendFunc(GLint, GLint) {}
+
 inline void glTexEnvf(GLint, GLint, GLint) {}
 inline void glOrtho(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat){}
 inline void glVertex2i(GLint,GLint) {}
@@ -867,10 +871,9 @@
 inline void glFogfv(GLint, const GLfloat*) {}
 inline void glFogf(GLint, GLfloat) {}
 inline void glRasterPos2f(GLfloat, GLfloat) {}
-inline void glPolygonStipple(void*) {}
 inline void glTexParameterf(GLint, GLint, GLint) {};
   */
-void glPolygonStipple(void* a); //TODO: implement
+void glPolygonStipple(void* a);
 /* non compatible functions */
 
 void glDebug(GLint mode);
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -42,9 +42,12 @@
 #define RGB_TO_PIXEL(r,g,b) \
   ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b) >> 8))
 #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)
-typedef unsigned int PIXEL;
+#define GET_BLUEER(p) ((p & 0xff)<<8)
+typedef GLuint PIXEL;
 #define PSZB 4
 #define PSZSH 5
 
@@ -52,11 +55,17 @@
 
 /* 16 bit mode */
 #define RGB_TO_PIXEL(r,g,b) (((r) & 0xF800) | (((g) >> 5) & 0x07E0) | ((b) >> 11))
+
 #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 unsigned short PIXEL;
+typedef GLushort PIXEL;
 #define PSZB 2 
 #define PSZSH 4 
 
@@ -78,59 +87,95 @@
 #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;
+	}\
+}
+#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
+
+
 typedef struct {
-    int xsize,ysize;
-    int linesize; /* line size, in bytes */
-    int mode;
+    GLint xsize,ysize;
+    GLint linesize; /* line size, in bytes */
+    GLint mode;
     
-    unsigned short *zbuf;
+    GLushort *zbuf;
     PIXEL *pbuf;
-    int frame_buffer_allocated;
+    GLint frame_buffer_allocated;
     
      
-    int nb_colors;
+    GLint nb_colors;
     unsigned char *dctable;
-    int *ctable;
+    GLint *ctable;
     PIXEL *current_texture;
     /* opengl polygon stipple*/
-    int dostipple;
+    GLint dostipple;
 #if TGL_FEATURE_POLYGON_STIPPLE == 1
     unsigned char stipplepattern[TGL_POLYGON_STIPPLE_BYTES];
+
+    /* opengl blending */
+    
 #endif
+	GLenum blendeq, sfactor, dfactor;
+    GLint enable_blend;
 } ZBuffer;
 
 typedef struct {
-  int x,y,z;     /* integer coordinates in the zbuffer */
-  int s,t;       /* coordinates for the mapping */
-  int r,g,b;     /* color indexes */
+  GLint x,y,z;     /* integer coordinates in the zbuffer */
+  GLint s,t;       /* coordinates for the mapping */
+  GLint r,g,b;     /* color indexes */
   
-  float sz,tz;   /* temporary coordinates for mapping */
+  GLfloat sz,tz;   /* temporary coordinates for mapping */
 } ZBufferPoint;
 
 /* zbuffer.c */
 
 ZBuffer *ZB_open(int xsize,int ysize,int mode,
-		 int nb_colors,
+		 GLint nb_colors,
 		 unsigned char *color_indexes,
-		 int *color_table,
+		 GLint *color_table,
 		 void *frame_buffer);
 
 
 void ZB_close(ZBuffer *zb);
 
-void ZB_resize(ZBuffer *zb,void *frame_buffer,int xsize,int ysize);
-void ZB_clear(ZBuffer *zb,int clear_z,int z,
-	      int clear_color,int r,int g,int b);
+void ZB_resize(ZBuffer *zb,void *frame_buffer,GLint xsize,GLint ysize);
+void ZB_clear(ZBuffer *zb,GLint clear_z,GLint z,
+	      GLint clear_color,GLint r,GLint g,GLint b);
 /* linesize is in BYTES */
-void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,int linesize);
+void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,GLint linesize);
 
 /* zdither.c */
 
-void ZB_initDither(ZBuffer *zb,int nb_colors,
-		   unsigned char *color_indexes,int *color_table);
+void ZB_initDither(ZBuffer *zb,GLint nb_colors,
+		   unsigned char *color_indexes,GLint *color_table);
 void ZB_closeDither(ZBuffer *zb);
 void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest,
-			  int linesize);
+			  GLint linesize);
 
 /* zline.c */
 
@@ -162,7 +207,7 @@
 
 /* memory.c */
 void gl_free(void *p);
-void *gl_malloc(int size);
-void *gl_zalloc(int size);
+void *gl_malloc(GLint size);
+void *gl_zalloc(GLint size);
 
 #endif /* _tgl_zbuffer_h_ */
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -10,6 +10,7 @@
 //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
 //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/init.c
+++ b/src/init.c
@@ -133,6 +133,12 @@
 	/* textures */
 	glInitTextures(c);
 
+	/* blending */
+	c->zb->enable_blend = 0;
+	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;
--- a/src/misc.c
+++ b/src/misc.c
@@ -47,7 +47,27 @@
 		c->viewport.updated = 1;
 	}
 }
-
+void glBlendFunc(GLenum sfactor, GLenum dfactor){
+	GLParam p[3];
+	p[0].op = OP_BlendFunc;
+	p[1].i = sfactor;
+	p[2].i = dfactor;
+	gl_add_op(p);
+	return;
+}
+void glopBlendFunc(GLContext* c, GLParam* p){
+	c->zb->sfactor = p[1].i;
+	c->zb->dfactor = p[2].i;
+}
+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 glopEnableDisable(GLContext* c, GLParam* p) {
 	GLint code = p[1].i;
 	GLint v = p[2].i;
@@ -64,6 +84,9 @@
 		break;
 	case GL_TEXTURE_2D:
 		c->texture_2d_enabled = v;
+		break;
+	case GL_BLEND:
+		c->zb->enable_blend = v;
 		break;
 	case GL_NORMALIZE:
 		c->normalize_enabled = v;
--- a/src/opinfo.h
+++ b/src/opinfo.h
@@ -68,8 +68,13 @@
 /* opengl 1.1 polygon offset */
 ADD_OP(PolygonOffset, 2, "%f %f")
 
+/* blending */
+ADD_OP(BlendEquation, 1, "%d")
+ADD_OP(BlendFunc, 2, "%d %d")
 /* Gek's Added Functions */
 ADD_OP(PlotPixel, 2, "%d %d")
 ADD_OP(TextSize, 1, "%d")
+
+
 
 #undef ADD_OP
--- a/src/zgl.h
+++ b/src/zgl.h
@@ -260,6 +260,9 @@
 	GLfloat offset_units;
 	GLint offset_states;
 
+	/* opengl blending */
+	//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;
--- a/src/zline.c
+++ b/src/zline.c
@@ -3,31 +3,20 @@
 
 #define ZCMP(z, zpix) ((z) >= (zpix))
 
+/* TODO: Implement blending for lines.*/
+
+
 void ZB_plot(ZBuffer* zb, ZBufferPoint* p) {
 	GLushort* pz;
 	PIXEL* pp;
 	GLint zz;
-#if TGL_FEATURE_NO_DRAW_COLOR == 1
-	PIXEL col;
-#endif
+//	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)) {
-//#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);
-#endif
-		//#endif
+		//*pp = 
+		TGL_BLEND_FUNC_RGB(p->r, p->g, p->b, (*pp))
 		*pz = zz;
 	}
 }
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -46,7 +46,7 @@
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		if (ZCMPSIMP(zz, pz[_a], _a, color)) {                                                                                                                 \
-			pp[_a] = color;                                                                                                                                    \
+			TGL_BLEND_FUNC(color, (pp[_a]))/*pp[_a] = color;*/                                                                                                                                  \
 			pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -78,8 +78,9 @@
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		if (ZCMPSIMP(zz, pz[_a], _a, RGB_TO_PIXEL(or1, og1, ob1))) {                                                                                           \
-			pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);                                                                                                              \
+		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;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -91,9 +92,10 @@
 #define PUT_PIXEL(_a)                                                                                                                                          \
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
-		c = RGB_TO_PIXEL(or1, og1, ob1);                                                                                                                       \
-		if (ZCMPSIMP(zz, pz[_a], _a, c)) {                                                                                                                     \
-			pp[_a] = c;                                                                                                                                        \
+		/*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;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -128,7 +130,8 @@
 	{                                                                                                                                                          \
 		zz = z >> ZB_POINT_Z_FRAC_BITS;                                                                                                                        \
 		if (ZCMPSIMP(zz, pz[_a], _a, 0)) {                                                                                                                     \
-			pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);                                                                                                              \
+			/*pp[_a] = RGB_TO_PIXEL(or1, og1, ob1);*/                                                                                                             \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));;                                                                                                              \
 			pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -140,9 +143,10 @@
 #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;                                                                                                                                        \
+			/*pp[_a] = c;*/                                                                                                                                        \
+			TGL_BLEND_FUNC_RGB(or1, og1, ob1, (pp[_a]));                                                                                             \
 			pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -259,7 +263,8 @@
 	{                                                                                                                                                          \
 		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))));                       \
+			/*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;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -273,7 +278,8 @@
 		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);                                                                                                           \
+			/*pp[_a] = RGB_MIX_FUNC(or1, og1, ob1, c);*/                                                                                                           \
+			TGL_BLEND_FUNC(RGB_MIX_FUNC(or1, og1, ob1, c), (pp[_a]));                                                                                                           \
 			pz[_a] = zz;                                                                                                                                       \
 		}                                                                                                                                                      \
 		z += dzdx;                                                                                                                                             \
@@ -346,6 +352,9 @@
 }
 
 #endif // if 1
+
+
+//Not maintained by Gek
 
 #if 0