dos3d

annotate src/scantmpl.h @ 12:c29a6e024950

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Nov 2011 05:03:22 +0200
parents bce78aaafc68
children 1e9f0b3616fa
rev   line source
nuclear@0 1 /*
nuclear@0 2 256-color 3D graphics hack for real-mode DOS.
nuclear@0 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 4
nuclear@0 5 This program is free software: you can redistribute it and/or modify
nuclear@0 6 it under the terms of the GNU General Public License as published by
nuclear@0 7 the Free Software Foundation, either version 3 of the License, or
nuclear@0 8 (at your option) any later version.
nuclear@0 9
nuclear@0 10 This program is distributed in the hope that it will be useful,
nuclear@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 13 GNU General Public License for more details.
nuclear@0 14
nuclear@0 15 You should have received a copy of the GNU General Public License
nuclear@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@0 17 */
nuclear@0 18 static void SCAN_EDGE(struct vertex *v0, struct vertex *v1)
nuclear@0 19 {
nuclear@0 20 int i, start, end;
nuclear@0 21 float dx, dy, dfdx;
nuclear@0 22 #ifdef INTERP_DEPTH
nuclear@0 23 float z, dz, dfdz;
nuclear@0 24 #endif
nuclear@0 25 #ifdef INTERP_ENERGY
nuclear@0 26 float e, de, dfde;
nuclear@0 27 #endif
nuclear@3 28 #ifdef INTERP_TEX
nuclear@3 29 float u, v, du, dv, dfdu, dfdv;
nuclear@3 30 #endif
nuclear@3 31 float x;
nuclear@0 32 struct vertex *edge;
nuclear@0 33
nuclear@0 34 dy = v1->pos.y - v0->pos.y;
nuclear@0 35 if(dy < 1e-6 && dy > -1e-6) {
nuclear@0 36 return;
nuclear@0 37 }
nuclear@0 38
nuclear@0 39 dx = v1->pos.x - v0->pos.x;
nuclear@0 40 dfdx = dx / dy;
nuclear@0 41
nuclear@0 42 #ifdef INTERP_DEPTH
nuclear@0 43 dz = v1->pos.z - v0->pos.z;
nuclear@0 44 dfdz = dz / dy;
nuclear@0 45 #endif
nuclear@0 46 #ifdef INTERP_ENERGY
nuclear@0 47 de = v1->energy - v0->energy;
nuclear@0 48 dfde = de / dy;
nuclear@0 49 #endif
nuclear@3 50 #ifdef INTERP_TEX
nuclear@3 51 du = v1->tc.x - v0->tc.x;
nuclear@3 52 dv = v1->tc.y - v0->tc.y;
nuclear@3 53 dfdu = du / dy;
nuclear@3 54 dfdv = dv / dy;
nuclear@3 55 #endif
nuclear@0 56
nuclear@0 57 if(dy < 0.0) {
nuclear@0 58 struct vertex *tmp = v0;
nuclear@0 59 v0 = v1;
nuclear@0 60 v1 = tmp;
nuclear@0 61 edge = (st->ord == MGL_CCW) ? vright : vleft;
nuclear@0 62 } else {
nuclear@0 63 edge = (st->ord == MGL_CCW) ? vleft : vright;
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 start = (int)ROUND(v0->pos.y);
nuclear@0 67 end = (int)ROUND(v1->pos.y);
nuclear@0 68
nuclear@9 69 if(start >= 0) {
nuclear@9 70
nuclear@9 71 x = v0->pos.x;
nuclear@0 72 #ifdef INTERP_DEPTH
nuclear@9 73 z = v0->pos.z;
nuclear@0 74 #endif
nuclear@0 75 #ifdef INTERP_ENERGY
nuclear@9 76 e = v0->energy;
nuclear@0 77 #endif
nuclear@3 78 #ifdef INTERP_TEX
nuclear@9 79 u = v0->tc.x;
nuclear@9 80 v = v0->tc.y;
nuclear@3 81 #endif
nuclear@9 82 } else {
nuclear@9 83 float lines = -v0->pos.y;
nuclear@9 84
nuclear@9 85 x = v0->pos.x + dfdx * lines;
nuclear@9 86 #ifdef INTERP_DEPTH
nuclear@9 87 z = v0->pos.z + dfdz * lines;
nuclear@9 88 #endif
nuclear@9 89 #ifdef INTERP_ENERGY
nuclear@9 90 e = v0->energy + dfde * lines;
nuclear@9 91 #endif
nuclear@9 92 #ifdef INTERP_TEX
nuclear@9 93 u = v0->tc.x + dfdu * lines;
nuclear@9 94 v = v0->tc.y + dfdv * lines;
nuclear@9 95 #endif
nuclear@9 96 start = 0;
nuclear@9 97 }
nuclear@9 98
nuclear@9 99 if(end >= fb->height) {
nuclear@9 100 end = fb->height - 1;
nuclear@9 101 }
nuclear@9 102
nuclear@9 103
nuclear@0 104 for(i=start; i<end; i++) {
nuclear@0 105 edge[i].pos.x = x;
nuclear@0 106 x += dfdx;
nuclear@0 107
nuclear@0 108 edge[i].cidx = v0->cidx;
nuclear@0 109 #ifdef INTERP_DEPTH
nuclear@0 110 edge[i].pos.z = z;
nuclear@0 111 z += dfdz;
nuclear@0 112 #endif
nuclear@0 113
nuclear@0 114 #ifdef INTERP_ENERGY
nuclear@0 115 edge[i].energy = e;
nuclear@0 116 e += dfde;
nuclear@0 117 #else
nuclear@0 118 edge[i].energy = v0->energy;
nuclear@0 119 #endif
nuclear@3 120
nuclear@3 121 #ifdef INTERP_TEX
nuclear@3 122 edge[i].tc.x = u;
nuclear@3 123 edge[i].tc.y = v;
nuclear@3 124 u += dfdu;
nuclear@3 125 v += dfdv;
nuclear@3 126 #endif
nuclear@0 127 }
nuclear@0 128 }
nuclear@0 129
nuclear@0 130 static void SCAN_LINE(int y, unsigned char *sline)
nuclear@0 131 {
nuclear@3 132 int x0, x1, len, tmp, cidx;
nuclear@3 133 #if defined(INTERP_DEPTH) || defined(INTERP_ENERGY) || defined(INTERP_TEX)
nuclear@3 134 int i;
nuclear@0 135 float x, dx;
nuclear@0 136 #endif
nuclear@0 137 #ifdef INTERP_DEPTH
nuclear@0 138 float z, dz, dfdz;
nuclear@0 139 #endif
nuclear@0 140 #ifdef INTERP_ENERGY
nuclear@0 141 float e, de, dfde;
nuclear@0 142 #endif
nuclear@3 143 #ifdef INTERP_TEX
nuclear@3 144 int tx, ty;
nuclear@3 145 float u, v, du, dv, dfdu, dfdv;
nuclear@3 146 #endif
nuclear@0 147 struct vertex *left, *right;
nuclear@0 148
nuclear@0 149 x0 = (int)ROUND(vleft[y].pos.x);
nuclear@0 150 x1 = (int)ROUND(vright[y].pos.x);
nuclear@0 151
nuclear@0 152 if(x1 < x0) {
nuclear@0 153 if(st->flags & MGL_CULL_FACE) {
nuclear@0 154 return;
nuclear@0 155 }
nuclear@0 156 tmp = x0;
nuclear@0 157 x0 = x1;
nuclear@0 158 x1 = tmp;
nuclear@0 159
nuclear@0 160 left = vright;
nuclear@0 161 right = vleft;
nuclear@0 162 } else {
nuclear@0 163 left = vleft;
nuclear@0 164 right = vright;
nuclear@0 165 }
nuclear@0 166
nuclear@9 167 if(x1 >= fb->width) {
nuclear@9 168 x1 = fb->width - 1;
nuclear@9 169 }
nuclear@0 170
nuclear@0 171 cidx = left[y].cidx;
nuclear@3 172 #if !defined(INTERP_DEPTH) && !defined(INTERP_ENERGY) && !defined(INTERP_TEX)
nuclear@9 173 if(x0 < 0) x0 = 0;
nuclear@9 174 len = x1 - x0;
nuclear@9 175 assert(len >= 0);
nuclear@0 176 /* no interpolation at all, just memset the whole scanline */
nuclear@0 177 memset(sline + x0, cidx + left[y].energy * st->col_range, len);
nuclear@0 178 #else
nuclear@0 179 /* otherwise do a loop and interpolate whatever needs interpolating */
nuclear@0 180 x = left[y].pos.x;
nuclear@0 181 dx = right[y].pos.x - x;
nuclear@0 182
nuclear@0 183 if(dx < 0.5 && dx > -0.5) {
nuclear@0 184 return;
nuclear@0 185 }
nuclear@0 186
nuclear@9 187 if(x0 >= 0) {
nuclear@0 188 #ifdef INTERP_DEPTH
nuclear@9 189 z = left[y].pos.z;
nuclear@9 190 dz = right[y].pos.z - z;
nuclear@9 191 dfdz = dz / dx;
nuclear@0 192 #endif
nuclear@0 193 #ifdef INTERP_ENERGY
nuclear@9 194 e = left[y].energy;
nuclear@9 195 de = right[y].energy - e;
nuclear@9 196 dfde = de / dx;
nuclear@0 197 #endif
nuclear@3 198 #ifdef INTERP_TEX
nuclear@9 199 u = left[y].tc.x;
nuclear@9 200 v = left[y].tc.y;
nuclear@9 201 du = right[y].tc.x - u;
nuclear@9 202 dv = right[y].tc.y - v;
nuclear@9 203 dfdu = du / dx;
nuclear@9 204 dfdv = dv / dx;
nuclear@3 205 #endif
nuclear@9 206 } else {
nuclear@9 207 float dist = -left[y].pos.x;
nuclear@9 208
nuclear@9 209 #ifdef INTERP_DEPTH
nuclear@9 210 dz = right[y].pos.z - left[y].pos.z;
nuclear@9 211 dfdz = dz / dx;
nuclear@9 212 z = left[y].pos.z + dfdz * dist;
nuclear@9 213 #endif
nuclear@9 214 #ifdef INTERP_ENERGY
nuclear@9 215 de = right[y].energy - left[y].energy;
nuclear@9 216 dfde = de / dx;
nuclear@9 217 e = left[y].energy + dfde * dist;
nuclear@9 218 #endif
nuclear@9 219 #ifdef INTERP_TEX
nuclear@9 220 du = right[y].tc.x - left[y].tc.x;
nuclear@9 221 dv = right[y].tc.y - left[y].tc.y;
nuclear@9 222 dfdu = du / dx;
nuclear@9 223 dfdv = dv / dx;
nuclear@9 224 u = left[y].tc.x + dfdu * dist;
nuclear@9 225 v = left[y].tc.y + dfdv * dist;
nuclear@9 226 #endif
nuclear@9 227 x0 = 0;
nuclear@9 228 }
nuclear@9 229
nuclear@9 230 len = x1 - x0;
nuclear@0 231
nuclear@0 232 for(i=0; i<len; i++) {
nuclear@3 233 int c = cidx;
nuclear@3 234
nuclear@3 235 #ifdef INTERP_DEPTH
nuclear@10 236 long pix = (sline + x0 + i) - fb->pixels;
nuclear@9 237 unsigned short zval = (unsigned short)(z * USHRT_MAX);
nuclear@9 238 unsigned short *zptr = fb->zbuf[ZTILE(pix)] + ZTILE_OFFS(pix);
nuclear@9 239
nuclear@9 240 if(z < 0.0 || z >= 1.0 || zval > *zptr) {
nuclear@9 241 # ifdef INTERP_TEX
nuclear@9 242 u += dfdu;
nuclear@9 243 v += dfdv;
nuclear@9 244 # endif
nuclear@9 245 # ifdef INTERP_ENERGY
nuclear@9 246 e += dfde;
nuclear@9 247 # endif
nuclear@9 248 z += dfdz;
nuclear@9 249 continue;
nuclear@9 250 }
nuclear@9 251
nuclear@9 252 *zptr = zval;
nuclear@3 253 z += dfdz;
nuclear@3 254 #endif
nuclear@3 255 #ifdef INTERP_TEX
nuclear@3 256 tx = (int)(u * st->tex.width) & st->tex.xmask;
nuclear@3 257 ty = (int)(v * st->tex.height) & st->tex.ymask;
nuclear@3 258 c = st->tex.pixels[(ty << st->tex.xshift) + tx];
nuclear@3 259
nuclear@3 260 u += dfdu;
nuclear@3 261 v += dfdv;
nuclear@3 262 #endif
nuclear@0 263 #ifdef INTERP_ENERGY
nuclear@3 264 c += e * st->col_range;
nuclear@0 265 e += dfde;
nuclear@3 266 #else
nuclear@3 267 c += left[y].energy * st->col_range;
nuclear@0 268 #endif
nuclear@3 269 sline[x0 + i] = c;
nuclear@0 270 }
nuclear@0 271 #endif /* flat */
nuclear@0 272 }