dos3d

annotate src/mglrast.c @ 1:0b7f840afe4a

palette management
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 08:35:58 +0200
parents
children 0e781cc43178
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@0 35 #include "scantmpl.h"
nuclear@0 36 #undef SCAN_EDGE
nuclear@0 37 #undef SCAN_LINE
nuclear@0 38
nuclear@0 39 #define SCAN_EDGE scan_edge_z
nuclear@0 40 #define SCAN_LINE scan_line_z
nuclear@0 41 #define INTERP_DEPTH
nuclear@0 42 #undef INTERP_ENERGY
nuclear@0 43 #include "scantmpl.h"
nuclear@0 44 #undef SCAN_EDGE
nuclear@0 45 #undef SCAN_LINE
nuclear@0 46
nuclear@0 47 #define SCAN_EDGE scan_edge_e
nuclear@0 48 #define SCAN_LINE scan_line_e
nuclear@0 49 #undef INTERP_DEPTH
nuclear@0 50 #define INTERP_ENERGY
nuclear@0 51 #include "scantmpl.h"
nuclear@0 52 #undef SCAN_EDGE
nuclear@0 53 #undef SCAN_LINE
nuclear@0 54
nuclear@0 55 #define SCAN_EDGE scan_edge_ze
nuclear@0 56 #define SCAN_LINE scan_line_ze
nuclear@0 57 #define INTERP_DEPTH
nuclear@0 58 #define INTERP_ENERGY
nuclear@0 59 #include "scantmpl.h"
nuclear@0 60 #undef SCAN_EDGE
nuclear@0 61 #undef SCAN_LINE
nuclear@0 62
nuclear@0 63 static void (*scan_edge)(struct vertex*, struct vertex*);
nuclear@0 64 static void (*scan_line)(int, unsigned char*);
nuclear@0 65
nuclear@0 66 int mgl_rast_init(struct state *state, struct framebuffer *fbuf)
nuclear@0 67 {
nuclear@0 68 fb = fbuf;
nuclear@0 69 st = state;
nuclear@0 70
nuclear@0 71 if(!(vleft = malloc(fb->height * sizeof *vleft))) {
nuclear@0 72 return -1;
nuclear@0 73 }
nuclear@0 74 if(!(vright = malloc(fb->height * sizeof *vright))) {
nuclear@0 75 free(vleft);
nuclear@0 76 return -1;
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 scan_edge = scan_edge_flat;
nuclear@0 80 scan_line = scan_line_flat;
nuclear@0 81
nuclear@0 82 return 0;
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 void mgl_rast_cleanup(void)
nuclear@0 86 {
nuclear@0 87 free(vleft);
nuclear@0 88 free(vright);
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 void mgl_rast_prepare(void)
nuclear@0 92 {
nuclear@0 93 static void (*sedge[])(struct vertex*, struct vertex*) = {
nuclear@0 94 scan_edge_flat, /* 00 */
nuclear@0 95 scan_edge_z, /* 01 */
nuclear@0 96 scan_edge_e, /* 10 */
nuclear@0 97 scan_edge_ze /* 11 */
nuclear@0 98 };
nuclear@0 99 static void (*sline[])(int, unsigned char*) = {
nuclear@0 100 scan_line_flat, /* 00 */
nuclear@0 101 scan_line_z, /* 01 */
nuclear@0 102 scan_line_e, /* 10 */
nuclear@0 103 scan_line_ze /* 11 */
nuclear@0 104 };
nuclear@0 105 int bits = 0;
nuclear@0 106
nuclear@0 107 if(st->flags & MGL_SMOOTH) {
nuclear@0 108 bits |= 2;
nuclear@0 109 }
nuclear@0 110 if((st->flags & MGL_DEPTH_TEST) && fb->zbuf) {
nuclear@0 111 bits |= 1;
nuclear@0 112 }
nuclear@0 113
nuclear@0 114 scan_edge = sedge[bits];
nuclear@0 115 scan_line = sline[bits];
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 void mgl_draw_point(struct vertex *v)
nuclear@0 119 {
nuclear@0 120 int x = (int)ROUND(v->pos.x);
nuclear@0 121 int y = (int)ROUND(v->pos.y);
nuclear@0 122
nuclear@0 123 if(x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
nuclear@0 124 int cidx = v->cidx + v->energy * st->col_range;
nuclear@0 125 fb->pixels[y * fb->width + x] = cidx;
nuclear@0 126 }
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 void mgl_draw_line(struct vertex *v0, struct vertex *v1)
nuclear@0 130 {
nuclear@0 131 /* TODO */
nuclear@0 132 fprintf(stderr, "draw_line unimplemented\n");
nuclear@0 133 abort();
nuclear@0 134 }
nuclear@0 135
nuclear@0 136 void mgl_draw_poly(struct vertex *v, int numv)
nuclear@0 137 {
nuclear@0 138 int ybeg, yend, i;
nuclear@0 139 unsigned char *sline;
nuclear@0 140
nuclear@0 141 ybeg = fb->height;
nuclear@0 142 yend = 0;
nuclear@0 143
nuclear@0 144 for(i=0; i<numv; i++) {
nuclear@0 145 struct vertex *v0 = v + i;
nuclear@0 146 struct vertex *v1 = v + (i + 1) % numv;
nuclear@0 147 int y = (int)ROUND(v0->pos.y);
nuclear@0 148
nuclear@0 149 scan_edge(v0, v1);
nuclear@0 150
nuclear@0 151 if(y > yend) yend = y;
nuclear@0 152 if(y < ybeg) ybeg = y;
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 if(ybeg < 0) ybeg = 0;
nuclear@0 156 if(yend >= fb->height) yend = fb->height - 1;
nuclear@0 157
nuclear@0 158 sline = fb->pixels + ybeg * fb->width;
nuclear@0 159 for(i=ybeg; i<yend; i++) {
nuclear@0 160 scan_line(i, sline);
nuclear@0 161 sline += fb->width;
nuclear@0 162 }
nuclear@0 163 }