shithub: tinygl

Download patch

ref: 67a5468e01cce119ddb9080300374ebfb96cb109
parent: fca0ed59da6623f7afec9f4689b3b3e5138fde7e
author: David <gek@katherine>
date: Thu Feb 18 13:34:41 EST 2021

Working on adding glDrawPixels support

--- a/include/GL/gl.h
+++ b/include/GL/gl.h
@@ -30,6 +30,8 @@
 	GL_2_BYTES			= 0x1407,
 	GL_3_BYTES			= 0x1408,
 	GL_4_BYTES			= 0x1409,
+	GL_UNSIGNED_SHORT_5_6_5 = 0x140A,
+	GL_UNSIGNED_INT_8_8_8_8 = 0x140B,
 
 	/* Primitives */
 	GL_LINES			= 0x0001,
@@ -687,7 +689,7 @@
 typedef unsigned int	GLuint;		/* 4-byte unsigned */
 typedef float		GLfloat;	/* single precision float */
 typedef double		GLdouble;	/* double precision float */
-typedef int 		GLsizei;
+typedef GLint 		GLsizei; /* Same as GLint */
 
 
 #if COMPILETIME_TINYGL_COMPAT_TEST == 1
@@ -861,6 +863,17 @@
 
 /* Point Size */
 void glPointSize(GLfloat);
+
+/* Raster rendering */
+void glRasterPos2f(GLfloat, GLfloat);
+void glRasterPos3f(GLfloat, GLfloat, GLfloat);
+void glRasterPos4f(GLfloat, GLfloat, GLfloat, GLfloat);
+
+void glRasterPos2fv(GLfloat* v);
+void glRasterPos3fv(GLfloat* v);
+void glRasterPos4fv(GLfloat* v);
+void glDrawPixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void* data);
+
 /* not implemented, just added to compile  */
   /*
 inline void glPointSize(GLfloat) {}
--- a/src/Makefile
+++ b/src/Makefile
@@ -4,7 +4,7 @@
       misc.o clear.o light.o clip.o select.o get.o \
       zbuffer.o zline.o ztriangle.o \
       zmath.o image_util.o msghandling.o \
-      arrays.o specbuf.o memory.o ztext.o
+      arrays.o specbuf.o memory.o ztext.o zraster.o
 ifdef TINYGL_USE_GLX
 #OBJS += glx.o
 endif
--- a/src/image_util.c
+++ b/src/image_util.c
@@ -16,7 +16,7 @@
 	}
 }
 
-// This actually converts to ABGR!!!
+// This actually converts to ARGB!!!
 // This is the format of the entire engine!!!
 void gl_convertRGB_to_8A8R8G8B(GLuint* pixmap, GLubyte* rgb, GLint xsize, GLint ysize) {
 	GLint i, n;
--- a/src/init.c
+++ b/src/init.c
@@ -223,6 +223,14 @@
 	/* depth test */
 	c->zb->depth_test = 0;
 	c->zb->depth_write = 1;
+
+	/* raster position */
+	c->rasterpos.v[0] = 0;
+	c->rasterpos.v[1] = 0;
+	c->rasterpos.v[2] = 0;
+	c->rasterposvalid = 0;
+	c->pzoomx = 1;
+	c->pzoomy = 1;
 }
 
 void glClose(void) {
--- a/src/opinfo.h
+++ b/src/opinfo.h
@@ -75,6 +75,13 @@
 /* point size */
 ADD_OP(PointSize, 1, "%f")
 
+/* raster position */
+ADD_OP(RasterPos, 4, "%f %f %f %f")
+ADD_OP(PixelZoom, 2, "%f %f")
+/* Draw pixels*/
+/* Width, Height, Data*/
+ADD_OP(DrawPixels, 3, "%d %d %p")
+
 /* Gek's Added Functions */
 ADD_OP(PlotPixel, 2, "%d %d")
 ADD_OP(TextSize, 1, "%d")
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -133,11 +133,11 @@
 /* TODO : handle all cases */
 static inline void gl_vertex_transform(GLContext* c, GLVertex* v) {
 	GLfloat* m;
-	V4* n;
+	
 
 	if (c->lighting_enabled) {
 		/* eye coordinates needed for lighting */
-
+		V4* n;
 		m = &c->matrix_stack_ptr[0]->m[0][0];
 		v->ec.X = (v->coord.X * m[0] + v->coord.Y * m[1] + v->coord.Z * m[2] + m[3]);
 		v->ec.Y = (v->coord.X * m[4] + v->coord.Y * m[5] + v->coord.Z * m[6] + m[7]);
@@ -220,10 +220,6 @@
 	} else {
 		v->color = c->current_color;
 	}
-	/* Added by Gek to fix bug with rendering*/
-	// 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;
 	/* tex coords */
 
 	if (c->texture_2d_enabled) {
--- a/src/zgl.h
+++ b/src/zgl.h
@@ -228,7 +228,6 @@
 
 	/* current vertex state */
 	V4 current_color;
-	// GLuint longcurrent_color[3]; /* precomputed GLinteger color */
 	V4 current_normal;
 	V4 current_tex_coord;
 	GLint current_edge_flag;
@@ -275,6 +274,11 @@
 
 	/* depth test */
 	//Moved to Zbuffer.
+
+	/* raster position */
+	V3 rasterpos;
+	GLubyte rasterposvalid;
+	GLfloat pzoomx, pzoomy;
 } GLContext;
 
 extern GLContext* gl_ctx;
@@ -352,5 +356,8 @@
 	w = w1 * (1.0 + CLIP_EPSILON);
 	return (x < -w) | ((x > w) << 1) | ((y < -w) << 2) | ((y > w) << 3) | ((z < -w) << 4) | ((z > w) << 5);
 }
+
+
+
 
 #endif /* _tgl_zgl_h_ */
--- /dev/null
+++ b/src/zraster.c
@@ -1,0 +1,131 @@
+#include "../include/GL/gl.h"
+#include "../include/zbuffer.h"
+#include "zgl.h"
+#include "msghandling.h"
+
+static inline void gl_vertex_transform_raster(GLContext* c, GLVertex* v) {
+	
+	{
+		/* no eye coordinates needed, no normal */
+		/* NOTE: W = 1 is assumed */
+		GLfloat* m = &c->matrix_model_projection.m[0][0];
+
+		v->pc.X = (v->coord.X * m[0] + v->coord.Y * m[1] + v->coord.Z * m[2] + m[3]);
+		v->pc.Y = (v->coord.X * m[4] + v->coord.Y * m[5] + v->coord.Z * m[6] + m[7]);
+		v->pc.Z = (v->coord.X * m[8] + v->coord.Y * m[9] + v->coord.Z * m[10] + m[11]);
+		if (c->matrix_model_projection_no_w_transform) {
+			v->pc.W = m[15];
+		} else {
+			v->pc.W = (v->coord.X * m[12] + v->coord.Y * m[13] + v->coord.Z * m[14] + m[15]);
+		}
+	}
+
+	v->clip_code = gl_clipcode(v->pc.X, v->pc.Y, v->pc.Z, v->pc.W);
+}
+
+
+void glRasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w){
+	GLParam p[5];
+	p[0].op = OP_RasterPos;
+	p[1].f = x;
+	p[2].f = y;
+	p[3].f = z;
+	p[4].f = w;
+	gl_add_op(p);
+}
+void glopRasterPos(GLContext* c, GLParam* p){
+	GLVertex v;
+	v.coord.X = p[1].f;
+	v.coord.Y = p[2].f;
+	v.coord.Z = p[3].f;
+	v.coord.W = p[4].f;
+	gl_vertex_transform_raster(c, &v);
+	if (v.clip_code == 0)
+		{
+			{
+				GLfloat winv = 1.0 / v.pc.W;
+				v.zp.x = (GLint)(v.pc.X * winv * c->viewport.scale.X + c->viewport.trans.X);
+				v.zp.y = (GLint)(v.pc.Y * winv * c->viewport.scale.Y + c->viewport.trans.Y);
+				v.zp.z = (GLint)(v.pc.Z * winv * c->viewport.scale.Z + c->viewport.trans.Z);
+			}
+			c->rasterpos.v[0] = v.zp.x;
+			c->rasterpos.v[1] = v.zp.y;
+			c->rasterpos.v[2] = v.zp.z;
+			c->rasterposvalid = 1;
+		}
+	else
+		c->rasterposvalid = 0;
+}
+
+void glRasterPos2f(GLfloat x, GLfloat y){glRasterPos4f(x,y,0,1);}
+void glRasterPos3f(GLfloat x, GLfloat y, GLfloat z){glRasterPos4f(x,y,z,1);}
+
+void glRasterPos2fv(GLfloat* v){glRasterPos2f(v[0],v[1]);}
+void glRasterPos3fv(GLfloat* v){glRasterPos3f(v[0],v[1],v[2]);}
+void glRasterPos4fv(GLfloat* v){glRasterPos4f(v[0],v[1],v[2],v[3]);}
+
+
+void glDrawPixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void* data){
+	/* TODO */
+#if TGL_FEATURE_RENDER_BITS == 32
+	if( type != GL_UNSIGNED_INT &&
+		type != GL_UNSIGNED_INT_8_8_8_8 )
+	{
+		tgl_warning("\nERROR: Incorrect type for glDrawPixels. It MUST be GL_UNSIGNED_INT or GL_UNSIGNED_INT_8_8_8_8, A R G B!");
+		return;
+	}
+#elif TGL_FEATURE_RENDER_BITS == 16
+	if( type != GL_UNSIGNED_SHORT &&
+		type != GL_UNSIGNED_SHORT_5_6_5)
+	{
+		tgl_warning("\nERROR: Incorrect type for glDrawPixels. it MUST be GL_UNSIGNED_SHORT or GL_UNSIGNED_SHORT_5_6_5, R5 G6 B5!");
+		return;
+	}
+#else 
+#error "Bad TGL_FEATURE_RENDER_BITS"
+#endif
+	if( format != GL_RGB ){
+			tgl_warning("\nERROR: Incorrect format for glDrawPixels.");
+			return;
+		}
+	GLParam p[6];
+	p[0].op = OP_DrawPixels;
+	p[1].i = width;
+	p[2].i = height;
+	p[3].p = data;
+	gl_add_op(p);
+}
+void glopDrawPixels(GLContext* c, GLParam* p){
+	// p[3]
+	if(!c->rasterposvalid) return;
+	GLint w = p[1].i;
+	GLint h = p[2].i;
+#if TGL_FEATURE_RENDER_BITS == 32
+	unsigned int* d = p[3].p;
+#elif TGL_FEATURE_RENDER_BITS == 16
+	unsigned short* d = p[3].p;
+#else
+#error "bad TGL_FEATURE_RENDER_BITS"
+#endif
+	/*GLint mult = textsize;
+		for (GLint i = 0; i < mult; i++)
+		for (GLint j = 0; j < mult; j++)
+		glPlotPixel(y * mult + i + _x, x * mult + j + _y, p);
+	*/
+}
+
+
+
+
+void glPixelZoom(GLfloat x, GLfloat y){
+	GLParam p[3];
+	p[0].op = OP_PixelZoom;
+	p[1].f = x;
+	p[2].f = y;
+	gl_add_op(p);
+}
+
+void glopPixelZoom(GLContext* c, GLParam* p){
+	c->pzoomx = p[1].f;
+	c->pzoomx = p[2].f;
+}