deepstone

annotate src/scantmpl.h @ 3:0e781cc43178

adding textures
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 10:16:09 +0200
parents f04884489bad
children bce78aaafc68
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@0 69 x = v0->pos.x;
nuclear@0 70 #ifdef INTERP_DEPTH
nuclear@0 71 z = v0->pos.z;
nuclear@0 72 #endif
nuclear@0 73 #ifdef INTERP_ENERGY
nuclear@0 74 e = v0->energy;
nuclear@0 75 #endif
nuclear@3 76 #ifdef INTERP_TEX
nuclear@3 77 u = v0->tc.x;
nuclear@3 78 v = v0->tc.y;
nuclear@3 79 #endif
nuclear@0 80 for(i=start; i<end; i++) {
nuclear@0 81 edge[i].pos.x = x;
nuclear@0 82 x += dfdx;
nuclear@0 83
nuclear@0 84 edge[i].cidx = v0->cidx;
nuclear@0 85 #ifdef INTERP_DEPTH
nuclear@0 86 edge[i].pos.z = z;
nuclear@0 87 z += dfdz;
nuclear@0 88 #endif
nuclear@0 89
nuclear@0 90 #ifdef INTERP_ENERGY
nuclear@0 91 edge[i].energy = e;
nuclear@0 92 e += dfde;
nuclear@0 93 #else
nuclear@0 94 edge[i].energy = v0->energy;
nuclear@0 95 #endif
nuclear@3 96
nuclear@3 97 #ifdef INTERP_TEX
nuclear@3 98 edge[i].tc.x = u;
nuclear@3 99 edge[i].tc.y = v;
nuclear@3 100 u += dfdu;
nuclear@3 101 v += dfdv;
nuclear@3 102 #endif
nuclear@0 103 }
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 static void SCAN_LINE(int y, unsigned char *sline)
nuclear@0 107 {
nuclear@3 108 int x0, x1, len, tmp, cidx;
nuclear@3 109 #if defined(INTERP_DEPTH) || defined(INTERP_ENERGY) || defined(INTERP_TEX)
nuclear@3 110 int i;
nuclear@0 111 float x, dx;
nuclear@0 112 #endif
nuclear@0 113 #ifdef INTERP_DEPTH
nuclear@0 114 float z, dz, dfdz;
nuclear@0 115 #endif
nuclear@0 116 #ifdef INTERP_ENERGY
nuclear@0 117 float e, de, dfde;
nuclear@0 118 #endif
nuclear@3 119 #ifdef INTERP_TEX
nuclear@3 120 int tx, ty;
nuclear@3 121 float u, v, du, dv, dfdu, dfdv;
nuclear@3 122 #endif
nuclear@0 123 struct vertex *left, *right;
nuclear@0 124
nuclear@0 125 x0 = (int)ROUND(vleft[y].pos.x);
nuclear@0 126 x1 = (int)ROUND(vright[y].pos.x);
nuclear@0 127 len = x1 - x0;
nuclear@0 128
nuclear@0 129 if(x1 < x0) {
nuclear@0 130 if(st->flags & MGL_CULL_FACE) {
nuclear@0 131 return;
nuclear@0 132 }
nuclear@0 133 tmp = x0;
nuclear@0 134 x0 = x1;
nuclear@0 135 x1 = tmp;
nuclear@0 136 len = -len;
nuclear@0 137
nuclear@0 138 left = vright;
nuclear@0 139 right = vleft;
nuclear@0 140 } else {
nuclear@0 141 left = vleft;
nuclear@0 142 right = vright;
nuclear@0 143 }
nuclear@0 144
nuclear@0 145 if(x0 < 0) x0 = 0;
nuclear@0 146 if(x1 >= fb->width) x1 = fb->width - 1;
nuclear@0 147
nuclear@0 148 assert(len >= 0);
nuclear@0 149
nuclear@0 150 cidx = left[y].cidx;
nuclear@3 151 #if !defined(INTERP_DEPTH) && !defined(INTERP_ENERGY) && !defined(INTERP_TEX)
nuclear@0 152 /* no interpolation at all, just memset the whole scanline */
nuclear@0 153 memset(sline + x0, cidx + left[y].energy * st->col_range, len);
nuclear@0 154 #else
nuclear@0 155 /* otherwise do a loop and interpolate whatever needs interpolating */
nuclear@0 156 x = left[y].pos.x;
nuclear@0 157 dx = right[y].pos.x - x;
nuclear@0 158
nuclear@0 159 if(dx < 0.5 && dx > -0.5) {
nuclear@0 160 return;
nuclear@0 161 }
nuclear@0 162
nuclear@0 163 #ifdef INTERP_DEPTH
nuclear@0 164 z = left[y].pos.z;
nuclear@0 165 dz = right[y].pos.z - z;
nuclear@0 166 dfdz = dz / dx;
nuclear@0 167 #endif
nuclear@0 168 #ifdef INTERP_ENERGY
nuclear@0 169 e = left[y].energy;
nuclear@0 170 de = right[y].energy - e;
nuclear@0 171 dfde = de / dx;
nuclear@0 172 #endif
nuclear@3 173 #ifdef INTERP_TEX
nuclear@3 174 u = left[y].tc.x;
nuclear@3 175 v = left[y].tc.y;
nuclear@3 176 du = right[y].tc.x - u;
nuclear@3 177 dv = right[y].tc.y - v;
nuclear@3 178 dfdu = du / dx;
nuclear@3 179 dfdv = dv / dx;
nuclear@3 180 #endif
nuclear@0 181
nuclear@0 182 for(i=0; i<len; i++) {
nuclear@3 183 int c = cidx;
nuclear@3 184
nuclear@3 185 #ifdef INTERP_DEPTH
nuclear@3 186 z += dfdz;
nuclear@3 187 #endif
nuclear@3 188 #ifdef INTERP_TEX
nuclear@3 189 tx = (int)(u * st->tex.width) & st->tex.xmask;
nuclear@3 190 ty = (int)(v * st->tex.height) & st->tex.ymask;
nuclear@3 191 c = st->tex.pixels[(ty << st->tex.xshift) + tx];
nuclear@3 192
nuclear@3 193 u += dfdu;
nuclear@3 194 v += dfdv;
nuclear@3 195 #endif
nuclear@0 196 #ifdef INTERP_ENERGY
nuclear@3 197 c += e * st->col_range;
nuclear@0 198 e += dfde;
nuclear@3 199 #else
nuclear@3 200 c += left[y].energy * st->col_range;
nuclear@0 201 #endif
nuclear@3 202 sline[x0 + i] = c;
nuclear@0 203 }
nuclear@0 204 #endif /* flat */
nuclear@0 205 }