deepstone

annotate src/mglrast.c @ 5:c8ffdbc6139e

added cleandep removed verbosity from texture loader
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 12:00:13 +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 #include <stdio.h>
nuclear@0 19 #include <stdlib.h>
nuclear@0 20 #include <string.h>
nuclear@0 21 #include <assert.h>
nuclear@0 22 #include "mingl.h"
nuclear@0 23 #include "mglimpl.h"
nuclear@0 24
nuclear@0 25
nuclear@0 26 static struct vertex *vleft, *vright;
nuclear@0 27 static struct framebuffer *fb;
nuclear@0 28 static struct state *st;
nuclear@0 29
nuclear@0 30
nuclear@0 31 #define SCAN_EDGE scan_edge_flat
nuclear@0 32 #define SCAN_LINE scan_line_flat
nuclear@0 33 #undef INTERP_DEPTH
nuclear@0 34 #undef INTERP_ENERGY
nuclear@3 35 #undef INTERP_TEX
nuclear@0 36 #include "scantmpl.h"
nuclear@0 37 #undef SCAN_EDGE
nuclear@0 38 #undef SCAN_LINE
nuclear@0 39
nuclear@0 40 #define SCAN_EDGE scan_edge_z
nuclear@0 41 #define SCAN_LINE scan_line_z
nuclear@0 42 #define INTERP_DEPTH
nuclear@0 43 #undef INTERP_ENERGY
nuclear@3 44 #undef INTERP_TEX
nuclear@0 45 #include "scantmpl.h"
nuclear@0 46 #undef SCAN_EDGE
nuclear@0 47 #undef SCAN_LINE
nuclear@0 48
nuclear@0 49 #define SCAN_EDGE scan_edge_e
nuclear@0 50 #define SCAN_LINE scan_line_e
nuclear@0 51 #undef INTERP_DEPTH
nuclear@0 52 #define INTERP_ENERGY
nuclear@3 53 #undef INTERP_TEX
nuclear@0 54 #include "scantmpl.h"
nuclear@0 55 #undef SCAN_EDGE
nuclear@0 56 #undef SCAN_LINE
nuclear@0 57
nuclear@0 58 #define SCAN_EDGE scan_edge_ze
nuclear@0 59 #define SCAN_LINE scan_line_ze
nuclear@0 60 #define INTERP_DEPTH
nuclear@0 61 #define INTERP_ENERGY
nuclear@3 62 #undef INTERP_TEX
nuclear@0 63 #include "scantmpl.h"
nuclear@0 64 #undef SCAN_EDGE
nuclear@0 65 #undef SCAN_LINE
nuclear@0 66
nuclear@3 67 #define SCAN_EDGE scan_edge_t
nuclear@3 68 #define SCAN_LINE scan_line_t
nuclear@3 69 #undef INTERP_DEPTH
nuclear@3 70 #undef INTERP_ENERGY
nuclear@3 71 #define INTERP_TEX
nuclear@3 72 #include "scantmpl.h"
nuclear@3 73 #undef SCAN_EDGE
nuclear@3 74 #undef SCAN_LINE
nuclear@3 75
nuclear@3 76 #define SCAN_EDGE scan_edge_zt
nuclear@3 77 #define SCAN_LINE scan_line_zt
nuclear@3 78 #define INTERP_DEPTH
nuclear@3 79 #undef INTERP_ENERGY
nuclear@3 80 #define INTERP_TEX
nuclear@3 81 #include "scantmpl.h"
nuclear@3 82 #undef SCAN_EDGE
nuclear@3 83 #undef SCAN_LINE
nuclear@3 84
nuclear@3 85 #define SCAN_EDGE scan_edge_et
nuclear@3 86 #define SCAN_LINE scan_line_et
nuclear@3 87 #undef INTERP_DEPTH
nuclear@3 88 #define INTERP_ENERGY
nuclear@3 89 #define INTERP_TEX
nuclear@3 90 #include "scantmpl.h"
nuclear@3 91 #undef SCAN_EDGE
nuclear@3 92 #undef SCAN_LINE
nuclear@3 93
nuclear@3 94 #define SCAN_EDGE scan_edge_zet
nuclear@3 95 #define SCAN_LINE scan_line_zet
nuclear@3 96 #define INTERP_DEPTH
nuclear@3 97 #define INTERP_ENERGY
nuclear@3 98 #define INTERP_TEX
nuclear@3 99 #include "scantmpl.h"
nuclear@3 100 #undef SCAN_EDGE
nuclear@3 101 #undef SCAN_LINE
nuclear@3 102
nuclear@3 103
nuclear@0 104 static void (*scan_edge)(struct vertex*, struct vertex*);
nuclear@0 105 static void (*scan_line)(int, unsigned char*);
nuclear@0 106
nuclear@0 107 int mgl_rast_init(struct state *state, struct framebuffer *fbuf)
nuclear@0 108 {
nuclear@0 109 fb = fbuf;
nuclear@0 110 st = state;
nuclear@0 111
nuclear@0 112 if(!(vleft = malloc(fb->height * sizeof *vleft))) {
nuclear@0 113 return -1;
nuclear@0 114 }
nuclear@0 115 if(!(vright = malloc(fb->height * sizeof *vright))) {
nuclear@0 116 free(vleft);
nuclear@0 117 return -1;
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 scan_edge = scan_edge_flat;
nuclear@0 121 scan_line = scan_line_flat;
nuclear@0 122
nuclear@0 123 return 0;
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 void mgl_rast_cleanup(void)
nuclear@0 127 {
nuclear@0 128 free(vleft);
nuclear@0 129 free(vright);
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 void mgl_rast_prepare(void)
nuclear@0 133 {
nuclear@0 134 static void (*sedge[])(struct vertex*, struct vertex*) = {
nuclear@3 135 /* tez */
nuclear@3 136 scan_edge_flat, /* 000 */
nuclear@3 137 scan_edge_z, /* 001 */
nuclear@3 138 scan_edge_e, /* 010 */
nuclear@3 139 scan_edge_ze, /* 011 */
nuclear@3 140 scan_edge_t, /* 100 */
nuclear@3 141 scan_edge_zt, /* 101 */
nuclear@3 142 scan_edge_et, /* 110 */
nuclear@3 143 scan_edge_zet /* 111 */
nuclear@0 144 };
nuclear@0 145 static void (*sline[])(int, unsigned char*) = {
nuclear@3 146 /* tez */
nuclear@3 147 scan_line_flat, /* 000 */
nuclear@3 148 scan_line_z, /* 001 */
nuclear@3 149 scan_line_e, /* 010 */
nuclear@3 150 scan_line_ze, /* 011 */
nuclear@3 151 scan_line_t, /* 100 */
nuclear@3 152 scan_line_zt, /* 101 */
nuclear@3 153 scan_line_et, /* 110 */
nuclear@3 154 scan_line_zet /* 111 */
nuclear@0 155 };
nuclear@0 156 int bits = 0;
nuclear@0 157
nuclear@3 158 if((st->flags & MGL_TEXTURE_2D) && st->tex.pixels) {
nuclear@3 159 bits |= 4;
nuclear@3 160 }
nuclear@0 161 if(st->flags & MGL_SMOOTH) {
nuclear@0 162 bits |= 2;
nuclear@0 163 }
nuclear@0 164 if((st->flags & MGL_DEPTH_TEST) && fb->zbuf) {
nuclear@0 165 bits |= 1;
nuclear@0 166 }
nuclear@0 167
nuclear@0 168 scan_edge = sedge[bits];
nuclear@0 169 scan_line = sline[bits];
nuclear@0 170 }
nuclear@0 171
nuclear@0 172 void mgl_draw_point(struct vertex *v)
nuclear@0 173 {
nuclear@0 174 int x = (int)ROUND(v->pos.x);
nuclear@0 175 int y = (int)ROUND(v->pos.y);
nuclear@0 176
nuclear@0 177 if(x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
nuclear@0 178 int cidx = v->cidx + v->energy * st->col_range;
nuclear@0 179 fb->pixels[y * fb->width + x] = cidx;
nuclear@0 180 }
nuclear@0 181 }
nuclear@0 182
nuclear@0 183 void mgl_draw_line(struct vertex *v0, struct vertex *v1)
nuclear@0 184 {
nuclear@0 185 /* TODO */
nuclear@0 186 fprintf(stderr, "draw_line unimplemented\n");
nuclear@0 187 abort();
nuclear@0 188 }
nuclear@0 189
nuclear@0 190 void mgl_draw_poly(struct vertex *v, int numv)
nuclear@0 191 {
nuclear@0 192 int ybeg, yend, i;
nuclear@0 193 unsigned char *sline;
nuclear@0 194
nuclear@0 195 ybeg = fb->height;
nuclear@0 196 yend = 0;
nuclear@0 197
nuclear@0 198 for(i=0; i<numv; i++) {
nuclear@0 199 struct vertex *v0 = v + i;
nuclear@0 200 struct vertex *v1 = v + (i + 1) % numv;
nuclear@0 201 int y = (int)ROUND(v0->pos.y);
nuclear@0 202
nuclear@0 203 scan_edge(v0, v1);
nuclear@0 204
nuclear@0 205 if(y > yend) yend = y;
nuclear@0 206 if(y < ybeg) ybeg = y;
nuclear@0 207 }
nuclear@0 208
nuclear@0 209 if(ybeg < 0) ybeg = 0;
nuclear@0 210 if(yend >= fb->height) yend = fb->height - 1;
nuclear@0 211
nuclear@0 212 sline = fb->pixels + ybeg * fb->width;
nuclear@0 213 for(i=ybeg; i<yend; i++) {
nuclear@0 214 scan_line(i, sline);
nuclear@0 215 sline += fb->width;
nuclear@0 216 }
nuclear@0 217 }