deepstone

diff src/mingl.c @ 11:0909996838ff

fucked up lighting and enabled key repeat in dosemu
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Nov 2011 05:02:08 +0200
parents bce78aaafc68
children 7b574ba5758e
line diff
     1.1 --- a/src/mingl.c	Sat Nov 26 03:59:48 2011 +0200
     1.2 +++ b/src/mingl.c	Mon Nov 28 05:02:08 2011 +0200
     1.3 @@ -26,6 +26,17 @@
     1.4  
     1.5  #define DOT(a, b)	((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
     1.6  
     1.7 +#define NORMALIZE(v)	\
     1.8 +	do { \
     1.9 +		float mag = sqrt(DOT(v, v)); \
    1.10 +		if(fabs(mag) > 1e-6) { \
    1.11 +			float invmag = 1.0 / mag; \
    1.12 +			(v).x *= invmag; \
    1.13 +			(v).y *= invmag; \
    1.14 +			(v).z *= invmag; \
    1.15 +		} \
    1.16 +	} while(0)
    1.17 +
    1.18  static void transform(vec4_t *res, vec4_t *v, float *mat);
    1.19  static void transform3(vec3_t *res, vec3_t *v, float *mat);
    1.20  static void vertex_proc(struct vertex *vert);
    1.21 @@ -74,8 +85,8 @@
    1.22  
    1.23  	st.col_range = 256;
    1.24  	for(i=0; i<MAX_LIGHTS; i++) {
    1.25 -		st.ldir[i].x = st.ldir[i].y = 0.0f;
    1.26 -		st.ldir[i].z = 1.0f;
    1.27 +		st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
    1.28 +		st.lpos[i].z = 1.0f;
    1.29  		st.lint[i] = 0.0f;
    1.30  	}
    1.31  
    1.32 @@ -173,24 +184,26 @@
    1.33  	st.lint[ltidx] = intens;
    1.34  }
    1.35  
    1.36 -void mgl_light_direction(int ltidx, float x, float y, float z)
    1.37 +void mgl_light_position(int ltidx, float x, float y, float z, float w)
    1.38  {
    1.39 -	vec3_t dir;
    1.40 -	float mag;
    1.41 +	vec4_t pos;
    1.42  	assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
    1.43  
    1.44 -	dir.x = x;
    1.45 -	dir.y = y;
    1.46 -	dir.z = z;
    1.47 -	transform3(&st.ldir[ltidx], &dir, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
    1.48 +	pos.x = x;
    1.49 +	pos.y = y;
    1.50 +	pos.z = z;
    1.51 +	pos.w = w;
    1.52 +	transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
    1.53  
    1.54 -	mag = sqrt(DOT(st.ldir[ltidx], st.ldir[ltidx]));
    1.55 -	if(fabs(mag) < 1e-6) {
    1.56 -		mag = 1.0f;
    1.57 +	if(fabs(st.lpos[ltidx].w) < 1e-6) {
    1.58 +		st.lpos[ltidx].w = 1.0;
    1.59 +
    1.60 +		NORMALIZE(st.lpos[ltidx]);
    1.61 +	} else {
    1.62 +		st.lpos[ltidx].x /= st.lpos[ltidx].w;
    1.63 +		st.lpos[ltidx].y /= st.lpos[ltidx].w;
    1.64 +		st.lpos[ltidx].z /= st.lpos[ltidx].w;
    1.65  	}
    1.66 -	st.ldir[ltidx].x /= mag;
    1.67 -	st.ldir[ltidx].y /= mag;
    1.68 -	st.ldir[ltidx].z /= mag;
    1.69  }
    1.70  
    1.71  void mgl_begin(int prim)
    1.72 @@ -313,7 +326,22 @@
    1.73  
    1.74  			for(i=0; i<MAX_LIGHTS; i++) {
    1.75  				if(st.lint[i] > 1e-6f) {
    1.76 -					float ndotl = DOT(norm, st.ldir[i]);
    1.77 +					float ndotl;
    1.78 +					vec3_t ldir;
    1.79 +
    1.80 +					if(st.lpos[i].w == 0.0) {
    1.81 +						ldir.x = st.lpos[i].x;
    1.82 +						ldir.y = st.lpos[i].y;
    1.83 +						ldir.z = st.lpos[i].z;
    1.84 +					} else {
    1.85 +						ldir.x = st.lpos[i].x - pview.x;
    1.86 +						ldir.y = st.lpos[i].y - pview.y;
    1.87 +						ldir.z = st.lpos[i].z - pview.z;
    1.88 +
    1.89 +						NORMALIZE(ldir);
    1.90 +					}
    1.91 +
    1.92 +					ndotl = DOT(norm, ldir);
    1.93  					if(ndotl < 0.0) {
    1.94  						ndotl = 0.0;
    1.95  					}