dos3d

changeset 9:bce78aaafc68

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Nov 2011 03:59:48 +0200
parents 569d62294ae3
children 059bb38506b3 0909996838ff
files dosemu/conio.h dosemu/dosemu.c src/mglgen.c src/mglimpl.h src/mglrast.c src/mingl.c src/mingl.h src/scantmpl.h src/test.c
diffstat 9 files changed, 184 insertions(+), 55 deletions(-) [+]
line diff
     1.1 --- a/dosemu/conio.h	Mon Nov 21 12:45:18 2011 +0200
     1.2 +++ b/dosemu/conio.h	Sat Nov 26 03:59:48 2011 +0200
     1.3 @@ -2,6 +2,6 @@
     1.4  #define CONIO_H_
     1.5  
     1.6  int kbhit(void);
     1.7 -char getch(void);
     1.8 +int getch(void);
     1.9  
    1.10  #endif	/* CONIO_H_ */
     2.1 --- a/dosemu/dosemu.c	Mon Nov 21 12:45:18 2011 +0200
     2.2 +++ b/dosemu/dosemu.c	Sat Nov 26 03:59:48 2011 +0200
     2.3 @@ -38,16 +38,21 @@
     2.4  void set_video_mode(int mode)
     2.5  {
     2.6  	int resx = 320, resy = 200;
     2.7 +	unsigned int sdl_flags = SDL_HWPALETTE;
     2.8  
     2.9  	if(getenv("DOSEMU_DOUBLESIZE")) {
    2.10  		resx *= 2;
    2.11  		resy *= 2;
    2.12  	}
    2.13  
    2.14 +	if(getenv("DOSEMU_FULLSCREEN")) {
    2.15 +		sdl_flags |= SDL_FULLSCREEN;
    2.16 +	}
    2.17 +
    2.18  	switch(mode) {
    2.19  	case 0x13:
    2.20  		SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    2.21 -		if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, SDL_HWPALETTE))) {
    2.22 +		if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) {
    2.23  			fprintf(stderr, "failed to set video mode\n");
    2.24  			abort();
    2.25  		}
    2.26 @@ -120,9 +125,9 @@
    2.27  	return keybev != 0;
    2.28  }
    2.29  
    2.30 -char getch(void)
    2.31 +int getch(void)
    2.32  {
    2.33 -	char res;
    2.34 +	int res;
    2.35  
    2.36  	while(!keybev) {
    2.37  		SDL_Event ev;
     3.1 --- a/src/mglgen.c	Mon Nov 21 12:45:18 2011 +0200
     3.2 +++ b/src/mglgen.c	Sat Nov 26 03:59:48 2011 +0200
     3.3 @@ -27,40 +27,40 @@
     3.4  	mgl_begin(MGL_QUADS);
     3.5  	/* front */
     3.6  	mgl_normal(0, 0, 1);
     3.7 -	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, hsz);
     3.8 -	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, -hsz, hsz);
     3.9 -	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, -hsz, hsz);
    3.10 -	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, hsz);
    3.11 +	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, hsz);
    3.12 +	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, hsz);
    3.13 +	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, hsz);
    3.14 +	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, hsz);
    3.15  	/* back */
    3.16  	mgl_normal(0, 0, -1);
    3.17 -	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, hsz, -hsz);
    3.18 -	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, -hsz);
    3.19 -	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.20 -	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, hsz, -hsz);
    3.21 +	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, -hsz);
    3.22 +	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, -hsz);
    3.23 +	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.24 +	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, -hsz);
    3.25  	/* right */
    3.26  	mgl_normal(1, 0, 0);
    3.27 -	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, hsz, hsz);
    3.28 -	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, hsz);
    3.29 -	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, -hsz, -hsz);
    3.30 -	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, -hsz);
    3.31 +	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, hsz, hsz);
    3.32 +	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
    3.33 +	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, -hsz, -hsz);
    3.34 +	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
    3.35  	/* left */
    3.36  	mgl_normal(-1, 0, 0);
    3.37 -	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, -hsz);
    3.38 -	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.39 -	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, hsz);
    3.40 -	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, hsz, hsz);
    3.41 +	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
    3.42 +	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.43 +	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
    3.44 +	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, hsz, hsz);
    3.45  	/* top */
    3.46  	mgl_normal(0, 1, 0);
    3.47 -	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, -hsz);
    3.48 -	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, hsz);
    3.49 -	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, hsz);
    3.50 -	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, -hsz);
    3.51 +	mgl_texcoord2f(0, 0); mgl_vertex3f(-hsz, hsz, -hsz);
    3.52 +	mgl_texcoord2f(0, 1); mgl_vertex3f(-hsz, hsz, hsz);
    3.53 +	mgl_texcoord2f(1, 1); mgl_vertex3f(hsz, hsz, hsz);
    3.54 +	mgl_texcoord2f(1, 0); mgl_vertex3f(hsz, hsz, -hsz);
    3.55  	/* bottom */
    3.56  	mgl_normal(0, -1, 0);
    3.57 -	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, -hsz);
    3.58 -	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, hsz);
    3.59 -	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, hsz);
    3.60 -	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.61 +	mgl_texcoord2f(0, 0); mgl_vertex3f(hsz, -hsz, -hsz);
    3.62 +	mgl_texcoord2f(0, 1); mgl_vertex3f(hsz, -hsz, hsz);
    3.63 +	mgl_texcoord2f(1, 1); mgl_vertex3f(-hsz, -hsz, hsz);
    3.64 +	mgl_texcoord2f(1, 0); mgl_vertex3f(-hsz, -hsz, -hsz);
    3.65  	mgl_end();
    3.66  }
    3.67  
     4.1 --- a/src/mglimpl.h	Mon Nov 21 12:45:18 2011 +0200
     4.2 +++ b/src/mglimpl.h	Sat Nov 26 03:59:48 2011 +0200
     4.3 @@ -21,6 +21,13 @@
     4.4  #define MATRIX_STACK_SIZE	8
     4.5  #define MAX_LIGHTS			4
     4.6  
     4.7 +#define ZTILE_SIZE			16384
     4.8 +#define ZTILE_SHIFT			14
     4.9 +#define ZTILE_MASK			0x3fff
    4.10 +
    4.11 +#define ZTILE(x)			(((x) & ~ZTILE_MASK) >> ZTILE_SHIFT)
    4.12 +#define ZTILE_OFFS(x)		((x) & ZTILE_MASK)
    4.13 +
    4.14  #define ROUND(x)	((x) >= 0.0 ? (x) + 0.5 : (x) - 0.5)
    4.15  
    4.16  typedef struct {
    4.17 @@ -71,7 +78,8 @@
    4.18  struct framebuffer {
    4.19  	int width, height;
    4.20  	unsigned char *pixels;
    4.21 -	unsigned short **zbuf;	/* zbuffer broken in 64k tiles */
    4.22 +	unsigned short **zbuf;	/* zbuffer broken in ZTILE_SIZE tiles */
    4.23 +	int num_ztiles;
    4.24  };
    4.25  
    4.26  
     5.1 --- a/src/mglrast.c	Mon Nov 21 12:45:18 2011 +0200
     5.2 +++ b/src/mglrast.c	Sat Nov 26 03:59:48 2011 +0200
     5.3 @@ -18,6 +18,7 @@
     5.4  #include <stdio.h>
     5.5  #include <stdlib.h>
     5.6  #include <string.h>
     5.7 +#include <limits.h>
     5.8  #include <assert.h>
     5.9  #include "mingl.h"
    5.10  #include "mglimpl.h"
     6.1 --- a/src/mingl.c	Mon Nov 21 12:45:18 2011 +0200
     6.2 +++ b/src/mingl.c	Sat Nov 26 03:59:48 2011 +0200
     6.3 @@ -84,8 +84,19 @@
     6.4  
     6.5  void mgl_free(void)
     6.6  {
     6.7 +	int i;
     6.8 +
     6.9  	mgl_rast_cleanup();
    6.10  	free(fb.pixels);
    6.11 +	fb.pixels = 0;
    6.12 +
    6.13 +	if(fb.zbuf) {
    6.14 +		for(i=0; i<fb.num_ztiles; i++) {
    6.15 +			free(fb.zbuf[i]);
    6.16 +		}
    6.17 +		free(fb.zbuf);
    6.18 +		fb.zbuf = 0;
    6.19 +	}
    6.20  }
    6.21  
    6.22  unsigned char *mgl_framebuffer(void)
    6.23 @@ -98,6 +109,34 @@
    6.24  	memset(fb.pixels, cidx, fb.width * fb.height);
    6.25  }
    6.26  
    6.27 +void mgl_clear_depth(void)
    6.28 +{
    6.29 +	int i;
    6.30 +
    6.31 +	if(!fb.zbuf) {
    6.32 +		long num_pixels = (long)fb.width * (long)fb.height;
    6.33 +		fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
    6.34 +
    6.35 +		if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
    6.36 +			fprintf(stderr, "failed to allocate ztile array\n");
    6.37 +			abort();
    6.38 +		}
    6.39 +
    6.40 +		for(i=0; i<fb.num_ztiles; i++) {
    6.41 +			if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
    6.42 +				fprintf(stderr, "failed to allocate ztile %d\n", i);
    6.43 +				abort();
    6.44 +			}
    6.45 +			memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
    6.46 +		}
    6.47 +		return;
    6.48 +	}
    6.49 +
    6.50 +	for(i=0; i<fb.num_ztiles; i++) {
    6.51 +		memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
    6.52 +	}
    6.53 +}
    6.54 +
    6.55  void mgl_enable(unsigned int bit)
    6.56  {
    6.57  	st.flags |= bit;
     7.1 --- a/src/mingl.h	Mon Nov 21 12:45:18 2011 +0200
     7.2 +++ b/src/mingl.h	Sat Nov 26 03:59:48 2011 +0200
     7.3 @@ -48,6 +48,7 @@
     7.4  unsigned char *mgl_framebuffer(void);
     7.5  
     7.6  void mgl_clear(int cidx);
     7.7 +void mgl_clear_depth(void);
     7.8  
     7.9  void mgl_enable(unsigned int bit);
    7.10  void mgl_disable(unsigned int bit);
     8.1 --- a/src/scantmpl.h	Mon Nov 21 12:45:18 2011 +0200
     8.2 +++ b/src/scantmpl.h	Sat Nov 26 03:59:48 2011 +0200
     8.3 @@ -66,17 +66,41 @@
     8.4  	start = (int)ROUND(v0->pos.y);
     8.5  	end = (int)ROUND(v1->pos.y);
     8.6  
     8.7 -	x = v0->pos.x;
     8.8 +	if(start >= 0) {
     8.9 +
    8.10 +		x = v0->pos.x;
    8.11  #ifdef INTERP_DEPTH
    8.12 -	z = v0->pos.z;
    8.13 +		z = v0->pos.z;
    8.14  #endif
    8.15  #ifdef INTERP_ENERGY
    8.16 -	e = v0->energy;
    8.17 +		e = v0->energy;
    8.18  #endif
    8.19  #ifdef INTERP_TEX
    8.20 -	u = v0->tc.x;
    8.21 -	v = v0->tc.y;
    8.22 +		u = v0->tc.x;
    8.23 +		v = v0->tc.y;
    8.24  #endif
    8.25 +	} else {
    8.26 +		float lines = -v0->pos.y;
    8.27 +
    8.28 +		x = v0->pos.x + dfdx * lines;
    8.29 +#ifdef INTERP_DEPTH
    8.30 +		z = v0->pos.z + dfdz * lines;
    8.31 +#endif
    8.32 +#ifdef INTERP_ENERGY
    8.33 +		e = v0->energy + dfde * lines;
    8.34 +#endif
    8.35 +#ifdef INTERP_TEX
    8.36 +		u = v0->tc.x + dfdu * lines;
    8.37 +		v = v0->tc.y + dfdv * lines;
    8.38 +#endif
    8.39 +		start = 0;
    8.40 +	}
    8.41 +
    8.42 +	if(end >= fb->height) {
    8.43 +		end = fb->height - 1;
    8.44 +	}
    8.45 +
    8.46 +
    8.47  	for(i=start; i<end; i++) {
    8.48  		edge[i].pos.x = x;
    8.49  		x += dfdx;
    8.50 @@ -124,7 +148,6 @@
    8.51  
    8.52  	x0 = (int)ROUND(vleft[y].pos.x);
    8.53  	x1 = (int)ROUND(vright[y].pos.x);
    8.54 -	len = x1 - x0;
    8.55  
    8.56  	if(x1 < x0) {
    8.57  		if(st->flags & MGL_CULL_FACE) {
    8.58 @@ -133,7 +156,6 @@
    8.59  		tmp = x0;
    8.60  		x0 = x1;
    8.61  		x1 = tmp;
    8.62 -		len = -len;
    8.63  
    8.64  		left = vright;
    8.65  		right = vleft;
    8.66 @@ -142,13 +164,15 @@
    8.67  		right = vright;
    8.68  	}
    8.69  
    8.70 -	if(x0 < 0) x0 = 0;
    8.71 -	if(x1 >= fb->width) x1 = fb->width - 1;
    8.72 -
    8.73 -	assert(len >= 0);
    8.74 +	if(x1 >= fb->width) {
    8.75 +		x1 = fb->width - 1;
    8.76 +	}
    8.77  
    8.78  	cidx = left[y].cidx;
    8.79  #if !defined(INTERP_DEPTH) && !defined(INTERP_ENERGY) && !defined(INTERP_TEX)
    8.80 +	if(x0 < 0) x0 = 0;
    8.81 +	len = x1 - x0;
    8.82 +	assert(len >= 0);
    8.83  	/* no interpolation at all, just memset the whole scanline */
    8.84  	memset(sline + x0, cidx + left[y].energy * st->col_range, len);
    8.85  #else
    8.86 @@ -160,29 +184,72 @@
    8.87  		return;
    8.88  	}
    8.89  
    8.90 +	if(x0 >= 0) {
    8.91  #ifdef INTERP_DEPTH
    8.92 -	z = left[y].pos.z;
    8.93 -	dz = right[y].pos.z - z;
    8.94 -	dfdz = dz / dx;
    8.95 +		z = left[y].pos.z;
    8.96 +		dz = right[y].pos.z - z;
    8.97 +		dfdz = dz / dx;
    8.98  #endif
    8.99  #ifdef INTERP_ENERGY
   8.100 -	e = left[y].energy;
   8.101 -	de = right[y].energy - e;
   8.102 -	dfde = de / dx;
   8.103 +		e = left[y].energy;
   8.104 +		de = right[y].energy - e;
   8.105 +		dfde = de / dx;
   8.106  #endif
   8.107  #ifdef INTERP_TEX
   8.108 -	u = left[y].tc.x;
   8.109 -	v = left[y].tc.y;
   8.110 -	du = right[y].tc.x - u;
   8.111 -	dv = right[y].tc.y - v;
   8.112 -	dfdu = du / dx;
   8.113 -	dfdv = dv / dx;
   8.114 +		u = left[y].tc.x;
   8.115 +		v = left[y].tc.y;
   8.116 +		du = right[y].tc.x - u;
   8.117 +		dv = right[y].tc.y - v;
   8.118 +		dfdu = du / dx;
   8.119 +		dfdv = dv / dx;
   8.120  #endif
   8.121 +	} else {
   8.122 +		float dist = -left[y].pos.x;
   8.123 +
   8.124 +#ifdef INTERP_DEPTH
   8.125 +		dz = right[y].pos.z - left[y].pos.z;
   8.126 +		dfdz = dz / dx;
   8.127 +		z = left[y].pos.z + dfdz * dist;
   8.128 +#endif
   8.129 +#ifdef INTERP_ENERGY
   8.130 +		de = right[y].energy - left[y].energy;
   8.131 +		dfde = de / dx;
   8.132 +		e = left[y].energy + dfde * dist;
   8.133 +#endif
   8.134 +#ifdef INTERP_TEX
   8.135 +		du = right[y].tc.x - left[y].tc.x;
   8.136 +		dv = right[y].tc.y - left[y].tc.y;
   8.137 +		dfdu = du / dx;
   8.138 +		dfdv = dv / dx;
   8.139 +		u = left[y].tc.x + dfdu * dist;
   8.140 +		v = left[y].tc.y + dfdv * dist;
   8.141 +#endif
   8.142 +		x0 = 0;
   8.143 +	}
   8.144 +
   8.145 +	len = x1 - x0;
   8.146  
   8.147  	for(i=0; i<len; i++) {
   8.148  		int c = cidx;
   8.149  
   8.150  #ifdef INTERP_DEPTH
   8.151 +		int pix = (sline + x0 + i) - fb->pixels;
   8.152 +		unsigned short zval = (unsigned short)(z * USHRT_MAX);
   8.153 +		unsigned short *zptr = fb->zbuf[ZTILE(pix)] + ZTILE_OFFS(pix);
   8.154 +
   8.155 +		if(z < 0.0 || z >= 1.0 || zval > *zptr) {
   8.156 +# ifdef INTERP_TEX
   8.157 +			u += dfdu;
   8.158 +			v += dfdv;
   8.159 +# endif
   8.160 +# ifdef INTERP_ENERGY
   8.161 +			e += dfde;
   8.162 +# endif
   8.163 +			z += dfdz;
   8.164 +			continue;
   8.165 +		}
   8.166 +
   8.167 +		*zptr = zval;
   8.168  		z += dfdz;
   8.169  #endif
   8.170  #ifdef INTERP_TEX
     9.1 --- a/src/test.c	Mon Nov 21 12:45:18 2011 +0200
     9.2 +++ b/src/test.c	Sat Nov 26 03:59:48 2011 +0200
     9.3 @@ -53,7 +53,7 @@
     9.4  enum { CUBE, SPHERE, TORUS, NUM_PRIMS };
     9.5  static int prim = SPHERE;
     9.6  static int auto_rotate = 1;
     9.7 -static float cam_theta, cam_phi;
     9.8 +static float cam_theta, cam_phi, cam_zoom = 4.0;
     9.9  
    9.10  static int mx, my;
    9.11  
    9.12 @@ -154,6 +154,7 @@
    9.13  	}
    9.14  
    9.15  	mgl_enable(MGL_CULL_FACE);
    9.16 +	mgl_enable(MGL_DEPTH_TEST);
    9.17  	mgl_enable(MGL_SMOOTH);
    9.18  	mgl_color_range(grad_range - 1);	/* gradient range */
    9.19  
    9.20 @@ -180,6 +181,7 @@
    9.21  {
    9.22  	float angle = get_msec() / 10.0;
    9.23  	mgl_clear(0);
    9.24 +	mgl_clear_depth();
    9.25  
    9.26  	mgl_matrix_mode(MGL_MODELVIEW);
    9.27  	mgl_load_identity();
    9.28 @@ -190,7 +192,7 @@
    9.29  		mgl_rotate(cam_theta, 0, 1, 0);
    9.30  		mgl_rotate(cam_phi, 1, 0, 0);
    9.31  	}
    9.32 -	mgl_translate(0, 0, -4);
    9.33 +	mgl_translate(0, 0, -cam_zoom);
    9.34  
    9.35  	switch(prim) {
    9.36  	case TORUS:
    9.37 @@ -316,13 +318,19 @@
    9.38  	prev_x = x;
    9.39  	prev_y = y;
    9.40  
    9.41 -	if(bnstate) {
    9.42 +	if(bnstate & MOUSE_LEFT) {
    9.43  		cam_theta += dx;
    9.44  		cam_phi += dy;
    9.45  
    9.46  		if(cam_phi > 90) cam_phi = 90;
    9.47  		if(cam_phi < -90) cam_phi = -90;
    9.48  	}
    9.49 +	if(bnstate & MOUSE_RIGHT) {
    9.50 +		cam_zoom += dy * 0.1;
    9.51 +		if(cam_zoom < 0.0) {
    9.52 +			cam_zoom = 0.0;
    9.53 +		}
    9.54 +	}
    9.55  }
    9.56  
    9.57  static void sighandler(int s)