dos3d

annotate src/mglrast.c @ 12:c29a6e024950

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